1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_CHROMEOS_BOOT_TIMES_LOADER_H_
6 #define CHROME_BROWSER_CHROMEOS_BOOT_TIMES_LOADER_H_
12 #include "base/atomic_sequence_num.h"
13 #include "base/callback_forward.h"
14 #include "base/compiler_specific.h"
15 #include "base/time.h"
16 #include "chrome/browser/cancelable_request.h"
17 #include "content/browser/renderer_host/render_widget_host.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
23 // BootTimesLoader loads the bootimes of Chrome OS from the file system.
24 // Loading is done asynchronously on the file thread. Once loaded,
25 // BootTimesLoader calls back to a method of your choice with the boot times.
26 // To use BootTimesLoader do the following:
28 // . In your class define a member field of type chromeos::BootTimesLoader and
29 // CancelableRequestConsumerBase.
30 // . Define the callback method, something like:
31 // void OnBootTimesLoader(chromeos::BootTimesLoader::Handle,
32 // BootTimesLoader::BootTimes boot_times);
33 // . When you want the version invoke: loader.GetBootTimes(&consumer, callback);
35 : public CancelableRequestProvider
,
36 public content::NotificationObserver
{
39 virtual ~BootTimesLoader();
41 // All fields are 0.0 if they couldn't be found.
42 typedef struct BootTimes
{
43 double firmware
; // Time from power button to kernel being loaded.
44 double pre_startup
; // Time from kernel to system code being called.
45 double x_started
; // Time X server is ready to be connected to.
46 double chrome_exec
; // Time session manager executed Chrome.
47 double chrome_main
; // Time chrome's main() was called.
48 double login_prompt_ready
; // Time login (or OOB) panel is displayed.
49 double system
; // Time system took to start chrome.
50 double chrome
; // Time chrome took to display login panel.
51 double total
; // Time from power button to login panel.
53 BootTimes() : firmware(0),
58 login_prompt_ready(0),
65 typedef base::Callback
<void(Handle
, BootTimes
)> GetBootTimesCallback
;
67 typedef CancelableRequest
<GetBootTimesCallback
> GetBootTimesRequest
;
69 static BootTimesLoader
* Get();
71 // Asynchronously requests the info.
73 CancelableRequestConsumerBase
* consumer
,
74 const GetBootTimesCallback
& callback
);
76 // Add a time marker for login. A timeline will be dumped to
77 // /tmp/login-times-sent after login is done. If |send_to_uma| is true
78 // the time between this marker and the last will be sent to UMA with
79 // the identifier BootTime.|marker_name|.
80 void AddLoginTimeMarker(const std::string
& marker_name
, bool send_to_uma
);
82 // Add a time marker for logout. A timeline will be dumped to
83 // /tmp/logout-times-sent after logout is done. If |send_to_uma| is true
84 // the time between this marker and the last will be sent to UMA with
85 // the identifier ShutdownTime.|marker_name|.
86 void AddLogoutTimeMarker(const std::string
& marker_name
, bool send_to_uma
);
88 // Records current uptime and disk usage for metrics use.
89 // Posts task to file thread.
90 // name will be used as part of file names in /tmp.
91 // Existing stats files will not be overwritten.
92 void RecordCurrentStats(const std::string
& name
);
94 // Saves away the stats at main, so the can be recorded later. At main() time
95 // the necessary threads don't exist yet for recording the data.
96 void SaveChromeMainStats();
98 // Records the data previously saved by SaveChromeMainStats(), using the
99 // file thread. Existing stats files will not be overwritten.
100 void RecordChromeMainStats();
102 // Records the time that a login was attempted. This will overwrite any
103 // previous login attempt times.
104 void RecordLoginAttempted();
106 // content::NotificationObserver implementation.
107 virtual void Observe(int type
,
108 const content::NotificationSource
& source
,
109 const content::NotificationDetails
& details
) OVERRIDE
;
111 // Writes the logout times to a /tmp/logout-times-sent. Unlike login
112 // times, we manually call this function for logout times, as we cannot
113 // rely on notification service to tell when the logout is done.
114 void WriteLogoutTimes();
117 // BootTimesLoader calls into the Backend on the file thread to load
119 class Backend
: public base::RefCountedThreadSafe
<Backend
> {
123 void GetBootTimes(const scoped_refptr
<GetBootTimesRequest
>& request
);
126 friend class base::RefCountedThreadSafe
<Backend
>;
130 DISALLOW_COPY_AND_ASSIGN(Backend
);
135 TimeMarker(const std::string
& name
, bool send_to_uma
)
137 time_(base::Time::NowFromSystemTime()),
138 send_to_uma_(send_to_uma
) {}
139 std::string
name() const { return name_
; }
140 base::Time
time() const { return time_
; }
141 bool send_to_uma() const { return send_to_uma_
; }
144 friend class std::vector
<TimeMarker
>;
156 static void RecordStats(
157 const std::string
& name
, const Stats
& stats
);
158 static Stats
GetCurrentStats();
159 static void WriteTimes(const std::string base_name
,
160 const std::string uma_name
,
161 const std::string uma_prefix
,
162 const std::vector
<TimeMarker
> login_times
);
165 // Used to hold the stats at main().
166 Stats chrome_main_stats_
;
167 scoped_refptr
<Backend
> backend_
;
169 // Used to track notifications for login.
170 content::NotificationRegistrar registrar_
;
171 base::AtomicSequenceNumber num_tabs_
;
172 bool have_registered_
;
174 std::vector
<TimeMarker
> login_time_markers_
;
175 std::vector
<TimeMarker
> logout_time_markers_
;
176 std::set
<RenderWidgetHost
*> render_widget_hosts_loading_
;
178 DISALLOW_COPY_AND_ASSIGN(BootTimesLoader
);
181 } // namespace chromeos
183 #endif // CHROME_BROWSER_CHROMEOS_BOOT_TIMES_LOADER_H_