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 */ 16 17#include "metrics/timer.h" 18 19#include <string> 20 21#include "metrics/metrics_library.h" 22 23namespace chromeos_metrics { 24 25base::TimeTicks ClockWrapper::GetCurrentTime() const { 26 return base::TimeTicks::Now(); 27} 28 29Timer::Timer() 30 : timer_state_(kTimerStopped), 31 clock_wrapper_(new ClockWrapper()) {} 32 33bool Timer::Start() { 34 elapsed_time_ = base::TimeDelta(); // Sets elapsed_time_ to zero. 35 start_time_ = clock_wrapper_->GetCurrentTime(); 36 timer_state_ = kTimerRunning; 37 return true; 38} 39 40bool Timer::Stop() { 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; 46 return true; 47} 48 49bool 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 78bool Timer::Reset() { 79 elapsed_time_ = base::TimeDelta(); // Sets elapsed_time_ to zero. 80 timer_state_ = kTimerStopped; 81 return true; 82} 83 84bool Timer::HasStarted() const { 85 return timer_state_ != kTimerStopped; 86} 87 88bool Timer::GetElapsedTime(base::TimeDelta* elapsed_time) const { 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_; 94 } 95 return true; 96} 97 98// static 99MetricsLibraryInterface* TimerReporter::metrics_lib_ = nullptr; 100 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 119