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 <string> | 9 #include <string> |
9 | 10 |
10 #include "base/callback.h" | 11 #include "base/callback.h" |
11 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 13 #include "base/memory/linked_ptr.h" |
12 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/singleton.h" | 15 #include "base/memory/singleton.h" |
| 16 #include "base/process.h" |
| 17 #include "base/process_util.h" |
14 #include "base/timer.h" | 18 #include "base/timer.h" |
15 #include "chrome/browser/performance_monitor/database.h" | 19 #include "chrome/browser/performance_monitor/database.h" |
16 #include "chrome/browser/performance_monitor/event.h" | 20 #include "chrome/browser/performance_monitor/event.h" |
17 #include "content/public/browser/notification_details.h" | 21 #include "content/public/browser/notification_details.h" |
18 #include "content/public/browser/notification_observer.h" | 22 #include "content/public/browser/notification_observer.h" |
19 #include "content/public/browser/notification_registrar.h" | 23 #include "content/public/browser/notification_registrar.h" |
20 #include "content/public/browser/notification_source.h" | 24 #include "content/public/browser/notification_source.h" |
21 | 25 |
22 namespace performance_monitor { | 26 namespace performance_monitor { |
23 class Database; | 27 class Database; |
24 | 28 |
25 class PerformanceMonitor : public content::NotificationObserver { | 29 class PerformanceMonitor : public content::NotificationObserver { |
26 public: | 30 public: |
27 typedef base::Callback<void(const std::string&)> StateValueCallback; | 31 typedef base::Callback<void(const std::string&)> StateValueCallback; |
28 | 32 |
| 33 typedef std::map<base::ProcessHandle, |
| 34 linked_ptr<base::ProcessMetrics> > MetricsMap; |
| 35 |
29 // Set the path which the PerformanceMonitor should use for the database files | 36 // Set the path which the PerformanceMonitor should use for the database files |
30 // constructed. This must be done prior to the initialization of the | 37 // constructed. This must be done prior to the initialization of the |
31 // PerformanceMonitor. Returns true on success, false on failure (failure | 38 // PerformanceMonitor. Returns true on success, false on failure (failure |
32 // likely indicates that PerformanceMonitor has already been started at the | 39 // likely indicates that PerformanceMonitor has already been started at the |
33 // time of the call). | 40 // time of the call). |
34 bool SetDatabasePath(const FilePath& path); | 41 bool SetDatabasePath(const FilePath& path); |
35 | 42 |
36 // Returns the current PerformanceMonitor instance if one exists; otherwise | 43 // Returns the current PerformanceMonitor instance if one exists; otherwise |
37 // constructs a new PerformanceMonitor. | 44 // constructs a new PerformanceMonitor. |
38 static PerformanceMonitor* GetInstance(); | 45 static PerformanceMonitor* GetInstance(); |
39 | 46 |
40 // Begins the initialization process for the PerformanceMonitor in order to | 47 // Begins the initialization process for the PerformanceMonitor in order to |
41 // start collecting data. | 48 // start collecting data. |
42 void Start(); | 49 void Start(); |
43 | 50 |
| 51 // Gathers CPU usage and memory usage of all Chrome processes. |
| 52 void GatherStatisticsOnBackgroundThread(); |
| 53 |
44 // content::NotificationObserver | 54 // content::NotificationObserver |
45 // Wait for various notifications; insert events into the database upon | 55 // Wait for various notifications; insert events into the database upon |
46 // occurance. | 56 // occurance. |
47 virtual void Observe(int type, | 57 virtual void Observe(int type, |
48 const content::NotificationSource& source, | 58 const content::NotificationSource& source, |
49 const content::NotificationDetails& details) OVERRIDE; | 59 const content::NotificationDetails& details) OVERRIDE; |
50 | 60 |
51 Database* database() { return database_.get(); } | 61 Database* database() { return database_.get(); } |
52 FilePath database_path() { return database_path_; } | 62 FilePath database_path() { return database_path_; } |
53 | 63 |
(...skipping 28 matching lines...) Expand all Loading... |
82 | 92 |
83 // Check the previous Chrome version from the Database and determine if | 93 // Check the previous Chrome version from the Database and determine if |
84 // it has been updated. If it has, insert an event in the database. | 94 // it has been updated. If it has, insert an event in the database. |
85 void CheckForVersionUpdateOnBackgroundThread(); | 95 void CheckForVersionUpdateOnBackgroundThread(); |
86 | 96 |
87 // Wrapper function for inserting events into the database. | 97 // Wrapper function for inserting events into the database. |
88 void AddEvent(scoped_ptr<Event> event); | 98 void AddEvent(scoped_ptr<Event> event); |
89 | 99 |
90 void AddEventOnBackgroundThread(scoped_ptr<Event> event); | 100 void AddEventOnBackgroundThread(scoped_ptr<Event> event); |
91 | 101 |
| 102 // Gathers the CPU usage of every Chrome process that has been running since |
| 103 // the last call to GatherStatistics(). |
| 104 void GatherCPUUsageOnBackgroundThread(); |
| 105 |
| 106 // Gathers the memory usage of every process in the current list of processes. |
| 107 void GatherMemoryUsageOnBackgroundThread(); |
| 108 |
| 109 // Updates the ProcessMetrics map with the current list of processes. |
| 110 void UpdateMetricsMapOnBackgroundThread(); |
| 111 |
92 // Gets the corresponding value of |key| from the database, and then runs | 112 // Gets the corresponding value of |key| from the database, and then runs |
93 // |callback| on the UI thread with that value as a parameter. | 113 // |callback| on the UI thread with that value as a parameter. |
94 void GetStateValueOnBackgroundThread( | 114 void GetStateValueOnBackgroundThread( |
95 const std::string& key, | 115 const std::string& key, |
96 const StateValueCallback& callback); | 116 const StateValueCallback& callback); |
97 | 117 |
98 // Notify any listeners that PerformanceMonitor has finished the initializing. | 118 // Notify any listeners that PerformanceMonitor has finished the initializing. |
99 void NotifyInitialized(); | 119 void NotifyInitialized(); |
100 | 120 |
101 // Update the database record of the last time the active profiles were | 121 // Update the database record of the last time the active profiles were |
102 // running; this is used in determining when an unclean exit occurred. | 122 // running; this is used in determining when an unclean exit occurred. |
103 void UpdateLiveProfiles(); | 123 void UpdateLiveProfiles(); |
104 void UpdateLiveProfilesHelper( | 124 void UpdateLiveProfilesHelper( |
105 scoped_ptr<std::set<std::string> > active_profiles, std::string time); | 125 scoped_ptr<std::set<std::string> > active_profiles, std::string time); |
106 | 126 |
107 // Perform any collections that are done on a timed basis. | 127 // Perform any collections that are done on a timed basis. |
108 void DoTimedCollections(); | 128 void DoTimedCollections(); |
109 | 129 |
110 // The location at which the database files are stored; if empty, the database | 130 // The location at which the database files are stored; if empty, the database |
111 // will default to '<user_data_dir>/performance_monitor_dbs'. | 131 // will default to '<user_data_dir>/performance_monitor_dbs'. |
112 FilePath database_path_; | 132 FilePath database_path_; |
113 | 133 |
114 scoped_ptr<Database> database_; | 134 scoped_ptr<Database> database_; |
115 | 135 |
| 136 // A map of currently running ProcessHandles to ProcessMetrics. |
| 137 MetricsMap metrics_map_; |
| 138 |
116 // The timer to signal PerformanceMonitor to perform its timed collections. | 139 // The timer to signal PerformanceMonitor to perform its timed collections. |
117 base::RepeatingTimer<PerformanceMonitor> timer_; | 140 base::RepeatingTimer<PerformanceMonitor> timer_; |
118 | 141 |
119 content::NotificationRegistrar registrar_; | 142 content::NotificationRegistrar registrar_; |
120 | 143 |
121 DISALLOW_COPY_AND_ASSIGN(PerformanceMonitor); | 144 DISALLOW_COPY_AND_ASSIGN(PerformanceMonitor); |
122 }; | 145 }; |
123 | 146 |
124 } // namespace performance_monitor | 147 } // namespace performance_monitor |
125 | 148 |
126 #endif // CHROME_BROWSER_PERFORMANCE_MONITOR_PERFORMANCE_MONITOR_H_ | 149 #endif // CHROME_BROWSER_PERFORMANCE_MONITOR_PERFORMANCE_MONITOR_H_ |
OLD | NEW |