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 : metrics_(metrics.Pass()), | |
34 profile_(profile), | |
35 last_origin_(origin), | |
36 last_cpu_percent_(0), | |
37 seen_this_cycle_(true) { | |
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 double total_cpu_percent = SynchronizeProcesses(); | |
62 RecordCpuUsageByOrigin(total_cpu_percent); | |
63 } | |
64 | |
65 double ProcessPowerCollector::SynchronizeProcesses() { | |
66 // Update all tabs. | |
67 for (TabContentsIterator it; !it.done(); it.Next()) { | |
68 content::RenderProcessHost* render_process = it->GetRenderProcessHost(); | |
69 // Skip incognito web contents. | |
70 if (render_process->GetBrowserContext()->IsOffTheRecord()) | |
71 continue; | |
72 UpdateProcessInMap(render_process, it->GetLastCommittedURL().GetOrigin()); | |
73 } | |
74 | |
75 // Iterate over all profiles to find all app windows to attribute all apps. | |
76 ProfileManager* pm = g_browser_process->profile_manager(); | |
77 std::vector<Profile*> open_profiles = pm->GetLoadedProfiles(); | |
78 for (std::vector<Profile*>::const_iterator it = open_profiles.begin(); | |
79 it != open_profiles.end(); | |
80 ++it) { | |
81 const apps::AppWindowRegistry::AppWindowList& app_windows = | |
82 apps::AppWindowRegistry::Get(*it)->app_windows(); | |
83 for (apps::AppWindowRegistry::AppWindowList::const_iterator itr = | |
84 app_windows.begin(); | |
85 itr != app_windows.end(); | |
86 ++itr) { | |
87 content::WebContents* web_contents = (*itr)->web_contents(); | |
88 | |
89 UpdateProcessInMap(web_contents->GetRenderProcessHost(), | |
90 web_contents->GetLastCommittedURL().GetOrigin()); | |
91 } | |
92 } | |
93 | |
94 // Remove invalid processes and sum up the cpu cycle. | |
95 double total_cpu_percent = 0.0; | |
96 ProcessMetricsMap::iterator it = metrics_map_.begin(); | |
97 while (it != metrics_map_.end()) { | |
98 if (!it->second->seen_this_cycle()) { | |
99 metrics_map_.erase(it++); | |
100 continue; | |
101 } | |
102 | |
103 // Skip errors. | |
104 if (it->second->last_cpu_percent() < 0) { | |
Daniel Erat
2014/08/20 19:55:07
nit: move this into UpdateProcessInMap() too
Daniel Nishi
2014/08/20 20:56:34
Done.
| |
105 it->second->set_last_cpu_percent(0); | |
106 continue; | |
Daniel Erat
2014/08/20 19:55:07
this will be addressed by the previous comment, bu
| |
107 } | |
108 | |
109 total_cpu_percent += it->second->last_cpu_percent(); | |
110 it++; | |
111 } | |
112 | |
113 return total_cpu_percent; | |
114 } | |
115 | |
116 void ProcessPowerCollector::RecordCpuUsageByOrigin(double total_cpu_percent) { | |
117 #if defined(OS_CHROMEOS) | |
118 // Don't record data yet if no power data exists yet. | |
119 chromeos::PowerDataCollector* power_data_collector = | |
120 chromeos::PowerDataCollector::Get(); | |
121 if (power_data_collector->power_supply_data().empty()) | |
122 return; | |
123 | |
124 chromeos::PowerDataCollector::PowerSupplySample sample = | |
125 power_data_collector->power_supply_data().back(); | |
126 | |
127 // Don't record data if plugged in. | |
128 if (sample.external_power) | |
129 return; | |
130 #endif | |
131 | |
132 for (ProcessMetricsMap::iterator it = metrics_map_.begin(); | |
133 it != metrics_map_.end(); | |
134 ++it) { | |
135 double last_process_power_usage = it->second->last_cpu_percent(); | |
136 #if defined(OS_CHROMEOS) | |
137 // Only query discharge rate if there is a battery. | |
138 if (sample.battery_percent > -1 && total_cpu_percent > 0) { | |
139 last_process_power_usage = last_process_power_usage * | |
140 sample.battery_discharge_rate / | |
141 total_cpu_percent; | |
142 } | |
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::UpdateProcessInMap( | |
158 const content::RenderProcessHost* rph, | |
159 const GURL& origin) { | |
160 base::ProcessHandle handle = rph->GetHandle(); | |
161 if (metrics_map_.find(handle) == metrics_map_.end()) { | |
Daniel Erat
2014/08/20 19:55:07
nit: to avoid the repeated lookups below, do somet
| |
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_last_cpu_percent( | |
174 metrics_map_[handle]->metrics()->GetCPUUsage()); | |
175 metrics_map_[handle]->set_seen_this_cycle(true); | |
176 } | |
OLD | NEW |