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 "chromeos/dbus/dbus_thread_manager.h" | |
23 #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.
| |
24 #include "chromeos/dbus/power_manager_client.h" | |
25 #endif | |
26 | |
27 namespace { | |
28 const int kSecondsPerSample = 30; | |
29 } | |
30 | |
31 ProcessPowerCollector::PerProcessData::PerProcessData( | |
32 scoped_ptr<base::ProcessMetrics> metrics, | |
33 const GURL& origin, | |
34 Profile* profile) | |
35 : metrics_(metrics.Pass()), | |
36 profile_(profile), | |
37 last_origin_(origin), | |
38 last_cpu_percent_(0), | |
39 seen_this_cycle_(true) { | |
40 } | |
41 | |
42 ProcessPowerCollector::PerProcessData::PerProcessData() { | |
43 } | |
44 | |
45 ProcessPowerCollector::PerProcessData::~PerProcessData() { | |
46 } | |
47 | |
48 ProcessPowerCollector::ProcessPowerCollector() | |
49 #if defined(OS_CHROMEOS) | |
50 : should_update(false), | |
51 battery_discharge_rate_(0.0) | |
52 #endif | |
53 { | |
54 } | |
55 | |
56 ProcessPowerCollector::~ProcessPowerCollector() { | |
57 } | |
58 | |
59 void ProcessPowerCollector::Initialize() { | |
60 DCHECK(!timer_.IsRunning()); | |
61 #if defined(OS_CHROMEOS) | |
62 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( | |
63 this); | |
64 #endif | |
65 timer_.Start(FROM_HERE, | |
66 base::TimeDelta::FromSeconds(kSecondsPerSample), | |
67 this, | |
68 &ProcessPowerCollector::HandleUpdateTimeout); | |
69 } | |
70 | |
71 void ProcessPowerCollector::Shutdown() { | |
72 #if defined(OS_CHROMEOS) | |
73 chromeos::DBusThreadManager* dbus_manager = | |
74 chromeos::DBusThreadManager::Get(); | |
75 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.
| |
76 dbus_manager->GetPowerManagerClient()->RemoveObserver(this); | |
77 #endif | |
78 } | |
79 | |
80 #if defined(OS_CHROMEOS) | |
81 void ProcessPowerCollector::PowerChanged( | |
82 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.
| |
83 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.
| |
84 power_manager::PowerSupplyProperties::DISCONNECTED); | |
85 battery_discharge_rate_ = prop.battery_discharge_rate(); | |
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) | |
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/21 23:51:20
nit: omit curly brackets
Daniel Nishi
2014/08/22 01:06:18
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 | |
188 // Skip errors. | |
189 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.
| |
190 process_data->set_last_cpu_percent(0); | |
191 } | |
192 process_data->set_seen_this_cycle(true); | |
193 } | |
194 | |
195 double ProcessPowerCollector::UpdatePowerConsumptionForTesting() { | |
196 return UpdatePowerConsumption(); | |
197 } | |
198 | |
199 double ProcessPowerCollector::UpdatePowerConsumption() { | |
200 double total_cpu_percent = SynchronizeProcesses(); | |
201 RecordCpuUsageByOrigin(total_cpu_percent); | |
202 return total_cpu_percent; | |
203 } | |
204 | |
205 void ProcessPowerCollector::HandleUpdateTimeout() { | |
206 UpdatePowerConsumption(); | |
207 } | |
OLD | NEW |