Bertrand SIMONNET | 52e5b99 | 2015-08-10 15:18:00 -0700 | [diff] [blame] | 1 | /* |
| 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 Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 16 | |
Samuel Tan | 1c4d8f1 | 2015-09-15 11:40:47 -0700 | [diff] [blame] | 17 | #include "metrics/timer.h" |
Bruno Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 18 | |
| 19 | #include <string> |
| 20 | |
| 21 | #include <base/memory/scoped_ptr.h> |
Bruno Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 22 | |
Bertrand SIMONNET | e6cfd64 | 2014-07-09 16:35:23 -0700 | [diff] [blame] | 23 | #include "metrics/metrics_library.h" |
Bruno Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 24 | |
| 25 | namespace chromeos_metrics { |
| 26 | |
| 27 | base::TimeTicks ClockWrapper::GetCurrentTime() const { |
| 28 | return base::TimeTicks::Now(); |
| 29 | } |
| 30 | |
| 31 | Timer::Timer() |
repo sync | 0672655 | 2013-05-28 14:19:53 -0700 | [diff] [blame] | 32 | : timer_state_(kTimerStopped), |
Bruno Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 33 | clock_wrapper_(new ClockWrapper()) {} |
| 34 | |
| 35 | bool Timer::Start() { |
repo sync | 0672655 | 2013-05-28 14:19:53 -0700 | [diff] [blame] | 36 | elapsed_time_ = base::TimeDelta(); // Sets elapsed_time_ to zero. |
Bruno Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 37 | start_time_ = clock_wrapper_->GetCurrentTime(); |
repo sync | 0672655 | 2013-05-28 14:19:53 -0700 | [diff] [blame] | 38 | timer_state_ = kTimerRunning; |
Bruno Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 39 | return true; |
| 40 | } |
| 41 | |
| 42 | bool Timer::Stop() { |
repo sync | 0672655 | 2013-05-28 14:19:53 -0700 | [diff] [blame] | 43 | 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 Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 48 | return true; |
| 49 | } |
| 50 | |
repo sync | 0672655 | 2013-05-28 14:19:53 -0700 | [diff] [blame] | 51 | bool 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 | |
| 67 | bool 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 Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 80 | bool Timer::Reset() { |
repo sync | 0672655 | 2013-05-28 14:19:53 -0700 | [diff] [blame] | 81 | elapsed_time_ = base::TimeDelta(); // Sets elapsed_time_ to zero. |
| 82 | timer_state_ = kTimerStopped; |
Bruno Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 83 | return true; |
| 84 | } |
| 85 | |
| 86 | bool Timer::HasStarted() const { |
repo sync | 0672655 | 2013-05-28 14:19:53 -0700 | [diff] [blame] | 87 | return timer_state_ != kTimerStopped; |
Bruno Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | bool Timer::GetElapsedTime(base::TimeDelta* elapsed_time) const { |
repo sync | 0672655 | 2013-05-28 14:19:53 -0700 | [diff] [blame] | 91 | 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 Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | // static |
Alex Vakulenko | 1459503 | 2014-08-28 14:59:56 -0700 | [diff] [blame] | 101 | MetricsLibraryInterface* TimerReporter::metrics_lib_ = nullptr; |
Bruno Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 102 | |
| 103 | TimerReporter::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 | |
| 110 | bool 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 |