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.
5 #ifndef ASH_METRICS_TASK_SWITCH_TIME_TRACKER_H_
6 #define ASH_METRICS_TASK_SWITCH_TIME_TRACKER_H_
10 #include "ash/ash_export.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/time/time.h"
22 class TaskSwitchTimeTrackerTestAPI
;
25 // Tracks time deltas between task switches and records them in a histogram.
26 class ASH_EXPORT TaskSwitchTimeTracker
{
28 // Create a TaskSwitchTimeTracker that will record data to the histogram with
29 // the given |histogram_name|.
30 explicit TaskSwitchTimeTracker(const std::string
& histogram_name
);
32 ~TaskSwitchTimeTracker();
34 // Notifies |this| that a task switch has occurred. A histogram data point
35 // will be recorded for all calls but the first.
39 friend class test::TaskSwitchTimeTrackerTestAPI
;
41 // Private constructor that the test::TaskSwitchTimeTrackerTestAPI can use to
42 // inject a custom |tick_clock|.
43 TaskSwitchTimeTracker(const std::string
& histogram_name
,
44 scoped_ptr
<base::TickClock
> tick_clock
);
46 // Returns true if |last_action_time_| has a valid value.
47 bool HasLastActionTime() const;
49 // Sets the |last_action_time_| to |tick_clock_|'s current value and returns
50 // the previous value for |last_action_time_|.
51 base::TimeTicks
SetLastActionTime();
53 // Records a data point in the histogram.
54 void RecordTimeDelta();
56 // Lazily obtains and sets the |histogram_|.
57 base::HistogramBase
* GetHistogram();
59 // The histogram name to record data to.
60 std::string histogram_name_
;
62 // The histogram to log data to. Set via GetHistogram() using lazy load.
63 base::HistogramBase
* histogram_
= nullptr;
65 // Tracks the last time OnTaskSwitch() was called. A value of
66 // base::TimeTicks() should be interpreted as not set.
67 base::TimeTicks last_action_time_
= base::TimeTicks();
69 // The clock used to determine the |last_action_time_|.
70 scoped_ptr
<base::TickClock
> tick_clock_
;
72 DISALLOW_COPY_AND_ASSIGN(TaskSwitchTimeTracker
);
77 #endif // ASH_METRICS_TASK_SWITCH_TIME_TRACKE_H_