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 DCHECK(elapsed_startup_time_ == base::TimeDelta()); |
| 50 |
| 51 startup_type_ = startup_type; |
| 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 void StartupTimer::PauseTimer() { |
| 64 // Check that the timer is not already paused. |
| 65 DCHECK(g_startup_timer_->pause_started_ == base::TimeTicks()); |
| 66 |
| 67 g_startup_timer_->pause_started_ = base::TimeTicks::Now(); |
| 68 } |
| 69 |
| 70 // static |
| 71 void StartupTimer::UnpauseTimer() { |
| 72 // Check that the timer has been paused. |
| 73 DCHECK(g_startup_timer_->pause_started_ != base::TimeTicks()); |
| 74 |
| 75 g_startup_timer_->total_pause_ += base::TimeTicks::Now() - |
| 76 g_startup_timer_->pause_started_; |
| 77 |
| 78 g_startup_timer_->pause_started_ = base::TimeTicks(); |
| 79 } |
| 80 |
| 81 void StartupTimer::Observe(int type, |
| 82 const content::NotificationSource& source, |
| 83 const content::NotificationDetails& details) { |
| 84 CHECK(type == chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED); |
| 85 performance_monitor_initialized_ = true; |
| 86 if (elapsed_startup_time_ != base::TimeDelta()) |
| 87 InsertElapsedStartupTime(); |
| 88 if (elapsed_session_restore_times_.size()) |
| 89 InsertElapsedSessionRestoreTime(); |
| 90 } |
| 91 |
| 92 // static |
| 93 void StartupTimer::SetElapsedSessionRestoreTime( |
| 94 const base::TimeDelta& elapsed_session_restore_time) { |
| 95 g_startup_timer_->elapsed_session_restore_times_.push_back( |
| 96 elapsed_session_restore_time); |
| 97 |
| 98 if (g_startup_timer_->performance_monitor_initialized_) |
| 99 g_startup_timer_->InsertElapsedSessionRestoreTime(); |
| 100 } |
| 101 |
| 102 void StartupTimer::InsertElapsedStartupTime() { |
| 103 content::BrowserThread::PostBlockingPoolSequencedTask( |
| 104 Database::kDatabaseSequenceToken, |
| 105 FROM_HERE, |
| 106 base::Bind( |
| 107 &AddMetricToDatabaseOnBackgroundThread, |
| 108 base::Unretained(PerformanceMonitor::GetInstance()->database()), |
| 109 startup_type_ == STARTUP_NORMAL ? METRIC_STARTUP_TIME |
| 110 : METRIC_TEST_STARTUP_TIME, |
| 111 base::Int64ToString(elapsed_startup_time_.ToInternalValue()))); |
| 112 } |
| 113 |
| 114 void StartupTimer::InsertElapsedSessionRestoreTime() { |
| 115 for (std::vector<base::TimeDelta>::const_iterator iter = |
| 116 elapsed_session_restore_times_.begin(); |
| 117 iter != elapsed_session_restore_times_.end(); ++iter) { |
| 118 content::BrowserThread::PostBlockingPoolSequencedTask( |
| 119 Database::kDatabaseSequenceToken, |
| 120 FROM_HERE, |
| 121 base::Bind( |
| 122 &AddMetricToDatabaseOnBackgroundThread, |
| 123 base::Unretained(PerformanceMonitor::GetInstance()->database()), |
| 124 METRIC_SESSION_RESTORE_TIME, |
| 125 base::Int64ToString(iter->ToInternalValue()))); |
| 126 } |
| 127 } |
| 128 |
| 129 } // namespace performance_monitor |
OLD | NEW |