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> |
9 #include <string> | 10 #include <string> |
10 | 11 |
11 #include "base/callback.h" | |
12 #include "base/files/file_path.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 "base/time/time.h" |
17 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
18 #include "chrome/browser/performance_monitor/database.h" | 17 #include "chrome/browser/performance_monitor/event_type.h" |
19 #include "chrome/browser/performance_monitor/event.h" | 18 #include "chrome/browser/performance_monitor/process_metrics_history.h" |
20 #include "content/public/browser/notification_details.h" | |
21 #include "content/public/browser/notification_observer.h" | 19 #include "content/public/browser/notification_observer.h" |
22 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
23 #include "content/public/browser/notification_source.h" | |
24 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
25 | 22 |
| 23 template <typename Type> |
| 24 struct DefaultSingletonTraits; |
| 25 |
26 namespace extensions { | 26 namespace extensions { |
27 class Extension; | 27 class Extension; |
28 } | 28 } |
29 | 29 |
30 namespace net { | 30 namespace net { |
31 class URLRequest; | 31 class URLRequest; |
32 } | 32 } |
33 | 33 |
34 namespace performance_monitor { | 34 namespace performance_monitor { |
35 class Database; | 35 class Database; |
| 36 class Event; |
| 37 struct Metric; |
36 | 38 |
37 // PerformanceMonitor is a tool which will allow the user to view information | 39 // 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 | 40 // 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 | 41 // pertaining to performance-oriented areas (e.g. CPU usage, memory usage, and |
40 // network usage) and will also store information about significant events which | 42 // network usage) and will also store information about significant events which |
41 // are related to performance, either being indicative (e.g. crashes) or | 43 // are related to performance, either being indicative (e.g. crashes) or |
42 // potentially causal (e.g. extension installation/uninstallation). | 44 // potentially causal (e.g. extension installation/uninstallation). |
43 // | 45 // |
44 // Thread Safety: PerformanceMonitor lives on multiple threads. When interacting | 46 // Thread Safety: PerformanceMonitor lives on multiple threads. When interacting |
45 // with the Database, PerformanceMonitor acts on a background thread (which has | 47 // with the Database, PerformanceMonitor acts on a background thread (which has |
46 // the sequence guaranteed by a token, Database::kDatabaseSequenceToken). At | 48 // the sequence guaranteed by a token, Database::kDatabaseSequenceToken). At |
47 // other times, the PerformanceMonitor will act on the appropriate thread for | 49 // other times, the PerformanceMonitor will act on the appropriate thread for |
48 // the task (for instance, gathering statistics about CPU and memory usage | 50 // 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 | 51 // is done on the background thread, but most notifications occur on the UI |
50 // thread). | 52 // thread). |
51 class PerformanceMonitor : public content::NotificationObserver { | 53 class PerformanceMonitor : public content::NotificationObserver { |
52 public: | 54 public: |
53 struct PerformanceDataForIOThread { | 55 struct PerformanceDataForIOThread { |
54 PerformanceDataForIOThread(); | 56 PerformanceDataForIOThread(); |
55 | 57 |
56 uint64 network_bytes_read; | 58 uint64 network_bytes_read; |
57 }; | 59 }; |
58 | 60 |
59 typedef std::map<base::ProcessHandle, | 61 typedef std::map<base::ProcessHandle, ProcessMetricsHistory> MetricsMap; |
60 linked_ptr<base::ProcessMetrics> > MetricsMap; | |
61 | 62 |
62 // Set the path which the PerformanceMonitor should use for the database files | 63 // Set the path which the PerformanceMonitor should use for the database files |
63 // constructed. This must be done prior to the initialization of the | 64 // constructed. This must be done prior to the initialization of the |
64 // PerformanceMonitor. Returns true on success, false on failure (failure | 65 // PerformanceMonitor. Returns true on success, false on failure (failure |
65 // likely indicates that PerformanceMonitor has already been started at the | 66 // likely indicates that PerformanceMonitor has already been started at the |
66 // time of the call). | 67 // time of the call). |
67 bool SetDatabasePath(const base::FilePath& path); | 68 bool SetDatabasePath(const base::FilePath& path); |
68 | 69 |
| 70 bool database_logging_enabled() const { return database_logging_enabled_; } |
| 71 |
69 // Returns the current PerformanceMonitor instance if one exists; otherwise | 72 // Returns the current PerformanceMonitor instance if one exists; otherwise |
70 // constructs a new PerformanceMonitor. | 73 // constructs a new PerformanceMonitor. |
71 static PerformanceMonitor* GetInstance(); | 74 static PerformanceMonitor* GetInstance(); |
72 | 75 |
73 // Begins the initialization process for the PerformanceMonitor in order to | 76 // Begins the initialization process for the PerformanceMonitor in order to |
74 // start collecting data. | 77 // start collecting data. |
75 void Start(); | 78 void Start(); |
76 | 79 |
77 // Inform PerformanceMonitor that bytes have been read; if these came across | 80 // Inform PerformanceMonitor that bytes have been read; if these came across |
78 // the network (and PerformanceMonitor is initialized), then increment the | 81 // the network (and PerformanceMonitor is initialized), then increment the |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 void AddMetricOnBackgroundThread(const Metric& metric); | 137 void AddMetricOnBackgroundThread(const Metric& metric); |
135 | 138 |
136 // Notify any listeners that PerformanceMonitor has finished the initializing. | 139 // Notify any listeners that PerformanceMonitor has finished the initializing. |
137 void NotifyInitialized(); | 140 void NotifyInitialized(); |
138 | 141 |
139 // Perform any collections that are done on a timed basis. | 142 // Perform any collections that are done on a timed basis. |
140 void DoTimedCollections(); | 143 void DoTimedCollections(); |
141 | 144 |
142 // Update the database record of the last time the active profiles were | 145 // Update the database record of the last time the active profiles were |
143 // running; this is used in determining when an unclean exit occurred. | 146 // running; this is used in determining when an unclean exit occurred. |
| 147 #if !defined(OS_ANDROID) |
144 void UpdateLiveProfiles(); | 148 void UpdateLiveProfiles(); |
145 void UpdateLiveProfilesHelper( | 149 void UpdateLiveProfilesHelper( |
146 scoped_ptr<std::set<std::string> > active_profiles, std::string time); | 150 scoped_ptr<std::set<std::string> > active_profiles, std::string time); |
| 151 #endif |
147 | 152 |
148 // Gathers CPU usage and memory usage of all Chrome processes in order to. | 153 // Stores CPU/memory usage metrics to the database. |
149 void GatherStatisticsOnBackgroundThread(); | 154 void StoreMetricsOnBackgroundThread( |
| 155 int current_update_sequence, |
| 156 const PerformanceDataForIOThread& performance_data_for_io_thread); |
150 | 157 |
151 // Gathers the CPU usage of every Chrome process that has been running since | 158 // Mark the given process as alive in the current update iteration. |
152 // the last call to GatherStatistics(). | 159 // This means adding an entry to the map of watched processes if it's not |
153 void GatherCPUUsageOnBackgroundThread(); | 160 // already present. |
| 161 void MarkProcessAsAlive(const base::ProcessHandle& handle, |
| 162 int process_type, |
| 163 int current_update_sequence); |
154 | 164 |
155 // Gathers the memory usage of every process in the current list of processes. | 165 // Updates the ProcessMetrics map with the current list of processes and |
156 void GatherMemoryUsageOnBackgroundThread(); | 166 // gathers metrics from each entry. |
| 167 void GatherMetricsMapOnUIThread(); |
| 168 void GatherMetricsMapOnIOThread(int current_update_sequence); |
157 | 169 |
158 // Updates the ProcessMetrics map with the current list of processes. | 170 // Called at the end of the metrics gathering process, to start |
159 void UpdateMetricsMapOnBackgroundThread(); | 171 // our timer for the next run. |
| 172 void ResetMetricsTimerOnUIThread(); |
160 | 173 |
161 // Generate an appropriate ExtensionEvent for an extension-related occurrance | 174 // Generate an appropriate ExtensionEvent for an extension-related occurrance |
162 // and insert it in the database. | 175 // and insert it in the database. |
163 void AddExtensionEvent(EventType type, | 176 void AddExtensionEvent(EventType type, |
164 const extensions::Extension* extension); | 177 const extensions::Extension* extension); |
165 | 178 |
166 // Generate an appropriate RendererFailure for a renderer crash and insert it | 179 // Generate an appropriate RendererFailure for a renderer crash and insert it |
167 // in the database. | 180 // in the database. |
168 void AddRendererClosedEvent( | 181 void AddRendererClosedEvent( |
169 content::RenderProcessHost* host, | 182 content::RenderProcessHost* host, |
170 const content::RenderProcessHost::RendererClosedDetails& details); | 183 const content::RenderProcessHost::RendererClosedDetails& details); |
171 | 184 |
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 | 185 // The store for all performance data that must be gathered from the IO |
182 // thread. | 186 // thread. |
183 PerformanceDataForIOThread performance_data_for_io_thread_; | 187 PerformanceDataForIOThread performance_data_for_io_thread_; |
184 | 188 |
185 // The location at which the database files are stored; if empty, the database | 189 // The location at which the database files are stored; if empty, the database |
186 // will default to '<user_data_dir>/performance_monitor_dbs'. | 190 // will default to '<user_data_dir>/performance_monitor_dbs'. |
187 base::FilePath database_path_; | 191 base::FilePath database_path_; |
188 | 192 |
189 scoped_ptr<Database> database_; | 193 scoped_ptr<Database> database_; |
190 | 194 |
191 // A map of currently running ProcessHandles to ProcessMetrics. | 195 // A map of currently running ProcessHandles to ProcessMetrics. |
192 scoped_ptr<MetricsMap> metrics_map_; | 196 MetricsMap metrics_map_; |
| 197 |
| 198 // The next time we should collect averages from the performance metrics |
| 199 // and act on them. |
| 200 base::Time next_collection_time_; |
| 201 |
| 202 // How long to wait between collections. |
| 203 int gather_interval_in_seconds_; |
| 204 |
| 205 // Enable persistent logging of performance metrics to a database. |
| 206 bool database_logging_enabled_; |
193 | 207 |
194 // The timer to signal PerformanceMonitor to perform its timed collections. | 208 // The timer to signal PerformanceMonitor to perform its timed collections. |
195 base::RepeatingTimer<PerformanceMonitor> timer_; | 209 base::DelayTimer<PerformanceMonitor> timer_; |
196 | 210 |
197 content::NotificationRegistrar registrar_; | 211 content::NotificationRegistrar registrar_; |
198 | 212 |
199 // A flag indicating whether or not PerformanceMonitor is initialized. Any | 213 // A flag indicating whether or not PerformanceMonitor is initialized. Any |
200 // external sources accessing PerformanceMonitor should either wait for | 214 // external sources accessing PerformanceMonitor should either wait for |
201 // the PERFORMANCE_MONITOR_INITIALIZED notification or should check this | 215 // the PERFORMANCE_MONITOR_INITIALIZED notification or should check this |
202 // flag. | 216 // flag. |
203 static bool initialized_; | 217 static bool initialized_; |
204 | 218 |
| 219 // Disable auto-starting the collection timer; used for tests. |
| 220 bool disable_timer_autostart_for_testing_; |
| 221 |
205 DISALLOW_COPY_AND_ASSIGN(PerformanceMonitor); | 222 DISALLOW_COPY_AND_ASSIGN(PerformanceMonitor); |
206 }; | 223 }; |
207 | 224 |
208 } // namespace performance_monitor | 225 } // namespace performance_monitor |
209 | 226 |
210 #endif // CHROME_BROWSER_PERFORMANCE_MONITOR_PERFORMANCE_MONITOR_H_ | 227 #endif // CHROME_BROWSER_PERFORMANCE_MONITOR_PERFORMANCE_MONITOR_H_ |
OLD | NEW |