Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 #include "chrome/browser/power/process_power_collector.h" | |
| 5 | |
| 6 #include "apps/app_window.h" | |
| 7 #include "apps/app_window_registry.h" | |
| 8 #include "base/process/process_handle.h" | |
| 9 #include "base/process/process_metrics.h" | |
| 10 #include "chrome/browser/browser_process.h" | |
| 11 #include "chrome/browser/profiles/profile.h" | |
| 12 #include "chrome/browser/profiles/profile_manager.h" | |
| 13 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | |
| 14 #include "components/power/origin_power_map.h" | |
| 15 #include "components/power/origin_power_map_factory.h" | |
| 16 #include "content/public/browser/browser_context.h" | |
| 17 #include "content/public/browser/render_process_host.h" | |
| 18 #include "content/public/browser/web_contents.h" | |
| 19 #include "url/gurl.h" | |
| 20 | |
| 21 #if defined(OS_CHROMEOS) | |
| 22 #include "chrome/browser/chromeos/power/power_data_collector.h" | |
| 23 #endif | |
| 24 | |
| 25 namespace { | |
| 26 const int kSecondsPerSample = 30; | |
| 27 } | |
| 28 | |
| 29 ProcessPowerCollector::PerProcessData::PerProcessData( | |
| 30 scoped_ptr<base::ProcessMetrics> metrics, | |
| 31 const GURL& origin, | |
| 32 Profile* profile) | |
| 33 : metrics_(metrics.Pass()), | |
| 34 profile_(profile), | |
| 35 last_origin_(origin), | |
| 36 last_cpu_percent_(0), | |
| 37 seen_this_cycle_(true) { | |
| 38 } | |
| 39 | |
| 40 ProcessPowerCollector::PerProcessData::PerProcessData() { | |
| 41 } | |
| 42 | |
| 43 ProcessPowerCollector::PerProcessData::~PerProcessData() { | |
| 44 } | |
| 45 | |
| 46 ProcessPowerCollector::ProcessPowerCollector() { | |
| 47 } | |
| 48 | |
| 49 ProcessPowerCollector::~ProcessPowerCollector() { | |
| 50 } | |
| 51 | |
| 52 void ProcessPowerCollector::StartUpdating() { | |
| 53 DCHECK(!timer_.IsRunning()); | |
| 54 timer_.Start(FROM_HERE, | |
| 55 base::TimeDelta::FromSeconds(kSecondsPerSample), | |
| 56 this, | |
| 57 &ProcessPowerCollector::UpdatePowerConsumptionCallback); | |
| 58 } | |
| 59 | |
| 60 void ProcessPowerCollector::UpdatePowerConsumptionCallback() { | |
| 61 double total_cpu_percent = SynchronizeProcesses(); | |
| 62 RecordCpuUsageByOrigin(total_cpu_percent); | |
| 63 } | |
| 64 | |
| 65 double ProcessPowerCollector::SynchronizeProcesses() { | |
| 66 // Update all tabs. | |
| 67 for (TabContentsIterator it; !it.done(); it.Next()) { | |
| 68 content::RenderProcessHost* render_process = it->GetRenderProcessHost(); | |
| 69 // Skip incognito web contents. | |
| 70 if (render_process->GetBrowserContext()->IsOffTheRecord()) | |
| 71 continue; | |
| 72 UpdateProcessInMap(render_process, it->GetLastCommittedURL().GetOrigin()); | |
| 73 } | |
| 74 | |
| 75 // Iterate over all profiles to find all app windows to attribute all apps. | |
| 76 ProfileManager* pm = g_browser_process->profile_manager(); | |
| 77 std::vector<Profile*> open_profiles = pm->GetLoadedProfiles(); | |
| 78 for (std::vector<Profile*>::const_iterator it = open_profiles.begin(); | |
| 79 it != open_profiles.end(); | |
| 80 ++it) { | |
| 81 const apps::AppWindowRegistry::AppWindowList& app_windows = | |
| 82 apps::AppWindowRegistry::Get(*it)->app_windows(); | |
| 83 for (apps::AppWindowRegistry::AppWindowList::const_iterator itr = | |
| 84 app_windows.begin(); | |
| 85 itr != app_windows.end(); | |
| 86 ++itr) { | |
| 87 content::WebContents* web_contents = (*itr)->web_contents(); | |
| 88 | |
| 89 UpdateProcessInMap(web_contents->GetRenderProcessHost(), | |
| 90 web_contents->GetLastCommittedURL().GetOrigin()); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 // Remove invalid processes and sum up the cpu cycle. | |
| 95 double total_cpu_percent = 0.0; | |
| 96 ProcessMetricsMap::iterator it = metrics_map_.begin(); | |
| 97 while (it != metrics_map_.end()) { | |
| 98 if (!it->second->seen_this_cycle()) { | |
| 99 metrics_map_.erase(it++); | |
| 100 continue; | |
| 101 } | |
| 102 | |
| 103 total_cpu_percent += it->second->last_cpu_percent(); | |
| 104 it++; | |
| 105 } | |
| 106 | |
| 107 return total_cpu_percent; | |
| 108 } | |
| 109 | |
| 110 void ProcessPowerCollector::RecordCpuUsageByOrigin(double total_cpu_percent) { | |
| 111 #if defined(OS_CHROMEOS) | |
| 112 // Don't record data yet if no power data exists yet. | |
| 113 chromeos::PowerDataCollector* power_data_collector = | |
| 114 chromeos::PowerDataCollector::Get(); | |
| 115 if (power_data_collector->power_supply_data().empty()) | |
| 116 return; | |
| 117 | |
| 118 chromeos::PowerDataCollector::PowerSupplySample sample = | |
| 119 power_data_collector->power_supply_data().back(); | |
| 120 | |
| 121 // Don't record data if plugged in. | |
| 122 if (sample.external_power) | |
| 123 return; | |
| 124 #endif | |
| 125 | |
| 126 for (ProcessMetricsMap::iterator it = metrics_map_.begin(); | |
| 127 it != metrics_map_.end(); | |
| 128 ++it) { | |
| 129 double last_process_power_usage = it->second->last_cpu_percent(); | |
| 130 #if defined(OS_CHROMEOS) | |
| 131 // Only query discharge rate if there is a battery. | |
| 132 if (sample.battery_percent > -1 && total_cpu_percent > 0) { | |
|
Daniel Erat
2014/08/20 21:53:14
nit: sample.battery_percent > 0?
and can't you pu
Daniel Nishi
2014/08/21 17:53:55
I tested on a Chromebox a while back and Chromebox
| |
| 133 last_process_power_usage = last_process_power_usage * | |
|
Daniel Erat
2014/08/20 21:53:14
nit:
last_process_power_usage *= sample.battery
Daniel Nishi
2014/08/21 17:53:55
Done.
| |
| 134 sample.battery_discharge_rate / | |
| 135 total_cpu_percent; | |
| 136 } | |
| 137 #endif | |
| 138 | |
| 139 GURL origin = it->second->last_origin(); | |
| 140 power::OriginPowerMap* origin_power_map = | |
| 141 power::OriginPowerMapFactory::GetForBrowserContext( | |
| 142 it->second->profile()); | |
| 143 DCHECK(origin_power_map); | |
| 144 origin_power_map->AddPowerForOrigin(origin, last_process_power_usage); | |
| 145 | |
| 146 // Invalidate |last_cpu| for the next cycle. | |
| 147 it->second->set_seen_this_cycle(false); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 void ProcessPowerCollector::UpdateProcessInMap( | |
| 152 const content::RenderProcessHost* rph, | |
| 153 const GURL& origin) { | |
| 154 base::ProcessHandle handle = rph->GetHandle(); | |
| 155 if (metrics_map_.find(handle) == metrics_map_.end()) { | |
| 156 metrics_map_[handle] = linked_ptr<PerProcessData>(new PerProcessData( | |
| 157 #if defined(OS_MACOSX) | |
| 158 scoped_ptr<base::ProcessMetrics>( | |
| 159 base::ProcessMetrics::CreateProcessMetrics(handle, NULL)), | |
| 160 #else | |
| 161 scoped_ptr<base::ProcessMetrics>( | |
| 162 base::ProcessMetrics::CreateProcessMetrics(handle)), | |
| 163 #endif | |
| 164 origin, | |
| 165 Profile::FromBrowserContext(rph->GetBrowserContext()))); | |
| 166 } | |
| 167 | |
| 168 linked_ptr<PerProcessData>& process_data = metrics_map_[handle]; | |
| 169 if (cpu_usage_callback_.is_null()) { | |
| 170 process_data->set_last_cpu_percent(process_data->metrics()->GetCPUUsage()); | |
| 171 } else { | |
| 172 process_data->set_last_cpu_percent(cpu_usage_callback_.Run(handle)); | |
| 173 } | |
| 174 | |
| 175 // Skip errors. | |
| 176 if (process_data->last_cpu_percent() < 0) { | |
| 177 process_data->set_last_cpu_percent(0); | |
| 178 } | |
| 179 process_data->set_seen_this_cycle(true); | |
| 180 } | |
| 181 | |
| 182 void ProcessPowerCollector::SetGetCpuUsageCallbackForTesting( | |
| 183 const CpuUsageCallback& callback) { | |
| 184 cpu_usage_callback_ = callback; | |
| 185 } | |
| 186 | |
| 187 double ProcessPowerCollector::UpdatePowerConsumptionForTesting() { | |
|
Daniel Erat
2014/08/20 21:53:14
instead of duplicating UpdatePowerConsumptionCallb
Daniel Nishi
2014/08/21 17:53:55
Implemented similar to this.
| |
| 188 double total_cpu_percent = SynchronizeProcesses(); | |
| 189 RecordCpuUsageByOrigin(total_cpu_percent); | |
| 190 return total_cpu_percent; | |
| 191 } | |
| OLD | NEW |