OLD | NEW |
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> | |
9 | 8 |
10 #include "base/bind.h" | 9 #include "base/bind.h" |
11 #include "base/logging.h" | 10 #include "base/logging.h" |
12 #include "base/process_util.h" | 11 #include "base/process_util.h" |
13 #include "base/stl_util.h" | |
14 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
15 #include "base/threading/worker_pool.h" | 13 #include "base/threading/worker_pool.h" |
16 #include "base/time.h" | 14 #include "base/time.h" |
17 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/browser_shutdown.h" | 16 #include "chrome/browser/browser_shutdown.h" |
19 #include "chrome/browser/extensions/crx_installer.h" | 17 #include "chrome/browser/extensions/crx_installer.h" |
20 #include "chrome/browser/performance_monitor/constants.h" | 18 #include "chrome/browser/performance_monitor/constants.h" |
| 19 #include "chrome/browser/performance_monitor/database.h" |
21 #include "chrome/browser/performance_monitor/performance_monitor_util.h" | 20 #include "chrome/browser/performance_monitor/performance_monitor_util.h" |
22 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
23 #include "chrome/browser/profiles/profile_manager.h" | 22 #include "chrome/browser/profiles/profile_manager.h" |
24 #include "chrome/browser/ui/browser.h" | 23 #include "chrome/browser/ui/browser.h" |
25 #include "chrome/browser/ui/browser_list.h" | 24 #include "chrome/browser/ui/browser_list.h" |
26 #include "chrome/common/chrome_notification_types.h" | 25 #include "chrome/common/chrome_notification_types.h" |
27 #include "chrome/common/chrome_version_info.h" | 26 #include "chrome/common/chrome_version_info.h" |
28 #include "chrome/common/extensions/extension.h" | 27 #include "chrome/common/extensions/extension.h" |
29 #include "chrome/common/extensions/extension_constants.h" | 28 #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" | |
32 #include "content/public/browser/browser_thread.h" | 29 #include "content/public/browser/browser_thread.h" |
33 #include "content/public/browser/notification_service.h" | 30 #include "content/public/browser/notification_service.h" |
34 #include "content/public/browser/notification_types.h" | 31 #include "content/public/browser/notification_types.h" |
35 #include "content/public/browser/render_process_host.h" | 32 #include "content/public/browser/render_process_host.h" |
36 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
37 | 34 |
38 using content::BrowserThread; | 35 using content::BrowserThread; |
39 using extensions::Extension; | 36 using extensions::Extension; |
40 | 37 |
41 namespace { | 38 namespace { |
42 const uint32 kAccessFlags = base::kProcessAccessDuplicateHandle | | |
43 base::kProcessAccessQueryInformation | | |
44 base::kProcessAccessTerminate | | |
45 base::kProcessAccessWaitForTermination; | |
46 | 39 |
47 std::string TimeToString(base::Time time) { | 40 std::string TimeToString(base::Time time) { |
48 int64 time_int64 = time.ToInternalValue(); | 41 int64 time_int64 = time.ToInternalValue(); |
49 return base::Int64ToString(time_int64); | 42 return base::Int64ToString(time_int64); |
50 } | 43 } |
51 | 44 |
52 bool StringToTime(std::string time, base::Time* output) { | 45 bool StringToTime(std::string time, base::Time* output) { |
53 int64 time_int64 = 0; | 46 int64 time_int64 = 0; |
54 if (!base::StringToInt64(time, &time_int64)) | 47 if (!base::StringToInt64(time, &time_int64)) |
55 return false; | 48 return false; |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 base::Bind(callback, state_value)); | 233 base::Bind(callback, state_value)); |
241 } | 234 } |
242 | 235 |
243 void PerformanceMonitor::NotifyInitialized() { | 236 void PerformanceMonitor::NotifyInitialized() { |
244 content::NotificationService::current()->Notify( | 237 content::NotificationService::current()->Notify( |
245 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, | 238 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, |
246 content::Source<PerformanceMonitor>(this), | 239 content::Source<PerformanceMonitor>(this), |
247 content::NotificationService::NoDetails()); | 240 content::NotificationService::NoDetails()); |
248 } | 241 } |
249 | 242 |
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 | |
334 void PerformanceMonitor::UpdateLiveProfiles() { | 243 void PerformanceMonitor::UpdateLiveProfiles() { |
335 std::string time = TimeToString(base::Time::Now()); | 244 std::string time = TimeToString(base::Time::Now()); |
336 scoped_ptr<std::set<std::string> > active_profiles( | 245 scoped_ptr<std::set<std::string> > active_profiles( |
337 new std::set<std::string>()); | 246 new std::set<std::string>()); |
338 | 247 |
339 for (BrowserList::const_iterator iter = BrowserList::begin(); | 248 for (BrowserList::const_iterator iter = BrowserList::begin(); |
340 iter != BrowserList::end(); ++iter) { | 249 iter != BrowserList::end(); ++iter) { |
341 active_profiles->insert((*iter)->profile()->GetDebugName()); | 250 active_profiles->insert((*iter)->profile()->GetDebugName()); |
342 } | 251 } |
343 | 252 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 break; | 383 break; |
475 } | 384 } |
476 default: { | 385 default: { |
477 NOTREACHED(); | 386 NOTREACHED(); |
478 break; | 387 break; |
479 } | 388 } |
480 } | 389 } |
481 } | 390 } |
482 | 391 |
483 } // namespace performance_monitor | 392 } // namespace performance_monitor |
OLD | NEW |