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 7e6122f45297ed4ec8fd34da2ebda48ea09d9b7a..a5dc0f2076d5e6cdd0403d91812a7c8ebe6550c7 100644 |
--- a/chrome/browser/performance_monitor/performance_monitor.cc |
+++ b/chrome/browser/performance_monitor/performance_monitor.cc |
@@ -4,20 +4,25 @@ |
#include "chrome/browser/performance_monitor/performance_monitor.h" |
+#include <algorithm> |
Devlin
2012/07/24 15:30:48
Is this still needed, now that you removed FindMed
mitchellwrosen
2012/07/27 19:24:51
Nope!
|
+#include <vector> |
+ |
#include "base/bind.h" |
#include "base/logging.h" |
-#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/browser_shutdown.h" |
#include "chrome/browser/performance_monitor/constants.h" |
-#include "chrome/browser/performance_monitor/database.h" |
#include "chrome/browser/performance_monitor/performance_monitor_util.h" |
#include "chrome/browser/extensions/crx_installer.h" |
#include "chrome/common/chrome_notification_types.h" |
#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_child_process_host.h" |
#include "content/public/browser/browser_thread.h" |
#include "content/public/browser/notification_service.h" |
#include "content/public/browser/notification_types.h" |
@@ -164,6 +169,92 @@ void PerformanceMonitor::NotifyInitialized() { |
content::NotificationService::NoDetails()); |
} |
+void PerformanceMonitor::GatherStatisticsOnBackgroundThread() { |
+ CHECK(!BrowserThread::CurrentlyOn(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() { |
+ if (metrics_map_.size()) { |
+ double cpu_usage = 0; |
+ for (MetricsMap::const_iterator iter = metrics_map_.begin(); |
+ iter != metrics_map_.end(); ++iter) { |
+ cpu_usage += iter->second->GetCPUUsage(); |
+ } |
+ |
+ database_->AddMetric(performance_monitor::kMetricCPUUsage, |
+ base::DoubleToString(cpu_usage)); |
+ } |
+} |
+ |
+void PerformanceMonitor::GatherMemoryUsage() { |
+ size_t private_memory_sum = 0; |
+ size_t shared_memory_sum = 0; |
+ for (MetricsMap::const_iterator iter = metrics_map_.begin(); |
+ iter != metrics_map_.end(); ++iter) { |
+ size_t private_memory = 0; |
+ size_t shared_memory = 0; |
+ if (iter->second->GetMemoryBytes(&private_memory, &shared_memory)) { |
+ private_memory_sum += private_memory; |
+ shared_memory_sum += shared_memory; |
+ } else { |
+ LOG(WARNING) << "GetMemoryBytes returned NULL (platform-specific error)"; |
+ } |
+ } |
+ |
+ database_->AddMetric(performance_monitor::kMetricPrivateMemoryUsage, |
+ base::Uint64ToString(private_memory_sum)); |
+ database_->AddMetric(performance_monitor::kMetricSharedMemoryUsage, |
+ base::Uint64ToString(shared_memory_sum)); |
+} |
+ |
+void PerformanceMonitor::UpdateMetricsMap() { |
+ // Remove old process handles. Use two iterators to safely call erase() on the |
+ // current element. |
+ for (MetricsMap::iterator iter_next = metrics_map_.begin(); |
+ iter_next != metrics_map_.end();) { |
+ MetricsMap::iterator iter = 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) && |
+ !ContainsKey(metrics_map_, process_handle)) { |
+#if defined(OS_MACOSX) |
+ linked_ptr<base::ProcessMetrics> process_metrics( |
+ base::ProcessMetrics::CreateProcessMetrics(process_handle, |
+ content::BrowserChildProcessHost::GetPortProvider())); |
+#else |
+ linked_ptr<base::ProcessMetrics> process_metrics( |
+ base::ProcessMetrics::CreateProcessMetrics(process_handle)); |
+#endif |
+ // 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) { |