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. 4 5 #ifndef CHROME_BROWSER_METRICS_PERF_PROVIDER_CHROMEOS_H_ 6 #define CHROME_BROWSER_METRICS_PERF_PROVIDER_CHROMEOS_H_ 7 8 #include <string> 9 #include <vector> 10 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 "chromeos/dbus/power_manager_client.h" 16 #include "chromeos/login/login_state.h" 17 #include "components/metrics/proto/sampled_profile.pb.h" 18 #include "content/public/browser/notification_observer.h" 19 #include "content/public/browser/notification_registrar.h" 20 21 namespace metrics { 22 23 class WindowedIncognitoObserver; 24 25 // Provides access to ChromeOS perf data. perf aka "perf events" is a 26 // performance profiling infrastructure built into the linux kernel. For more 27 // information, see: https://perf.wiki.kernel.org/index.php/Main_Page. 28 class PerfProvider : public base::NonThreadSafe, 29 public chromeos::PowerManagerClient::Observer, 30 public content::NotificationObserver { 31 public: 32 PerfProvider(); 33 virtual ~PerfProvider(); 34 35 // Stores collected perf data protobufs in |sampled_profiles|. Clears all the 36 // stored profile data. Returns true if it wrote to |sampled_profiles|. 37 bool GetSampledProfiles(std::vector<SampledProfile>* sampled_profiles); 38 39 private: 40 // Class that listens for changes to the login state. When a normal user logs 41 // in, it updates PerfProvider to start collecting data. 42 class LoginObserver : public chromeos::LoginState::Observer { 43 public: 44 explicit LoginObserver(PerfProvider* perf_provider); 45 46 // Called when either the login state or the logged in user type changes. 47 // Activates |perf_provider_| to start collecting. 48 virtual void LoggedInStateChanged() OVERRIDE; 49 50 private: 51 // Points to a PerfProvider instance that can be turned on or off based on 52 // the login state. 53 PerfProvider* perf_provider_; 54 }; 55 56 // Called when a suspend finishes. This is either a successful suspend 57 // followed by a resume, or a suspend that was canceled. Inherited from 58 // PowerManagerClient::Observer. 59 virtual void SuspendDone(const base::TimeDelta& sleep_duration) OVERRIDE; 60 61 // Turns on perf collection. Resets the timer that's used to schedule 62 // collections. 63 void OnUserLoggedIn(); 64 65 // Called when a session restore has finished. 66 // Inherited from content::NotificationObserver. 67 virtual void Observe(int type, 68 const content::NotificationSource& source, 69 const content::NotificationDetails& details) OVERRIDE; 70 71 // Turns off perf collection. Does not delete any data that was already 72 // collected and stored in |cached_perf_data_|. 73 void Deactivate(); 74 75 // Selects a random time in the upcoming profiling interval that begins at 76 // |next_profiling_interval_start_|. Schedules |timer_| to invoke 77 // DoPeriodicCollection() when that time comes. 78 void ScheduleIntervalCollection(); 79 80 // Collects perf data for a given |trigger_event|. Calls perf via the ChromeOS 81 // debug daemon's dbus interface. 82 void CollectIfNecessary(scoped_ptr<SampledProfile> sampled_profile); 83 84 // Collects perf data on a repeating basis by calling CollectIfNecessary() and 85 // reschedules it to be collected again. 86 void DoPeriodicCollection(); 87 88 // Collects perf data after a resume. |sleep_duration| is the duration the 89 // system was suspended before resuming. |time_after_resume_ms| is how long 90 // ago the system resumed. 91 void CollectPerfDataAfterResume(const base::TimeDelta& sleep_duration, 92 const base::TimeDelta& time_after_resume); 93 94 // Collects perf data after a session restore. |time_after_restore| is how 95 // long ago the session restore started. 96 void CollectPerfDataAfterSessionRestore( 97 const base::TimeDelta& time_after_restore); 98 99 // Parses a perf data protobuf from the |data| passed in only if the 100 // |incognito_observer| indicates that no incognito window had been opened 101 // during the profile collection period. 102 // |trigger_event| is the cause of the perf data collection. 103 void ParseProtoIfValid( 104 scoped_ptr<WindowedIncognitoObserver> incognito_observer, 105 scoped_ptr<SampledProfile> sampled_profile, 106 const std::vector<uint8>& data); 107 108 // Vector of SampledProfile protobufs containing perf profiles. 109 std::vector<SampledProfile> cached_perf_data_; 110 111 // For scheduling collection of perf data. 112 base::OneShotTimer<PerfProvider> timer_; 113 114 // For detecting when changes to the login state. 115 LoginObserver login_observer_; 116 117 // Record of the last login time. 118 base::TimeTicks login_time_; 119 120 // Record of the start of the upcoming profiling interval. 121 base::TimeTicks next_profiling_interval_start_; 122 123 // Used to register objects of this class as observers to be notified of 124 // session restore events. 125 content::NotificationRegistrar session_restore_registrar_; 126 127 // Tracks the last time a session restore was collected. 128 base::TimeTicks last_session_restore_collection_time_; 129 130 // To pass around the "this" pointer across threads safely. 131 base::WeakPtrFactory<PerfProvider> weak_factory_; 132 133 DISALLOW_COPY_AND_ASSIGN(PerfProvider); 134 }; 135 136 } // namespace metrics 137 138 #endif // CHROME_BROWSER_METRICS_PERF_PROVIDER_CHROMEOS_H_ 139