158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#ifndef COMPONENTS_STARTUP_METRIC_UTILS_STARTUP_METRIC_UTILS_H_
658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#define COMPONENTS_STARTUP_METRIC_UTILS_STARTUP_METRIC_UTILS_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Utility functions to support metric collection for browser startup.
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace startup_metric_utils {
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true if any UI other than the browser window has been displayed
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// so far.  Useful to test if UI has been displayed before the first browser
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// window was shown, which would invalidate any surrounding timing metrics.
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool WasNonBrowserUIDisplayed();
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Call this when displaying UI that might potentially delay the appearance
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of the initial browser window on Chrome startup.
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note on usage: This function is idempotent and its overhead is low enough
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in comparison with UI display that it's OK to call it on every
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// UI invocation regardless of whether the browser window has already
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// been displayed or not.
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void SetNonBrowserUIDisplayed();
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Call this as early as possible in the startup process to record a
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// timestamp.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void RecordMainEntryPointTime();
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Call this when the executable is loaded and main() is entered. Can be
35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// different from |RecordMainEntryPointTime| when the startup process is
36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// contained in a separate dll, such as with chrome.exe / chrome.dll on Windows.
37f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void RecordExeMainEntryTime();
38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
3958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#if defined(OS_ANDROID)
4058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// On Android the entry point time is the time at which the Java code starts.
4158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// This is recorded on the Java side, and then passed to the C++ side once the
4258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)// C++ library is loaded and running.
4358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void RecordSavedMainEntryPointTime(const base::Time& entry_point_time);
4458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif // OS_ANDROID
4558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Called just before the message loop is about to start. Entry point to record
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// startup stats.
48b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// |is_first_run| - is the current launch part of a first run.
49b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)void OnBrowserStartupComplete(bool is_first_run);
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Called when the initial page load has finished in order to record startup
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// stats.
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void OnInitialPageLoadComplete();
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Returns the time of main entry recorded from RecordMainEntryPointTime.
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Returns NULL if that method has not yet been called.
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// This method is expected to be called from the UI thread.
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdochconst base::Time* MainEntryPointTime();
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Scoper that records the time period before it's destructed in a histogram
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// with the given name. The histogram is only recorded for slow chrome startups.
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Useful for trying to figure out what parts of Chrome cause slow startup.
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class ScopedSlowStartupUMA {
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  explicit ScopedSlowStartupUMA(const std::string& histogram_name)
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      : start_time_(base::TimeTicks::Now()),
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        histogram_name_(histogram_name) {}
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ~ScopedSlowStartupUMA();
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const base::TimeTicks start_time_;
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const std::string histogram_name_;
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ScopedSlowStartupUMA);
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace startup_metric_utils
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#endif  // COMPONENTS_STARTUP_METRIC_UTILS_STARTUP_METRIC_UTILS_H_
81