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..d221d39bd026290d4e716fe1d210cf66537a26ba |
--- /dev/null |
+++ b/chrome/browser/power/process_power_collector.cc |
@@ -0,0 +1,174 @@ |
+// 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) |
+ : profile_(profile), |
+ last_origin_(origin), |
+ last_cpu_percent_(0), |
+ seen_this_cycle_(true) { |
+ metrics_ = metrics.Pass(); |
Daniel Erat
2014/08/20 17:47:58
nit: can you just do:
metrics_(metrics.Pass())
Daniel Nishi
2014/08/20 18:13:35
For some reason I was under the impression you cou
|
+} |
+ |
+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() { |
+ RecordCpuUsageByOrigin(SynchronizeProcesses()); |
Daniel Erat
2014/08/20 17:47:58
nit: break this into two lines to make it clearer
Daniel Nishi
2014/08/20 18:13:34
Done.
|
+} |
+ |
+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; |
+ 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()); |
+ } |
+ } |
+ |
+ // Remoev invalid processes and sum up the cpu cycle. |
Daniel Erat
2014/08/20 17:47:58
nit: Remove
Daniel Nishi
2014/08/20 18:13:35
Done.
|
+ double cpu_cycle = 0.0; |
Daniel Erat
2014/08/20 17:47:59
nit: rename to |total_cpu_percent|?
Daniel Nishi
2014/08/20 18:13:34
Done.
|
+ ProcessMetricsMap::iterator it = metrics_map_.begin(); |
+ while (it != metrics_map_.end()) { |
+ if (!it->second->seen_this_cycle()) { |
+ metrics_map_.erase(it++); |
+ continue; |
+ } |
+ |
+ it->second->set_last_cpu_percent(it->second->metrics()->GetCPUUsage()); |
Daniel Erat
2014/08/20 17:47:59
could you also do this in AddProcessToMap()? maybe
Daniel Nishi
2014/08/20 18:13:34
Added and changed to UpdateProcessInMap()
|
+ it->second->set_seen_this_cycle(true); |
Daniel Erat
2014/08/20 17:47:58
do you actually need this call? AddProcessToMap()
Daniel Nishi
2014/08/20 18:13:35
Correct.
Removed.
|
+ |
+ // Skip errors. |
+ if (it->second->last_cpu_percent() < 0) { |
+ it->second->set_last_cpu_percent(0); |
+ continue; |
+ } |
+ |
+ cpu_cycle += it->second->last_cpu_percent(); |
+ it++; |
+ } |
+ |
+ return cpu_cycle; |
+} |
+ |
+void ProcessPowerCollector::RecordCpuUsageByOrigin(double cpu_cycle) { |
Daniel Erat
2014/08/20 17:47:58
nit: rename |cpu_cycle| to |total_cpu_percent|?
(
Daniel Nishi
2014/08/20 18:13:35
Done.
|
+#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 && cpu_cycle > 0) |
Daniel Erat
2014/08/20 17:47:59
nit: use curly brackets here since the statement p
Daniel Nishi
2014/08/20 18:13:35
Done.
|
+ last_process_power_usage = |
+ last_process_power_usage * 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, last_process_power_usage); |
+ |
+ // Invalidate |last_cpu| for the next cycle. |
+ it->second->set_seen_this_cycle(false); |
+ } |
+} |
+ |
+void ProcessPowerCollector::AddProcessToMap( |
+ 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()))); |
+ } |
+ metrics_map_[handle]->set_seen_this_cycle(true); |
+} |