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 "chrome/browser/chromeos/power/power_data_collector.h" | |
23 #endif | |
24 | |
25 namespace { | |
26 const int kSecondsPerSample = 30; | |
27 } | |
28 | |
29 ProcessPowerCollector::PerProcessData::PerProcessData( | |
30 scoped_ptr<base::ProcessMetrics> metrics, | |
31 const GURL& origin, | |
32 Profile* profile) | |
33 : profile_(profile), | |
34 last_origin_(origin), | |
35 last_cpu_percent_(0), | |
36 seen_this_cycle_(true) { | |
37 metrics_ = metrics.Pass(); | |
Daniel Erat
2014/08/20 17:47:58
nit: can you just do:
metrics_(metrics.Pass())
Daniel Nishi
2014/08/20 18:13:35
For some reason I was under the impression you cou
| |
38 } | |
39 | |
40 ProcessPowerCollector::PerProcessData::PerProcessData() { | |
41 } | |
42 | |
43 ProcessPowerCollector::PerProcessData::~PerProcessData() { | |
44 } | |
45 | |
46 ProcessPowerCollector::ProcessPowerCollector() { | |
47 } | |
48 | |
49 ProcessPowerCollector::~ProcessPowerCollector() { | |
50 } | |
51 | |
52 void ProcessPowerCollector::StartUpdating() { | |
53 DCHECK(!timer_.IsRunning()); | |
54 timer_.Start(FROM_HERE, | |
55 base::TimeDelta::FromSeconds(kSecondsPerSample), | |
56 this, | |
57 &ProcessPowerCollector::UpdatePowerConsumptionCallback); | |
58 } | |
59 | |
60 void ProcessPowerCollector::UpdatePowerConsumptionCallback() { | |
61 RecordCpuUsageByOrigin(SynchronizeProcesses()); | |
Daniel Erat
2014/08/20 17:47:58
nit: break this into two lines to make it clearer
Daniel Nishi
2014/08/20 18:13:34
Done.
| |
62 } | |
63 | |
64 double ProcessPowerCollector::SynchronizeProcesses() { | |
65 // Update all tabs. | |
66 for (TabContentsIterator it; !it.done(); it.Next()) { | |
67 content::RenderProcessHost* render_process = it->GetRenderProcessHost(); | |
68 // Skip incognito web contents. | |
69 if (render_process->GetBrowserContext()->IsOffTheRecord()) | |
70 continue; | |
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 // Remoev invalid processes and sum up the cpu cycle. | |
Daniel Erat
2014/08/20 17:47:58
nit: Remove
Daniel Nishi
2014/08/20 18:13:35
Done.
| |
94 double cpu_cycle = 0.0; | |
Daniel Erat
2014/08/20 17:47:59
nit: rename to |total_cpu_percent|?
Daniel Nishi
2014/08/20 18:13:34
Done.
| |
95 ProcessMetricsMap::iterator it = metrics_map_.begin(); | |
96 while (it != metrics_map_.end()) { | |
97 if (!it->second->seen_this_cycle()) { | |
98 metrics_map_.erase(it++); | |
99 continue; | |
100 } | |
101 | |
102 it->second->set_last_cpu_percent(it->second->metrics()->GetCPUUsage()); | |
Daniel Erat
2014/08/20 17:47:59
could you also do this in AddProcessToMap()? maybe
Daniel Nishi
2014/08/20 18:13:34
Added and changed to UpdateProcessInMap()
| |
103 it->second->set_seen_this_cycle(true); | |
Daniel Erat
2014/08/20 17:47:58
do you actually need this call? AddProcessToMap()
Daniel Nishi
2014/08/20 18:13:35
Correct.
Removed.
| |
104 | |
105 // Skip errors. | |
106 if (it->second->last_cpu_percent() < 0) { | |
107 it->second->set_last_cpu_percent(0); | |
108 continue; | |
109 } | |
110 | |
111 cpu_cycle += it->second->last_cpu_percent(); | |
112 it++; | |
113 } | |
114 | |
115 return cpu_cycle; | |
116 } | |
117 | |
118 void ProcessPowerCollector::RecordCpuUsageByOrigin(double cpu_cycle) { | |
Daniel Erat
2014/08/20 17:47:58
nit: rename |cpu_cycle| to |total_cpu_percent|?
(
Daniel Nishi
2014/08/20 18:13:35
Done.
| |
119 #if defined(OS_CHROMEOS) | |
120 // Don't record data yet if no power data exists yet. | |
121 chromeos::PowerDataCollector* power_data_collector = | |
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) | |
131 return; | |
132 #endif | |
133 | |
134 for (ProcessMetricsMap::iterator it = metrics_map_.begin(); | |
135 it != metrics_map_.end(); | |
136 ++it) { | |
137 double last_process_power_usage = it->second->last_cpu_percent(); | |
138 #if defined(OS_CHROMEOS) | |
139 // Only query discharge rate if there is a battery. | |
140 if (sample.battery_percent > -1 && cpu_cycle > 0) | |
Daniel Erat
2014/08/20 17:47:59
nit: use curly brackets here since the statement p
Daniel Nishi
2014/08/20 18:13:35
Done.
| |
141 last_process_power_usage = | |
142 last_process_power_usage * sample.battery_discharge_rate / cpu_cycle; | |
143 #endif | |
144 | |
145 GURL origin = it->second->last_origin(); | |
146 power::OriginPowerMap* origin_power_map = | |
147 power::OriginPowerMapFactory::GetForBrowserContext( | |
148 it->second->profile()); | |
149 DCHECK(origin_power_map); | |
150 origin_power_map->AddPowerForOrigin(origin, last_process_power_usage); | |
151 | |
152 // Invalidate |last_cpu| for the next cycle. | |
153 it->second->set_seen_this_cycle(false); | |
154 } | |
155 } | |
156 | |
157 void ProcessPowerCollector::AddProcessToMap( | |
158 const content::RenderProcessHost* rph, | |
159 const GURL& origin) { | |
160 base::ProcessHandle handle = rph->GetHandle(); | |
161 if (metrics_map_.find(handle) == metrics_map_.end()) { | |
162 metrics_map_[handle] = linked_ptr<PerProcessData>(new PerProcessData( | |
163 #if defined(OS_MACOSX) | |
164 scoped_ptr<base::ProcessMetrics>( | |
165 base::ProcessMetrics::CreateProcessMetrics(handle, NULL)), | |
166 #else | |
167 scoped_ptr<base::ProcessMetrics>( | |
168 base::ProcessMetrics::CreateProcessMetrics(handle)), | |
169 #endif | |
170 origin, | |
171 Profile::FromBrowserContext(rph->GetBrowserContext()))); | |
172 } | |
173 metrics_map_[handle]->set_seen_this_cycle(true); | |
174 } | |
OLD | NEW |