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 <string> | 9 #include <string> |
10 | 10 |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "content/public/browser/notification_details.h" | 21 #include "content/public/browser/notification_details.h" |
22 #include "content/public/browser/notification_observer.h" | 22 #include "content/public/browser/notification_observer.h" |
23 #include "content/public/browser/notification_registrar.h" | 23 #include "content/public/browser/notification_registrar.h" |
24 #include "content/public/browser/notification_source.h" | 24 #include "content/public/browser/notification_source.h" |
25 #include "content/public/browser/render_process_host.h" | 25 #include "content/public/browser/render_process_host.h" |
26 | 26 |
27 namespace extensions { | 27 namespace extensions { |
28 class Extension; | 28 class Extension; |
29 } | 29 } |
30 | 30 |
| 31 namespace net { |
| 32 class URLRequest; |
| 33 } |
| 34 |
31 namespace performance_monitor { | 35 namespace performance_monitor { |
32 class Database; | 36 class Database; |
33 | 37 |
| 38 // PerformanceMonitor is a tool which will allow the user to view information |
| 39 // about Chrome's performance over a period of time. It will gather statistics |
| 40 // pertaining to performance-oriented areas (e.g. CPU usage, memory usage, and |
| 41 // network usage) and will also store information about significant events which |
| 42 // are related to performance, either being indicative (e.g. crashes) or |
| 43 // potentially causal (e.g. extension installation/uninstallation). |
| 44 // |
| 45 // Thread Safety: PerformanceMonitor lives on multiple threads. When interacting |
| 46 // with the Database, PerformanceMonitor acts on a background thread (which has |
| 47 // the sequence guaranteed by a token, Database::kDatabaseSequenceToken). At |
| 48 // other times, the PerformanceMonitor will act on the appropriate thread for |
| 49 // the task (for instance, gathering statistics about CPU and memory usage |
| 50 // is done on the background thread, but most notifications occur on the UI |
| 51 // thread). |
34 class PerformanceMonitor : public content::NotificationObserver { | 52 class PerformanceMonitor : public content::NotificationObserver { |
35 public: | 53 public: |
| 54 struct PerformanceDataForIOThread { |
| 55 PerformanceDataForIOThread(); |
| 56 |
| 57 uint64 network_bytes_read; |
| 58 }; |
| 59 |
36 typedef std::map<base::ProcessHandle, | 60 typedef std::map<base::ProcessHandle, |
37 linked_ptr<base::ProcessMetrics> > MetricsMap; | 61 linked_ptr<base::ProcessMetrics> > MetricsMap; |
38 | 62 |
39 // 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 |
40 // constructed. This must be done prior to the initialization of the | 64 // constructed. This must be done prior to the initialization of the |
41 // PerformanceMonitor. Returns true on success, false on failure (failure | 65 // PerformanceMonitor. Returns true on success, false on failure (failure |
42 // likely indicates that PerformanceMonitor has already been started at the | 66 // likely indicates that PerformanceMonitor has already been started at the |
43 // time of the call). | 67 // time of the call). |
44 bool SetDatabasePath(const FilePath& path); | 68 bool SetDatabasePath(const FilePath& path); |
45 | 69 |
46 // Returns the current PerformanceMonitor instance if one exists; otherwise | 70 // Returns the current PerformanceMonitor instance if one exists; otherwise |
47 // constructs a new PerformanceMonitor. | 71 // constructs a new PerformanceMonitor. |
48 static PerformanceMonitor* GetInstance(); | 72 static PerformanceMonitor* GetInstance(); |
49 | 73 |
50 // Begins the initialization process for the PerformanceMonitor in order to | 74 // Begins the initialization process for the PerformanceMonitor in order to |
51 // start collecting data. | 75 // start collecting data. |
52 void Start(); | 76 void Start(); |
53 | 77 |
| 78 // Inform PerformanceMonitor that bytes have been read; if these came across |
| 79 // the network, then increment the count accordingly. |
| 80 void BytesReadOnIOThread(const net::URLRequest& request, const int bytes); |
| 81 |
54 // content::NotificationObserver | 82 // content::NotificationObserver |
55 // Wait for various notifications; insert events into the database upon | 83 // Wait for various notifications; insert events into the database upon |
56 // occurance. | 84 // occurance. |
57 virtual void Observe(int type, | 85 virtual void Observe(int type, |
58 const content::NotificationSource& source, | 86 const content::NotificationSource& source, |
59 const content::NotificationDetails& details) OVERRIDE; | 87 const content::NotificationDetails& details) OVERRIDE; |
60 | 88 |
61 Database* database() { return database_.get(); } | 89 Database* database() { return database_.get(); } |
62 FilePath database_path() { return database_path_; } | 90 FilePath database_path() { return database_path_; } |
| 91 static bool initialized() { return initialized_; } |
63 | 92 |
64 private: | 93 private: |
65 friend struct DefaultSingletonTraits<PerformanceMonitor>; | 94 friend struct DefaultSingletonTraits<PerformanceMonitor>; |
66 friend class PerformanceMonitorBrowserTest; | 95 friend class PerformanceMonitorBrowserTest; |
67 FRIEND_TEST_ALL_PREFIXES(PerformanceMonitorUncleanExitBrowserTest, | 96 FRIEND_TEST_ALL_PREFIXES(PerformanceMonitorUncleanExitBrowserTest, |
68 OneProfileUncleanExit); | 97 OneProfileUncleanExit); |
69 FRIEND_TEST_ALL_PREFIXES(PerformanceMonitorUncleanExitBrowserTest, | 98 FRIEND_TEST_ALL_PREFIXES(PerformanceMonitorUncleanExitBrowserTest, |
70 TwoProfileUncleanExit); | 99 TwoProfileUncleanExit); |
| 100 FRIEND_TEST_ALL_PREFIXES(PerformanceMonitorBrowserTest, NetworkBytesRead); |
71 | 101 |
72 PerformanceMonitor(); | 102 PerformanceMonitor(); |
73 virtual ~PerformanceMonitor(); | 103 virtual ~PerformanceMonitor(); |
74 | 104 |
75 // Perform any additional initialization which must be performed on a | 105 // Perform any additional initialization which must be performed on a |
76 // background thread (e.g. constructing the database). | 106 // background thread (e.g. constructing the database). |
77 void InitOnBackgroundThread(); | 107 void InitOnBackgroundThread(); |
78 | 108 |
79 void FinishInit(); | 109 void FinishInit(); |
80 | 110 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 // Generate an appropriate ExtensionEvent for an extension-related occurrance | 161 // Generate an appropriate ExtensionEvent for an extension-related occurrance |
132 // and insert it in the database. | 162 // and insert it in the database. |
133 void AddExtensionEvent(EventType type, | 163 void AddExtensionEvent(EventType type, |
134 const extensions::Extension* extension); | 164 const extensions::Extension* extension); |
135 | 165 |
136 // Generate an appropriate CrashEvent for a renderer crash and insert it in | 166 // Generate an appropriate CrashEvent for a renderer crash and insert it in |
137 // the database. | 167 // the database. |
138 void AddCrashEvent( | 168 void AddCrashEvent( |
139 const content::RenderProcessHost::RendererClosedDetails& details); | 169 const content::RenderProcessHost::RendererClosedDetails& details); |
140 | 170 |
| 171 // Called on the IO thread, this will call InsertIOData on the background |
| 172 // thread with a copy of the PerformanceDataForIOThread object to prevent |
| 173 // any possible race conditions. |
| 174 void CallInsertIOData(); |
| 175 |
| 176 // Insert the collected IO data into the database (deliberately not const & so |
| 177 // we create a copy and it becomes thread-safe). |
| 178 void InsertIOData(PerformanceDataForIOThread performance_data_for_io_thread); |
| 179 |
| 180 // The store for all performance data that must be gathered from the IO |
| 181 // thread. |
| 182 PerformanceDataForIOThread performance_data_for_io_thread_; |
| 183 |
141 // The location at which the database files are stored; if empty, the database | 184 // The location at which the database files are stored; if empty, the database |
142 // will default to '<user_data_dir>/performance_monitor_dbs'. | 185 // will default to '<user_data_dir>/performance_monitor_dbs'. |
143 FilePath database_path_; | 186 FilePath database_path_; |
144 | 187 |
145 scoped_ptr<Database> database_; | 188 scoped_ptr<Database> database_; |
146 | 189 |
147 // A map of currently running ProcessHandles to ProcessMetrics. | 190 // A map of currently running ProcessHandles to ProcessMetrics. |
148 MetricsMap metrics_map_; | 191 MetricsMap metrics_map_; |
149 | 192 |
150 // The timer to signal PerformanceMonitor to perform its timed collections. | 193 // The timer to signal PerformanceMonitor to perform its timed collections. |
151 base::RepeatingTimer<PerformanceMonitor> timer_; | 194 base::RepeatingTimer<PerformanceMonitor> timer_; |
152 | 195 |
153 content::NotificationRegistrar registrar_; | 196 content::NotificationRegistrar registrar_; |
154 | 197 |
| 198 // A flag indicating whether or not PerformanceMonitor is initialized. Any |
| 199 // external sources accessing PerformanceMonitor should either wait for |
| 200 // the PERFORMANCE_MONITOR_INITIALIZED notification or should check this |
| 201 // flag. |
| 202 static bool initialized_; |
| 203 |
155 DISALLOW_COPY_AND_ASSIGN(PerformanceMonitor); | 204 DISALLOW_COPY_AND_ASSIGN(PerformanceMonitor); |
156 }; | 205 }; |
157 | 206 |
158 } // namespace performance_monitor | 207 } // namespace performance_monitor |
159 | 208 |
160 #endif // CHROME_BROWSER_PERFORMANCE_MONITOR_PERFORMANCE_MONITOR_H_ | 209 #endif // CHROME_BROWSER_PERFORMANCE_MONITOR_PERFORMANCE_MONITOR_H_ |
OLD | NEW |