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/bind.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/process/process_handle.h" | |
11 #include "base/process/process_metrics.h" | |
12 #include "chrome/browser/browser_process.h" | |
13 #include "chrome/browser/profiles/profile.h" | |
14 #include "chrome/browser/profiles/profile_manager.h" | |
15 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | |
16 #include "components/power/origin_power_map.h" | |
17 #include "components/power/origin_power_map_factory.h" | |
18 #include "content/public/browser/browser_context.h" | |
19 #include "content/public/browser/render_process_host.h" | |
20 #include "content/public/browser/web_contents.h" | |
21 #include "url/gurl.h" | |
22 | |
23 #if defined(OS_CHROMEOS) | |
24 #include "chrome/browser/chromeos/power/power_data_collector.h" | |
25 #endif | |
26 | |
27 namespace { | |
28 const int kSecondsPerSample = 30; | |
29 } | |
30 | |
31 ProcessPowerCollector::ProcessPowerCollector() | |
32 : metrics_map_(new ProcessMetricsMap), | |
33 update_requests_(0), | |
34 weak_ptr_factory_(this) { | |
35 } | |
36 | |
37 ProcessPowerCollector::~ProcessPowerCollector() { | |
38 } | |
39 | |
40 void ProcessPowerCollector::StartUpdating() { | |
41 // Don't allow multiple start updates. | |
42 update_requests_++; | |
Daniel Erat
2014/08/15 20:22:12
why do you need to track the number of updates? if
Daniel Nishi
2014/08/18 17:38:47
Done.
| |
43 if (update_requests_ > 1) { | |
44 return; | |
45 } | |
46 DCHECK_EQ(1, update_requests_); | |
47 | |
48 UpdatePowerConsumptionCallback(); | |
49 } | |
50 | |
51 void ProcessPowerCollector::UpdatePowerConsumptionCallback() { | |
52 UpdateMetricsMap(); | |
53 RecordCpuUsageByOrigin(PopulateCpuUsageByOrigin()); | |
54 | |
55 // Schedule next update. | |
56 base::MessageLoop::current()->PostDelayedTask( | |
Daniel Erat
2014/08/15 20:22:12
any reason not to use base::RepeatingTimer instead
Daniel Nishi
2014/08/18 17:38:47
Done.
| |
57 FROM_HERE, | |
58 base::Bind(&ProcessPowerCollector::UpdatePowerConsumptionCallback, | |
59 weak_ptr_factory_.GetWeakPtr()), | |
60 base::TimeDelta::FromSeconds(kSecondsPerSample)); | |
61 } | |
62 | |
63 void ProcessPowerCollector::UpdateMetricsMap() { | |
64 // Update all tabs. | |
65 for (TabContentsIterator it; !it.done(); it.Next()) { | |
66 content::RenderProcessHost* render_process = it->GetRenderProcessHost(); | |
67 // Skip incognito web contents. | |
68 if (render_process->GetBrowserContext()->IsOffTheRecord()) { | |
69 continue; | |
70 } | |
71 AddProcessToMap(render_process, it->GetLastCommittedURL().GetOrigin()); | |
72 } | |
73 | |
74 // Iterate over all profiles to find all app windows to attribute all apps. | |
75 ProfileManager* pm = g_browser_process->profile_manager(); | |
76 std::vector<Profile*> open_profiles = pm->GetLoadedProfiles(); | |
77 for (std::vector<Profile*>::const_iterator it = open_profiles.begin(); | |
78 it != open_profiles.end(); | |
79 ++it) { | |
80 const apps::AppWindowRegistry::AppWindowList& app_windows = | |
81 apps::AppWindowRegistry::Get(*it)->app_windows(); | |
82 for (apps::AppWindowRegistry::AppWindowList::const_iterator itr = | |
83 app_windows.begin(); | |
84 itr != app_windows.end(); | |
85 ++itr) { | |
86 content::WebContents* web_contents = (*itr)->web_contents(); | |
87 | |
88 AddProcessToMap(web_contents->GetRenderProcessHost(), | |
89 web_contents->GetLastCommittedURL().GetOrigin()); | |
90 } | |
91 } | |
92 } | |
93 | |
94 double ProcessPowerCollector::PopulateCpuUsageByOrigin() { | |
95 ProcessMetricsMap::iterator it = metrics_map_->begin(); | |
96 double cpu_cycle = 0.0; | |
97 while (it != metrics_map_->end()) { | |
98 // If the process doesn't exist anymore, remove it. | |
99 if (it->second.last_cpu < 0) { | |
100 metrics_map_->erase(it++); | |
101 continue; | |
102 } | |
103 it->second.last_cpu = it->second.metrics->GetCPUUsage(); | |
104 | |
105 // Skip errors. | |
106 if (it->second.last_cpu < 0) { | |
107 it++; | |
108 continue; | |
109 } | |
110 | |
111 cpu_cycle += it->second.last_cpu; | |
112 it++; | |
113 } | |
114 | |
115 return cpu_cycle; | |
116 } | |
117 | |
118 void ProcessPowerCollector::RecordCpuUsageByOrigin(double cpu_cycle) { | |
119 #if defined(OS_CHROMEOS) | |
120 // Don't record data yet if no power data exists yet. | |
121 chromeos::PowerDataCollector* power_data_collector = | |
Daniel Erat
2014/08/15 20:22:12
can you just observe chromeos/dbus/power_manager_c
Daniel Nishi
2014/08/18 17:38:47
The only power event this needs access to is the m
| |
122 chromeos::PowerDataCollector::Get(); | |
123 if (power_data_collector->power_supply_data().empty()) | |
124 return; | |
125 | |
126 chromeos::PowerDataCollector::PowerSupplySample sample = | |
127 power_data_collector->power_supply_data().back(); | |
128 | |
129 // Don't record data if plugged in. | |
130 if (sample.external_power) { | |
Daniel Erat
2014/08/15 20:22:12
nit: omit curly brackets
Daniel Nishi
2014/08/18 17:38:48
Done.
| |
131 return; | |
132 } | |
133 #endif | |
Daniel Erat
2014/08/15 20:22:12
shouldn't you also be checking whether we're on ba
Daniel Nishi
2014/08/18 17:38:48
The API for monitoring battery status on other pla
Daniel Erat
2014/08/18 20:43:04
what about base/power_monitor/power_monitor.h? sad
Daniel Nishi
2014/08/19 19:24:20
It's probably worth saying that the main reason we
| |
134 | |
135 ProcessMetricsMap::iterator it = metrics_map_->begin(); | |
136 while (it != metrics_map_->end()) { | |
137 double site_delta = it->second.last_cpu; | |
138 #if defined(OS_CHROMEOS) | |
139 // Only query discharge rate if there is a battery. | |
140 if (sample.battery_percent > -1 && cpu_cycle > 0) | |
141 site_delta = site_delta * sample.battery_discharge_rate / cpu_cycle; | |
Daniel Erat
2014/08/15 20:22:12
i don't understand this code -- what does this sca
Daniel Nishi
2014/08/18 17:38:47
This attributes the actual discharge rate of the s
| |
142 #endif | |
143 | |
144 GURL origin = it->second.last_origin; | |
145 power::OriginPowerMap* origin_power_map = | |
146 power::OriginPowerMapFactory::GetForBrowserContext(it->second.profile); | |
147 DCHECK(origin_power_map); | |
148 origin_power_map->AddPowerForOrigin(origin, site_delta); | |
149 | |
150 // Invalidate |last_cpu| for the next cycle. | |
151 it->second.last_cpu = -1; | |
152 it++; | |
153 } | |
154 } | |
155 | |
156 void ProcessPowerCollector::AddProcessToMap( | |
157 const content::RenderProcessHost* rph, | |
158 const GURL& origin) { | |
159 base::ProcessHandle handle = rph->GetHandle(); | |
160 if (metrics_map_->find(handle) == metrics_map_->end()) { | |
161 #if defined(OS_MACOSX) | |
162 (*metrics_map_)[handle] = PerProcessData( | |
163 base::ProcessMetrics::CreateProcessMetrics(handle, NULL)); | |
164 #else | |
165 (*metrics_map_)[handle] = | |
166 PerProcessData(base::ProcessMetrics::CreateProcessMetrics(handle)); | |
167 #endif | |
168 } | |
169 (*metrics_map_)[handle].last_origin = origin; | |
170 (*metrics_map_)[handle].profile = | |
171 Profile::FromBrowserContext(rph->GetBrowserContext()); | |
172 (*metrics_map_)[handle].last_cpu = 0; | |
173 } | |
OLD | NEW |