Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_PERFORMANCE_MONITOR_PERFORMANCE_MONITOR_H_ | 5 #ifndef CHROME_BROWSER_PERFORMANCE_MONITOR_PERFORMANCE_MONITOR_H_ |
| 6 #define CHROME_BROWSER_PERFORMANCE_MONITOR_PERFORMANCE_MONITOR_H_ | 6 #define CHROME_BROWSER_PERFORMANCE_MONITOR_PERFORMANCE_MONITOR_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | |
| 10 | |
| 9 #include <string> | 11 #include <string> |
| 10 | 12 |
| 11 #include "base/callback.h" | |
| 12 #include "base/files/file_path.h" | |
| 13 #include "base/memory/linked_ptr.h" | |
| 14 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/singleton.h" | 14 #include "base/process/process_handle.h" |
| 16 #include "base/process/process_metrics.h" | 15 #include "chrome/browser/performance_monitor/event_type.h" |
| 17 #include "base/timer/timer.h" | 16 #include "chrome/browser/performance_monitor/process_metrics_history.h" |
| 18 #include "chrome/browser/performance_monitor/database.h" | |
| 19 #include "chrome/browser/performance_monitor/event.h" | |
| 20 #include "content/public/browser/notification_details.h" | |
| 21 #include "content/public/browser/notification_observer.h" | 17 #include "content/public/browser/notification_observer.h" |
| 22 #include "content/public/browser/notification_registrar.h" | 18 #include "content/public/browser/notification_registrar.h" |
| 23 #include "content/public/browser/notification_source.h" | |
| 24 #include "content/public/browser/render_process_host.h" | 19 #include "content/public/browser/render_process_host.h" |
| 25 | 20 |
| 21 template <typename Type> | |
| 22 struct DefaultSingletonTraits; | |
| 23 | |
| 24 namespace content { | |
| 25 class NotificationRegistrar; | |
| 26 } | |
| 27 | |
| 26 namespace extensions { | 28 namespace extensions { |
| 27 class Extension; | 29 class Extension; |
| 28 } | 30 } |
| 29 | 31 |
| 30 namespace net { | 32 namespace net { |
| 31 class URLRequest; | 33 class URLRequest; |
| 32 } | 34 } |
| 33 | 35 |
| 34 namespace performance_monitor { | 36 namespace performance_monitor { |
| 35 class Database; | 37 class Database; |
| 38 class Event; | |
| 39 struct Metric; | |
| 36 | 40 |
| 37 // PerformanceMonitor is a tool which will allow the user to view information | 41 // PerformanceMonitor is a tool which will allow the user to view information |
| 38 // about Chrome's performance over a period of time. It will gather statistics | 42 // about Chrome's performance over a period of time. It will gather statistics |
| 39 // pertaining to performance-oriented areas (e.g. CPU usage, memory usage, and | 43 // pertaining to performance-oriented areas (e.g. CPU usage, memory usage, and |
| 40 // network usage) and will also store information about significant events which | 44 // network usage) and will also store information about significant events which |
| 41 // are related to performance, either being indicative (e.g. crashes) or | 45 // are related to performance, either being indicative (e.g. crashes) or |
| 42 // potentially causal (e.g. extension installation/uninstallation). | 46 // potentially causal (e.g. extension installation/uninstallation). |
| 43 // | 47 // |
| 44 // Thread Safety: PerformanceMonitor lives on multiple threads. When interacting | 48 // Thread Safety: PerformanceMonitor lives on multiple threads. When interacting |
| 45 // with the Database, PerformanceMonitor acts on a background thread (which has | 49 // with the Database, PerformanceMonitor acts on a background thread (which has |
| 46 // the sequence guaranteed by a token, Database::kDatabaseSequenceToken). At | 50 // the sequence guaranteed by a token, Database::kDatabaseSequenceToken). At |
| 47 // other times, the PerformanceMonitor will act on the appropriate thread for | 51 // other times, the PerformanceMonitor will act on the appropriate thread for |
| 48 // the task (for instance, gathering statistics about CPU and memory usage | 52 // the task (for instance, gathering statistics about CPU and memory usage |
| 49 // is done on the background thread, but most notifications occur on the UI | 53 // is done on the background thread, but most notifications occur on the UI |
| 50 // thread). | 54 // thread). |
| 51 class PerformanceMonitor : public content::NotificationObserver { | 55 class PerformanceMonitor : public content::NotificationObserver { |
| 52 public: | 56 public: |
| 53 struct PerformanceDataForIOThread { | 57 struct PerformanceDataForIOThread { |
| 54 PerformanceDataForIOThread(); | 58 PerformanceDataForIOThread(); |
| 55 | 59 |
| 56 uint64 network_bytes_read; | 60 uint64 network_bytes_read; |
| 57 }; | 61 }; |
| 58 | 62 |
| 59 typedef std::map<base::ProcessHandle, | 63 typedef std::map<base::ProcessHandle, ProcessMetricsHistory> MetricsMap; |
| 60 linked_ptr<base::ProcessMetrics> > MetricsMap; | |
| 61 | 64 |
| 62 // Set the path which the PerformanceMonitor should use for the database files | 65 // Set the path which the PerformanceMonitor should use for the database files |
| 63 // constructed. This must be done prior to the initialization of the | 66 // constructed. This must be done prior to the initialization of the |
| 64 // PerformanceMonitor. Returns true on success, false on failure (failure | 67 // PerformanceMonitor. Returns true on success, false on failure (failure |
| 65 // likely indicates that PerformanceMonitor has already been started at the | 68 // likely indicates that PerformanceMonitor has already been started at the |
| 66 // time of the call). | 69 // time of the call). |
| 67 bool SetDatabasePath(const base::FilePath& path); | 70 bool SetDatabasePath(const base::FilePath& path); |
| 68 | 71 |
| 72 bool IsDatabaseLoggingEnabled() const { return database_logging_enabled_; } | |
| 73 | |
| 69 // Returns the current PerformanceMonitor instance if one exists; otherwise | 74 // Returns the current PerformanceMonitor instance if one exists; otherwise |
| 70 // constructs a new PerformanceMonitor. | 75 // constructs a new PerformanceMonitor. |
| 71 static PerformanceMonitor* GetInstance(); | 76 static PerformanceMonitor* GetInstance(); |
| 72 | 77 |
| 73 // Begins the initialization process for the PerformanceMonitor in order to | 78 // Begins the initialization process for the PerformanceMonitor in order to |
| 74 // start collecting data. | 79 // start collecting data. |
| 75 void Start(); | 80 void Start(); |
| 76 | 81 |
| 82 // Prevents Start() from starting the collection timer. Used for tests. | |
|
tonyg
2013/09/10 17:58:50
I'm not quite sure how I feel about increasing the
| |
| 83 void DisableTimerAutostartForTesting() { disable_timer_autostart_ = true; } | |
| 84 void EnableDatabaseLoggingForTesting() { database_logging_enabled_ = true; } | |
| 85 void ResetCollectionTimeForTesting() { | |
| 86 next_collection_time_ = base::Time::Now(); | |
| 87 } | |
| 88 | |
| 77 // Inform PerformanceMonitor that bytes have been read; if these came across | 89 // Inform PerformanceMonitor that bytes have been read; if these came across |
| 78 // the network (and PerformanceMonitor is initialized), then increment the | 90 // the network (and PerformanceMonitor is initialized), then increment the |
| 79 // count accordingly. | 91 // count accordingly. |
| 80 void BytesReadOnIOThread(const net::URLRequest& request, const int bytes); | 92 void BytesReadOnIOThread(const net::URLRequest& request, const int bytes); |
| 81 | 93 |
| 82 // content::NotificationObserver | 94 // content::NotificationObserver |
| 83 // Wait for various notifications; insert events into the database upon | 95 // Wait for various notifications; insert events into the database upon |
| 84 // occurance. | 96 // occurance. |
| 85 virtual void Observe(int type, | 97 virtual void Observe(int type, |
| 86 const content::NotificationSource& source, | 98 const content::NotificationSource& source, |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 | 150 |
| 139 // Perform any collections that are done on a timed basis. | 151 // Perform any collections that are done on a timed basis. |
| 140 void DoTimedCollections(); | 152 void DoTimedCollections(); |
| 141 | 153 |
| 142 // Update the database record of the last time the active profiles were | 154 // Update the database record of the last time the active profiles were |
| 143 // running; this is used in determining when an unclean exit occurred. | 155 // running; this is used in determining when an unclean exit occurred. |
| 144 void UpdateLiveProfiles(); | 156 void UpdateLiveProfiles(); |
| 145 void UpdateLiveProfilesHelper( | 157 void UpdateLiveProfilesHelper( |
| 146 scoped_ptr<std::set<std::string> > active_profiles, std::string time); | 158 scoped_ptr<std::set<std::string> > active_profiles, std::string time); |
| 147 | 159 |
| 148 // Gathers CPU usage and memory usage of all Chrome processes in order to. | 160 // Stores CPU/memory usage metrics to the database. |
| 149 void GatherStatisticsOnBackgroundThread(); | 161 void StoreMetricsOnBackgroundThread( |
| 162 int current_update_sequence, | |
| 163 const PerformanceDataForIOThread& performance_data_for_io_thread); | |
| 150 | 164 |
| 151 // Gathers the CPU usage of every Chrome process that has been running since | 165 void ProcessIsAlive(const base::ProcessHandle& handle, |
| 152 // the last call to GatherStatistics(). | 166 int process_type, |
| 153 void GatherCPUUsageOnBackgroundThread(); | 167 int current_update_sequence); |
| 154 | 168 |
| 155 // Gathers the memory usage of every process in the current list of processes. | 169 // Updates the ProcessMetrics map with the current list of processes and |
| 156 void GatherMemoryUsageOnBackgroundThread(); | 170 // gathers metrics from each entry. |
| 171 void GatherMetricsMapOnIOThread(int current_update_sequence); | |
| 157 | 172 |
| 158 // Updates the ProcessMetrics map with the current list of processes. | 173 // Called at the end of the metrics gathering process, to start |
| 159 void UpdateMetricsMapOnBackgroundThread(); | 174 // our timer for the next run. |
| 175 void ResetMetricsTimerOnUIThread(); | |
| 160 | 176 |
| 161 // Generate an appropriate ExtensionEvent for an extension-related occurrance | 177 // Generate an appropriate ExtensionEvent for an extension-related occurrance |
| 162 // and insert it in the database. | 178 // and insert it in the database. |
| 163 void AddExtensionEvent(EventType type, | 179 void AddExtensionEvent(EventType type, |
| 164 const extensions::Extension* extension); | 180 const extensions::Extension* extension); |
| 165 | 181 |
| 166 // Generate an appropriate RendererFailure for a renderer crash and insert it | 182 // Generate an appropriate RendererFailure for a renderer crash and insert it |
| 167 // in the database. | 183 // in the database. |
| 168 void AddRendererClosedEvent( | 184 void AddRendererClosedEvent( |
| 169 content::RenderProcessHost* host, | 185 content::RenderProcessHost* host, |
| 170 const content::RenderProcessHost::RendererClosedDetails& details); | 186 const content::RenderProcessHost::RendererClosedDetails& details); |
| 171 | 187 |
| 172 // Called on the IO thread, this will call InsertIOData on the background | |
| 173 // thread with a copy of the PerformanceDataForIOThread object to prevent | |
| 174 // any possible race conditions. | |
| 175 void CallInsertIOData(); | |
| 176 | |
| 177 // Insert the collected IO data into the database. | |
| 178 void InsertIOData( | |
| 179 const PerformanceDataForIOThread& performance_data_for_io_thread); | |
| 180 | |
| 181 // The store for all performance data that must be gathered from the IO | 188 // The store for all performance data that must be gathered from the IO |
| 182 // thread. | 189 // thread. |
| 183 PerformanceDataForIOThread performance_data_for_io_thread_; | 190 PerformanceDataForIOThread performance_data_for_io_thread_; |
| 184 | 191 |
| 185 // The location at which the database files are stored; if empty, the database | 192 // The location at which the database files are stored; if empty, the database |
| 186 // will default to '<user_data_dir>/performance_monitor_dbs'. | 193 // will default to '<user_data_dir>/performance_monitor_dbs'. |
| 187 base::FilePath database_path_; | 194 base::FilePath database_path_; |
| 188 | 195 |
| 189 scoped_ptr<Database> database_; | 196 scoped_ptr<Database> database_; |
| 190 | 197 |
| 191 // A map of currently running ProcessHandles to ProcessMetrics. | 198 // A map of currently running ProcessHandles to ProcessMetrics. |
| 192 scoped_ptr<MetricsMap> metrics_map_; | 199 MetricsMap metrics_map_; |
| 200 | |
| 201 // Next time we should collect averages from the performance metrics, | |
| 202 // and act on them. | |
| 203 base::Time next_collection_time_; | |
| 204 | |
| 205 // How long to wait between collections. | |
| 206 int gather_interval_in_seconds_; | |
| 207 | |
| 208 // Enable persistent logging of performance metrics to a database. | |
| 209 bool database_logging_enabled_; | |
| 193 | 210 |
| 194 // The timer to signal PerformanceMonitor to perform its timed collections. | 211 // The timer to signal PerformanceMonitor to perform its timed collections. |
| 195 base::RepeatingTimer<PerformanceMonitor> timer_; | 212 base::DelayTimer<PerformanceMonitor> timer_; |
| 196 | 213 |
| 197 content::NotificationRegistrar registrar_; | 214 content::NotificationRegistrar registrar_; |
| 198 | 215 |
| 199 // A flag indicating whether or not PerformanceMonitor is initialized. Any | 216 // A flag indicating whether or not PerformanceMonitor is initialized. Any |
| 200 // external sources accessing PerformanceMonitor should either wait for | 217 // external sources accessing PerformanceMonitor should either wait for |
| 201 // the PERFORMANCE_MONITOR_INITIALIZED notification or should check this | 218 // the PERFORMANCE_MONITOR_INITIALIZED notification or should check this |
| 202 // flag. | 219 // flag. |
| 203 static bool initialized_; | 220 static bool initialized_; |
| 204 | 221 |
| 222 // Disable auto-starting the collection timer; used for tests. | |
| 223 bool disable_timer_autostart_; | |
| 224 | |
| 205 DISALLOW_COPY_AND_ASSIGN(PerformanceMonitor); | 225 DISALLOW_COPY_AND_ASSIGN(PerformanceMonitor); |
| 206 }; | 226 }; |
| 207 | 227 |
| 208 } // namespace performance_monitor | 228 } // namespace performance_monitor |
| 209 | 229 |
| 210 #endif // CHROME_BROWSER_PERFORMANCE_MONITOR_PERFORMANCE_MONITOR_H_ | 230 #endif // CHROME_BROWSER_PERFORMANCE_MONITOR_PERFORMANCE_MONITOR_H_ |
| OLD | NEW |