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 // See IMPORTANT note below if you want to change this value! |
| 16 const size_t kBucketCount = 50; |
| 17 |
| 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; |
| 21 |
| 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; |
| 25 |
| 26 } // namespace |
| 27 |
| 28 TaskSwitchTimeTracker::TaskSwitchTimeTracker(const std::string& histogram_name) |
| 29 : histogram_name_(histogram_name), |
| 30 tick_clock_(new base::DefaultTickClock()) { |
| 31 } |
| 32 |
| 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()) { |
| 37 } |
| 38 |
| 39 TaskSwitchTimeTracker::~TaskSwitchTimeTracker() { |
| 40 } |
| 41 |
| 42 void TaskSwitchTimeTracker::OnTaskSwitch() { |
| 43 if (!HasLastActionTime()) |
| 44 SetLastActionTime(); |
| 45 else |
| 46 RecordTimeDelta(); |
| 47 } |
| 48 |
| 49 bool TaskSwitchTimeTracker::HasLastActionTime() const { |
| 50 return last_action_time_ != base::TimeTicks(); |
| 51 } |
| 52 |
| 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; |
| 57 } |
| 58 |
| 59 void TaskSwitchTimeTracker::RecordTimeDelta() { |
| 60 base::TimeTicks previous_last_action_time = SetLastActionTime(); |
| 61 base::TimeDelta time_delta = last_action_time_ - previous_last_action_time; |
| 62 |
| 63 CHECK_GE(time_delta, base::TimeDelta()); |
| 64 |
| 65 GetHistogram()->Add(time_delta.InSeconds()); |
| 66 } |
| 67 |
| 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); |
| 78 } |
| 79 |
| 80 #if DCHECK_IS_ON() |
| 81 histogram_->CheckName(histogram_name_); |
| 82 #endif // DCHECK_IS_ON() |
| 83 |
| 84 return histogram_; |
| 85 } |
| 86 |
| 87 } // namespace ash |
OLD | NEW |