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

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: Missed one move. 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() {
sky 2014/08/25 15:59:31 Initially all pod types here, eg profile_, last_cp
Daniel Nishi 2014/08/25 17:06:50 Done.
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 #if defined(OS_CHROMEOS)
54 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
55 this);
56 #endif
57 }
58
59 ProcessPowerCollector::~ProcessPowerCollector() {
60 #if defined(OS_CHROMEOS)
61 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver(
62 this);
63 #endif
64 }
65
66 #if defined(OS_CHROMEOS)
67 void ProcessPowerCollector::PowerChanged(
68 const power_manager::PowerSupplyProperties& prop) {
69 if (prop.battery_state() ==
70 power_manager::PowerSupplyProperties::DISCHARGING) {
71 should_update_ = true;
sky 2014/08/25 15:59:31 Does should_update_ and battery_discharge_rate_ ne
Daniel Nishi 2014/08/25 17:06:50 I've changed the |should_update| into changing if
72 battery_discharge_rate_ = prop.battery_discharge_rate();
73 } else {
74 should_update_ = false;
sky 2014/08/25 15:59:31 Is there a reason when this goes false the timer n
Daniel Nishi 2014/08/25 17:06:50 Changed to stop the timer.
75 battery_discharge_rate_ = 0.0;
76 }
77 }
78 #endif
79
80 void ProcessPowerCollector::Initialize() {
81 DCHECK(!timer_.IsRunning());
82 timer_.Start(FROM_HERE,
83 base::TimeDelta::FromSeconds(kSecondsPerSample),
84 this,
85 &ProcessPowerCollector::HandleUpdateTimeout);
86 }
87
88 double ProcessPowerCollector::UpdatePowerConsumptionForTesting() {
89 return UpdatePowerConsumption();
90 }
91
92 double ProcessPowerCollector::UpdatePowerConsumption() {
93 double total_cpu_percent = SynchronizeProcesses();
94 RecordCpuUsageByOrigin(total_cpu_percent);
95 return total_cpu_percent;
96 }
97
98 void ProcessPowerCollector::HandleUpdateTimeout() {
99 UpdatePowerConsumption();
100 }
101
102 double ProcessPowerCollector::SynchronizeProcesses() {
103 // Update all tabs.
104 for (TabContentsIterator it; !it.done(); it.Next()) {
105 content::RenderProcessHost* render_process = it->GetRenderProcessHost();
106 // Skip incognito web contents.
107 if (render_process->GetBrowserContext()->IsOffTheRecord())
108 continue;
109 UpdateProcessInMap(render_process, it->GetLastCommittedURL().GetOrigin());
110 }
111
112 // Iterate over all profiles to find all app windows to attribute all apps.
113 ProfileManager* pm = g_browser_process->profile_manager();
114 std::vector<Profile*> open_profiles = pm->GetLoadedProfiles();
115 for (std::vector<Profile*>::const_iterator it = open_profiles.begin();
116 it != open_profiles.end();
117 ++it) {
118 const apps::AppWindowRegistry::AppWindowList& app_windows =
119 apps::AppWindowRegistry::Get(*it)->app_windows();
120 for (apps::AppWindowRegistry::AppWindowList::const_iterator itr =
121 app_windows.begin();
122 itr != app_windows.end();
123 ++itr) {
124 content::WebContents* web_contents = (*itr)->web_contents();
125
126 UpdateProcessInMap(web_contents->GetRenderProcessHost(),
127 web_contents->GetLastCommittedURL().GetOrigin());
128 }
129 }
130
131 // Remove invalid processes and sum up the cpu cycle.
132 double total_cpu_percent = 0.0;
133 ProcessMetricsMap::iterator it = metrics_map_.begin();
134 while (it != metrics_map_.end()) {
135 if (!it->second->seen_this_cycle()) {
136 metrics_map_.erase(it++);
137 continue;
138 }
139
140 total_cpu_percent += it->second->last_cpu_percent();
141 it++;
142 }
143
144 return total_cpu_percent;
145 }
146
147 void ProcessPowerCollector::RecordCpuUsageByOrigin(double total_cpu_percent) {
148 for (ProcessMetricsMap::iterator it = metrics_map_.begin();
149 it != metrics_map_.end();
150 ++it) {
151 // Invalidate the process for the next cycle.
152 it->second->set_seen_this_cycle(false);
153 }
154
155 if (total_cpu_percent <= 0)
156 return;
157
158 #if defined(OS_CHROMEOS)
159 if (!should_update_)
160 return;
161 #endif
162
163 for (ProcessMetricsMap::iterator it = metrics_map_.begin();
164 it != metrics_map_.end();
165 ++it) {
166 double last_process_power_usage = it->second->last_cpu_percent();
167 #if defined(OS_CHROMEOS)
168 // Chrome OS scales by battery discharge rate.
169 last_process_power_usage *= battery_discharge_rate_ / total_cpu_percent;
170 #endif
171
172 GURL origin = it->second->last_origin();
173 power::OriginPowerMap* origin_power_map =
174 power::OriginPowerMapFactory::GetForBrowserContext(
175 it->second->profile());
176 DCHECK(origin_power_map);
177 origin_power_map->AddPowerForOrigin(origin, last_process_power_usage);
178 }
179 }
180
181 void ProcessPowerCollector::UpdateProcessInMap(
182 const content::RenderProcessHost* rph,
183 const GURL& origin) {
184 base::ProcessHandle handle = rph->GetHandle();
185 if (metrics_map_.find(handle) == metrics_map_.end()) {
186 metrics_map_[handle] = linked_ptr<PerProcessData>(new PerProcessData(
187 #if defined(OS_MACOSX)
188 scoped_ptr<base::ProcessMetrics>(
189 base::ProcessMetrics::CreateProcessMetrics(handle, NULL)),
190 #else
191 scoped_ptr<base::ProcessMetrics>(
192 base::ProcessMetrics::CreateProcessMetrics(handle)),
193 #endif
194 origin,
195 Profile::FromBrowserContext(rph->GetBrowserContext())));
196 }
197
198 linked_ptr<PerProcessData>& process_data = metrics_map_[handle];
199 process_data->set_last_cpu_percent(
200 cpu_usage_callback_.is_null() ? process_data->metrics()->GetCPUUsage()
201 : cpu_usage_callback_.Run(handle));
202 // Skip errors.
203 if (process_data->last_cpu_percent() < 0)
204 process_data->set_last_cpu_percent(0);
205 process_data->set_seen_this_cycle(true);
206 }
OLDNEW
« no previous file with comments | « chrome/browser/power/process_power_collector.h ('k') | chrome/browser/power/process_power_collector_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698