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..02161ca1e6948b0c2e3aa199e9f99a6c20a2f58c |
--- /dev/null |
+++ b/chrome/browser/power/process_power_collector.cc |
@@ -0,0 +1,162 @@ |
+// 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() { |
+ delete metrics; |
+} |
+ |
+ProcessPowerCollector::ProcessPowerCollector() |
+ : metrics_map_(new ProcessMetricsMap) { |
+} |
+ |
+ProcessPowerCollector::~ProcessPowerCollector() { |
+} |
+ |
+void ProcessPowerCollector::StartUpdating() { |
+ DCHECK(!timer_.IsRunning()); |
+ timer_.Start(FROM_HERE, |
+ base::TimeDelta::FromSeconds(kSecondsPerSample), |
+ this, |
+ &ProcessPowerCollector::UpdatePowerConsumptionCallback); |
+} |
+ |
+void ProcessPowerCollector::UpdatePowerConsumptionCallback() { |
+ UpdateMetricsMap(); |
+ RecordCpuUsageByOrigin(PopulateCpuUsageByOrigin()); |
+} |
+ |
+void ProcessPowerCollector::UpdateMetricsMap() { |
+ // 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()) { |
Daniel Erat
2014/08/18 20:43:04
nit: omit curly brackets
Daniel Nishi
2014/08/19 19:24:20
Done.
|
+ continue; |
+ } |
+ AddProcessToMap(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(); |
+ |
+ AddProcessToMap(web_contents->GetRenderProcessHost(), |
+ web_contents->GetLastCommittedURL().GetOrigin()); |
+ } |
+ } |
+} |
+ |
+double ProcessPowerCollector::PopulateCpuUsageByOrigin() { |
+ ProcessMetricsMap::iterator it = metrics_map_->begin(); |
+ double cpu_cycle = 0.0; |
+ while (it != metrics_map_->end()) { |
+ // If the process doesn't exist anymore, remove it. |
+ if (it->second->last_cpu < 0) { |
+ metrics_map_->erase(it++); |
+ continue; |
+ } |
+ it->second->last_cpu = it->second->metrics->GetCPUUsage(); |
+ |
+ // Skip errors. |
+ if (it->second->last_cpu < 0) { |
+ it++; |
+ continue; |
+ } |
+ |
+ cpu_cycle += it->second->last_cpu; |
+ it++; |
+ } |
+ |
+ return cpu_cycle; |
+} |
+ |
+void ProcessPowerCollector::RecordCpuUsageByOrigin(double cpu_cycle) { |
+#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 |
+ |
+ ProcessMetricsMap::iterator it = metrics_map_->begin(); |
+ while (it != metrics_map_->end()) { |
Daniel Erat
2014/08/18 20:43:04
nit: just use a for loop here
Daniel Nishi
2014/08/19 19:24:20
Done.
|
+ double site_delta = it->second->last_cpu; |
Daniel Erat
2014/08/18 20:43:04
please give this variable a better name -- is it r
Daniel Nishi
2014/08/19 19:24:20
On non-CrOS platforms, that's correct. It's scaled
|
+#if defined(OS_CHROMEOS) |
+ // Only query discharge rate if there is a battery. |
+ if (sample.battery_percent > -1 && cpu_cycle > 0) |
+ site_delta = site_delta * sample.battery_discharge_rate / cpu_cycle; |
+#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, site_delta); |
+ |
+ // Invalidate |last_cpu| for the next cycle. |
+ it->second->last_cpu = -1; |
+ it++; |
+ } |
+} |
+ |
+void ProcessPowerCollector::AddProcessToMap( |
+ const content::RenderProcessHost* rph, |
+ const GURL& origin) { |
+ base::ProcessHandle handle = rph->GetHandle(); |
+ if (metrics_map_->find(handle) == metrics_map_->end()) { |
+#if defined(OS_MACOSX) |
+ (*metrics_map_)[handle] = PerProcessData( |
Daniel Erat
2014/08/18 20:43:04
does this even compile on mac? how about restructu
Daniel Nishi
2014/08/19 19:24:20
SGTM. I'm trying to pick up a OS X box, but I'll u
|
+ base::ProcessMetrics::CreateProcessMetrics(handle, NULL)); |
+#else |
+ (*metrics_map_)[handle] = linked_ptr<PerProcessData>( |
+ new PerProcessData(base::ProcessMetrics::CreateProcessMetrics(handle))); |
+#endif |
+ } |
+ (*metrics_map_)[handle]->last_origin = origin; |
Daniel Erat
2014/08/18 20:43:04
nit: copy the linked_ptr to a local variable so yo
Daniel Nishi
2014/08/19 19:24:20
Is this fine now that most of the lookups have got
|
+ (*metrics_map_)[handle]->profile = |
+ Profile::FromBrowserContext(rph->GetBrowserContext()); |
+ (*metrics_map_)[handle]->last_cpu = 0; |
+} |