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::ByteCount::ByteCount() : disk(0), network(0) { } | |
68 | |
64 PerformanceMonitor::PerformanceMonitor() : database_(NULL) { | 69 PerformanceMonitor::PerformanceMonitor() : database_(NULL) { |
65 } | 70 } |
66 | 71 |
67 PerformanceMonitor::~PerformanceMonitor() { | 72 PerformanceMonitor::~PerformanceMonitor() { |
68 } | 73 } |
69 | 74 |
70 bool PerformanceMonitor::SetDatabasePath(const FilePath& path) { | 75 bool PerformanceMonitor::SetDatabasePath(const FilePath& path) { |
71 if (!database_.get()) { | 76 if (!database_.get()) { |
72 database_path_ = path; | 77 database_path_ = path; |
73 return true; | 78 return true; |
(...skipping 13 matching lines...) Expand all Loading... | |
87 FROM_HERE, | 92 FROM_HERE, |
88 base::Bind(&PerformanceMonitor::InitOnBackgroundThread, | 93 base::Bind(&PerformanceMonitor::InitOnBackgroundThread, |
89 base::Unretained(this)), | 94 base::Unretained(this)), |
90 base::Bind(&PerformanceMonitor::FinishInit, | 95 base::Bind(&PerformanceMonitor::FinishInit, |
91 base::Unretained(this))); | 96 base::Unretained(this))); |
92 } | 97 } |
93 | 98 |
94 void PerformanceMonitor::InitOnBackgroundThread() { | 99 void PerformanceMonitor::InitOnBackgroundThread() { |
95 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | 100 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
96 database_ = Database::Create(database_path_); | 101 database_ = Database::Create(database_path_); |
102 | |
103 // Initialize each member of bytes_read_ to the value in the database; if | |
104 // there isn't a recording in the database, the value stays at 0. | |
105 MetricInfo info; | |
106 if (database_->GetRecentStatsForActivityAndMetric(METRIC_DISK_BYTES_READ, | |
107 &info)) { | |
108 bytes_read_.disk = info.value; | |
109 } | |
110 if (database_->GetRecentStatsForActivityAndMetric(METRIC_NETWORK_BYTES_READ, | |
111 &info)) { | |
112 bytes_read_.network = info.value; | |
113 } | |
97 } | 114 } |
98 | 115 |
99 void PerformanceMonitor::FinishInit() { | 116 void PerformanceMonitor::FinishInit() { |
100 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 117 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
101 RegisterForNotifications(); | 118 RegisterForNotifications(); |
102 CheckForUncleanExits(); | 119 CheckForUncleanExits(); |
103 BrowserThread::PostBlockingPoolSequencedTask( | 120 BrowserThread::PostBlockingPoolSequencedTask( |
104 Database::kDatabaseSequenceToken, | 121 Database::kDatabaseSequenceToken, |
105 FROM_HERE, | 122 FROM_HERE, |
106 base::Bind(&PerformanceMonitor::CheckForVersionUpdateOnBackgroundThread, | 123 base::Bind(&PerformanceMonitor::CheckForVersionUpdateOnBackgroundThread, |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
237 const std::string& value) { | 254 const std::string& value) { |
238 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | 255 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
239 database_->AddMetric(type, value); | 256 database_->AddMetric(type, value); |
240 } | 257 } |
241 | 258 |
242 void PerformanceMonitor::NotifyInitialized() { | 259 void PerformanceMonitor::NotifyInitialized() { |
243 content::NotificationService::current()->Notify( | 260 content::NotificationService::current()->Notify( |
244 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, | 261 chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, |
245 content::Source<PerformanceMonitor>(this), | 262 content::Source<PerformanceMonitor>(this), |
246 content::NotificationService::NoDetails()); | 263 content::NotificationService::NoDetails()); |
264 | |
265 initialized_ = true; | |
247 } | 266 } |
248 | 267 |
249 void PerformanceMonitor::GatherStatisticsOnBackgroundThread() { | 268 void PerformanceMonitor::GatherStatisticsOnBackgroundThread() { |
250 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | 269 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
251 | 270 |
271 database_->AddMetric(METRIC_DISK_BYTES_READ, | |
272 base::Int64ToString(bytes_read_.disk)); | |
battre
2012/08/21 20:37:33
If you access bytes_read_ here, you must be on the
Devlin
2012/08/21 22:51:52
Done.
| |
273 database_->AddMetric(METRIC_NETWORK_BYTES_READ, | |
274 base::Int64ToString(bytes_read_.network)); | |
275 | |
252 // Because the CPU usage is gathered as an average since the last time the | 276 // 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 | 277 // 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. | 278 // usage, the CPU usage is gathered before the metrics map is wiped. |
255 GatherCPUUsageOnBackgroundThread(); | 279 GatherCPUUsageOnBackgroundThread(); |
256 UpdateMetricsMapOnBackgroundThread(); | 280 UpdateMetricsMapOnBackgroundThread(); |
257 GatherMemoryUsageOnBackgroundThread(); | 281 GatherMemoryUsageOnBackgroundThread(); |
258 } | 282 } |
259 | 283 |
260 void PerformanceMonitor::GatherCPUUsageOnBackgroundThread() { | 284 void PerformanceMonitor::GatherCPUUsageOnBackgroundThread() { |
261 if (metrics_map_.size()) { | 285 if (metrics_map_.size()) { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
355 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | 379 CHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
356 | 380 |
357 for (std::set<std::string>::const_iterator iter = active_profiles->begin(); | 381 for (std::set<std::string>::const_iterator iter = active_profiles->begin(); |
358 iter != active_profiles->end(); ++iter) { | 382 iter != active_profiles->end(); ++iter) { |
359 database_->AddStateValue(kStateProfilePrefix + *iter, time); | 383 database_->AddStateValue(kStateProfilePrefix + *iter, time); |
360 } | 384 } |
361 } | 385 } |
362 | 386 |
363 void PerformanceMonitor::DoTimedCollections() { | 387 void PerformanceMonitor::DoTimedCollections() { |
364 UpdateLiveProfiles(); | 388 UpdateLiveProfiles(); |
389 | |
390 BrowserThread::PostBlockingPoolSequencedTask( | |
391 Database::kDatabaseSequenceToken, | |
392 FROM_HERE, | |
393 base::Bind(&PerformanceMonitor::GatherStatisticsOnBackgroundThread, | |
394 base::Unretained(this))); | |
395 } | |
396 | |
397 void PerformanceMonitor::BytesRead(const net::URLRequest& request, | |
398 const int bytes_read) { | |
399 // Strictly speaking, we don't care that this is done on the I/O thread - we | |
400 // only care that it is always done on the *same* thread. This should be the | |
401 // only place which modifies the value of bytes_read_; thus we avoid race | |
402 // conditions (inserting into the database is not a race condition, because if | |
403 // it is not inserted in one iteration, it will be in the next, and it was | |
404 // close enough to justify being in either one). | |
405 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
406 | |
407 if (request.url().SchemeIsFile()) | |
408 bytes_read_.disk += bytes_read; | |
409 else | |
410 bytes_read_.network += bytes_read; | |
365 } | 411 } |
366 | 412 |
367 void PerformanceMonitor::Observe(int type, | 413 void PerformanceMonitor::Observe(int type, |
368 const content::NotificationSource& source, | 414 const content::NotificationSource& source, |
369 const content::NotificationDetails& details) { | 415 const content::NotificationDetails& details) { |
370 switch (type) { | 416 switch (type) { |
371 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { | 417 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { |
372 AddExtensionEvent(EVENT_EXTENSION_INSTALL, | 418 AddExtensionEvent(EVENT_EXTENSION_INSTALL, |
373 content::Details<Extension>(details).ptr()); | 419 content::Details<Extension>(details).ptr()); |
374 break; | 420 break; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
478 | 524 |
479 // Determine the type of crash. | 525 // Determine the type of crash. |
480 EventType type = | 526 EventType type = |
481 details.status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED ? | 527 details.status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED ? |
482 EVENT_KILLED_BY_OS_CRASH : EVENT_RENDERER_CRASH; | 528 EVENT_KILLED_BY_OS_CRASH : EVENT_RENDERER_CRASH; |
483 | 529 |
484 AddEvent(util::CreateCrashEvent(base::Time::Now(), type)); | 530 AddEvent(util::CreateCrashEvent(base::Time::Now(), type)); |
485 } | 531 } |
486 | 532 |
487 } // namespace performance_monitor | 533 } // namespace performance_monitor |
OLD | NEW |