Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/performance_monitor/startup_timer.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/string_number_conversions.h" | |
| 10 #include "chrome/browser/performance_monitor/database.h" | |
| 11 #include "chrome/browser/performance_monitor/performance_monitor.h" | |
| 12 #include "chrome/common/chrome_notification_types.h" | |
| 13 #include "content/public/browser/browser_thread.h" | |
| 14 #include "content/public/browser/notification_details.h" | |
| 15 #include "content/public/browser/notification_service.h" | |
| 16 #include "content/public/browser/notification_source.h" | |
| 17 #include "content/public/browser/notification_types.h" | |
| 18 | |
| 19 namespace performance_monitor { | |
| 20 | |
| 21 namespace { | |
| 22 // Needed because Database::AddMetric is overloaded, so base::Bind doesn't work. | |
| 23 void AddMetricToDatabaseOnBackgroundThread(Database* database, | |
| 24 MetricType metric, | |
| 25 std::string value) { | |
| 26 database->AddMetric(metric, value); | |
| 27 } | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 // static | |
| 32 StartupTimer* StartupTimer::g_startup_timer_ = NULL; | |
| 33 | |
| 34 StartupTimer::StartupTimer() : startup_begin_(base::TimeTicks::Now()), | |
| 35 normal_startup_(true), | |
| 36 performance_monitor_initialized_(false) { | |
| 37 CHECK(!g_startup_timer_); | |
| 38 g_startup_timer_ = this; | |
| 39 registrar_.Add(this, chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, | |
| 40 content::NotificationService::AllSources()); | |
| 41 } | |
| 42 | |
| 43 StartupTimer::~StartupTimer() { | |
| 44 DCHECK(this == g_startup_timer_); | |
| 45 g_startup_timer_ = NULL; | |
| 46 } | |
| 47 | |
| 48 bool StartupTimer::SignalStartupComplete(bool normal_startup) { | |
| 49 normal_startup_ = normal_startup; | |
| 50 if (!elapsed_startup_time_.is_null()) | |
| 51 return false; | |
| 52 | |
| 53 elapsed_startup_time_ = | |
| 54 base::TimeTicks::Now() - total_pause_ - startup_begin_; | |
| 55 | |
| 56 if (performance_monitor_initialized_) | |
| 57 InsertStartupTime(); | |
| 58 | |
| 59 return true; | |
| 60 } | |
| 61 | |
| 62 // static | |
| 63 bool StartupTimer::PauseTimer() { | |
| 64 return g_startup_timer_->PauseTimerImpl(); | |
| 65 } | |
| 66 | |
| 67 bool StartupTimer::PauseTimerImpl() { | |
| 68 if (!pause_started_.is_null()) | |
| 69 return false; | |
| 70 | |
| 71 pause_started_ = base::TimeTicks::Now(); | |
| 72 return true; | |
| 73 } | |
| 74 | |
| 75 // static | |
| 76 bool StartupTimer::UnpauseTimer() { | |
| 77 return g_startup_timer_->UnpauseTimerImpl(); | |
| 78 } | |
| 79 | |
| 80 bool StartupTimer::UnpauseTimerImpl() { | |
| 81 if (pause_started_.is_null()) | |
| 82 return false; | |
| 83 | |
| 84 total_pause_ = base::TimeTicks::Now() - pause_started_; | |
|
Yoyo Zhou
2012/07/31 09:57:32
If this = was += you could probably support more t
Devlin
2012/07/31 16:43:40
D'oh! Meant to have that... Done.
| |
| 85 pause_started_ = base::TimeTicks(); | |
| 86 return true; | |
| 87 } | |
| 88 | |
| 89 void StartupTimer::Observe(int type, | |
| 90 const content::NotificationSource& source, | |
| 91 const content::NotificationDetails& details) { | |
| 92 CHECK(type == chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED); | |
| 93 performance_monitor_initialized_ = true; | |
| 94 if (!elapsed_startup_time_.is_null()) | |
| 95 InsertStartupTime(); | |
| 96 if (!session_restore_time_.is_null()) | |
| 97 InsertSessionRestoreTime(); | |
| 98 } | |
| 99 | |
| 100 // static | |
| 101 void StartupTimer::SetSessionRestoreTime( | |
| 102 const base::TimeDelta& session_restore_time) { | |
| 103 g_startup_timer_->session_restore_time_ = session_restore_time; | |
| 104 | |
| 105 if (g_startup_timer_->performance_monitor_initialized_) | |
| 106 g_startup_timer_->InsertSessionRestoreTime(); | |
| 107 } | |
| 108 | |
| 109 void StartupTimer::InsertStartupTime() { | |
| 110 content::BrowserThread::PostBlockingPoolSequencedTask( | |
| 111 Database::kDatabaseSequenceToken, | |
| 112 FROM_HERE, | |
| 113 base::Bind( | |
| 114 &AddMetricToDatabaseOnBackgroundThread, | |
| 115 base::Unretained(PerformanceMonitor::GetInstance()->database()), | |
| 116 normal_startup_ ? METRIC_STARTUP_TIME : METRIC_TEST_STARTUP_TIME, | |
| 117 base::Int64ToString(elapsed_startup_time_.ToInternalValue()))); | |
| 118 } | |
| 119 | |
| 120 void StartupTimer::InsertSessionRestoreTime() { | |
| 121 content::BrowserThread::PostBlockingPoolSequencedTask( | |
| 122 Database::kDatabaseSequenceToken, | |
| 123 FROM_HERE, | |
| 124 base::Bind( | |
| 125 &AddMetricToDatabaseOnBackgroundThread, | |
| 126 base::Unretained(PerformanceMonitor::GetInstance()->database()), | |
| 127 METRIC_SESSION_RESTORE_TIME, | |
| 128 base::Int64ToString(session_restore_time_.ToInternalValue()))); | |
| 129 } | |
| 130 | |
| 131 } // namespace performance_monitor | |
| OLD | NEW |