OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_TASK_SCHEDULER_TASK_TRACKER_H_ | 5 #ifndef BASE_TASK_SCHEDULER_TASK_TRACKER_H_ |
6 #define BASE_TASK_SCHEDULER_TASK_TRACKER_H_ | 6 #define BASE_TASK_SCHEDULER_TASK_TRACKER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/metrics/histogram_base.h" | 14 #include "base/metrics/histogram_base.h" |
15 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
16 #include "base/task_scheduler/scheduler_lock.h" | 16 #include "base/task_scheduler/scheduler_lock.h" |
17 #include "base/task_scheduler/sequence.h" | 17 #include "base/task_scheduler/sequence.h" |
18 #include "base/task_scheduler/task.h" | 18 #include "base/task_scheduler/task.h" |
19 #include "base/task_scheduler/task_traits.h" | 19 #include "base/task_scheduler/task_traits.h" |
20 | 20 |
21 namespace base { | 21 namespace base { |
22 | 22 |
23 class ConditionVariable; | 23 class ConditionVariable; |
| 24 class HistogramBase; |
24 class SequenceToken; | 25 class SequenceToken; |
25 | 26 |
26 namespace internal { | 27 namespace internal { |
27 | 28 |
28 // All tasks go through the scheduler's TaskTracker when they are posted and | 29 // All tasks go through the scheduler's TaskTracker when they are posted and |
29 // when they are executed. The TaskTracker enforces shutdown semantics and takes | 30 // when they are executed. The TaskTracker sets up the environment to run tasks, |
30 // care of tracing and profiling. This class is thread-safe. | 31 // enforces shutdown semantics, records metrics, and takes care of tracing and |
| 32 // profiling. This class is thread-safe. |
31 class BASE_EXPORT TaskTracker { | 33 class BASE_EXPORT TaskTracker { |
32 public: | 34 public: |
33 TaskTracker(); | 35 TaskTracker(); |
34 ~TaskTracker(); | 36 ~TaskTracker(); |
35 | 37 |
36 // Synchronously shuts down the scheduler. Once this is called, only tasks | 38 // Synchronously shuts down the scheduler. Once this is called, only tasks |
37 // posted with the BLOCK_SHUTDOWN behavior will be run. Returns when: | 39 // posted with the BLOCK_SHUTDOWN behavior will be run. Returns when: |
38 // - All SKIP_ON_SHUTDOWN tasks that were already running have completed their | 40 // - All SKIP_ON_SHUTDOWN tasks that were already running have completed their |
39 // execution. | 41 // execution. |
40 // - All posted BLOCK_SHUTDOWN tasks have completed their execution. | 42 // - All posted BLOCK_SHUTDOWN tasks have completed their execution. |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 void AfterRunTask(TaskShutdownBehavior shutdown_behavior); | 101 void AfterRunTask(TaskShutdownBehavior shutdown_behavior); |
100 | 102 |
101 // Called when the number of tasks blocking shutdown becomes zero after | 103 // Called when the number of tasks blocking shutdown becomes zero after |
102 // shutdown has started. | 104 // shutdown has started. |
103 void OnBlockingShutdownTasksComplete(); | 105 void OnBlockingShutdownTasksComplete(); |
104 | 106 |
105 // Decrements the number of pending undelayed tasks and signals |flush_cv_| if | 107 // Decrements the number of pending undelayed tasks and signals |flush_cv_| if |
106 // it reaches zero. | 108 // it reaches zero. |
107 void DecrementNumPendingUndelayedTasks(); | 109 void DecrementNumPendingUndelayedTasks(); |
108 | 110 |
| 111 // Records the TaskScheduler.TaskLatency.[task priority].[may block] histogram |
| 112 // for |task|. |
| 113 void RecordTaskLatencyHistogram(Task* task); |
| 114 |
109 // Number of tasks blocking shutdown and boolean indicating whether shutdown | 115 // Number of tasks blocking shutdown and boolean indicating whether shutdown |
110 // has started. | 116 // has started. |
111 const std::unique_ptr<State> state_; | 117 const std::unique_ptr<State> state_; |
112 | 118 |
113 // Number of undelayed tasks that haven't completed their execution. Is | 119 // Number of undelayed tasks that haven't completed their execution. Is |
114 // incremented and decremented without a barrier. When it reaches zero, | 120 // incremented and decremented without a barrier. When it reaches zero, |
115 // |flush_lock_| is acquired (forcing memory synchronization) and |flush_cv_| | 121 // |flush_lock_| is acquired (forcing memory synchronization) and |flush_cv_| |
116 // is signaled. | 122 // is signaled. |
117 subtle::Atomic32 num_pending_undelayed_tasks_ = 0; | 123 subtle::Atomic32 num_pending_undelayed_tasks_ = 0; |
118 | 124 |
119 // Lock associated with |flush_cv_|. Partially synchronizes access to | 125 // Lock associated with |flush_cv_|. Partially synchronizes access to |
120 // |num_pending_undelayed_tasks_|. Full synchronization isn't needed because | 126 // |num_pending_undelayed_tasks_|. Full synchronization isn't needed because |
121 // it's atomic, but synchronization is needed to coordinate waking and | 127 // it's atomic, but synchronization is needed to coordinate waking and |
122 // sleeping at the right time. | 128 // sleeping at the right time. |
123 mutable SchedulerLock flush_lock_; | 129 mutable SchedulerLock flush_lock_; |
124 | 130 |
125 // Signaled when |num_pending_undelayed_tasks_| is zero or when shutdown | 131 // Signaled when |num_pending_undelayed_tasks_| is zero or when shutdown |
126 // completes. | 132 // completes. |
127 const std::unique_ptr<ConditionVariable> flush_cv_; | 133 const std::unique_ptr<ConditionVariable> flush_cv_; |
128 | 134 |
129 // Synchronizes access to shutdown related members below. | 135 // Synchronizes access to shutdown related members below. |
130 mutable SchedulerLock shutdown_lock_; | 136 mutable SchedulerLock shutdown_lock_; |
131 | 137 |
132 // Event instantiated when shutdown starts and signaled when shutdown | 138 // Event instantiated when shutdown starts and signaled when shutdown |
133 // completes. | 139 // completes. |
134 std::unique_ptr<WaitableEvent> shutdown_event_; | 140 std::unique_ptr<WaitableEvent> shutdown_event_; |
135 | 141 |
| 142 // TaskScheduler.TaskLatency.[task priority].[may block] histograms. The first |
| 143 // index is a TaskPriority. The second index is 0 for non-blocking tasks, 1 |
| 144 // for blocking tasks. Intentionally leaked. |
| 145 HistogramBase* const |
| 146 task_latency_histograms_[static_cast<int>(TaskPriority::HIGHEST) + 1][2]; |
| 147 |
136 // Number of BLOCK_SHUTDOWN tasks posted during shutdown. | 148 // Number of BLOCK_SHUTDOWN tasks posted during shutdown. |
137 HistogramBase::Sample num_block_shutdown_tasks_posted_during_shutdown_ = 0; | 149 HistogramBase::Sample num_block_shutdown_tasks_posted_during_shutdown_ = 0; |
138 | 150 |
139 DISALLOW_COPY_AND_ASSIGN(TaskTracker); | 151 DISALLOW_COPY_AND_ASSIGN(TaskTracker); |
140 }; | 152 }; |
141 | 153 |
142 } // namespace internal | 154 } // namespace internal |
143 } // namespace base | 155 } // namespace base |
144 | 156 |
145 #endif // BASE_TASK_SCHEDULER_TASK_TRACKER_H_ | 157 #endif // BASE_TASK_SCHEDULER_TASK_TRACKER_H_ |
OLD | NEW |