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_METRICS_PERF_PROVIDER_CHROMEOS_H_
6 #define CHROME_BROWSER_METRICS_PERF_PROVIDER_CHROMEOS_H_
11 #include "base/basictypes.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "base/time/time.h"
14 #include "base/timer/timer.h"
15 #include "chrome/browser/sessions/session_restore.h"
16 #include "chromeos/dbus/power_manager_client.h"
17 #include "chromeos/login/login_state.h"
18 #include "components/metrics/proto/sampled_profile.pb.h"
22 class WindowedIncognitoObserver
;
24 // Provides access to ChromeOS perf data. perf aka "perf events" is a
25 // performance profiling infrastructure built into the linux kernel. For more
26 // information, see: https://perf.wiki.kernel.org/index.php/Main_Page.
27 class PerfProvider
: public base::NonThreadSafe
,
28 public chromeos::PowerManagerClient::Observer
{
31 ~PerfProvider() override
;
33 // Stores collected perf data protobufs in |sampled_profiles|. Clears all the
34 // stored profile data. Returns true if it wrote to |sampled_profiles|.
35 bool GetSampledProfiles(std::vector
<SampledProfile
>* sampled_profiles
);
38 // Parses a PerfDataProto from serialized data |perf_data|, if it exists.
39 // Parses a PerfStatProto from serialized data |perf_stat|, if it exists.
40 // Only one of these may contain data. If both |perf_data| and |perf_stat|
41 // contain data, it is counted as an error and neither is parsed.
42 // |incognito_observer| indicates whether an incognito window had been opened
43 // during the profile collection period. If there was an incognito window,
44 // discard the incoming data.
45 // |trigger_event| is the cause of the perf data collection.
46 // |result| is the return value of running perf/quipper. It is 0 if successful
47 // and nonzero if not successful.
48 void ParseOutputProtoIfValid(
49 scoped_ptr
<WindowedIncognitoObserver
> incognito_observer
,
50 scoped_ptr
<SampledProfile
> sampled_profile
,
52 const std::vector
<uint8
>& perf_data
,
53 const std::vector
<uint8
>& perf_stat
);
56 // Class that listens for changes to the login state. When a normal user logs
57 // in, it updates PerfProvider to start collecting data.
58 class LoginObserver
: public chromeos::LoginState::Observer
{
60 explicit LoginObserver(PerfProvider
* perf_provider
);
62 // Called when either the login state or the logged in user type changes.
63 // Activates |perf_provider_| to start collecting.
64 void LoggedInStateChanged() override
;
67 // Points to a PerfProvider instance that can be turned on or off based on
69 PerfProvider
* perf_provider_
;
72 // Called when a suspend finishes. This is either a successful suspend
73 // followed by a resume, or a suspend that was canceled. Inherited from
74 // PowerManagerClient::Observer.
75 void SuspendDone(const base::TimeDelta
& sleep_duration
) override
;
77 // Turns on perf collection. Resets the timer that's used to schedule
79 void OnUserLoggedIn();
81 // Called when a session restore has finished.
82 void OnSessionRestoreDone(int num_tabs_restored
);
84 // Turns off perf collection. Does not delete any data that was already
85 // collected and stored in |cached_perf_data_|.
88 // Selects a random time in the upcoming profiling interval that begins at
89 // |next_profiling_interval_start_|. Schedules |timer_| to invoke
90 // DoPeriodicCollection() when that time comes.
91 void ScheduleIntervalCollection();
93 // Collects perf data for a given |trigger_event|. Calls perf via the ChromeOS
94 // debug daemon's dbus interface.
95 void CollectIfNecessary(scoped_ptr
<SampledProfile
> sampled_profile
);
97 // Collects perf data on a repeating basis by calling CollectIfNecessary() and
98 // reschedules it to be collected again.
99 void DoPeriodicCollection();
101 // Collects perf data after a resume. |sleep_duration| is the duration the
102 // system was suspended before resuming. |time_after_resume_ms| is how long
103 // ago the system resumed.
104 void CollectPerfDataAfterResume(const base::TimeDelta
& sleep_duration
,
105 const base::TimeDelta
& time_after_resume
);
107 // Collects perf data after a session restore. |time_after_restore| is how
108 // long ago the session restore started. |num_tabs_restored| is the total
109 // number of tabs being restored.
110 void CollectPerfDataAfterSessionRestore(
111 const base::TimeDelta
& time_after_restore
,
112 int num_tabs_restored
);
114 // Vector of SampledProfile protobufs containing perf profiles.
115 std::vector
<SampledProfile
> cached_perf_data_
;
117 // For scheduling collection of perf data.
118 base::OneShotTimer
<PerfProvider
> timer_
;
120 // For detecting when changes to the login state.
121 LoginObserver login_observer_
;
123 // Record of the last login time.
124 base::TimeTicks login_time_
;
126 // Record of the start of the upcoming profiling interval.
127 base::TimeTicks next_profiling_interval_start_
;
129 // Tracks the last time a session restore was collected.
130 base::TimeTicks last_session_restore_collection_time_
;
132 // Points to the on-session-restored callback that was registered with
133 // SessionRestore's callback list. When objects of this class are destroyed,
134 // the subscription object's destructor will automatically unregister the
135 // callback in SessionRestore, so that the callback list does not contain any
136 // obsolete callbacks.
137 SessionRestore::CallbackSubscription
138 on_session_restored_callback_subscription_
;
140 // To pass around the "this" pointer across threads safely.
141 base::WeakPtrFactory
<PerfProvider
> weak_factory_
;
143 DISALLOW_COPY_AND_ASSIGN(PerfProvider
);
146 } // namespace metrics
148 #endif // CHROME_BROWSER_METRICS_PERF_PROVIDER_CHROMEOS_H_