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

Side by Side Diff: chrome/browser/performance_monitor/performance_monitor.cc

Issue 10656052: Performance monitor stats gathering. (Closed) Base URL: http://git.chromium.org/chromium/src.git@cpm_main
Patch Set: Created 8 years, 5 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/performance_monitor/performance_monitor.h" 5 #include "chrome/browser/performance_monitor/performance_monitor.h"
6 6
7 #include <algorithm>
8 #include <vector>
9
7 #include "base/bind.h" 10 #include "base/bind.h"
8 #include "base/logging.h" 11 #include "base/logging.h"
9 #include "base/process_util.h" 12 #include "base/stl_util.h"
13 #include "base/string_number_conversions.h"
10 #include "base/threading/worker_pool.h" 14 #include "base/threading/worker_pool.h"
11 #include "base/time.h" 15 #include "base/time.h"
12 #include "chrome/browser/browser_shutdown.h" 16 #include "chrome/browser/browser_shutdown.h"
13 #include "chrome/browser/performance_monitor/constants.h" 17 #include "chrome/browser/performance_monitor/constants.h"
14 #include "chrome/browser/performance_monitor/database.h"
15 #include "chrome/browser/performance_monitor/performance_monitor_util.h" 18 #include "chrome/browser/performance_monitor/performance_monitor_util.h"
16 #include "chrome/browser/extensions/crx_installer.h" 19 #include "chrome/browser/extensions/crx_installer.h"
17 #include "chrome/common/chrome_notification_types.h" 20 #include "chrome/common/chrome_notification_types.h"
18 #include "chrome/common/chrome_version_info.h" 21 #include "chrome/common/chrome_version_info.h"
19 #include "chrome/common/extensions/extension.h" 22 #include "chrome/common/extensions/extension.h"
20 #include "chrome/common/extensions/extension_constants.h" 23 #include "chrome/common/extensions/extension_constants.h"
24 #include "chrome/test/base/chrome_process_util.h"
21 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/notification_service.h" 26 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/notification_types.h" 27 #include "content/public/browser/notification_types.h"
24 #include "content/public/browser/render_process_host.h" 28 #include "content/public/browser/render_process_host.h"
25 #include "content/public/browser/web_contents.h" 29 #include "content/public/browser/web_contents.h"
26 30
27 using content::BrowserThread; 31 using content::BrowserThread;
28 using extensions::Extension; 32 using extensions::Extension;
29 33
34 namespace {
35
36 template <class T>
37 T GetMedian(std::vector<T> vec) {
38 typedef typename std::vector<T>::size_type vec_size;
39
40 vec_size size = vec.size();
41 DCHECK(size != 0);
42 std::sort(vec.begin(), vec.end());
43 vec_size mid = size / 2;
44
45 return size % 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid];
46 }
47
48 } // namespace
49
30 namespace performance_monitor { 50 namespace performance_monitor {
31 51
32 PerformanceMonitor::PerformanceMonitor() : database_(NULL) { 52 PerformanceMonitor::PerformanceMonitor() : database_(NULL) {
33 } 53 }
34 54
35 PerformanceMonitor::~PerformanceMonitor() { 55 PerformanceMonitor::~PerformanceMonitor() {
36 } 56 }
37 57
38 bool PerformanceMonitor::SetDatabasePath(const FilePath& path) { 58 bool PerformanceMonitor::SetDatabasePath(const FilePath& path) {
39 if (!database_.get()) { 59 if (!database_.get()) {
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 base::Bind(callback, state_value)); 177 base::Bind(callback, state_value));
158 } 178 }
159 179
160 void PerformanceMonitor::NotifyInitialized() { 180 void PerformanceMonitor::NotifyInitialized() {
161 content::NotificationService::current()->Notify( 181 content::NotificationService::current()->Notify(
162 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, 182 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED,
163 content::Source<PerformanceMonitor>(this), 183 content::Source<PerformanceMonitor>(this),
164 content::NotificationService::NoDetails()); 184 content::NotificationService::NoDetails());
165 } 185 }
166 186
187 void PerformanceMonitor::GatherStatisticsOnBackgroundThread() {
188 CHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
Devlin 2012/07/23 21:10:33 you can remove the preceding content::'s now.
mitchellwrosen 2012/07/23 23:58:51 Done.
189
190 // Because the CPU usage is gathered as an average since the last time the
191 // function was called, while the memory usage is gathered as an instantaneous
192 // usage, the CPU usage is gathered before the metrics map is wiped.
193 GatherCPUUsage();
194 UpdateMetricsMap();
195 GatherMemoryUsage();
196 }
197
198 void PerformanceMonitor::GatherCPUUsage() {
199 std::vector<double> cpu_usage_vec;
200
201 if (metrics_map_.size()) {
202 for (MetricsMap::const_iterator iter = metrics_map_.begin();
203 iter != metrics_map_.end(); ++iter)
204 cpu_usage_vec.push_back(iter->second->GetCPUUsage());
205
206 std::sort(cpu_usage_vec.begin(), cpu_usage_vec.end());
207 database_->AddMetric(performance_monitor::kMetricCPUUsage,
208 base::DoubleToString(GetMedian(cpu_usage_vec)));
Devlin 2012/07/23 21:10:33 I think these should be sums, not medians. Medians
Aaron Boodman 2012/07/23 23:12:31 Agree.
mitchellwrosen 2012/07/23 23:58:51 Done.
209 }
210 }
211
212 void PerformanceMonitor::GatherMemoryUsage() {
213 std::vector<size_t> private_memory_vec;
214 std::vector<size_t> shared_memory_vec;
215
216 size_t private_memory = 0;
217 size_t shared_memory = 0;
218 for (MetricsMap::const_iterator iter = metrics_map_.begin();
219 iter != metrics_map_.end(); ++iter) {
220 if (iter->second->GetMemoryBytes(&private_memory, &shared_memory)) {
221 private_memory_vec.push_back(private_memory);
222 shared_memory_vec.push_back(shared_memory);
223 } else {
224 LOG(ERROR) << "GetMemoryBytes returned NULL (platform-specific error)";
225 }
226 }
227
228 if (!private_memory_vec.size())
229 return;
230
231 std::sort(private_memory_vec.begin(), private_memory_vec.end());
232 std::sort(shared_memory_vec.begin(), shared_memory_vec.end());
233
234 database_->AddMetric(performance_monitor::kMetricPrivateMemoryUsage,
235 base::Uint64ToString(GetMedian(private_memory_vec)));
Devlin 2012/07/23 21:10:33 See above comment.
mitchellwrosen 2012/07/23 23:58:51 Done.
236 database_->AddMetric(performance_monitor::kMetricSharedMemoryUsage,
237 base::Uint64ToString(GetMedian(shared_memory_vec)));
238 }
239
240 void PerformanceMonitor::UpdateMetricsMap() {
241 // Remove old process handles. Use two iterators to safely call erase() on the
242 // current element.
243 for (MetricsMap::iterator iter_next = metrics_map_.begin();
244 iter_next != metrics_map_.end();) {
245 MetricsMap::iterator iter = iter_next++;
246
247 if (base::GetTerminationStatus(iter->first, NULL) !=
248 base::TERMINATION_STATUS_STILL_RUNNING) {
249 base::CloseProcessHandle(iter->first);
250 metrics_map_.erase(iter);
251 } else {
252 // Prime the CPUUsage to be gathered next time.
253 iter->second->GetCPUUsage();
254 }
255 }
256
257 // Add new process handles.
258 base::ProcessId browser_pid = base::GetCurrentProcId();
259 ChromeProcessList chrome_processes(GetRunningChromeProcesses(browser_pid));
260 for (ChromeProcessList::const_iterator pid_iter = chrome_processes.begin();
261 pid_iter != chrome_processes.end(); ++pid_iter) {
262 base::ProcessHandle process_handle;
263 if (base::OpenProcessHandle(*pid_iter, &process_handle) &&
264 !ContainsKey(metrics_map_, process_handle)) {
265 #if !defined(OS_MACOSX)
Devlin 2012/07/23 21:10:33 nit: This might be clearer as #if defined(OS_MACOS
mitchellwrosen 2012/07/23 23:58:51 Tomato, tomato. See task_manager.cc:756. Anyways,
266 linked_ptr<base::ProcessMetrics> process_metrics(
267 base::ProcessMetrics::CreateProcessMetrics(process_handle));
268 #else
269 linked_ptr<base::ProcessMetrics> process_metrics(
270 base::ProcessMetrics::CreateProcessMetrics(process_handle,
271 content::BrowserChildProcessHost::GetPortProvider()));
272 #endif
273 // Prime the CPUUsage to be gathered next time.
274 process_metrics->GetCPUUsage();
275
276 metrics_map_[process_handle] = process_metrics;
277 }
278 }
279 }
280
167 void PerformanceMonitor::Observe(int type, 281 void PerformanceMonitor::Observe(int type,
168 const content::NotificationSource& source, 282 const content::NotificationSource& source,
169 const content::NotificationDetails& details) { 283 const content::NotificationDetails& details) {
170 switch (type) { 284 switch (type) {
171 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { 285 case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
172 const Extension* extension = content::Details<Extension>(details).ptr(); 286 const Extension* extension = content::Details<Extension>(details).ptr();
173 AddEvent(util::CreateExtensionInstallEvent(base::Time::Now(), 287 AddEvent(util::CreateExtensionInstallEvent(base::Time::Now(),
174 extension->id(), 288 extension->id(),
175 extension->name(), 289 extension->name(),
176 extension->url().spec(), 290 extension->url().spec(),
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 break; 374 break;
261 } 375 }
262 default: { 376 default: {
263 NOTREACHED(); 377 NOTREACHED();
264 break; 378 break;
265 } 379 }
266 } 380 }
267 } 381 }
268 382
269 } // namespace performance_monitor 383 } // namespace performance_monitor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698