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

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: Shutdown() no longer exists -- now in d'tor. 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 #if defined(OS_CHROMEOS)
57 chromeos::DBusThreadManager* dbus_manager =
58 chromeos::DBusThreadManager::Get();
59 dbus_manager->GetPowerManagerClient()->RemoveObserver(this);
Daniel Erat 2014/08/22 22:41:38 you have an unbalanced Remove call here if Initial
Daniel Nishi 2014/08/22 23:21:31 Since we're controlling the life time, we can put
60 #endif
61 }
62
63 void ProcessPowerCollector::Initialize() {
64 DCHECK(!timer_.IsRunning());
65 #if defined(OS_CHROMEOS)
66 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
67 this);
68 #endif
69 timer_.Start(FROM_HERE,
70 base::TimeDelta::FromSeconds(kSecondsPerSample),
71 this,
72 &ProcessPowerCollector::HandleUpdateTimeout);
73 }
74
75 #if defined(OS_CHROMEOS)
76 void ProcessPowerCollector::PowerChanged(
77 const power_manager::PowerSupplyProperties& prop) {
78 if (prop.battery_state() ==
79 power_manager::PowerSupplyProperties::DISCHARGING) {
80 should_update_ = true;
81 battery_discharge_rate_ = prop.battery_discharge_rate();
82 } else {
83 should_update_ = false;
84 battery_discharge_rate_ = 0.0;
85 }
86 }
87 #endif
88
89 double ProcessPowerCollector::SynchronizeProcesses() {
90 // Update all tabs.
91 for (TabContentsIterator it; !it.done(); it.Next()) {
92 content::RenderProcessHost* render_process = it->GetRenderProcessHost();
93 // Skip incognito web contents.
94 if (render_process->GetBrowserContext()->IsOffTheRecord())
95 continue;
96 UpdateProcessInMap(render_process, it->GetLastCommittedURL().GetOrigin());
97 }
98
99 // Iterate over all profiles to find all app windows to attribute all apps.
100 ProfileManager* pm = g_browser_process->profile_manager();
101 std::vector<Profile*> open_profiles = pm->GetLoadedProfiles();
102 for (std::vector<Profile*>::const_iterator it = open_profiles.begin();
103 it != open_profiles.end();
104 ++it) {
105 const apps::AppWindowRegistry::AppWindowList& app_windows =
106 apps::AppWindowRegistry::Get(*it)->app_windows();
107 for (apps::AppWindowRegistry::AppWindowList::const_iterator itr =
108 app_windows.begin();
109 itr != app_windows.end();
110 ++itr) {
111 content::WebContents* web_contents = (*itr)->web_contents();
112
113 UpdateProcessInMap(web_contents->GetRenderProcessHost(),
114 web_contents->GetLastCommittedURL().GetOrigin());
115 }
116 }
117
118 // Remove invalid processes and sum up the cpu cycle.
119 double total_cpu_percent = 0.0;
120 ProcessMetricsMap::iterator it = metrics_map_.begin();
121 while (it != metrics_map_.end()) {
122 if (!it->second->seen_this_cycle()) {
123 metrics_map_.erase(it++);
124 continue;
125 }
126
127 total_cpu_percent += it->second->last_cpu_percent();
128 it++;
129 }
130
131 return total_cpu_percent;
132 }
133
134 void ProcessPowerCollector::RecordCpuUsageByOrigin(double total_cpu_percent) {
135 if (total_cpu_percent <= 0)
Daniel Erat 2014/08/22 22:41:38 actually, doing this means that you won't call set
Daniel Nishi 2014/08/22 23:21:31 Done.
136 return;
137
138 #if defined(OS_CHROMEOS)
139 if (!should_update_)
140 return;
141 #endif
142
143 for (ProcessMetricsMap::iterator it = metrics_map_.begin();
144 it != metrics_map_.end();
145 ++it) {
146 double last_process_power_usage = it->second->last_cpu_percent();
147 #if defined(OS_CHROMEOS)
148 // Chrome OS scales by battery discharge rate.
149 last_process_power_usage *= battery_discharge_rate_ / total_cpu_percent;
150 #endif
151
152 GURL origin = it->second->last_origin();
153 power::OriginPowerMap* origin_power_map =
154 power::OriginPowerMapFactory::GetForBrowserContext(
155 it->second->profile());
156 DCHECK(origin_power_map);
157 origin_power_map->AddPowerForOrigin(origin, last_process_power_usage);
158
159 // Invalidate |last_cpu| for the next cycle.
160 it->second->set_seen_this_cycle(false);
161 }
162 }
163
164 void ProcessPowerCollector::UpdateProcessInMap(
165 const content::RenderProcessHost* rph,
166 const GURL& origin) {
167 base::ProcessHandle handle = rph->GetHandle();
168 if (metrics_map_.find(handle) == metrics_map_.end()) {
169 metrics_map_[handle] = linked_ptr<PerProcessData>(new PerProcessData(
170 #if defined(OS_MACOSX)
171 scoped_ptr<base::ProcessMetrics>(
172 base::ProcessMetrics::CreateProcessMetrics(handle, NULL)),
173 #else
174 scoped_ptr<base::ProcessMetrics>(
175 base::ProcessMetrics::CreateProcessMetrics(handle)),
176 #endif
177 origin,
178 Profile::FromBrowserContext(rph->GetBrowserContext())));
179 }
180
181 linked_ptr<PerProcessData>& process_data = metrics_map_[handle];
182 if (cpu_usage_callback_.is_null())
Daniel Erat 2014/08/22 22:41:38 nit: reorder this, maybe: process_data->set_las
Daniel Nishi 2014/08/22 23:21:31 Done.
183 process_data->set_last_cpu_percent(process_data->metrics()->GetCPUUsage());
184 else
185 process_data->set_last_cpu_percent(cpu_usage_callback_.Run(handle));
186
187 // Skip errors.
188 if (process_data->last_cpu_percent() < 0) {
Daniel Erat 2014/08/22 22:41:38 nit: omit curly brackets here
Daniel Nishi 2014/08/22 23:21:31 Done.
189 process_data->set_last_cpu_percent(0);
190 }
191 process_data->set_seen_this_cycle(true);
192 }
193
194 double ProcessPowerCollector::UpdatePowerConsumptionForTesting() {
195 return UpdatePowerConsumption();
196 }
197
198 double ProcessPowerCollector::UpdatePowerConsumption() {
199 double total_cpu_percent = SynchronizeProcesses();
200 RecordCpuUsageByOrigin(total_cpu_percent);
201 return total_cpu_percent;
202 }
203
204 void ProcessPowerCollector::HandleUpdateTimeout() {
Daniel Erat 2014/08/22 22:41:38 please reorder the methods in the .cc file to matc
Daniel Nishi 2014/08/22 23:21:31 Done.
205 UpdatePowerConsumption();
206 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698