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

Side by Side Diff: chrome/browser/power/process_power_collector.cc

Issue 2181493002: Return unique_ptrs from base::ProcessMetrics. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove os_resource_win.* Created 4 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/power/process_power_collector.h" 5 #include "chrome/browser/power/process_power_collector.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/memory/ptr_util.h"
9 #include "base/process/process_handle.h" 10 #include "base/process/process_handle.h"
10 #include "base/process/process_metrics.h" 11 #include "base/process/process_metrics.h"
12 #include "base/stl_util.h"
11 #include "build/build_config.h" 13 #include "build/build_config.h"
12 #include "chrome/browser/browser_process.h" 14 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/profiles/profile_manager.h" 16 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" 17 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
16 #include "components/power/origin_power_map.h" 18 #include "components/power/origin_power_map.h"
17 #include "components/power/origin_power_map_factory.h" 19 #include "components/power/origin_power_map_factory.h"
18 #include "content/public/browser/browser_context.h" 20 #include "content/public/browser/browser_context.h"
19 #include "content/public/browser/render_process_host.h" 21 #include "content/public/browser/render_process_host.h"
20 #include "content/public/browser/web_contents.h" 22 #include "content/public/browser/web_contents.h"
(...skipping 14 matching lines...) Expand all
35 std::unique_ptr<base::ProcessMetrics> metrics, 37 std::unique_ptr<base::ProcessMetrics> metrics,
36 const GURL& origin, 38 const GURL& origin,
37 Profile* profile) 39 Profile* profile)
38 : metrics_(std::move(metrics)), 40 : metrics_(std::move(metrics)),
39 profile_(profile), 41 profile_(profile),
40 last_origin_(origin), 42 last_origin_(origin),
41 last_cpu_percent_(0), 43 last_cpu_percent_(0),
42 seen_this_cycle_(true) {} 44 seen_this_cycle_(true) {}
43 45
44 ProcessPowerCollector::PerProcessData::PerProcessData() 46 ProcessPowerCollector::PerProcessData::PerProcessData()
45 : profile_(NULL), 47 : profile_(nullptr), last_cpu_percent_(0.0), seen_this_cycle_(false) {}
46 last_cpu_percent_(0.0),
47 seen_this_cycle_(false) {
48 }
49 48
50 ProcessPowerCollector::PerProcessData::~PerProcessData() { 49 ProcessPowerCollector::PerProcessData::~PerProcessData() {
51 } 50 }
52 51
53 ProcessPowerCollector::ProcessPowerCollector() 52 ProcessPowerCollector::ProcessPowerCollector()
54 : scale_factor_(1.0) { 53 : scale_factor_(1.0) {
55 #if defined(OS_CHROMEOS) 54 #if defined(OS_CHROMEOS)
56 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( 55 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver(
57 this); 56 this);
58 #endif 57 #endif
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 DCHECK(!timer_.IsRunning()); 90 DCHECK(!timer_.IsRunning());
92 timer_.Start(FROM_HERE, 91 timer_.Start(FROM_HERE,
93 base::TimeDelta::FromSeconds(kSecondsPerSample), 92 base::TimeDelta::FromSeconds(kSecondsPerSample),
94 this, 93 this,
95 &ProcessPowerCollector::HandleUpdateTimeout); 94 &ProcessPowerCollector::HandleUpdateTimeout);
96 } 95 }
97 96
98 double ProcessPowerCollector::UpdatePowerConsumption() { 97 double ProcessPowerCollector::UpdatePowerConsumption() {
99 double total_cpu_percent = SynchronizeProcesses(); 98 double total_cpu_percent = SynchronizeProcesses();
100 99
101 for (ProcessMetricsMap::iterator it = metrics_map_.begin(); 100 // Invalidate the process for the next cycle.
102 it != metrics_map_.end(); 101 for (auto& metrics : metrics_map_)
103 ++it) { 102 metrics.second->set_seen_this_cycle(false);
104 // Invalidate the process for the next cycle.
105 it->second->set_seen_this_cycle(false);
106 }
107 103
108 RecordCpuUsageByOrigin(total_cpu_percent); 104 RecordCpuUsageByOrigin(total_cpu_percent);
109 return total_cpu_percent; 105 return total_cpu_percent;
110 } 106 }
111 107
112 void ProcessPowerCollector::HandleUpdateTimeout() { 108 void ProcessPowerCollector::HandleUpdateTimeout() {
113 UpdatePowerConsumption(); 109 UpdatePowerConsumption();
114 } 110 }
115 111
116 double ProcessPowerCollector::SynchronizeProcesses() { 112 double ProcessPowerCollector::SynchronizeProcesses() {
117 // Update all tabs. 113 // Update all tabs.
118 for (TabContentsIterator it; !it.done(); it.Next()) { 114 for (TabContentsIterator it; !it.done(); it.Next()) {
119 content::RenderProcessHost* render_process = it->GetRenderProcessHost(); 115 content::RenderProcessHost* render_process = it->GetRenderProcessHost();
120 // Skip incognito web contents. 116 // Skip incognito web contents.
121 if (render_process->GetBrowserContext()->IsOffTheRecord()) 117 if (render_process->GetBrowserContext()->IsOffTheRecord())
122 continue; 118 continue;
123 UpdateProcessInMap(render_process, it->GetLastCommittedURL().GetOrigin()); 119 UpdateProcessInMap(render_process, it->GetLastCommittedURL().GetOrigin());
124 } 120 }
125 121
126 // Iterate over all profiles to find all app windows to attribute all apps. 122 // Iterate over all profiles to find all app windows to attribute all apps.
127 ProfileManager* pm = g_browser_process->profile_manager(); 123 ProfileManager* pm = g_browser_process->profile_manager();
128 std::vector<Profile*> open_profiles = pm->GetLoadedProfiles(); 124 std::vector<Profile*> open_profiles = pm->GetLoadedProfiles();
129 for (std::vector<Profile*>::const_iterator it = open_profiles.begin(); 125 for (Profile* profile : open_profiles) {
130 it != open_profiles.end();
131 ++it) {
132 const extensions::AppWindowRegistry::AppWindowList& app_windows = 126 const extensions::AppWindowRegistry::AppWindowList& app_windows =
133 extensions::AppWindowRegistry::Get(*it)->app_windows(); 127 extensions::AppWindowRegistry::Get(profile)->app_windows();
134 for (extensions::AppWindowRegistry::AppWindowList::const_iterator itr = 128 for (auto* window : app_windows) {
135 app_windows.begin(); 129 content::WebContents* web_contents = window->web_contents();
136 itr != app_windows.end();
137 ++itr) {
138 content::WebContents* web_contents = (*itr)->web_contents();
139
140 UpdateProcessInMap(web_contents->GetRenderProcessHost(), 130 UpdateProcessInMap(web_contents->GetRenderProcessHost(),
141 web_contents->GetLastCommittedURL().GetOrigin()); 131 web_contents->GetLastCommittedURL().GetOrigin());
142 } 132 }
143 } 133 }
144 134
145 // Remove invalid processes and sum up the cpu cycle. 135 // Remove invalid processes and sum up the cpu cycle.
146 double total_cpu_percent = 0.0; 136 double total_cpu_percent = 0.0;
147 ProcessMetricsMap::iterator it = metrics_map_.begin(); 137 ProcessMetricsMap::iterator it = metrics_map_.begin();
148 while (it != metrics_map_.end()) { 138 while (it != metrics_map_.end()) {
149 if (!it->second->seen_this_cycle()) { 139 if (!it->second->seen_this_cycle()) {
150 metrics_map_.erase(it++); 140 metrics_map_.erase(it++);
151 continue; 141 continue;
152 } 142 }
153 143
154 total_cpu_percent += it->second->last_cpu_percent(); 144 total_cpu_percent += it->second->last_cpu_percent();
155 ++it; 145 ++it;
156 } 146 }
157 147
158 return total_cpu_percent; 148 return total_cpu_percent;
159 } 149 }
160 150
161 void ProcessPowerCollector::RecordCpuUsageByOrigin(double total_cpu_percent) { 151 void ProcessPowerCollector::RecordCpuUsageByOrigin(double total_cpu_percent) {
162 DCHECK_GE(total_cpu_percent, 0); 152 DCHECK_GE(total_cpu_percent, 0);
163 if (total_cpu_percent == 0) 153 if (total_cpu_percent == 0)
164 return; 154 return;
165 155
166 for (ProcessMetricsMap::iterator it = metrics_map_.begin(); 156 for (auto& metrics : metrics_map_) {
167 it != metrics_map_.end(); 157 double last_process_power_usage = metrics.second->last_cpu_percent();
168 ++it) {
169 double last_process_power_usage = it->second->last_cpu_percent();
170 last_process_power_usage *= scale_factor_ / total_cpu_percent; 158 last_process_power_usage *= scale_factor_ / total_cpu_percent;
171 159
172 GURL origin = it->second->last_origin(); 160 const GURL& origin = metrics.second->last_origin();
173 power::OriginPowerMap* origin_power_map = 161 power::OriginPowerMap* origin_power_map =
174 power::OriginPowerMapFactory::GetForBrowserContext( 162 power::OriginPowerMapFactory::GetForBrowserContext(
175 it->second->profile()); 163 metrics.second->profile());
176 // |origin_power_map| can be NULL, if the profile is a guest profile in 164 // |origin_power_map| can be NULL, if the profile is a guest profile in
177 // Chrome OS. 165 // Chrome OS.
178 if (!origin_power_map) 166 if (origin_power_map)
179 continue; 167 origin_power_map->AddPowerForOrigin(origin, last_process_power_usage);
180 origin_power_map->AddPowerForOrigin(origin, last_process_power_usage);
181 } 168 }
182 169
183 // Iterate over all profiles to let them know we've finished updating. 170 // Iterate over all profiles to let them know we've finished updating.
184 ProfileManager* pm = g_browser_process->profile_manager(); 171 ProfileManager* pm = g_browser_process->profile_manager();
185 std::vector<Profile*> open_profiles = pm->GetLoadedProfiles(); 172 std::vector<Profile*> open_profiles = pm->GetLoadedProfiles();
186 for (std::vector<Profile*>::const_iterator it = open_profiles.begin(); 173 for (Profile* profile : open_profiles) {
187 it != open_profiles.end();
188 ++it) {
189 power::OriginPowerMap* origin_power_map = 174 power::OriginPowerMap* origin_power_map =
190 power::OriginPowerMapFactory::GetForBrowserContext(*it); 175 power::OriginPowerMapFactory::GetForBrowserContext(profile);
191 if (!origin_power_map) 176 if (origin_power_map)
192 continue; 177 origin_power_map->OnAllOriginsUpdated();
193 origin_power_map->OnAllOriginsUpdated();
194 } 178 }
195 } 179 }
196 180
197 void ProcessPowerCollector::UpdateProcessInMap( 181 void ProcessPowerCollector::UpdateProcessInMap(
198 const content::RenderProcessHost* rph, 182 const content::RenderProcessHost* rph,
199 const GURL& origin) { 183 const GURL& origin) {
200 base::ProcessHandle handle = rph->GetHandle(); 184 base::ProcessHandle handle = rph->GetHandle();
201 if (metrics_map_.find(handle) == metrics_map_.end()) { 185 if (!ContainsKey(metrics_map_, handle)) {
202 metrics_map_[handle] = linked_ptr<PerProcessData>(new PerProcessData( 186 metrics_map_[handle] = base::MakeUnique<PerProcessData>(
203 #if defined(OS_MACOSX) 187 #if defined(OS_MACOSX)
204 std::unique_ptr<base::ProcessMetrics>( 188 base::ProcessMetrics::CreateProcessMetrics(handle, nullptr),
205 base::ProcessMetrics::CreateProcessMetrics(handle, NULL)),
206 #else 189 #else
207 std::unique_ptr<base::ProcessMetrics>( 190 base::ProcessMetrics::CreateProcessMetrics(handle),
208 base::ProcessMetrics::CreateProcessMetrics(handle)),
209 #endif 191 #endif
210 origin, Profile::FromBrowserContext(rph->GetBrowserContext()))); 192 origin, Profile::FromBrowserContext(rph->GetBrowserContext()));
211 } 193 }
212 194
213 linked_ptr<PerProcessData>& process_data = metrics_map_[handle]; 195 PerProcessData* process_data = metrics_map_[handle].get();
214 process_data->set_last_cpu_percent(std::max(0.0, 196 process_data->set_last_cpu_percent(std::max(0.0,
215 cpu_usage_callback_.is_null() ? process_data->metrics()->GetCPUUsage() 197 cpu_usage_callback_.is_null() ? process_data->metrics()->GetCPUUsage()
216 : cpu_usage_callback_.Run(handle))); 198 : cpu_usage_callback_.Run(handle)));
217 process_data->set_seen_this_cycle(true); 199 process_data->set_seen_this_cycle(true);
218 } 200 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698