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