blob: 06fc33626d795a516d043f7e885621d40f1d987c [file] [log] [blame]
Bertrand SIMONNET52e5b992015-08-10 15:18:00 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Bruno Rochabe388f32011-08-02 12:40:17 -070016
Samuel Tan1c4d8f12015-09-15 11:40:47 -070017#include "metrics/timer.h"
Bruno Rochabe388f32011-08-02 12:40:17 -070018
19#include <string>
20
Bertrand SIMONNETe6cfd642014-07-09 16:35:23 -070021#include "metrics/metrics_library.h"
Bruno Rochabe388f32011-08-02 12:40:17 -070022
23namespace chromeos_metrics {
24
25base::TimeTicks ClockWrapper::GetCurrentTime() const {
26 return base::TimeTicks::Now();
27}
28
29Timer::Timer()
repo sync06726552013-05-28 14:19:53 -070030 : timer_state_(kTimerStopped),
Bruno Rochabe388f32011-08-02 12:40:17 -070031 clock_wrapper_(new ClockWrapper()) {}
32
33bool Timer::Start() {
repo sync06726552013-05-28 14:19:53 -070034 elapsed_time_ = base::TimeDelta(); // Sets elapsed_time_ to zero.
Bruno Rochabe388f32011-08-02 12:40:17 -070035 start_time_ = clock_wrapper_->GetCurrentTime();
repo sync06726552013-05-28 14:19:53 -070036 timer_state_ = kTimerRunning;
Bruno Rochabe388f32011-08-02 12:40:17 -070037 return true;
38}
39
40bool Timer::Stop() {
repo sync06726552013-05-28 14:19:53 -070041 if (timer_state_ == kTimerStopped)
42 return false;
43 if (timer_state_ == kTimerRunning)
44 elapsed_time_ += clock_wrapper_->GetCurrentTime() - start_time_;
45 timer_state_ = kTimerStopped;
Bruno Rochabe388f32011-08-02 12:40:17 -070046 return true;
47}
48
repo sync06726552013-05-28 14:19:53 -070049bool Timer::Pause() {
50 switch (timer_state_) {
51 case kTimerStopped:
52 if (!Start())
53 return false;
54 timer_state_ = kTimerPaused;
55 return true;
56 case kTimerRunning:
57 timer_state_ = kTimerPaused;
58 elapsed_time_ += clock_wrapper_->GetCurrentTime() - start_time_;
59 return true;
60 default:
61 return false;
62 }
63}
64
65bool Timer::Resume() {
66 switch (timer_state_) {
67 case kTimerStopped:
68 return Start();
69 case kTimerPaused:
70 start_time_ = clock_wrapper_->GetCurrentTime();
71 timer_state_ = kTimerRunning;
72 return true;
73 default:
74 return false;
75 }
76}
77
Bruno Rochabe388f32011-08-02 12:40:17 -070078bool Timer::Reset() {
repo sync06726552013-05-28 14:19:53 -070079 elapsed_time_ = base::TimeDelta(); // Sets elapsed_time_ to zero.
80 timer_state_ = kTimerStopped;
Bruno Rochabe388f32011-08-02 12:40:17 -070081 return true;
82}
83
84bool Timer::HasStarted() const {
repo sync06726552013-05-28 14:19:53 -070085 return timer_state_ != kTimerStopped;
Bruno Rochabe388f32011-08-02 12:40:17 -070086}
87
88bool Timer::GetElapsedTime(base::TimeDelta* elapsed_time) const {
repo sync06726552013-05-28 14:19:53 -070089 if (start_time_.is_null() || !elapsed_time)
90 return false;
91 *elapsed_time = elapsed_time_;
92 if (timer_state_ == kTimerRunning) {
93 *elapsed_time += clock_wrapper_->GetCurrentTime() - start_time_;
Bruno Rochabe388f32011-08-02 12:40:17 -070094 }
95 return true;
96}
97
98// static
Alex Vakulenko14595032014-08-28 14:59:56 -070099MetricsLibraryInterface* TimerReporter::metrics_lib_ = nullptr;
Bruno Rochabe388f32011-08-02 12:40:17 -0700100
101TimerReporter::TimerReporter(const std::string& histogram_name, int min,
102 int max, int num_buckets)
103 : histogram_name_(histogram_name),
104 min_(min),
105 max_(max),
106 num_buckets_(num_buckets) {}
107
108bool TimerReporter::ReportMilliseconds() const {
109 base::TimeDelta elapsed_time;
110 if (!metrics_lib_ || !GetElapsedTime(&elapsed_time)) return false;
111 return metrics_lib_->SendToUMA(histogram_name_,
112 elapsed_time.InMilliseconds(),
113 min_,
114 max_,
115 num_buckets_);
116}
117
118} // namespace chromeos_metrics