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 startup_type_(STARTUP_NORMAL), |
| 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(StartupType startup_type) { |
| 49 startup_type_ = startup_type; |
| 50 if (elapsed_startup_time_ != base::TimeDelta()) |
| 51 return false; |
| 52 |
| 53 elapsed_startup_time_ = |
| 54 base::TimeTicks::Now() - total_pause_ - startup_begin_; |
| 55 |
| 56 if (performance_monitor_initialized_) |
| 57 InsertElapsedStartupTime(); |
| 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_ != base::TimeTicks()) |
| 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_ == base::TimeTicks()) |
| 82 return false; |
| 83 |
| 84 total_pause_ += base::TimeTicks::Now() - pause_started_; |
| 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_ != base::TimeDelta()) |
| 95 InsertElapsedStartupTime(); |
| 96 if (elapsed_session_restore_time_ != base::TimeDelta()) |
| 97 InsertElapsedSessionRestoreTime(); |
| 98 } |
| 99 |
| 100 // static |
| 101 void StartupTimer::SetElapsedSessionRestoreTime( |
| 102 const base::TimeDelta& elapsed_session_restore_time) { |
| 103 g_startup_timer_->elapsed_session_restore_time_ = |
| 104 elapsed_session_restore_time; |
| 105 |
| 106 if (g_startup_timer_->performance_monitor_initialized_) |
| 107 g_startup_timer_->InsertElapsedSessionRestoreTime(); |
| 108 } |
| 109 |
| 110 void StartupTimer::InsertElapsedStartupTime() { |
| 111 content::BrowserThread::PostBlockingPoolSequencedTask( |
| 112 Database::kDatabaseSequenceToken, |
| 113 FROM_HERE, |
| 114 base::Bind( |
| 115 &AddMetricToDatabaseOnBackgroundThread, |
| 116 base::Unretained(PerformanceMonitor::GetInstance()->database()), |
| 117 startup_type_ == STARTUP_NORMAL ? METRIC_STARTUP_TIME |
| 118 : METRIC_TEST_STARTUP_TIME, |
| 119 base::Int64ToString(elapsed_startup_time_.ToInternalValue()))); |
| 120 } |
| 121 |
| 122 void StartupTimer::InsertElapsedSessionRestoreTime() { |
| 123 content::BrowserThread::PostBlockingPoolSequencedTask( |
| 124 Database::kDatabaseSequenceToken, |
| 125 FROM_HERE, |
| 126 base::Bind( |
| 127 &AddMetricToDatabaseOnBackgroundThread, |
| 128 base::Unretained(PerformanceMonitor::GetInstance()->database()), |
| 129 METRIC_SESSION_RESTORE_TIME, |
| 130 base::Int64ToString( |
| 131 elapsed_session_restore_time_.ToInternalValue()))); |
| 132 } |
| 133 |
| 134 } // namespace performance_monitor |
OLD | NEW |