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

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: Sync with trunk Created 8 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 (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 <set> 7 #include <set>
8 #include <vector>
8 9
9 #include "base/bind.h" 10 #include "base/bind.h"
10 #include "base/logging.h" 11 #include "base/logging.h"
11 #include "base/process_util.h" 12 #include "base/process_util.h"
13 #include "base/stl_util.h"
12 #include "base/string_number_conversions.h" 14 #include "base/string_number_conversions.h"
13 #include "base/threading/worker_pool.h" 15 #include "base/threading/worker_pool.h"
14 #include "base/time.h" 16 #include "base/time.h"
15 #include "chrome/browser/browser_process.h" 17 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/browser_shutdown.h" 18 #include "chrome/browser/browser_shutdown.h"
17 #include "chrome/browser/extensions/crx_installer.h" 19 #include "chrome/browser/extensions/crx_installer.h"
18 #include "chrome/browser/performance_monitor/constants.h" 20 #include "chrome/browser/performance_monitor/constants.h"
19 #include "chrome/browser/performance_monitor/database.h"
20 #include "chrome/browser/performance_monitor/performance_monitor_util.h" 21 #include "chrome/browser/performance_monitor/performance_monitor_util.h"
21 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/profiles/profile_manager.h" 23 #include "chrome/browser/profiles/profile_manager.h"
23 #include "chrome/browser/ui/browser.h" 24 #include "chrome/browser/ui/browser.h"
24 #include "chrome/browser/ui/browser_list.h" 25 #include "chrome/browser/ui/browser_list.h"
25 #include "chrome/common/chrome_notification_types.h" 26 #include "chrome/common/chrome_notification_types.h"
26 #include "chrome/common/chrome_version_info.h" 27 #include "chrome/common/chrome_version_info.h"
27 #include "chrome/common/extensions/extension.h" 28 #include "chrome/common/extensions/extension.h"
28 #include "chrome/common/extensions/extension_constants.h" 29 #include "chrome/common/extensions/extension_constants.h"
30 #include "chrome/test/base/chrome_process_util.h"
31 #include "content/public/browser/browser_child_process_host.h"
29 #include "content/public/browser/browser_thread.h" 32 #include "content/public/browser/browser_thread.h"
30 #include "content/public/browser/notification_service.h" 33 #include "content/public/browser/notification_service.h"
31 #include "content/public/browser/notification_types.h" 34 #include "content/public/browser/notification_types.h"
32 #include "content/public/browser/render_process_host.h" 35 #include "content/public/browser/render_process_host.h"
33 #include "content/public/browser/web_contents.h" 36 #include "content/public/browser/web_contents.h"
34 37
35 using content::BrowserThread; 38 using content::BrowserThread;
36 using extensions::Extension; 39 using extensions::Extension;
37 40
38 namespace { 41 namespace {
42 const uint32 kAccessFlags = base::kProcessAccessDuplicateHandle |
43 base::kProcessAccessQueryInformation |
44 base::kProcessAccessTerminate |
45 base::kProcessAccessWaitForTermination;
39 46
40 std::string TimeToString(base::Time time) { 47 std::string TimeToString(base::Time time) {
41 int64 time_int64 = time.ToInternalValue(); 48 int64 time_int64 = time.ToInternalValue();
42 return base::Int64ToString(time_int64); 49 return base::Int64ToString(time_int64);
43 } 50 }
44 51
45 bool StringToTime(std::string time, base::Time* output) { 52 bool StringToTime(std::string time, base::Time* output) {
46 int64 time_int64 = 0; 53 int64 time_int64 = 0;
47 if (!base::StringToInt64(time, &time_int64)) 54 if (!base::StringToInt64(time, &time_int64))
48 return false; 55 return false;
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 base::Bind(callback, state_value)); 240 base::Bind(callback, state_value));
234 } 241 }
235 242
236 void PerformanceMonitor::NotifyInitialized() { 243 void PerformanceMonitor::NotifyInitialized() {
237 content::NotificationService::current()->Notify( 244 content::NotificationService::current()->Notify(
238 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, 245 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED,
239 content::Source<PerformanceMonitor>(this), 246 content::Source<PerformanceMonitor>(this),
240 content::NotificationService::NoDetails()); 247 content::NotificationService::NoDetails());
241 } 248 }
242 249
250 void PerformanceMonitor::GatherStatisticsOnBackgroundThread() {
251 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
252
253 // Because the CPU usage is gathered as an average since the last time the
254 // function was called, while the memory usage is gathered as an instantaneous
255 // usage, the CPU usage is gathered before the metrics map is wiped.
256 GatherCPUUsageOnBackgroundThread();
257 UpdateMetricsMapOnBackgroundThread();
258 GatherMemoryUsageOnBackgroundThread();
259 }
260
261 void PerformanceMonitor::GatherCPUUsageOnBackgroundThread() {
262 if (metrics_map_.size()) {
263 double cpu_usage = 0;
264 for (MetricsMap::const_iterator iter = metrics_map_.begin();
265 iter != metrics_map_.end(); ++iter) {
266 cpu_usage += iter->second->GetCPUUsage();
267 }
268
269 database_->AddMetric(METRIC_CPU_USAGE, base::DoubleToString(cpu_usage));
270 }
271 }
272
273 void PerformanceMonitor::GatherMemoryUsageOnBackgroundThread() {
274 size_t private_memory_sum = 0;
275 size_t shared_memory_sum = 0;
276 for (MetricsMap::const_iterator iter = metrics_map_.begin();
277 iter != metrics_map_.end(); ++iter) {
278 size_t private_memory = 0;
279 size_t shared_memory = 0;
280 if (iter->second->GetMemoryBytes(&private_memory, &shared_memory)) {
281 private_memory_sum += private_memory;
282 shared_memory_sum += shared_memory;
283 } else {
284 LOG(WARNING) << "GetMemoryBytes returned NULL (platform-specific error)";
285 }
286 }
287
288 database_->AddMetric(METRIC_PRIVATE_MEMORY_USAGE,
289 base::Uint64ToString(private_memory_sum));
290 database_->AddMetric(METRIC_SHARED_MEMORY_USAGE,
291 base::Uint64ToString(shared_memory_sum));
292 }
293
294 void PerformanceMonitor::UpdateMetricsMapOnBackgroundThread() {
295 // Remove old process handles. Use two iterators to safely call erase() on the
296 // current element.
297 for (MetricsMap::iterator iter_next = metrics_map_.begin();
298 iter_next != metrics_map_.end();) {
299 MetricsMap::iterator iter = iter_next++;
300
301 if (base::GetTerminationStatus(iter->first, NULL) !=
302 base::TERMINATION_STATUS_STILL_RUNNING) {
303 base::CloseProcessHandle(iter->first);
304 metrics_map_.erase(iter);
305 }
306 }
307
308 // Add new process handles.
309 base::ProcessId browser_pid = base::GetCurrentProcId();
310 ChromeProcessList chrome_processes(GetRunningChromeProcesses(browser_pid));
311 for (ChromeProcessList::const_iterator pid_iter = chrome_processes.begin();
312 pid_iter != chrome_processes.end(); ++pid_iter) {
313 base::ProcessHandle process_handle;
314 if (base::OpenProcessHandleWithAccess(*pid_iter,
315 kAccessFlags,
316 &process_handle) &&
317 !ContainsKey(metrics_map_, process_handle)) {
318 #if defined(OS_MACOSX)
319 linked_ptr<base::ProcessMetrics> process_metrics(
320 base::ProcessMetrics::CreateProcessMetrics(process_handle,
321 content::BrowserChildProcessHost::GetPortProvider()));
322 #else
323 linked_ptr<base::ProcessMetrics> process_metrics(
324 base::ProcessMetrics::CreateProcessMetrics(process_handle));
325 #endif
326 // Prime the CPUUsage to be gathered next time.
327 process_metrics->GetCPUUsage();
328
329 metrics_map_[process_handle] = process_metrics;
330 }
331 }
332 }
333
243 void PerformanceMonitor::UpdateLiveProfiles() { 334 void PerformanceMonitor::UpdateLiveProfiles() {
244 std::string time = TimeToString(base::Time::Now()); 335 std::string time = TimeToString(base::Time::Now());
245 scoped_ptr<std::set<std::string> > active_profiles( 336 scoped_ptr<std::set<std::string> > active_profiles(
246 new std::set<std::string>()); 337 new std::set<std::string>());
247 338
248 for (BrowserList::const_iterator iter = BrowserList::begin(); 339 for (BrowserList::const_iterator iter = BrowserList::begin();
249 iter != BrowserList::end(); ++iter) { 340 iter != BrowserList::end(); ++iter) {
250 active_profiles->insert((*iter)->profile()->GetDebugName()); 341 active_profiles->insert((*iter)->profile()->GetDebugName());
251 } 342 }
252 343
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 break; 474 break;
384 } 475 }
385 default: { 476 default: {
386 NOTREACHED(); 477 NOTREACHED();
387 break; 478 break;
388 } 479 }
389 } 480 }
390 } 481 }
391 482
392 } // namespace performance_monitor 483 } // namespace performance_monitor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698