Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / ash / metrics / task_switch_time_tracker.cc
blobbd28e466ddec32c5abaecb575ba395b2a677613a
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/metrics/task_switch_time_tracker.h"
7 #include "base/metrics/histogram.h"
8 #include "base/time/default_tick_clock.h"
10 namespace ash {
12 namespace {
14 // The number of buckets in the histogram.
15 // See IMPORTANT note below if you want to change this value!
16 const size_t kBucketCount = 50;
18 // The underflow (aka minimum) bucket size for the histogram.
19 // See IMPORTANT note below if you want to change this value!
20 const int kMinBucketSizeInSeconds = 0;
22 // The overflow (aka maximium) bucket size for the histogram.
23 // See IMPORTANT note below if you want to change this value!
24 const int kMaxBucketSizeInSeconds = 60 * 60;
26 } // namespace
28 TaskSwitchTimeTracker::TaskSwitchTimeTracker(const std::string& histogram_name)
29 : histogram_name_(histogram_name),
30 tick_clock_(new base::DefaultTickClock()) {
33 TaskSwitchTimeTracker::TaskSwitchTimeTracker(
34 const std::string& histogram_name,
35 scoped_ptr<base::TickClock> tick_clock)
36 : histogram_name_(histogram_name), tick_clock_(tick_clock.release()) {
39 TaskSwitchTimeTracker::~TaskSwitchTimeTracker() {
42 void TaskSwitchTimeTracker::OnTaskSwitch() {
43 if (!HasLastActionTime())
44 SetLastActionTime();
45 else
46 RecordTimeDelta();
49 bool TaskSwitchTimeTracker::HasLastActionTime() const {
50 return last_action_time_ != base::TimeTicks();
53 base::TimeTicks TaskSwitchTimeTracker::SetLastActionTime() {
54 base::TimeTicks previous_last_action_time = last_action_time_;
55 last_action_time_ = tick_clock_->NowTicks();
56 return previous_last_action_time;
59 void TaskSwitchTimeTracker::RecordTimeDelta() {
60 base::TimeTicks previous_last_action_time = SetLastActionTime();
61 base::TimeDelta time_delta = last_action_time_ - previous_last_action_time;
63 CHECK_GE(time_delta, base::TimeDelta());
65 GetHistogram()->Add(time_delta.InSeconds());
68 base::HistogramBase* TaskSwitchTimeTracker::GetHistogram() {
69 if (!histogram_) {
70 // IMPORTANT: If you change the type of histogram or any values that define
71 // its bucket construction then you must rename all of the histograms using
72 // the TaskSwitchTimeTracker mechanism.
73 histogram_ = base::Histogram::FactoryGet(
74 histogram_name_,
75 base::TimeDelta::FromSeconds(kMinBucketSizeInSeconds).InSeconds(),
76 base::TimeDelta::FromSeconds(kMaxBucketSizeInSeconds).InSeconds(),
77 kBucketCount, base::HistogramBase::kUmaTargetedHistogramFlag);
80 #if DCHECK_IS_ON()
81 histogram_->CheckName(histogram_name_);
82 #endif // DCHECK_IS_ON()
84 return histogram_;
87 } // namespace ash