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> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 16 matching lines...) Expand all Loading... | |
27 #include "chrome/common/chrome_version_info.h" | 27 #include "chrome/common/chrome_version_info.h" |
28 #include "chrome/common/extensions/extension.h" | 28 #include "chrome/common/extensions/extension.h" |
29 #include "chrome/common/extensions/extension_constants.h" | 29 #include "chrome/common/extensions/extension_constants.h" |
30 #include "chrome/test/base/chrome_process_util.h" | 30 #include "chrome/test/base/chrome_process_util.h" |
31 #include "content/public/browser/browser_child_process_host.h" | 31 #include "content/public/browser/browser_child_process_host.h" |
32 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
33 #include "content/public/browser/load_notification_details.h" | 33 #include "content/public/browser/load_notification_details.h" |
34 #include "content/public/browser/notification_service.h" | 34 #include "content/public/browser/notification_service.h" |
35 #include "content/public/browser/notification_types.h" | 35 #include "content/public/browser/notification_types.h" |
36 #include "content/public/browser/web_contents.h" | 36 #include "content/public/browser/web_contents.h" |
37 #include "net/url_request/url_request.h" | |
37 | 38 |
38 using content::BrowserThread; | 39 using content::BrowserThread; |
39 using extensions::Extension; | 40 using extensions::Extension; |
40 | 41 |
41 namespace { | 42 namespace { |
42 const uint32 kAccessFlags = base::kProcessAccessDuplicateHandle | | 43 const uint32 kAccessFlags = base::kProcessAccessDuplicateHandle | |
43 base::kProcessAccessQueryInformation | | 44 base::kProcessAccessQueryInformation | |
44 base::kProcessAccessTerminate | | 45 base::kProcessAccessTerminate | |
45 base::kProcessAccessWaitForTermination; | 46 base::kProcessAccessWaitForTermination; |
46 | 47 |
47 std::string TimeToString(base::Time time) { | 48 std::string TimeToString(base::Time time) { |
48 int64 time_int64 = time.ToInternalValue(); | 49 int64 time_int64 = time.ToInternalValue(); |
49 return base::Int64ToString(time_int64); | 50 return base::Int64ToString(time_int64); |
50 } | 51 } |
51 | 52 |
52 bool StringToTime(std::string time, base::Time* output) { | 53 bool StringToTime(std::string time, base::Time* output) { |
53 int64 time_int64 = 0; | 54 int64 time_int64 = 0; |
54 if (!base::StringToInt64(time, &time_int64)) | 55 if (!base::StringToInt64(time, &time_int64)) |
55 return false; | 56 return false; |
56 *output = base::Time::FromInternalValue(time_int64); | 57 *output = base::Time::FromInternalValue(time_int64); |
57 return true; | 58 return true; |
58 } | 59 } |
59 | 60 |
60 } // namespace | 61 } // namespace |
61 | 62 |
62 namespace performance_monitor { | 63 namespace performance_monitor { |
63 | 64 |
65 bool PerformanceMonitor::initialized_ = false; | |
66 | |
67 PerformanceMonitor::PerformanceDataForIOThread::PerformanceDataForIOThread() | |
68 : network_bytes_read(0) { | |
69 } | |
70 | |
64 PerformanceMonitor::PerformanceMonitor() : database_(NULL) { | 71 PerformanceMonitor::PerformanceMonitor() : database_(NULL) { |
65 } | 72 } |
66 | 73 |
67 PerformanceMonitor::~PerformanceMonitor() { | 74 PerformanceMonitor::~PerformanceMonitor() { |
68 } | 75 } |
69 | 76 |
70 bool PerformanceMonitor::SetDatabasePath(const FilePath& path) { | 77 bool PerformanceMonitor::SetDatabasePath(const FilePath& path) { |
71 if (!database_.get()) { | 78 if (!database_.get()) { |
72 database_path_ = path; | 79 database_path_ = path; |
73 return true; | 80 return true; |
(...skipping 13 matching lines...) Expand all Loading... | |
87 FROM_HERE, | 94 FROM_HERE, |
88 base::Bind(&PerformanceMonitor::InitOnBackgroundThread, | 95 base::Bind(&PerformanceMonitor::InitOnBackgroundThread, |
89 base::Unretained(this)), | 96 base::Unretained(this)), |
90 base::Bind(&PerformanceMonitor::FinishInit, | 97 base::Bind(&PerformanceMonitor::FinishInit, |
91 base::Unretained(this))); | 98 base::Unretained(this))); |
92 } | 99 } |
93 | 100 |
94 void PerformanceMonitor::InitOnBackgroundThread() { | 101 void PerformanceMonitor::InitOnBackgroundThread() { |
95 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | 102 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
96 database_ = Database::Create(database_path_); | 103 database_ = Database::Create(database_path_); |
104 | |
105 // Initialize the io thread's performance data to the value in the database; | |
106 // if there isn't a recording in the database, the value stays at 0. | |
107 MetricInfo info; | |
108 if (database_->GetRecentStatsForActivityAndMetric(METRIC_NETWORK_BYTES_READ, | |
109 &info)) { | |
110 performance_data_for_io_thread_.network_bytes_read = info.value; | |
111 } | |
97 } | 112 } |
98 | 113 |
99 void PerformanceMonitor::FinishInit() { | 114 void PerformanceMonitor::FinishInit() { |
100 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 115 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
101 RegisterForNotifications(); | 116 RegisterForNotifications(); |
102 CheckForUncleanExits(); | 117 CheckForUncleanExits(); |
103 BrowserThread::PostBlockingPoolSequencedTask( | 118 BrowserThread::PostBlockingPoolSequencedTask( |
104 Database::kDatabaseSequenceToken, | 119 Database::kDatabaseSequenceToken, |
105 FROM_HERE, | 120 FROM_HERE, |
106 base::Bind(&PerformanceMonitor::CheckForVersionUpdateOnBackgroundThread, | 121 base::Bind(&PerformanceMonitor::CheckForVersionUpdateOnBackgroundThread, |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 const std::string& value) { | 252 const std::string& value) { |
238 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | 253 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
239 database_->AddMetric(type, value); | 254 database_->AddMetric(type, value); |
240 } | 255 } |
241 | 256 |
242 void PerformanceMonitor::NotifyInitialized() { | 257 void PerformanceMonitor::NotifyInitialized() { |
243 content::NotificationService::current()->Notify( | 258 content::NotificationService::current()->Notify( |
244 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, | 259 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, |
245 content::Source<PerformanceMonitor>(this), | 260 content::Source<PerformanceMonitor>(this), |
246 content::NotificationService::NoDetails()); | 261 content::NotificationService::NoDetails()); |
262 | |
263 initialized_ = true; | |
247 } | 264 } |
248 | 265 |
249 void PerformanceMonitor::GatherStatisticsOnBackgroundThread() { | 266 void PerformanceMonitor::GatherStatisticsOnBackgroundThread() { |
250 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | 267 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
251 | 268 |
252 // Because the CPU usage is gathered as an average since the last time the | 269 // Because the CPU usage is gathered as an average since the last time the |
253 // function was called, while the memory usage is gathered as an instantaneous | 270 // function was called, while the memory usage is gathered as an instantaneous |
254 // usage, the CPU usage is gathered before the metrics map is wiped. | 271 // usage, the CPU usage is gathered before the metrics map is wiped. |
255 GatherCPUUsageOnBackgroundThread(); | 272 GatherCPUUsageOnBackgroundThread(); |
256 UpdateMetricsMapOnBackgroundThread(); | 273 UpdateMetricsMapOnBackgroundThread(); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
355 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | 372 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
356 | 373 |
357 for (std::set<std::string>::const_iterator iter = active_profiles->begin(); | 374 for (std::set<std::string>::const_iterator iter = active_profiles->begin(); |
358 iter != active_profiles->end(); ++iter) { | 375 iter != active_profiles->end(); ++iter) { |
359 database_->AddStateValue(kStateProfilePrefix + *iter, time); | 376 database_->AddStateValue(kStateProfilePrefix + *iter, time); |
360 } | 377 } |
361 } | 378 } |
362 | 379 |
363 void PerformanceMonitor::DoTimedCollections() { | 380 void PerformanceMonitor::DoTimedCollections() { |
364 UpdateLiveProfiles(); | 381 UpdateLiveProfiles(); |
382 | |
383 BrowserThread::PostBlockingPoolSequencedTask( | |
384 Database::kDatabaseSequenceToken, | |
385 FROM_HERE, | |
386 base::Bind(&PerformanceMonitor::GatherStatisticsOnBackgroundThread, | |
387 base::Unretained(this))); | |
388 | |
389 BrowserThread::PostTask( | |
390 BrowserThread::IO, | |
391 FROM_HERE, | |
392 base::Bind(&PerformanceMonitor::CallInsertIOData, | |
393 base::Unretained(this))); | |
394 } | |
395 | |
396 void PerformanceMonitor::CallInsertIOData() { | |
397 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
398 | |
399 BrowserThread::PostBlockingPoolSequencedTask( | |
400 Database::kDatabaseSequenceToken, | |
401 FROM_HERE, | |
402 base::Bind(&PerformanceMonitor::InsertIOData, | |
403 base::Unretained(this), | |
404 performance_data_for_io_thread_)); | |
405 } | |
406 | |
407 void PerformanceMonitor::InsertIOData( | |
408 const PerformanceDataForIOThread& performance_data_for_io_thread) { | |
battre
2012/08/22 08:25:24
you need to pass a copy instead of a const ref her
Devlin
2012/08/22 16:22:05
Done.
| |
409 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
410 database_->AddMetric( | |
411 METRIC_NETWORK_BYTES_READ, | |
412 base::Uint64ToString(performance_data_for_io_thread.network_bytes_read)); | |
413 } | |
414 | |
415 void PerformanceMonitor::BytesReadOnIOThread(const net::URLRequest& request, | |
416 const int bytes_read) { | |
417 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
418 | |
419 if (!request.url().SchemeIsFile()) | |
420 performance_data_for_io_thread_.network_bytes_read += bytes_read; | |
365 } | 421 } |
366 | 422 |
367 void PerformanceMonitor::Observe(int type, | 423 void PerformanceMonitor::Observe(int type, |
368 const content::NotificationSource& source, | 424 const content::NotificationSource& source, |
369 const content::NotificationDetails& details) { | 425 const content::NotificationDetails& details) { |
370 switch (type) { | 426 switch (type) { |
371 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { | 427 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { |
372 AddExtensionEvent(EVENT_EXTENSION_INSTALL, | 428 AddExtensionEvent(EVENT_EXTENSION_INSTALL, |
373 content::Details<Extension>(details).ptr()); | 429 content::Details<Extension>(details).ptr()); |
374 break; | 430 break; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
478 | 534 |
479 // Determine the type of crash. | 535 // Determine the type of crash. |
480 EventType type = | 536 EventType type = |
481 details.status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED ? | 537 details.status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED ? |
482 EVENT_KILLED_BY_OS_CRASH : EVENT_RENDERER_CRASH; | 538 EVENT_KILLED_BY_OS_CRASH : EVENT_RENDERER_CRASH; |
483 | 539 |
484 AddEvent(util::CreateCrashEvent(base::Time::Now(), type)); | 540 AddEvent(util::CreateCrashEvent(base::Time::Now(), type)); |
485 } | 541 } |
486 | 542 |
487 } // namespace performance_monitor | 543 } // namespace performance_monitor |
OLD | NEW |