blob: c8e617612ad4fcf6c316e29b1747aaddb1b66ba8 [file] [log] [blame]
Bruno Rochabe388f32011-08-02 12:40:17 -07001// Copyright (c) 2011 The Chromium OS 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 "timer.h"
6
7#include <string>
8
9#include <base/memory/scoped_ptr.h>
10#include <base/time.h>
11
12#include "metrics_library.h"
13
14namespace chromeos_metrics {
15
16base::TimeTicks ClockWrapper::GetCurrentTime() const {
17 return base::TimeTicks::Now();
18}
19
20Timer::Timer()
repo sync06726552013-05-28 14:19:53 -070021 : timer_state_(kTimerStopped),
Bruno Rochabe388f32011-08-02 12:40:17 -070022 clock_wrapper_(new ClockWrapper()) {}
23
24bool Timer::Start() {
repo sync06726552013-05-28 14:19:53 -070025 elapsed_time_ = base::TimeDelta(); // Sets elapsed_time_ to zero.
Bruno Rochabe388f32011-08-02 12:40:17 -070026 start_time_ = clock_wrapper_->GetCurrentTime();
repo sync06726552013-05-28 14:19:53 -070027 timer_state_ = kTimerRunning;
Bruno Rochabe388f32011-08-02 12:40:17 -070028 return true;
29}
30
31bool Timer::Stop() {
repo sync06726552013-05-28 14:19:53 -070032 if (timer_state_ == kTimerStopped)
33 return false;
34 if (timer_state_ == kTimerRunning)
35 elapsed_time_ += clock_wrapper_->GetCurrentTime() - start_time_;
36 timer_state_ = kTimerStopped;
Bruno Rochabe388f32011-08-02 12:40:17 -070037 return true;
38}
39
repo sync06726552013-05-28 14:19:53 -070040bool Timer::Pause() {
41 switch (timer_state_) {
42 case kTimerStopped:
43 if (!Start())
44 return false;
45 timer_state_ = kTimerPaused;
46 return true;
47 case kTimerRunning:
48 timer_state_ = kTimerPaused;
49 elapsed_time_ += clock_wrapper_->GetCurrentTime() - start_time_;
50 return true;
51 default:
52 return false;
53 }
54}
55
56bool Timer::Resume() {
57 switch (timer_state_) {
58 case kTimerStopped:
59 return Start();
60 case kTimerPaused:
61 start_time_ = clock_wrapper_->GetCurrentTime();
62 timer_state_ = kTimerRunning;
63 return true;
64 default:
65 return false;
66 }
67}
68
Bruno Rochabe388f32011-08-02 12:40:17 -070069bool Timer::Reset() {
repo sync06726552013-05-28 14:19:53 -070070 elapsed_time_ = base::TimeDelta(); // Sets elapsed_time_ to zero.
71 timer_state_ = kTimerStopped;
Bruno Rochabe388f32011-08-02 12:40:17 -070072 return true;
73}
74
75bool Timer::HasStarted() const {
repo sync06726552013-05-28 14:19:53 -070076 return timer_state_ != kTimerStopped;
Bruno Rochabe388f32011-08-02 12:40:17 -070077}
78
79bool Timer::GetElapsedTime(base::TimeDelta* elapsed_time) const {
repo sync06726552013-05-28 14:19:53 -070080 if (start_time_.is_null() || !elapsed_time)
81 return false;
82 *elapsed_time = elapsed_time_;
83 if (timer_state_ == kTimerRunning) {
84 *elapsed_time += clock_wrapper_->GetCurrentTime() - start_time_;
Bruno Rochabe388f32011-08-02 12:40:17 -070085 }
86 return true;
87}
88
89// static
90MetricsLibraryInterface* TimerReporter::metrics_lib_ = NULL;
91
92TimerReporter::TimerReporter(const std::string& histogram_name, int min,
93 int max, int num_buckets)
94 : histogram_name_(histogram_name),
95 min_(min),
96 max_(max),
97 num_buckets_(num_buckets) {}
98
99bool TimerReporter::ReportMilliseconds() const {
100 base::TimeDelta elapsed_time;
101 if (!metrics_lib_ || !GetElapsedTime(&elapsed_time)) return false;
102 return metrics_lib_->SendToUMA(histogram_name_,
103 elapsed_time.InMilliseconds(),
104 min_,
105 max_,
106 num_buckets_);
107}
108
109} // namespace chromeos_metrics