Chromium Code Reviews| 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 #include "chrome/browser/performance_monitor/performance_monitor.h" | 5 #include "chrome/browser/performance_monitor/performance_monitor.h" |
| 6 | 6 |
| 7 #include <algorithm> | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 7 #include "base/bind.h" | 12 #include "base/bind.h" |
| 8 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/process.h" | |
| 15 #include "base/process_util.h" | |
| 16 #include "base/string_number_conversions.h" | |
| 9 #include "base/threading/worker_pool.h" | 17 #include "base/threading/worker_pool.h" |
| 10 #include "base/time.h" | 18 #include "base/time.h" |
| 11 #include "chrome/browser/performance_monitor/constants.h" | 19 #include "chrome/browser/performance_monitor/constants.h" |
| 12 #include "chrome/browser/performance_monitor/database.h" | 20 #include "chrome/browser/performance_monitor/database.h" |
| 13 #include "chrome/browser/performance_monitor/performance_monitor_util.h" | 21 #include "chrome/browser/performance_monitor/performance_monitor_util.h" |
| 14 #include "chrome/browser/extensions/crx_installer.h" | 22 #include "chrome/browser/extensions/crx_installer.h" |
| 15 #include "chrome/common/chrome_notification_types.h" | 23 #include "chrome/common/chrome_notification_types.h" |
| 16 #include "chrome/common/chrome_version_info.h" | 24 #include "chrome/common/chrome_version_info.h" |
| 17 #include "chrome/common/extensions/extension.h" | 25 #include "chrome/common/extensions/extension.h" |
| 18 #include "chrome/common/extensions/extension_constants.h" | 26 #include "chrome/common/extensions/extension_constants.h" |
| 27 #include "chrome/test/base/chrome_process_util.h" | |
| 19 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/notification_service.h" | 29 #include "content/public/browser/notification_service.h" |
| 21 | 30 |
| 22 using extensions::Extension; | 31 using extensions::Extension; |
| 23 | 32 |
| 33 namespace { | |
| 34 | |
| 35 template <class T> | |
| 36 T GetMedian(std::vector<T> vec) { | |
| 37 typedef typename std::vector<T>::size_type vec_size; | |
| 38 | |
| 39 vec_size size = vec.size(); | |
| 40 DCHECK(size != 0); | |
| 41 std::sort(vec.begin(), vec.end()); | |
| 42 vec_size mid = size / 2; | |
| 43 | |
| 44 return size % 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid]; | |
| 45 } | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 24 namespace performance_monitor { | 49 namespace performance_monitor { |
| 25 | 50 |
| 26 PerformanceMonitor::PerformanceMonitor() : database_(NULL) { | 51 PerformanceMonitor::PerformanceMonitor() : database_(NULL) { |
| 27 } | 52 } |
| 28 | 53 |
| 29 PerformanceMonitor::~PerformanceMonitor() { | 54 PerformanceMonitor::~PerformanceMonitor() { |
| 30 } | 55 } |
| 31 | 56 |
| 32 void PerformanceMonitor::Start() { | 57 void PerformanceMonitor::Start() { |
| 33 content::BrowserThread::PostBlockingPoolTaskAndReply( | 58 content::BrowserThread::PostBlockingPoolTaskAndReply( |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 FROM_HERE, | 107 FROM_HERE, |
| 83 base::Bind(&PerformanceMonitor::AddEventOnBackgroundThread, | 108 base::Bind(&PerformanceMonitor::AddEventOnBackgroundThread, |
| 84 base::Unretained(this), | 109 base::Unretained(this), |
| 85 base::Passed(event.Pass()))); | 110 base::Passed(event.Pass()))); |
| 86 } | 111 } |
| 87 | 112 |
| 88 void PerformanceMonitor::AddEventOnBackgroundThread(scoped_ptr<Event> event) { | 113 void PerformanceMonitor::AddEventOnBackgroundThread(scoped_ptr<Event> event) { |
| 89 database_->AddEvent(*event.get()); | 114 database_->AddEvent(*event.get()); |
| 90 } | 115 } |
| 91 | 116 |
| 117 void PerformanceMonitor::AddMetric(const std::string& key, | |
| 118 const std::string& value) { | |
| 119 content::BrowserThread::PostBlockingPoolSequencedTask( | |
| 120 Database::kDatabaseSequenceToken, | |
| 121 FROM_HERE, | |
| 122 base::Bind(&PerformanceMonitor::AddMetricOnBackgroundThread, | |
| 123 base::Unretained(this), | |
| 124 key, | |
| 125 value)); | |
| 126 } | |
| 127 | |
| 128 void PerformanceMonitor::AddMetricOnBackgroundThread(const std::string& key, | |
| 129 const std::string& value) { | |
| 130 database_->AddMetric(key, value); | |
|
Devlin
2012/07/11 16:28:08
These two functions can be combined, since they do
mitchellwrosen
2012/07/11 18:30:18
Done.
| |
| 131 } | |
| 132 | |
| 133 | |
| 92 void PerformanceMonitor::GetStateValueOnBackgroundThread( | 134 void PerformanceMonitor::GetStateValueOnBackgroundThread( |
| 93 const std::string& key, | 135 const std::string& key, |
| 94 const StateValueCallback& callback) { | 136 const StateValueCallback& callback) { |
| 95 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 137 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 96 std::string state_value = database_->GetStateValue(key); | 138 std::string state_value = database_->GetStateValue(key); |
| 97 | 139 |
| 98 callback.Run(state_value); | 140 callback.Run(state_value); |
| 99 } | 141 } |
| 100 | 142 |
| 101 void PerformanceMonitor::CheckForVersionUpdate() { | 143 void PerformanceMonitor::CheckForVersionUpdate() { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 current_version)); | 179 current_version)); |
| 138 if (!previous_version.empty()) { | 180 if (!previous_version.empty()) { |
| 139 AddEvent(util::CreateChromeUpdateEvent( | 181 AddEvent(util::CreateChromeUpdateEvent( |
| 140 base::Time::Now(), | 182 base::Time::Now(), |
| 141 previous_version, | 183 previous_version, |
| 142 current_version)); | 184 current_version)); |
| 143 } | 185 } |
| 144 } | 186 } |
| 145 } | 187 } |
| 146 | 188 |
| 189 void PerformanceMonitor::GatherStatistics() { | |
|
Devlin
2012/07/11 16:28:08
If the method is public and runs on the background
mitchellwrosen
2012/07/11 18:30:18
Done.
| |
| 190 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
|
Devlin
2012/07/11 16:28:08
Will gathering the stats on the background thread
| |
| 191 | |
| 192 // Because the CPU usage is gathered as an average since the last time the | |
| 193 // function was called, while the memory usage is gathered as an instantaneous | |
| 194 // usage, the CPU usage is gathered before the metrics map is wiped. | |
| 195 GatherCPUUsage(); | |
| 196 UpdateMetricsMap(); | |
| 197 GatherMemoryUsage(); | |
| 198 } | |
| 199 | |
| 200 void PerformanceMonitor::GatherCPUUsage() { | |
| 201 std::vector<double> cpu_usage_vec; | |
| 202 | |
| 203 if (metrics_map_.size()) { | |
| 204 for (MetricsMap::const_iterator iter = metrics_map_.begin(); | |
| 205 iter != metrics_map_.end(); ++iter) { | |
| 206 double cpu_usage = iter->second->GetCPUUsage(); | |
| 207 DCHECK_NE(cpu_usage, 0) << "Gathered CPU usage of 0"; | |
| 208 cpu_usage_vec.push_back(cpu_usage); | |
| 209 } | |
| 210 | |
| 211 std::sort(cpu_usage_vec.begin(), cpu_usage_vec.end()); | |
| 212 AddMetric(performance_monitor::kMetricCPUUsage, | |
|
Devlin
2012/07/11 16:28:08
If you're on the background thread, you don't need
mitchellwrosen
2012/07/11 18:30:18
Done.
| |
| 213 base::DoubleToString(GetMedian(cpu_usage_vec))); | |
| 214 } | |
| 215 } | |
| 216 | |
| 217 void PerformanceMonitor::GatherMemoryUsage() { | |
| 218 std::vector<size_t> private_memory_vec; | |
| 219 std::vector<size_t> shared_memory_vec; | |
| 220 | |
| 221 size_t private_memory = 0; | |
| 222 size_t shared_memory = 0; | |
| 223 for (MetricsMap::const_iterator iter = metrics_map_.begin(); | |
| 224 iter != metrics_map_.end(); ++iter) { | |
| 225 if (iter->second->GetMemoryBytes(&private_memory, &shared_memory)) { | |
| 226 private_memory_vec.push_back(private_memory); | |
| 227 shared_memory_vec.push_back(shared_memory); | |
| 228 } else { | |
| 229 LOG(ERROR) << "GetMemoryBytes returned NULL (platform-specific error)"; | |
| 230 } | |
| 231 } | |
| 232 | |
| 233 std::sort(private_memory_vec.begin(), private_memory_vec.end()); | |
|
Devlin
2012/07/11 16:28:08
Nit: Check the size before sorting (even though so
mitchellwrosen
2012/07/11 18:30:18
Done.
| |
| 234 std::sort(shared_memory_vec.begin(), shared_memory_vec.end()); | |
| 235 | |
| 236 if (private_memory_vec.size()) { | |
| 237 AddMetric(performance_monitor::kMetricPrivateMemoryUsage, | |
| 238 base::Uint64ToString(GetMedian(private_memory_vec))); | |
| 239 | |
| 240 AddMetric(performance_monitor::kMetricSharedMemoryUsage, | |
| 241 base::Uint64ToString(GetMedian(shared_memory_vec))); | |
| 242 } | |
| 243 } | |
| 244 | |
| 245 void PerformanceMonitor::UpdateMetricsMap() { | |
| 246 // Remove old process handles. | |
| 247 for (MetricsMap::iterator iter = metrics_map_.begin(); | |
| 248 iter != metrics_map_.end(); ++iter) { | |
| 249 if (iter->first == base::kNullProcessHandle) { | |
| 250 // base::CloseProcessHandle(iter->first); // Necessary? | |
| 251 metrics_map_.erase(iter); | |
| 252 } else { | |
| 253 // Prime the CPUUsage to be gathered next time. | |
| 254 iter->second->GetCPUUsage(); | |
| 255 } | |
| 256 } | |
| 257 | |
| 258 // Add new process handles | |
|
Devlin
2012/07/11 16:28:08
add a .
mitchellwrosen
2012/07/11 18:30:18
Done.
| |
| 259 base::ProcessId browser_pid = base::GetCurrentProcId(); | |
| 260 ChromeProcessList chrome_processes(GetRunningChromeProcesses(browser_pid)); | |
|
Devlin
2012/07/11 16:28:08
If you are getting the list of running processes,
mitchellwrosen
2012/07/11 18:30:18
I couldn't find anything to suggest otherwise. It'
| |
| 261 for (ChromeProcessList::const_iterator pid_iter = chrome_processes.begin(); | |
| 262 pid_iter != chrome_processes.end(); ++pid_iter) { | |
| 263 base::ProcessHandle process_handle; | |
| 264 if (base::OpenProcessHandle(*pid_iter, &process_handle)) { | |
| 265 linked_ptr<base::ProcessMetrics> process_metrics( | |
|
Yoyo Zhou
2012/07/10 21:58:00
This should be moved to inside the if statement.
mitchellwrosen
2012/07/11 18:30:18
Done.
Yoyo Zhou
2012/07/19 21:35:35
I did not mean the #if, I meant the if statement b
| |
| 266 #if !defined(OS_MACOSX) | |
| 267 base::ProcessMetrics::CreateProcessMetrics(process_handle)); | |
| 268 #else | |
| 269 base::ProcessMetrics::CreateProcessMetrics(process_handle, | |
| 270 content::BrowserChildProcessHost::GetPortProvider())); | |
| 271 #endif | |
| 272 if (!ContainsKey(metrics_map_, process_handle)) { | |
| 273 // Prime the CPUUsage to be gathered next time. | |
| 274 process_metrics->GetCPUUsage(); | |
| 275 | |
| 276 metrics_map_[process_handle] = process_metrics; | |
| 277 } | |
| 278 } | |
| 279 } | |
| 280 } | |
| 281 | |
| 147 void PerformanceMonitor::Observe(int type, | 282 void PerformanceMonitor::Observe(int type, |
| 148 const content::NotificationSource& source, | 283 const content::NotificationSource& source, |
| 149 const content::NotificationDetails& details) { | 284 const content::NotificationDetails& details) { |
| 150 switch (type) { | 285 switch (type) { |
| 151 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { | 286 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { |
| 152 const Extension* extension = content::Details<Extension>(details).ptr(); | 287 const Extension* extension = content::Details<Extension>(details).ptr(); |
| 153 AddEvent(util::CreateExtensionInstallEvent(base::Time::Now(), | 288 AddEvent(util::CreateExtensionInstallEvent(base::Time::Now(), |
| 154 extension->id(), | 289 extension->id(), |
| 155 extension->name(), | 290 extension->name(), |
| 156 extension->url().spec(), | 291 extension->url().spec(), |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 203 break; | 338 break; |
| 204 } | 339 } |
| 205 default: { | 340 default: { |
| 206 NOTREACHED(); | 341 NOTREACHED(); |
| 207 break; | 342 break; |
| 208 } | 343 } |
| 209 } | 344 } |
| 210 } | 345 } |
| 211 | 346 |
| 212 } // namespace performance_monitor | 347 } // namespace performance_monitor |
| OLD | NEW |