blob: 7b00cc074f048568c2c05475ded659fbaf4ca450 [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
Bertrand SIMONNET4b915ae2015-07-28 15:38:14 -070017#include "timer.h"
Bruno Rochabe388f32011-08-02 12:40:17 -070018
19#include <string>
20
21#include <base/memory/scoped_ptr.h>
Bruno Rochabe388f32011-08-02 12:40:17 -070022
Bertrand SIMONNETe6cfd642014-07-09 16:35:23 -070023#include "metrics/metrics_library.h"
Bruno Rochabe388f32011-08-02 12:40:17 -070024
25namespace chromeos_metrics {
26
27base::TimeTicks ClockWrapper::GetCurrentTime() const {
28 return base::TimeTicks::Now();
29}
30
31Timer::Timer()
repo sync06726552013-05-28 14:19:53 -070032 : timer_state_(kTimerStopped),
Bruno Rochabe388f32011-08-02 12:40:17 -070033 clock_wrapper_(new ClockWrapper()) {}
34
35bool Timer::Start() {
repo sync06726552013-05-28 14:19:53 -070036 elapsed_time_ = base::TimeDelta(); // Sets elapsed_time_ to zero.
Bruno Rochabe388f32011-08-02 12:40:17 -070037 start_time_ = clock_wrapper_->GetCurrentTime();
repo sync06726552013-05-28 14:19:53 -070038 timer_state_ = kTimerRunning;
Bruno Rochabe388f32011-08-02 12:40:17 -070039 return true;
40}
41
42bool Timer::Stop() {
repo sync06726552013-05-28 14:19:53 -070043 if (timer_state_ == kTimerStopped)
44 return false;
45 if (timer_state_ == kTimerRunning)
46 elapsed_time_ += clock_wrapper_->GetCurrentTime() - start_time_;
47 timer_state_ = kTimerStopped;
Bruno Rochabe388f32011-08-02 12:40:17 -070048 return true;
49}
50
repo sync06726552013-05-28 14:19:53 -070051bool Timer::Pause() {
52 switch (timer_state_) {
53 case kTimerStopped:
54 if (!Start())
55 return false;
56 timer_state_ = kTimerPaused;
57 return true;
58 case kTimerRunning:
59 timer_state_ = kTimerPaused;
60 elapsed_time_ += clock_wrapper_->GetCurrentTime() - start_time_;
61 return true;
62 default:
63 return false;
64 }
65}
66
67bool Timer::Resume() {
68 switch (timer_state_) {
69 case kTimerStopped:
70 return Start();
71 case kTimerPaused:
72 start_time_ = clock_wrapper_->GetCurrentTime();
73 timer_state_ = kTimerRunning;
74 return true;
75 default:
76 return false;
77 }
78}
79
Bruno Rochabe388f32011-08-02 12:40:17 -070080bool Timer::Reset() {
repo sync06726552013-05-28 14:19:53 -070081 elapsed_time_ = base::TimeDelta(); // Sets elapsed_time_ to zero.
82 timer_state_ = kTimerStopped;
Bruno Rochabe388f32011-08-02 12:40:17 -070083 return true;
84}
85
86bool Timer::HasStarted() const {
repo sync06726552013-05-28 14:19:53 -070087 return timer_state_ != kTimerStopped;
Bruno Rochabe388f32011-08-02 12:40:17 -070088}
89
90bool Timer::GetElapsedTime(base::TimeDelta* elapsed_time) const {
repo sync06726552013-05-28 14:19:53 -070091 if (start_time_.is_null() || !elapsed_time)
92 return false;
93 *elapsed_time = elapsed_time_;
94 if (timer_state_ == kTimerRunning) {
95 *elapsed_time += clock_wrapper_->GetCurrentTime() - start_time_;
Bruno Rochabe388f32011-08-02 12:40:17 -070096 }
97 return true;
98}
99
100// static
Alex Vakulenko14595032014-08-28 14:59:56 -0700101MetricsLibraryInterface* TimerReporter::metrics_lib_ = nullptr;
Bruno Rochabe388f32011-08-02 12:40:17 -0700102
103TimerReporter::TimerReporter(const std::string& histogram_name, int min,
104 int max, int num_buckets)
105 : histogram_name_(histogram_name),
106 min_(min),
107 max_(max),
108 num_buckets_(num_buckets) {}
109
110bool TimerReporter::ReportMilliseconds() const {
111 base::TimeDelta elapsed_time;
112 if (!metrics_lib_ || !GetElapsedTime(&elapsed_time)) return false;
113 return metrics_lib_->SendToUMA(histogram_name_,
114 elapsed_time.InMilliseconds(),
115 min_,
116 max_,
117 num_buckets_);
118}
119
120} // namespace chromeos_metrics