Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(354)

Side by Side Diff: chrome/browser/power/process_power_collector.cc

Issue 472383002: Add ProcessPowerCollector to audit power information. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "chromeos/dbus/dbus_thread_manager.h"
23 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h"
24 #endif
25
26 namespace {
27 const int kSecondsPerSample = 30;
28 }
29
30 ProcessPowerCollector::PerProcessData::PerProcessData(
31 scoped_ptr<base::ProcessMetrics> metrics,
32 const GURL& origin,
33 Profile* profile)
34 : metrics_(metrics.Pass()),
35 profile_(profile),
36 last_origin_(origin),
37 last_cpu_percent_(0),
38 seen_this_cycle_(true) {
39 }
40
41 ProcessPowerCollector::PerProcessData::PerProcessData() {
42 }
43
44 ProcessPowerCollector::PerProcessData::~PerProcessData() {
45 }
46
47 ProcessPowerCollector::ProcessPowerCollector()
48 #if defined(OS_CHROMEOS)
49 : should_update_(false),
50 battery_discharge_rate_(0.0)
51 #endif
52 {
53 }
54
55 ProcessPowerCollector::~ProcessPowerCollector() {
56 }
57
58 void ProcessPowerCollector::Initialize() {
59 DCHECK(!timer_.IsRunning());
60 #if defined(OS_CHROMEOS)
61 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
62 this);
63 #endif
64 timer_.Start(FROM_HERE,
65 base::TimeDelta::FromSeconds(kSecondsPerSample),
66 this,
67 &ProcessPowerCollector::HandleUpdateTimeout);
68 }
69
70 void ProcessPowerCollector::Shutdown() {
71 #if defined(OS_CHROMEOS)
72 chromeos::DBusThreadManager* dbus_manager =
73 chromeos::DBusThreadManager::Get();
74 dbus_manager->GetPowerManagerClient()->RemoveObserver(this);
75 #endif
76 }
77
78 #if defined(OS_CHROMEOS)
79 void ProcessPowerCollector::PowerChanged(
80 const power_manager::PowerSupplyProperties& prop) {
81 if (prop.battery_state() ==
82 power_manager::PowerSupplyProperties::DISCHARGING) {
83 should_update_ = true;
84 battery_discharge_rate_ = prop.battery_discharge_rate();
85 } else {
86 should_update_ = false;
87 battery_discharge_rate_ = 0.0;
88 }
89 }
90 #endif
91
92 double ProcessPowerCollector::SynchronizeProcesses() {
93 // Update all tabs.
94 for (TabContentsIterator it; !it.done(); it.Next()) {
95 content::RenderProcessHost* render_process = it->GetRenderProcessHost();
96 // Skip incognito web contents.
97 if (render_process->GetBrowserContext()->IsOffTheRecord())
98 continue;
99 UpdateProcessInMap(render_process, it->GetLastCommittedURL().GetOrigin());
100 }
101
102 // Iterate over all profiles to find all app windows to attribute all apps.
103 ProfileManager* pm = g_browser_process->profile_manager();
104 std::vector<Profile*> open_profiles = pm->GetLoadedProfiles();
105 for (std::vector<Profile*>::const_iterator it = open_profiles.begin();
106 it != open_profiles.end();
107 ++it) {
108 const apps::AppWindowRegistry::AppWindowList& app_windows =
109 apps::AppWindowRegistry::Get(*it)->app_windows();
110 for (apps::AppWindowRegistry::AppWindowList::const_iterator itr =
111 app_windows.begin();
112 itr != app_windows.end();
113 ++itr) {
114 content::WebContents* web_contents = (*itr)->web_contents();
115
116 UpdateProcessInMap(web_contents->GetRenderProcessHost(),
117 web_contents->GetLastCommittedURL().GetOrigin());
118 }
119 }
120
121 // Remove invalid processes and sum up the cpu cycle.
122 double total_cpu_percent = 0.0;
123 ProcessMetricsMap::iterator it = metrics_map_.begin();
124 while (it != metrics_map_.end()) {
125 if (!it->second->seen_this_cycle()) {
126 metrics_map_.erase(it++);
127 continue;
128 }
129
130 total_cpu_percent += it->second->last_cpu_percent();
131 it++;
132 }
133
134 return total_cpu_percent;
135 }
136
137 void ProcessPowerCollector::RecordCpuUsageByOrigin(double total_cpu_percent) {
138 if (total_cpu_percent <= 0)
139 return;
140
141 #if defined(OS_CHROMEOS)
142 if (!should_update_)
143 return;
144 #endif
145
146 for (ProcessMetricsMap::iterator it = metrics_map_.begin();
147 it != metrics_map_.end();
148 ++it) {
149 double last_process_power_usage = it->second->last_cpu_percent();
150 #if defined(OS_CHROMEOS)
151 // Chrome OS scales by battery discharge rate.
152 last_process_power_usage *= battery_discharge_rate_ / total_cpu_percent;
153 #endif
154
155 GURL origin = it->second->last_origin();
156 power::OriginPowerMap* origin_power_map =
157 power::OriginPowerMapFactory::GetForBrowserContext(
158 it->second->profile());
159 DCHECK(origin_power_map);
160 origin_power_map->AddPowerForOrigin(origin, last_process_power_usage);
161
162 // Invalidate |last_cpu| for the next cycle.
163 it->second->set_seen_this_cycle(false);
164 }
165 }
166
167 void ProcessPowerCollector::UpdateProcessInMap(
168 const content::RenderProcessHost* rph,
169 const GURL& origin) {
170 base::ProcessHandle handle = rph->GetHandle();
171 if (metrics_map_.find(handle) == metrics_map_.end()) {
172 metrics_map_[handle] = linked_ptr<PerProcessData>(new PerProcessData(
173 #if defined(OS_MACOSX)
174 scoped_ptr<base::ProcessMetrics>(
175 base::ProcessMetrics::CreateProcessMetrics(handle, NULL)),
176 #else
177 scoped_ptr<base::ProcessMetrics>(
178 base::ProcessMetrics::CreateProcessMetrics(handle)),
179 #endif
180 origin,
181 Profile::FromBrowserContext(rph->GetBrowserContext())));
182 }
183
184 linked_ptr<PerProcessData>& process_data = metrics_map_[handle];
185 if (cpu_usage_callback_.is_null())
186 process_data->set_last_cpu_percent(process_data->metrics()->GetCPUUsage());
187 else
188 process_data->set_last_cpu_percent(cpu_usage_callback_.Run(handle));
189
190 // Skip errors.
191 if (process_data->last_cpu_percent() < 0) {
192 process_data->set_last_cpu_percent(0);
193 }
194 process_data->set_seen_this_cycle(true);
195 }
196
197 double ProcessPowerCollector::UpdatePowerConsumptionForTesting() {
198 return UpdatePowerConsumption();
199 }
200
201 double ProcessPowerCollector::UpdatePowerConsumption() {
202 double total_cpu_percent = SynchronizeProcesses();
203 RecordCpuUsageByOrigin(total_cpu_percent);
204 return total_cpu_percent;
205 }
206
207 void ProcessPowerCollector::HandleUpdateTimeout() {
208 UpdatePowerConsumption();
209 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698