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"
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;
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())
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() {
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(
75 base::TimeDelta::FromSeconds(kMinBucketSizeInSeconds
).InSeconds(),
76 base::TimeDelta::FromSeconds(kMaxBucketSizeInSeconds
).InSeconds(),
77 kBucketCount
, base::HistogramBase::kUmaTargetedHistogramFlag
);
81 histogram_
->CheckName(histogram_name_
);
82 #endif // DCHECK_IS_ON()