2015-08-11 06:18:00 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2011-08-03 03:40:17 +08:00
|
|
|
|
2015-09-16 02:40:47 +08:00
|
|
|
#include "metrics/timer.h"
|
2011-08-03 03:40:17 +08:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <base/memory/scoped_ptr.h>
|
|
|
|
|
2014-07-10 07:35:23 +08:00
|
|
|
#include "metrics/metrics_library.h"
|
2011-08-03 03:40:17 +08:00
|
|
|
|
|
|
|
namespace chromeos_metrics {
|
|
|
|
|
|
|
|
base::TimeTicks ClockWrapper::GetCurrentTime() const {
|
|
|
|
return base::TimeTicks::Now();
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer::Timer()
|
2013-05-29 05:19:53 +08:00
|
|
|
: timer_state_(kTimerStopped),
|
2011-08-03 03:40:17 +08:00
|
|
|
clock_wrapper_(new ClockWrapper()) {}
|
|
|
|
|
|
|
|
bool Timer::Start() {
|
2013-05-29 05:19:53 +08:00
|
|
|
elapsed_time_ = base::TimeDelta(); // Sets elapsed_time_ to zero.
|
2011-08-03 03:40:17 +08:00
|
|
|
start_time_ = clock_wrapper_->GetCurrentTime();
|
2013-05-29 05:19:53 +08:00
|
|
|
timer_state_ = kTimerRunning;
|
2011-08-03 03:40:17 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Timer::Stop() {
|
2013-05-29 05:19:53 +08:00
|
|
|
if (timer_state_ == kTimerStopped)
|
|
|
|
return false;
|
|
|
|
if (timer_state_ == kTimerRunning)
|
|
|
|
elapsed_time_ += clock_wrapper_->GetCurrentTime() - start_time_;
|
|
|
|
timer_state_ = kTimerStopped;
|
2011-08-03 03:40:17 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-29 05:19:53 +08:00
|
|
|
bool Timer::Pause() {
|
|
|
|
switch (timer_state_) {
|
|
|
|
case kTimerStopped:
|
|
|
|
if (!Start())
|
|
|
|
return false;
|
|
|
|
timer_state_ = kTimerPaused;
|
|
|
|
return true;
|
|
|
|
case kTimerRunning:
|
|
|
|
timer_state_ = kTimerPaused;
|
|
|
|
elapsed_time_ += clock_wrapper_->GetCurrentTime() - start_time_;
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Timer::Resume() {
|
|
|
|
switch (timer_state_) {
|
|
|
|
case kTimerStopped:
|
|
|
|
return Start();
|
|
|
|
case kTimerPaused:
|
|
|
|
start_time_ = clock_wrapper_->GetCurrentTime();
|
|
|
|
timer_state_ = kTimerRunning;
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-03 03:40:17 +08:00
|
|
|
bool Timer::Reset() {
|
2013-05-29 05:19:53 +08:00
|
|
|
elapsed_time_ = base::TimeDelta(); // Sets elapsed_time_ to zero.
|
|
|
|
timer_state_ = kTimerStopped;
|
2011-08-03 03:40:17 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Timer::HasStarted() const {
|
2013-05-29 05:19:53 +08:00
|
|
|
return timer_state_ != kTimerStopped;
|
2011-08-03 03:40:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Timer::GetElapsedTime(base::TimeDelta* elapsed_time) const {
|
2013-05-29 05:19:53 +08:00
|
|
|
if (start_time_.is_null() || !elapsed_time)
|
|
|
|
return false;
|
|
|
|
*elapsed_time = elapsed_time_;
|
|
|
|
if (timer_state_ == kTimerRunning) {
|
|
|
|
*elapsed_time += clock_wrapper_->GetCurrentTime() - start_time_;
|
2011-08-03 03:40:17 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2014-08-29 05:59:56 +08:00
|
|
|
MetricsLibraryInterface* TimerReporter::metrics_lib_ = nullptr;
|
2011-08-03 03:40:17 +08:00
|
|
|
|
|
|
|
TimerReporter::TimerReporter(const std::string& histogram_name, int min,
|
|
|
|
int max, int num_buckets)
|
|
|
|
: histogram_name_(histogram_name),
|
|
|
|
min_(min),
|
|
|
|
max_(max),
|
|
|
|
num_buckets_(num_buckets) {}
|
|
|
|
|
|
|
|
bool TimerReporter::ReportMilliseconds() const {
|
|
|
|
base::TimeDelta elapsed_time;
|
|
|
|
if (!metrics_lib_ || !GetElapsedTime(&elapsed_time)) return false;
|
|
|
|
return metrics_lib_->SendToUMA(histogram_name_,
|
|
|
|
elapsed_time.InMilliseconds(),
|
|
|
|
min_,
|
|
|
|
max_,
|
|
|
|
num_buckets_);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace chromeos_metrics
|