Chromium Code Reviews| Index: ash/metrics/task_switch_time_tracker.cc |
| diff --git a/ash/metrics/task_switch_time_tracker.cc b/ash/metrics/task_switch_time_tracker.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ca28a2376255a8c752ee7032c0d52c7ba59bc02c |
| --- /dev/null |
| +++ b/ash/metrics/task_switch_time_tracker.cc |
| @@ -0,0 +1,74 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ash/metrics/task_switch_time_tracker.h" |
| + |
| +#include "base/metrics/histogram.h" |
| +#include "base/time/default_tick_clock.h" |
| + |
| +namespace ash { |
| + |
| +namespace { |
| + |
| +// The number of buckets in the histogram. |
| +const size_t kBucketCount = 50; |
| + |
| +// The underflow (aka minimum) bucket size for the histogram. |
| +const base::TimeDelta kMinBucket = base::TimeDelta::FromSeconds(0); |
| + |
| +// The overflow (aka maximium) bucket size for the histogram. |
| +const base::TimeDelta kMaxBucket = base::TimeDelta::FromHours(1); |
| + |
| +} // namespace |
| + |
| +TaskSwitchTimeTracker::TaskSwitchTimeTracker(const std::string& histogram_name) |
| + : histogram_name_(histogram_name), |
| + 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.
|
| +} |
| + |
| +TaskSwitchTimeTracker::TaskSwitchTimeTracker( |
| + const std::string& histogram_name, |
| + scoped_ptr<base::TickClock> tick_clock) |
| + : histogram_name_(histogram_name), tick_clock_(tick_clock.release()) { |
| +} |
| + |
| +TaskSwitchTimeTracker::~TaskSwitchTimeTracker() { |
| +} |
| + |
| +void TaskSwitchTimeTracker::OnTaskSwitch() { |
| + if (!HasLastActionTime()) |
| + SetLastActionTime(); |
| + else |
| + RecordTimeDelta(); |
| +} |
| + |
| +bool TaskSwitchTimeTracker::HasLastActionTime() const { |
| + return last_action_time_ != base::TimeTicks(); |
| +} |
| + |
| +base::TimeTicks TaskSwitchTimeTracker::SetLastActionTime() { |
| + base::TimeTicks previous_last_action_time = last_action_time_; |
| + last_action_time_ = tick_clock_->NowTicks(); |
| + return previous_last_action_time; |
| +} |
| + |
| +void TaskSwitchTimeTracker::RecordTimeDelta() { |
| + base::TimeTicks previous_last_action_time = SetLastActionTime(); |
| + base::TimeDelta time_delta = last_action_time_ - previous_last_action_time; |
| + |
|
tdanderson
2015/05/13 19:27:18
Maybe have a CHECK() that the delta >= 0 ?
bruthig
2015/05/13 21:16:21
Done.
|
| + GetHistogram()->Add(time_delta.InSeconds()); |
| +} |
| + |
| +base::HistogramBase* TaskSwitchTimeTracker::GetHistogram() { |
| + if (!histogram_) { |
| + histogram_ = base::Histogram::FactoryGet( |
| + histogram_name_, kMinBucket.InSeconds(), kMaxBucket.InSeconds(), |
| + kBucketCount, base::HistogramBase::kUmaTargetedHistogramFlag); |
| + } |
| + if (DCHECK_IS_ON()) |
| + histogram_->CheckName(histogram_name_); |
| + return histogram_; |
| +} |
| + |
| +} // namespace ash |