OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "ash/metrics/task_switch_time_tracker.h" | |
6 | |
7 #include "base/metrics/histogram.h" | |
8 #include "base/time/default_tick_clock.h" | |
9 | |
10 namespace ash { | |
11 | |
12 namespace { | |
13 | |
14 // The number of buckets in the histogram. | |
Alexei Svitkine (slow)
2015/05/14 14:51:54
Please add a comment somewhere that changing any o
bruthig
2015/05/14 15:35:20
Done.
| |
15 const size_t kBucketCount = 50; | |
16 | |
17 // The underflow (aka minimum) bucket size for the histogram. | |
18 const int kMinBucketSizeInSeconds = 0; | |
19 | |
20 // The overflow (aka maximium) bucket size for the histogram. | |
21 const int kMaxBucketSizeInSeconds = 60 * 60; | |
22 | |
23 } // namespace | |
24 | |
25 TaskSwitchTimeTracker::TaskSwitchTimeTracker(const std::string& histogram_name) | |
26 : histogram_name_(histogram_name), | |
27 tick_clock_(new base::DefaultTickClock()) { | |
28 } | |
29 | |
30 TaskSwitchTimeTracker::TaskSwitchTimeTracker( | |
31 const std::string& histogram_name, | |
32 scoped_ptr<base::TickClock> tick_clock) | |
33 : histogram_name_(histogram_name), tick_clock_(tick_clock.release()) { | |
34 } | |
35 | |
36 TaskSwitchTimeTracker::~TaskSwitchTimeTracker() { | |
37 } | |
38 | |
39 void TaskSwitchTimeTracker::OnTaskSwitch() { | |
40 if (!HasLastActionTime()) | |
41 SetLastActionTime(); | |
42 else | |
43 RecordTimeDelta(); | |
44 } | |
45 | |
46 bool TaskSwitchTimeTracker::HasLastActionTime() const { | |
47 return last_action_time_ != base::TimeTicks(); | |
48 } | |
49 | |
50 base::TimeTicks TaskSwitchTimeTracker::SetLastActionTime() { | |
51 base::TimeTicks previous_last_action_time = last_action_time_; | |
52 last_action_time_ = tick_clock_->NowTicks(); | |
53 return previous_last_action_time; | |
54 } | |
55 | |
56 void TaskSwitchTimeTracker::RecordTimeDelta() { | |
57 base::TimeTicks previous_last_action_time = SetLastActionTime(); | |
58 base::TimeDelta time_delta = last_action_time_ - previous_last_action_time; | |
59 | |
60 CHECK_GE(time_delta, base::TimeDelta()); | |
61 | |
62 GetHistogram()->Add(time_delta.InSeconds()); | |
63 } | |
64 | |
65 base::HistogramBase* TaskSwitchTimeTracker::GetHistogram() { | |
66 if (!histogram_) { | |
67 histogram_ = base::Histogram::FactoryGet( | |
68 histogram_name_, | |
69 base::TimeDelta::FromSeconds(kMinBucketSizeInSeconds).InSeconds(), | |
70 base::TimeDelta::FromSeconds(kMaxBucketSizeInSeconds).InSeconds(), | |
71 kBucketCount, base::HistogramBase::kUmaTargetedHistogramFlag); | |
72 } | |
73 | |
74 #if DCHECK_IS_ON() | |
75 histogram_->CheckName(histogram_name_); | |
76 #endif // DCHECK_IS_ON() | |
77 | |
78 return histogram_; | |
79 } | |
80 | |
81 } // namespace ash | |
OLD | NEW |