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 // Class that listens for changes to the login state. When a normal user logs
39 // in, it updates PerfProvider to start collecting data.
40 class LoginObserver
: public chromeos::LoginState::Observer
{
42 explicit LoginObserver(PerfProvider
* perf_provider
);
44 // Called when either the login state or the logged in user type changes.
45 // Activates |perf_provider_| to start collecting.
46 void LoggedInStateChanged() override
;
49 // Points to a PerfProvider instance that can be turned on or off based on
51 PerfProvider
* perf_provider_
;
54 // Called when a suspend finishes. This is either a successful suspend
55 // followed by a resume, or a suspend that was canceled. Inherited from
56 // PowerManagerClient::Observer.
57 void SuspendDone(const base::TimeDelta
& sleep_duration
) override
;
59 // Turns on perf collection. Resets the timer that's used to schedule
61 void OnUserLoggedIn();
63 // Called when a session restore has finished.
64 void OnSessionRestoreDone(int num_tabs_restored
);
66 // Turns off perf collection. Does not delete any data that was already
67 // collected and stored in |cached_perf_data_|.
70 // Selects a random time in the upcoming profiling interval that begins at
71 // |next_profiling_interval_start_|. Schedules |timer_| to invoke
72 // DoPeriodicCollection() when that time comes.
73 void ScheduleIntervalCollection();
75 // Collects perf data for a given |trigger_event|. Calls perf via the ChromeOS
76 // debug daemon's dbus interface.
77 void CollectIfNecessary(scoped_ptr
<SampledProfile
> sampled_profile
);
79 // Collects perf data on a repeating basis by calling CollectIfNecessary() and
80 // reschedules it to be collected again.
81 void DoPeriodicCollection();
83 // Collects perf data after a resume. |sleep_duration| is the duration the
84 // system was suspended before resuming. |time_after_resume_ms| is how long
85 // ago the system resumed.
86 void CollectPerfDataAfterResume(const base::TimeDelta
& sleep_duration
,
87 const base::TimeDelta
& time_after_resume
);
89 // Collects perf data after a session restore. |time_after_restore| is how
90 // long ago the session restore started. |num_tabs_restored| is the total
91 // number of tabs being restored.
92 void CollectPerfDataAfterSessionRestore(
93 const base::TimeDelta
& time_after_restore
,
94 int num_tabs_restored
);
96 // Parses a perf data protobuf from the |data| passed in only if the
97 // |incognito_observer| indicates that no incognito window had been opened
98 // during the profile collection period.
99 // |trigger_event| is the cause of the perf data collection.
100 void ParseProtoIfValid(
101 scoped_ptr
<WindowedIncognitoObserver
> incognito_observer
,
102 scoped_ptr
<SampledProfile
> sampled_profile
,
103 const std::vector
<uint8
>& data
);
105 // Vector of SampledProfile protobufs containing perf profiles.
106 std::vector
<SampledProfile
> cached_perf_data_
;
108 // For scheduling collection of perf data.
109 base::OneShotTimer
<PerfProvider
> timer_
;
111 // For detecting when changes to the login state.
112 LoginObserver login_observer_
;
114 // Record of the last login time.
115 base::TimeTicks login_time_
;
117 // Record of the start of the upcoming profiling interval.
118 base::TimeTicks next_profiling_interval_start_
;
120 // Tracks the last time a session restore was collected.
121 base::TimeTicks last_session_restore_collection_time_
;
123 // Points to the on-session-restored callback that was registered with
124 // SessionRestore's callback list. When objects of this class are destroyed,
125 // the subscription object's destructor will automatically unregister the
126 // callback in SessionRestore, so that the callback list does not contain any
127 // obsolete callbacks.
128 SessionRestore::CallbackSubscription
129 on_session_restored_callback_subscription_
;
131 // To pass around the "this" pointer across threads safely.
132 base::WeakPtrFactory
<PerfProvider
> weak_factory_
;
134 DISALLOW_COPY_AND_ASSIGN(PerfProvider
);
137 } // namespace metrics
139 #endif // CHROME_BROWSER_METRICS_PERF_PROVIDER_CHROMEOS_H_