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..7f56798eded56f63dcd64e36ac28b11a29a43064 |
--- /dev/null |
+++ b/chrome/browser/power/process_power_collector.cc |
@@ -0,0 +1,177 @@ |
+// 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( |
+ base::ProcessMetrics* metrics, |
+ const GURL& origin, |
+ Profile* profile) |
+ : metrics_(metrics), |
+ profile_(profile), |
+ last_origin_(origin), |
+ last_cpu_percent_(0) { |
+} |
+ |
+ProcessPowerCollector::PerProcessData::PerProcessData() { |
+} |
+ |
+ProcessPowerCollector::PerProcessData::~PerProcessData() { |
+} |
+ |
+void ProcessPowerCollector::PerProcessData::SetLastCpuPercent(int new_cpu) { |
+ last_cpu_percent_ = new_cpu; |
+} |
+ |
+ProcessPowerCollector::ProcessPowerCollector() { |
+} |
+ |
+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()) |
+ 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_used() < 0) { |
Daniel Erat
2014/08/20 00:10:42
should you be doing this after you update the last
Daniel Nishi
2014/08/20 17:16:46
We remove before we query the last percent to avoi
|
+ metrics_map_.erase(it++); |
Daniel Erat
2014/08/20 00:10:42
how about moving the stale-process-clearing to the
|
+ continue; |
+ } |
+ it->second->SetLastCpuPercent(it->second->metrics()->GetCPUUsage()); |
+ |
+ // Skip errors. |
+ if (it->second->last_cpu_used() < 0) { |
Daniel Erat
2014/08/20 00:10:42
hmm, so you skip negative usages here. this seems
Daniel Nishi
2014/08/20 17:16:46
Good catch. The intent was to just skip, since the
|
+ it++; |
+ continue; |
+ } |
+ |
+ cpu_cycle += it->second->last_cpu_used(); |
+ 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 |
+ |
+ for (ProcessMetricsMap::iterator it = metrics_map_.begin(); |
+ it != metrics_map_.end(); |
+ ++it) { |
+ double last_process_power_usage = it->second->last_cpu_used(); |
+#if defined(OS_CHROMEOS) |
+ // Only query discharge rate if there is a battery. |
+ if (sample.battery_percent > -1 && cpu_cycle > 0) |
+ 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->SetLastCpuPercent(-1); |
+ } |
+} |
+ |
+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) |
+ base::ProcessMetrics::CreateProcessMetrics(handle, NULL), |
+#else |
+ base::ProcessMetrics::CreateProcessMetrics(handle), |
+#endif |
+ origin, |
+ Profile::FromBrowserContext(rph->GetBrowserContext()))); |
+ } |
+ // Reset the CPU usage to 0 to signify that the process still exists. |
+ metrics_map_[handle]->SetLastCpuPercent(0); |
+} |