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..33aa8f8601509be437f97fe6555261f198fbb221 |
| --- /dev/null |
| +++ b/chrome/browser/power/process_power_collector.cc |
| @@ -0,0 +1,207 @@ |
| +// 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 "chromeos/dbus/dbus_thread_manager.h" |
| +#include "chromeos/dbus/power_manager/power_supply_properties.pb.h" |
|
Daniel Erat
2014/08/21 23:51:20
nit: don't need this and power_manager_client.h he
Daniel Nishi
2014/08/22 01:06:18
Done.
|
| +#include "chromeos/dbus/power_manager_client.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() |
| +#if defined(OS_CHROMEOS) |
| + : should_update(false), |
| + battery_discharge_rate_(0.0) |
| +#endif |
| +{ |
| +} |
| + |
| +ProcessPowerCollector::~ProcessPowerCollector() { |
| +} |
| + |
| +void ProcessPowerCollector::Initialize() { |
| + DCHECK(!timer_.IsRunning()); |
| +#if defined(OS_CHROMEOS) |
| + chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( |
| + this); |
| +#endif |
| + timer_.Start(FROM_HERE, |
| + base::TimeDelta::FromSeconds(kSecondsPerSample), |
| + this, |
| + &ProcessPowerCollector::HandleUpdateTimeout); |
| +} |
| + |
| +void ProcessPowerCollector::Shutdown() { |
| +#if defined(OS_CHROMEOS) |
| + chromeos::DBusThreadManager* dbus_manager = |
| + chromeos::DBusThreadManager::Get(); |
| + CHECK(dbus_manager); |
|
Daniel Erat
2014/08/21 23:51:20
you don't need this CHECK(); Get() already does it
Daniel Nishi
2014/08/22 01:06:19
Done.
|
| + dbus_manager->GetPowerManagerClient()->RemoveObserver(this); |
| +#endif |
| +} |
| + |
| +#if defined(OS_CHROMEOS) |
| +void ProcessPowerCollector::PowerChanged( |
| + const power_manager::PowerSupplyProperties& prop) { |
|
Daniel Erat
2014/08/21 23:51:20
thanks, listening for this feels much cleaner to m
Daniel Nishi
2014/08/22 01:06:18
Done.
|
| + should_update = (prop.external_power() == |
|
Daniel Erat
2014/08/21 23:51:21
as the person who works on the powerd side of this
Daniel Nishi
2014/08/22 01:06:18
Done.
|
| + power_manager::PowerSupplyProperties::DISCONNECTED); |
| + battery_discharge_rate_ = prop.battery_discharge_rate(); |
| +} |
| +#endif |
| + |
| +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 (total_cpu_percent <= 0) |
| + return; |
| + |
| +#if defined(OS_CHROMEOS) |
| + if (!should_update) |
| + 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) |
| + // Chrome OS scales by battery discharge rate. |
| + last_process_power_usage *= 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()) { |
|
Daniel Erat
2014/08/21 23:51:20
nit: omit curly brackets
Daniel Nishi
2014/08/22 01:06:18
Done.
|
| + 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) { |
|
Daniel Erat
2014/08/21 23:51:21
nit: omit curly brackets
Daniel Nishi
2014/08/22 01:06:18
Done.
|
| + process_data->set_last_cpu_percent(0); |
| + } |
| + process_data->set_seen_this_cycle(true); |
| +} |
| + |
| +double ProcessPowerCollector::UpdatePowerConsumptionForTesting() { |
| + return UpdatePowerConsumption(); |
| +} |
| + |
| +double ProcessPowerCollector::UpdatePowerConsumption() { |
| + double total_cpu_percent = SynchronizeProcesses(); |
| + RecordCpuUsageByOrigin(total_cpu_percent); |
| + return total_cpu_percent; |
| +} |
| + |
| +void ProcessPowerCollector::HandleUpdateTimeout() { |
| + UpdatePowerConsumption(); |
| +} |