| 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 |  | 
| Bertrand SIMONNET | e6cfd64 | 2014-07-09 16:35:23 -0700 | [diff] [blame] | 21 | #include "metrics/metrics_library.h" | 
| Bruno Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | namespace chromeos_metrics { | 
|  | 24 |  | 
|  | 25 | base::TimeTicks ClockWrapper::GetCurrentTime() const { | 
|  | 26 | return base::TimeTicks::Now(); | 
|  | 27 | } | 
|  | 28 |  | 
|  | 29 | Timer::Timer() | 
| repo sync | 0672655 | 2013-05-28 14:19:53 -0700 | [diff] [blame] | 30 | : timer_state_(kTimerStopped), | 
| Bruno Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 31 | clock_wrapper_(new ClockWrapper()) {} | 
|  | 32 |  | 
|  | 33 | bool Timer::Start() { | 
| repo sync | 0672655 | 2013-05-28 14:19:53 -0700 | [diff] [blame] | 34 | elapsed_time_ = base::TimeDelta();  // Sets elapsed_time_ to zero. | 
| Bruno Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 35 | start_time_ = clock_wrapper_->GetCurrentTime(); | 
| repo sync | 0672655 | 2013-05-28 14:19:53 -0700 | [diff] [blame] | 36 | timer_state_ = kTimerRunning; | 
| Bruno Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 37 | return true; | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | bool Timer::Stop() { | 
| repo sync | 0672655 | 2013-05-28 14:19:53 -0700 | [diff] [blame] | 41 | 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 Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 46 | return true; | 
|  | 47 | } | 
|  | 48 |  | 
| repo sync | 0672655 | 2013-05-28 14:19:53 -0700 | [diff] [blame] | 49 | bool 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 |  | 
|  | 65 | bool 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 Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 78 | bool Timer::Reset() { | 
| repo sync | 0672655 | 2013-05-28 14:19:53 -0700 | [diff] [blame] | 79 | elapsed_time_ = base::TimeDelta();  // Sets elapsed_time_ to zero. | 
|  | 80 | timer_state_ = kTimerStopped; | 
| Bruno Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 81 | return true; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | bool Timer::HasStarted() const { | 
| repo sync | 0672655 | 2013-05-28 14:19:53 -0700 | [diff] [blame] | 85 | return timer_state_ != kTimerStopped; | 
| Bruno Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
|  | 88 | bool Timer::GetElapsedTime(base::TimeDelta* elapsed_time) const { | 
| repo sync | 0672655 | 2013-05-28 14:19:53 -0700 | [diff] [blame] | 89 | 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 Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 94 | } | 
|  | 95 | return true; | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | // static | 
| Alex Vakulenko | 1459503 | 2014-08-28 14:59:56 -0700 | [diff] [blame] | 99 | MetricsLibraryInterface* TimerReporter::metrics_lib_ = nullptr; | 
| Bruno Rocha | be388f3 | 2011-08-02 12:40:17 -0700 | [diff] [blame] | 100 |  | 
|  | 101 | TimerReporter::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 |  | 
|  | 108 | bool 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 |