1 // Copyright (c) 2012 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_RECORDER_H_
6 #define CHROME_BROWSER_CHROMEOS_BOOT_TIMES_RECORDER_H_
11 #include "base/atomic_sequence_num.h"
12 #include "base/callback_forward.h"
13 #include "base/compiler_specific.h"
14 #include "base/task/cancelable_task_tracker.h"
15 #include "base/time/time.h"
16 #include "chromeos/login_event_recorder.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "content/public/browser/render_widget_host.h"
25 // BootTimesRecorder is used to record times of boot, login, and logout.
26 class BootTimesRecorder
: public content::NotificationObserver
,
27 public LoginEventRecorder::Delegate
{
30 ~BootTimesRecorder() override
;
32 static BootTimesRecorder
* Get();
34 // LoginEventRecorder::Delegate override.
35 void AddLoginTimeMarker(const std::string
& marker_name
,
36 bool send_to_uma
) override
;
37 void RecordAuthenticationSuccess() override
;
38 void RecordAuthenticationFailure() override
;
40 // Add a time marker for logout. A timeline will be dumped to
41 // /tmp/logout-times-sent after logout is done. If |send_to_uma| is true
42 // the time between this marker and the last will be sent to UMA with
43 // the identifier ShutdownTime.|marker_name|.
44 void AddLogoutTimeMarker(const std::string
& marker_name
, bool send_to_uma
);
46 // Records current uptime and disk usage for metrics use.
47 // Posts task to file thread.
48 // name will be used as part of file names in /tmp.
49 // Existing stats files will not be overwritten.
50 void RecordCurrentStats(const std::string
& name
);
52 // Saves away the stats at main, so the can be recorded later. At main() time
53 // the necessary threads don't exist yet for recording the data.
54 void SaveChromeMainStats();
56 // Records the data previously saved by SaveChromeMainStats(), using the
57 // file thread. Existing stats files will not be overwritten.
58 void RecordChromeMainStats();
60 // Records the time that a login was attempted. This will overwrite any
61 // previous login attempt times.
62 void RecordLoginAttempted();
64 // content::NotificationObserver implementation.
65 void Observe(int type
,
66 const content::NotificationSource
& source
,
67 const content::NotificationDetails
& details
) override
;
69 // Records "LoginDone" event.
70 void LoginDone(bool is_user_new
);
72 // Writes the logout times to a /tmp/logout-times-sent. Unlike login
73 // times, we manually call this function for logout times, as we cannot
74 // rely on notification service to tell when the logout is done.
75 void WriteLogoutTimes();
77 // Mark that WriteLogoutTimes should handle restart.
78 void set_restart_requested() { restart_requested_
= true; }
80 // This is called on Chrome process startup to write saved logout stats.
81 void OnChromeProcessStart();
83 // This saves logout-started metric to Local State.
84 void OnLogoutStarted(PrefService
* state
);
89 TimeMarker(const std::string
& name
, bool send_to_uma
)
91 time_(base::Time::NowFromSystemTime()),
92 send_to_uma_(send_to_uma
) {}
93 std::string
name() const { return name_
; }
94 base::Time
time() const { return time_
; }
95 bool send_to_uma() const { return send_to_uma_
; }
97 // comparitor for sorting
98 bool operator<(const TimeMarker
& other
) const {
99 return time_
< other
.time_
;
103 friend class std::vector
<TimeMarker
>;
111 // Initializes stats with current /proc values.
112 static Stats
GetCurrentStats();
114 // Returns JSON representation.
115 std::string
SerializeToString() const;
117 // Creates new object from JSON representation.
118 static Stats
DeserializeFromString(const std::string
& value
);
120 const std::string
& uptime() const { return uptime_
; }
121 const std::string
& disk() const { return disk_
; }
123 // Writes "uptime in seconds" to result. (This is first field in uptime_.)
124 // Returns true on successful conversion.
125 bool UptimeDouble(double* result
) const;
127 void RecordStats(const std::string
& name
) const;
128 void RecordStatsWithCallback(const std::string
& name
,
129 const base::Closure
& callback
) const;
132 // Runs on BlockingPool
133 void RecordStatsImpl(const std::string
& name
) const;
139 static void WriteTimes(const std::string base_name
,
140 const std::string uma_name
,
141 const std::string uma_prefix
,
142 std::vector
<TimeMarker
> login_times
);
143 static void AddMarker(std::vector
<TimeMarker
>* vector
, TimeMarker marker
);
145 // Clear saved logout-started metric in Local State.
146 // This method is called when logout-state was writen to file.
147 static void ClearLogoutStartedLastPreference();
149 // Used to hold the stats at main().
150 Stats chrome_main_stats_
;
152 // Used to track notifications for login.
153 content::NotificationRegistrar registrar_
;
154 base::AtomicSequenceNumber num_tabs_
;
155 bool have_registered_
;
157 std::vector
<TimeMarker
> login_time_markers_
;
158 std::vector
<TimeMarker
> logout_time_markers_
;
159 std::set
<content::RenderWidgetHost
*> render_widget_hosts_loading_
;
163 bool restart_requested_
;
165 DISALLOW_COPY_AND_ASSIGN(BootTimesRecorder
);
168 } // namespace chromeos
170 #endif // CHROME_BROWSER_CHROMEOS_BOOT_TIMES_RECORDER_H_