Chromium Code Reviews| Index: chrome/browser/performance_monitor/performance_monitor.cc |
| diff --git a/chrome/browser/performance_monitor/performance_monitor.cc b/chrome/browser/performance_monitor/performance_monitor.cc |
| index f72b845c2277c69f6dfe1c6d9a8d1b9fa2786ccb..0f68a4a5401828aa7300a34341c8e1f0679ced07 100644 |
| --- a/chrome/browser/performance_monitor/performance_monitor.cc |
| +++ b/chrome/browser/performance_monitor/performance_monitor.cc |
| @@ -4,8 +4,18 @@ |
| #include "chrome/browser/performance_monitor/performance_monitor.h" |
| +#include <algorithm> |
| +#include <map> |
|
Devlin
2012/07/17 18:07:58
map and string are included in the .h file; you ca
mitchellwrosen
2012/07/20 19:42:36
Isn't there a style rule against relying on transi
Aaron Boodman
2012/07/20 21:07:05
The rule seems to be special cased to omit a .cc f
|
| +#include <string> |
| +#include <vector> |
| + |
| #include "base/bind.h" |
| #include "base/logging.h" |
| +#include "base/memory/linked_ptr.h" |
| +#include "base/process.h" |
|
Devlin
2012/07/17 18:07:58
linked_ptr, process, process_util already included
|
| +#include "base/process_util.h" |
| +#include "base/stl_util.h" |
| +#include "base/string_number_conversions.h" |
| #include "base/threading/worker_pool.h" |
| #include "base/time.h" |
| #include "chrome/browser/performance_monitor/constants.h" |
| @@ -16,11 +26,28 @@ |
| #include "chrome/common/chrome_version_info.h" |
| #include "chrome/common/extensions/extension.h" |
| #include "chrome/common/extensions/extension_constants.h" |
| +#include "chrome/test/base/chrome_process_util.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "content/public/browser/notification_service.h" |
| using extensions::Extension; |
| +namespace { |
| + |
| +template <class T> |
| +T GetMedian(std::vector<T> vec) { |
| + typedef typename std::vector<T>::size_type vec_size; |
| + |
| + vec_size size = vec.size(); |
| + DCHECK(size != 0); |
| + std::sort(vec.begin(), vec.end()); |
| + vec_size mid = size / 2; |
| + |
| + return size % 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid]; |
| +} |
| + |
| +} // namespace |
| + |
| namespace performance_monitor { |
| PerformanceMonitor::PerformanceMonitor() : database_(NULL) { |
| @@ -144,6 +171,103 @@ void PerformanceMonitor::CheckForVersionUpdateHelper( |
| } |
| } |
| +void PerformanceMonitor::GatherStatisticsOnBackgroundThread() { |
| + CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| + |
| + // Because the CPU usage is gathered as an average since the last time the |
| + // function was called, while the memory usage is gathered as an instantaneous |
| + // usage, the CPU usage is gathered before the metrics map is wiped. |
| + GatherCPUUsage(); |
| + UpdateMetricsMap(); |
| + GatherMemoryUsage(); |
| +} |
| + |
| +void PerformanceMonitor::GatherCPUUsage() { |
| + std::vector<double> cpu_usage_vec; |
| + |
| + if (metrics_map_.size()) { |
| + MetricsMap::const_iterator iter = metrics_map_.begin(); |
| + for (; iter != metrics_map_.end(); ++iter) |
|
Devlin
2012/07/17 18:07:58
Why not declare iter here, as normal?
mitchellwrosen
2012/07/20 19:42:36
Done.
|
| + cpu_usage_vec.push_back(iter->second->GetCPUUsage()); |
| + |
| + std::sort(cpu_usage_vec.begin(), cpu_usage_vec.end()); |
| + database_->AddMetric(performance_monitor::kMetricCPUUsage, |
| + base::DoubleToString(GetMedian(cpu_usage_vec))); |
| + } |
| +} |
| + |
| +void PerformanceMonitor::GatherMemoryUsage() { |
| + std::vector<size_t> private_memory_vec; |
| + std::vector<size_t> shared_memory_vec; |
| + |
| + size_t private_memory = 0; |
| + size_t shared_memory = 0; |
| + for (MetricsMap::const_iterator iter = metrics_map_.begin(); |
| + iter != metrics_map_.end(); ++iter) { |
| + if (iter->second->GetMemoryBytes(&private_memory, &shared_memory)) { |
| + private_memory_vec.push_back(private_memory); |
| + shared_memory_vec.push_back(shared_memory); |
| + } else { |
| + LOG(ERROR) << "GetMemoryBytes returned NULL (platform-specific error)"; |
| + } |
| + } |
| + |
| + if (!private_memory_vec.size()) |
| + return; |
| + |
| + std::sort(private_memory_vec.begin(), private_memory_vec.end()); |
| + std::sort(shared_memory_vec.begin(), shared_memory_vec.end()); |
| + |
| + database_->AddMetric(performance_monitor::kMetricPrivateMemoryUsage, |
| + base::Uint64ToString(GetMedian(private_memory_vec))); |
| + database_->AddMetric(performance_monitor::kMetricSharedMemoryUsage, |
| + base::Uint64ToString(GetMedian(shared_memory_vec))); |
| +} |
| + |
| +void PerformanceMonitor::UpdateMetricsMap() { |
| + // Remove old process handles. Use two iteraterors to safely call erase() |
|
Yoyo Zhou
2012/07/19 21:35:35
typo: iterators
mitchellwrosen
2012/07/20 19:42:36
Done.
|
| + // on the current element, and advance by a call to iter = iter_next++. |
|
Devlin
2012/07/17 18:07:58
style violation - always use ++i, not i++
Yoyo Zhou
2012/07/19 21:35:35
It's fine if you're assigning the old value of i.
|
| + // The third iterator saves us from calling end() every loop. |
| + for (MetricsMap::iterator iter_next = metrics_map_.begin(), |
| + iter_end = metrics_map_.end(), |
| + iter = (iter_next == iter_end) ? iter_next : iter_next++; |
|
Yoyo Zhou
2012/07/19 21:35:35
Yikes, it's super unreadable to squeeze everything
mitchellwrosen
2012/07/20 19:42:36
Does it look better now? I can make it cleaner by
Yoyo Zhou
2012/07/20 20:47:30
It still looks scary. I have to think too much to
mitchellwrosen
2012/07/20 23:05:27
I'm not sure I understand. If only iter_next is in
Yoyo Zhou
2012/07/20 23:14:26
As I mentioned, you can look at the example in ala
|
| + iter != iter_next; |
| + iter = (iter_next == iter_end) ? iter_next : iter_next++) { |
| + if (base::GetTerminationStatus(iter->first, NULL) != |
| + base::TERMINATION_STATUS_STILL_RUNNING) { |
| + base::CloseProcessHandle(iter->first); |
| + metrics_map_.erase(iter); |
| + } else { |
| + // Prime the CPUUsage to be gathered next time. |
| + iter->second->GetCPUUsage(); |
| + } |
| + } |
| + |
| + // Add new process handles. |
| + base::ProcessId browser_pid = base::GetCurrentProcId(); |
| + ChromeProcessList chrome_processes(GetRunningChromeProcesses(browser_pid)); |
| + for (ChromeProcessList::const_iterator pid_iter = chrome_processes.begin(); |
| + pid_iter != chrome_processes.end(); ++pid_iter) { |
| + base::ProcessHandle process_handle; |
| + if (base::OpenProcessHandle(*pid_iter, &process_handle)) { |
| +#if !defined(OS_MACOSX) |
| + linked_ptr<base::ProcessMetrics> process_metrics( |
| + base::ProcessMetrics::CreateProcessMetrics(process_handle)); |
| +#else |
| + linked_ptr<base::ProcessMetrics> process_metrics( |
| + base::ProcessMetrics::CreateProcessMetrics(process_handle, |
| + content::BrowserChildProcessHost::GetPortProvider())); |
| +#endif |
| + if (!ContainsKey(metrics_map_, process_handle)) { |
| + // Prime the CPUUsage to be gathered next time. |
| + process_metrics->GetCPUUsage(); |
| + |
| + metrics_map_[process_handle] = process_metrics; |
| + } |
| + } |
| + } |
| +} |
| + |
| void PerformanceMonitor::Observe(int type, |
| const content::NotificationSource& source, |
| const content::NotificationDetails& details) { |