Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2234)

Unified Diff: chrome/browser/performance_monitor/performance_monitor.cc

Issue 10656052: Performance monitor stats gathering. (Closed) Base URL: http://git.chromium.org/chromium/src.git@cpm_main
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..06a824d42731824440ea9671c1f667d0455828e3 100644
--- a/chrome/browser/performance_monitor/performance_monitor.cc
+++ b/chrome/browser/performance_monitor/performance_monitor.cc
@@ -4,20 +4,24 @@
#include "chrome/browser/performance_monitor/performance_monitor.h"
+#include <algorithm>
+#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_thread.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
@@ -27,6 +31,22 @@
using content::BrowserThread;
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) {
@@ -164,6 +184,100 @@ void PerformanceMonitor::NotifyInitialized() {
content::NotificationService::NoDetails());
}
+void PerformanceMonitor::GatherStatisticsOnBackgroundThread() {
+ CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
Devlin 2012/07/23 21:10:33 you can remove the preceding content::'s now.
mitchellwrosen 2012/07/23 23:58:51 Done.
+
+ // 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()) {
+ for (MetricsMap::const_iterator iter = metrics_map_.begin();
+ iter != metrics_map_.end(); ++iter)
+ 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)));
Devlin 2012/07/23 21:10:33 I think these should be sums, not medians. Medians
Aaron Boodman 2012/07/23 23:12:31 Agree.
mitchellwrosen 2012/07/23 23:58:51 Done.
+ }
+}
+
+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)));
Devlin 2012/07/23 21:10:33 See above comment.
mitchellwrosen 2012/07/23 23:58:51 Done.
+ database_->AddMetric(performance_monitor::kMetricSharedMemoryUsage,
+ base::Uint64ToString(GetMedian(shared_memory_vec)));
+}
+
+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)
Devlin 2012/07/23 21:10:33 nit: This might be clearer as #if defined(OS_MACOS
mitchellwrosen 2012/07/23 23:58:51 Tomato, tomato. See task_manager.cc:756. Anyways,
+ 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
+ // 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) {

Powered by Google App Engine
This is Rietveld 408576698