Chromium Code Reviews| 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. | |
| 15 const size_t kBucketCount = 50; | |
| 16 | |
| 17 // The underflow (aka minimum) bucket size for the histogram. | |
| 18 const base::TimeDelta kMinBucket = base::TimeDelta::FromSeconds(0); | |
| 19 | |
| 20 // The overflow (aka maximium) bucket size for the histogram. | |
| 21 const base::TimeDelta kMaxBucket = base::TimeDelta::FromHours(1); | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 TaskSwitchTimeTracker::TaskSwitchTimeTracker(const std::string& histogram_name) | |
| 26 : histogram_name_(histogram_name), | |
| 27 tick_clock_(new base::DefaultTickClock()) { | |
|
tdanderson
2015/05/13 19:27:18
Are you purposely not initializing |last_action_ti
bruthig
2015/05/13 21:16:21
I added an explicit in-class initialization.
| |
| 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 | |
|
tdanderson
2015/05/13 19:27:18
Maybe have a CHECK() that the delta >= 0 ?
bruthig
2015/05/13 21:16:21
Done.
| |
| 60 GetHistogram()->Add(time_delta.InSeconds()); | |
| 61 } | |
| 62 | |
| 63 base::HistogramBase* TaskSwitchTimeTracker::GetHistogram() { | |
| 64 if (!histogram_) { | |
| 65 histogram_ = base::Histogram::FactoryGet( | |
| 66 histogram_name_, kMinBucket.InSeconds(), kMaxBucket.InSeconds(), | |
| 67 kBucketCount, base::HistogramBase::kUmaTargetedHistogramFlag); | |
| 68 } | |
| 69 if (DCHECK_IS_ON()) | |
| 70 histogram_->CheckName(histogram_name_); | |
| 71 return histogram_; | |
| 72 } | |
| 73 | |
| 74 } // namespace ash | |
| OLD | NEW |