Chromium Code Reviews| Index: chrome/browser/performance_monitor/startup_timer.cc |
| diff --git a/chrome/browser/performance_monitor/startup_timer.cc b/chrome/browser/performance_monitor/startup_timer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d1cd839e48f93db71739c08b7a0b66b50be90da3 |
| --- /dev/null |
| +++ b/chrome/browser/performance_monitor/startup_timer.cc |
| @@ -0,0 +1,131 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/performance_monitor/startup_timer.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "base/string_number_conversions.h" |
| +#include "chrome/browser/performance_monitor/database.h" |
| +#include "chrome/browser/performance_monitor/performance_monitor.h" |
| +#include "chrome/common/chrome_notification_types.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/notification_details.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/notification_source.h" |
| +#include "content/public/browser/notification_types.h" |
| + |
| +namespace performance_monitor { |
| + |
| +namespace { |
| +// Needed because Database::AddMetric is overloaded, so base::Bind doesn't work. |
| +void AddMetricToDatabaseOnBackgroundThread(Database* database, |
| + MetricType metric, |
| + std::string value) { |
| + database->AddMetric(metric, value); |
| +} |
| + |
| +} // namespace |
| + |
| +// static |
| +StartupTimer* StartupTimer::g_startup_timer_ = NULL; |
| + |
| +StartupTimer::StartupTimer() : startup_begin_(base::TimeTicks::Now()), |
| + normal_startup_(true), |
| + performance_monitor_initialized_(false) { |
| + CHECK(!g_startup_timer_); |
| + g_startup_timer_ = this; |
| + registrar_.Add(this, chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED, |
| + content::NotificationService::AllSources()); |
| +} |
| + |
| +StartupTimer::~StartupTimer() { |
| + DCHECK(this == g_startup_timer_); |
| + g_startup_timer_ = NULL; |
| +} |
| + |
| +bool StartupTimer::SignalStartupComplete(bool normal_startup) { |
| + normal_startup_ = normal_startup; |
| + if (!elapsed_startup_time_.is_null()) |
| + return false; |
| + |
| + elapsed_startup_time_ = |
| + base::TimeTicks::Now() - total_pause_ - startup_begin_; |
| + |
| + if (performance_monitor_initialized_) |
| + InsertStartupTime(); |
| + |
| + return true; |
| +} |
| + |
| +// static |
| +bool StartupTimer::PauseTimer() { |
| + return g_startup_timer_->PauseTimerImpl(); |
| +} |
| + |
| +bool StartupTimer::PauseTimerImpl() { |
| + if (!pause_started_.is_null()) |
| + return false; |
| + |
| + pause_started_ = base::TimeTicks::Now(); |
| + return true; |
| +} |
| + |
| +// static |
| +bool StartupTimer::UnpauseTimer() { |
| + return g_startup_timer_->UnpauseTimerImpl(); |
| +} |
| + |
| +bool StartupTimer::UnpauseTimerImpl() { |
| + if (pause_started_.is_null()) |
| + return false; |
| + |
| + 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.
|
| + pause_started_ = base::TimeTicks(); |
| + return true; |
| +} |
| + |
| +void StartupTimer::Observe(int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + CHECK(type == chrome::NOTIFICATION_PERFORMANCE_MONITOR_INITIALIZED); |
| + performance_monitor_initialized_ = true; |
| + if (!elapsed_startup_time_.is_null()) |
| + InsertStartupTime(); |
| + if (!session_restore_time_.is_null()) |
| + InsertSessionRestoreTime(); |
| +} |
| + |
| +// static |
| +void StartupTimer::SetSessionRestoreTime( |
| + const base::TimeDelta& session_restore_time) { |
| + g_startup_timer_->session_restore_time_ = session_restore_time; |
| + |
| + if (g_startup_timer_->performance_monitor_initialized_) |
| + g_startup_timer_->InsertSessionRestoreTime(); |
| +} |
| + |
| +void StartupTimer::InsertStartupTime() { |
| + content::BrowserThread::PostBlockingPoolSequencedTask( |
| + Database::kDatabaseSequenceToken, |
| + FROM_HERE, |
| + base::Bind( |
| + &AddMetricToDatabaseOnBackgroundThread, |
| + base::Unretained(PerformanceMonitor::GetInstance()->database()), |
| + normal_startup_ ? METRIC_STARTUP_TIME : METRIC_TEST_STARTUP_TIME, |
| + base::Int64ToString(elapsed_startup_time_.ToInternalValue()))); |
| +} |
| + |
| +void StartupTimer::InsertSessionRestoreTime() { |
| + content::BrowserThread::PostBlockingPoolSequencedTask( |
| + Database::kDatabaseSequenceToken, |
| + FROM_HERE, |
| + base::Bind( |
| + &AddMetricToDatabaseOnBackgroundThread, |
| + base::Unretained(PerformanceMonitor::GetInstance()->database()), |
| + METRIC_SESSION_RESTORE_TIME, |
| + base::Int64ToString(session_restore_time_.ToInternalValue()))); |
| +} |
| + |
| +} // namespace performance_monitor |