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