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..036476e5279a182d517f1aa99ef9db431391b524 |
--- /dev/null |
+++ b/ash/metrics/task_switch_time_tracker.cc |
@@ -0,0 +1,81 @@ |
+// 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. |
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.
|
+const size_t kBucketCount = 50; |
+ |
+// The underflow (aka minimum) bucket size for the histogram. |
+const int kMinBucketSizeInSeconds = 0; |
+ |
+// The overflow (aka maximium) bucket size for the histogram. |
+const int kMaxBucketSizeInSeconds = 60 * 60; |
+ |
+} // namespace |
+ |
+TaskSwitchTimeTracker::TaskSwitchTimeTracker(const std::string& histogram_name) |
+ : histogram_name_(histogram_name), |
+ tick_clock_(new base::DefaultTickClock()) { |
+} |
+ |
+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; |
+ |
+ CHECK_GE(time_delta, base::TimeDelta()); |
+ |
+ GetHistogram()->Add(time_delta.InSeconds()); |
+} |
+ |
+base::HistogramBase* TaskSwitchTimeTracker::GetHistogram() { |
+ if (!histogram_) { |
+ histogram_ = base::Histogram::FactoryGet( |
+ histogram_name_, |
+ base::TimeDelta::FromSeconds(kMinBucketSizeInSeconds).InSeconds(), |
+ base::TimeDelta::FromSeconds(kMaxBucketSizeInSeconds).InSeconds(), |
+ kBucketCount, base::HistogramBase::kUmaTargetedHistogramFlag); |
+ } |
+ |
+#if DCHECK_IS_ON() |
+ histogram_->CheckName(histogram_name_); |
+#endif // DCHECK_IS_ON() |
+ |
+ return histogram_; |
+} |
+ |
+} // namespace ash |