blob: ce4bf67a398f9d5fe8d935076f762d12ec5452e9 [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
Bertrand SIMONNET4b915ae2015-07-28 15:38:14 -07005#include "timer.h"
Bruno Rochabe388f32011-08-02 12:40:17 -07006
7#include <string>
8
9#include <base/memory/scoped_ptr.h>
Bruno Rochabe388f32011-08-02 12:40:17 -070010
Bertrand SIMONNETe6cfd642014-07-09 16:35:23 -070011#include "metrics/metrics_library.h"
Bruno Rochabe388f32011-08-02 12:40:17 -070012
13namespace chromeos_metrics {
14
15base::TimeTicks ClockWrapper::GetCurrentTime() const {
16 return base::TimeTicks::Now();
17}
18
19Timer::Timer()
repo sync06726552013-05-28 14:19:53 -070020 : timer_state_(kTimerStopped),
Bruno Rochabe388f32011-08-02 12:40:17 -070021 clock_wrapper_(new ClockWrapper()) {}
22
23bool Timer::Start() {
repo sync06726552013-05-28 14:19:53 -070024 elapsed_time_ = base::TimeDelta(); // Sets elapsed_time_ to zero.
Bruno Rochabe388f32011-08-02 12:40:17 -070025 start_time_ = clock_wrapper_->GetCurrentTime();
repo sync06726552013-05-28 14:19:53 -070026 timer_state_ = kTimerRunning;
Bruno Rochabe388f32011-08-02 12:40:17 -070027 return true;
28}
29
30bool Timer::Stop() {
repo sync06726552013-05-28 14:19:53 -070031 if (timer_state_ == kTimerStopped)
32 return false;
33 if (timer_state_ == kTimerRunning)
34 elapsed_time_ += clock_wrapper_->GetCurrentTime() - start_time_;
35 timer_state_ = kTimerStopped;
Bruno Rochabe388f32011-08-02 12:40:17 -070036 return true;
37}
38
repo sync06726552013-05-28 14:19:53 -070039bool Timer::Pause() {
40 switch (timer_state_) {
41 case kTimerStopped:
42 if (!Start())
43 return false;
44 timer_state_ = kTimerPaused;
45 return true;
46 case kTimerRunning:
47 timer_state_ = kTimerPaused;
48 elapsed_time_ += clock_wrapper_->GetCurrentTime() - start_time_;
49 return true;
50 default:
51 return false;
52 }
53}
54
55bool Timer::Resume() {
56 switch (timer_state_) {
57 case kTimerStopped:
58 return Start();
59 case kTimerPaused:
60 start_time_ = clock_wrapper_->GetCurrentTime();
61 timer_state_ = kTimerRunning;
62 return true;
63 default:
64 return false;
65 }
66}
67
Bruno Rochabe388f32011-08-02 12:40:17 -070068bool Timer::Reset() {
repo sync06726552013-05-28 14:19:53 -070069 elapsed_time_ = base::TimeDelta(); // Sets elapsed_time_ to zero.
70 timer_state_ = kTimerStopped;
Bruno Rochabe388f32011-08-02 12:40:17 -070071 return true;
72}
73
74bool Timer::HasStarted() const {
repo sync06726552013-05-28 14:19:53 -070075 return timer_state_ != kTimerStopped;
Bruno Rochabe388f32011-08-02 12:40:17 -070076}
77
78bool Timer::GetElapsedTime(base::TimeDelta* elapsed_time) const {
repo sync06726552013-05-28 14:19:53 -070079 if (start_time_.is_null() || !elapsed_time)
80 return false;
81 *elapsed_time = elapsed_time_;
82 if (timer_state_ == kTimerRunning) {
83 *elapsed_time += clock_wrapper_->GetCurrentTime() - start_time_;
Bruno Rochabe388f32011-08-02 12:40:17 -070084 }
85 return true;
86}
87
88// static
Alex Vakulenko14595032014-08-28 14:59:56 -070089MetricsLibraryInterface* TimerReporter::metrics_lib_ = nullptr;
Bruno Rochabe388f32011-08-02 12:40:17 -070090
91TimerReporter::TimerReporter(const std::string& histogram_name, int min,
92 int max, int num_buckets)
93 : histogram_name_(histogram_name),
94 min_(min),
95 max_(max),
96 num_buckets_(num_buckets) {}
97
98bool TimerReporter::ReportMilliseconds() const {
99 base::TimeDelta elapsed_time;
100 if (!metrics_lib_ || !GetElapsedTime(&elapsed_time)) return false;
101 return metrics_lib_->SendToUMA(histogram_name_,
102 elapsed_time.InMilliseconds(),
103 min_,
104 max_,
105 num_buckets_);
106}
107
108} // namespace chromeos_metrics