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

Unified Diff: chrome/browser/power/process_power_collector.cc

Issue 472383002: Add ProcessPowerCollector to audit power information. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Unittest clean-up. Created 6 years, 4 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/power/process_power_collector.cc
diff --git a/chrome/browser/power/process_power_collector.cc b/chrome/browser/power/process_power_collector.cc
new file mode 100644
index 0000000000000000000000000000000000000000..179113c71c7ec0721ed6e1c2bc0cd75c792ebd58
--- /dev/null
+++ b/chrome/browser/power/process_power_collector.cc
@@ -0,0 +1,191 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+#include "chrome/browser/power/process_power_collector.h"
+
+#include "apps/app_window.h"
+#include "apps/app_window_registry.h"
+#include "base/process/process_handle.h"
+#include "base/process/process_metrics.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/profiles/profile_manager.h"
+#include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
+#include "components/power/origin_power_map.h"
+#include "components/power/origin_power_map_factory.h"
+#include "content/public/browser/browser_context.h"
+#include "content/public/browser/render_process_host.h"
+#include "content/public/browser/web_contents.h"
+#include "url/gurl.h"
+
+#if defined(OS_CHROMEOS)
+#include "chrome/browser/chromeos/power/power_data_collector.h"
+#endif
+
+namespace {
+const int kSecondsPerSample = 30;
+}
+
+ProcessPowerCollector::PerProcessData::PerProcessData(
+ scoped_ptr<base::ProcessMetrics> metrics,
+ const GURL& origin,
+ Profile* profile)
+ : metrics_(metrics.Pass()),
+ profile_(profile),
+ last_origin_(origin),
+ last_cpu_percent_(0),
+ seen_this_cycle_(true) {
+}
+
+ProcessPowerCollector::PerProcessData::PerProcessData() {
+}
+
+ProcessPowerCollector::PerProcessData::~PerProcessData() {
+}
+
+ProcessPowerCollector::ProcessPowerCollector() {
+}
+
+ProcessPowerCollector::~ProcessPowerCollector() {
+}
+
+void ProcessPowerCollector::StartUpdating() {
+ DCHECK(!timer_.IsRunning());
+ timer_.Start(FROM_HERE,
+ base::TimeDelta::FromSeconds(kSecondsPerSample),
+ this,
+ &ProcessPowerCollector::UpdatePowerConsumptionCallback);
+}
+
+void ProcessPowerCollector::UpdatePowerConsumptionCallback() {
+ double total_cpu_percent = SynchronizeProcesses();
+ RecordCpuUsageByOrigin(total_cpu_percent);
+}
+
+double ProcessPowerCollector::SynchronizeProcesses() {
+ // Update all tabs.
+ for (TabContentsIterator it; !it.done(); it.Next()) {
+ content::RenderProcessHost* render_process = it->GetRenderProcessHost();
+ // Skip incognito web contents.
+ if (render_process->GetBrowserContext()->IsOffTheRecord())
+ continue;
+ UpdateProcessInMap(render_process, it->GetLastCommittedURL().GetOrigin());
+ }
+
+ // Iterate over all profiles to find all app windows to attribute all apps.
+ ProfileManager* pm = g_browser_process->profile_manager();
+ std::vector<Profile*> open_profiles = pm->GetLoadedProfiles();
+ for (std::vector<Profile*>::const_iterator it = open_profiles.begin();
+ it != open_profiles.end();
+ ++it) {
+ const apps::AppWindowRegistry::AppWindowList& app_windows =
+ apps::AppWindowRegistry::Get(*it)->app_windows();
+ for (apps::AppWindowRegistry::AppWindowList::const_iterator itr =
+ app_windows.begin();
+ itr != app_windows.end();
+ ++itr) {
+ content::WebContents* web_contents = (*itr)->web_contents();
+
+ UpdateProcessInMap(web_contents->GetRenderProcessHost(),
+ web_contents->GetLastCommittedURL().GetOrigin());
+ }
+ }
+
+ // Remove invalid processes and sum up the cpu cycle.
+ double total_cpu_percent = 0.0;
+ ProcessMetricsMap::iterator it = metrics_map_.begin();
+ while (it != metrics_map_.end()) {
+ if (!it->second->seen_this_cycle()) {
+ metrics_map_.erase(it++);
+ continue;
+ }
+
+ total_cpu_percent += it->second->last_cpu_percent();
+ it++;
+ }
+
+ return total_cpu_percent;
+}
+
+void ProcessPowerCollector::RecordCpuUsageByOrigin(double total_cpu_percent) {
+#if defined(OS_CHROMEOS)
+ // Don't record data yet if no power data exists yet.
+ chromeos::PowerDataCollector* power_data_collector =
+ chromeos::PowerDataCollector::Get();
+ if (power_data_collector->power_supply_data().empty())
+ return;
+
+ chromeos::PowerDataCollector::PowerSupplySample sample =
+ power_data_collector->power_supply_data().back();
+
+ // Don't record data if plugged in.
+ if (sample.external_power)
+ return;
+#endif
+
+ for (ProcessMetricsMap::iterator it = metrics_map_.begin();
+ it != metrics_map_.end();
+ ++it) {
+ double last_process_power_usage = it->second->last_cpu_percent();
+#if defined(OS_CHROMEOS)
+ // Only query discharge rate if there is a battery.
+ if (sample.battery_percent > -1 && total_cpu_percent > 0) {
Daniel Erat 2014/08/20 21:53:14 nit: sample.battery_percent > 0? and can't you pu
Daniel Nishi 2014/08/21 17:53:55 I tested on a Chromebox a while back and Chromebox
+ last_process_power_usage = last_process_power_usage *
Daniel Erat 2014/08/20 21:53:14 nit: last_process_power_usage *= sample.battery
Daniel Nishi 2014/08/21 17:53:55 Done.
+ sample.battery_discharge_rate /
+ total_cpu_percent;
+ }
+#endif
+
+ GURL origin = it->second->last_origin();
+ power::OriginPowerMap* origin_power_map =
+ power::OriginPowerMapFactory::GetForBrowserContext(
+ it->second->profile());
+ DCHECK(origin_power_map);
+ origin_power_map->AddPowerForOrigin(origin, last_process_power_usage);
+
+ // Invalidate |last_cpu| for the next cycle.
+ it->second->set_seen_this_cycle(false);
+ }
+}
+
+void ProcessPowerCollector::UpdateProcessInMap(
+ const content::RenderProcessHost* rph,
+ const GURL& origin) {
+ base::ProcessHandle handle = rph->GetHandle();
+ if (metrics_map_.find(handle) == metrics_map_.end()) {
+ metrics_map_[handle] = linked_ptr<PerProcessData>(new PerProcessData(
+#if defined(OS_MACOSX)
+ scoped_ptr<base::ProcessMetrics>(
+ base::ProcessMetrics::CreateProcessMetrics(handle, NULL)),
+#else
+ scoped_ptr<base::ProcessMetrics>(
+ base::ProcessMetrics::CreateProcessMetrics(handle)),
+#endif
+ origin,
+ Profile::FromBrowserContext(rph->GetBrowserContext())));
+ }
+
+ linked_ptr<PerProcessData>& process_data = metrics_map_[handle];
+ if (cpu_usage_callback_.is_null()) {
+ process_data->set_last_cpu_percent(process_data->metrics()->GetCPUUsage());
+ } else {
+ process_data->set_last_cpu_percent(cpu_usage_callback_.Run(handle));
+ }
+
+ // Skip errors.
+ if (process_data->last_cpu_percent() < 0) {
+ process_data->set_last_cpu_percent(0);
+ }
+ process_data->set_seen_this_cycle(true);
+}
+
+void ProcessPowerCollector::SetGetCpuUsageCallbackForTesting(
+ const CpuUsageCallback& callback) {
+ cpu_usage_callback_ = callback;
+}
+
+double ProcessPowerCollector::UpdatePowerConsumptionForTesting() {
Daniel Erat 2014/08/20 21:53:14 instead of duplicating UpdatePowerConsumptionCallb
Daniel Nishi 2014/08/21 17:53:55 Implemented similar to this.
+ double total_cpu_percent = SynchronizeProcesses();
+ RecordCpuUsageByOrigin(total_cpu_percent);
+ return total_cpu_percent;
+}

Powered by Google App Engine
This is Rietveld 408576698