Chromium Code Reviews| 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..c09082d94bb7770709b53c0081f5f4f6f3485fbe |
| --- /dev/null |
| +++ b/chrome/browser/power/process_power_collector.cc |
| @@ -0,0 +1,176 @@ |
| +// 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; |
| + } |
| + |
| + // Skip errors. |
| + if (it->second->last_cpu_percent() < 0) { |
|
Daniel Erat
2014/08/20 19:55:07
nit: move this into UpdateProcessInMap() too
Daniel Nishi
2014/08/20 20:56:34
Done.
|
| + it->second->set_last_cpu_percent(0); |
| + continue; |
|
Daniel Erat
2014/08/20 19:55:07
this will be addressed by the previous comment, bu
|
| + } |
| + |
| + 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) { |
| + last_process_power_usage = last_process_power_usage * |
| + 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()) { |
|
Daniel Erat
2014/08/20 19:55:07
nit: to avoid the repeated lookups below, do somet
|
| + 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_last_cpu_percent( |
| + metrics_map_[handle]->metrics()->GetCPUUsage()); |
| + metrics_map_[handle]->set_seen_this_cycle(true); |
| +} |