Chromium Code Reviews| 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/gtest_prod_util.h" | |
| 13 #include "base/macros.h" | 14 #include "base/macros.h" |
| 14 #include "base/metrics/histogram_base.h" | 15 #include "base/metrics/histogram_base.h" |
| 15 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
| 16 #include "base/task_scheduler/scheduler_lock.h" | 17 #include "base/task_scheduler/scheduler_lock.h" |
| 17 #include "base/task_scheduler/sequence.h" | 18 #include "base/task_scheduler/sequence.h" |
| 18 #include "base/task_scheduler/task.h" | 19 #include "base/task_scheduler/task.h" |
| 19 #include "base/task_scheduler/task_traits.h" | 20 #include "base/task_scheduler/task_traits.h" |
| 20 | 21 |
| 21 namespace base { | 22 namespace base { |
| 22 | 23 |
| 23 class ConditionVariable; | 24 class ConditionVariable; |
| 25 class HistogramBase; | |
| 24 class SequenceToken; | 26 class SequenceToken; |
| 25 | 27 |
| 26 namespace internal { | 28 namespace internal { |
| 27 | 29 |
| 28 // All tasks go through the scheduler's TaskTracker when they are posted and | 30 // 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 | 31 // when they are executed. The TaskTracker sets up the environment to run tasks, |
| 30 // care of tracing and profiling. This class is thread-safe. | 32 // enforces shutdown semantics, records metrics, and takes care of tracing and |
| 33 // profiling. This class is thread-safe. | |
| 31 class BASE_EXPORT TaskTracker { | 34 class BASE_EXPORT TaskTracker { |
| 32 public: | 35 public: |
| 33 TaskTracker(); | 36 TaskTracker(); |
| 34 ~TaskTracker(); | 37 ~TaskTracker(); |
| 35 | 38 |
| 36 // Synchronously shuts down the scheduler. Once this is called, only tasks | 39 // Synchronously shuts down the scheduler. Once this is called, only tasks |
| 37 // posted with the BLOCK_SHUTDOWN behavior will be run. Returns when: | 40 // posted with the BLOCK_SHUTDOWN behavior will be run. Returns when: |
| 38 // - All SKIP_ON_SHUTDOWN tasks that were already running have completed their | 41 // - All SKIP_ON_SHUTDOWN tasks that were already running have completed their |
| 39 // execution. | 42 // execution. |
| 40 // - All posted BLOCK_SHUTDOWN tasks have completed their execution. | 43 // - All posted BLOCK_SHUTDOWN tasks have completed their execution. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 // IsShutdownComplete() won't return true after this returns. Shutdown() | 75 // IsShutdownComplete() won't return true after this returns. Shutdown() |
| 73 // cannot be called after this. | 76 // cannot be called after this. |
| 74 void SetHasShutdownStartedForTesting(); | 77 void SetHasShutdownStartedForTesting(); |
| 75 | 78 |
| 76 protected: | 79 protected: |
| 77 // Runs |task|. An override is expected to call its parent's implementation | 80 // Runs |task|. An override is expected to call its parent's implementation |
| 78 // but is free to perform extra work before and after doing so. | 81 // but is free to perform extra work before and after doing so. |
| 79 virtual void PerformRunTask(std::unique_ptr<Task> task); | 82 virtual void PerformRunTask(std::unique_ptr<Task> task); |
| 80 | 83 |
| 81 private: | 84 private: |
| 85 FRIEND_TEST_ALL_PREFIXES(TaskSchedulerTaskTrackerHistogramTest, TaskLatency); | |
|
gab
2017/01/05 22:16:36
Drop friend?
fdoray
2017/01/05 22:31:58
Done.
| |
| 86 | |
| 82 class State; | 87 class State; |
| 83 | 88 |
| 84 void PerformShutdown(); | 89 void PerformShutdown(); |
| 85 | 90 |
| 86 // Called before WillPostTask() informs the tracing system that a task has | 91 // Called before WillPostTask() informs the tracing system that a task has |
| 87 // been posted. Updates |num_tasks_blocking_shutdown_| if necessary and | 92 // been posted. Updates |num_tasks_blocking_shutdown_| if necessary and |
| 88 // returns true if the current shutdown state allows the task to be posted. | 93 // returns true if the current shutdown state allows the task to be posted. |
| 89 bool BeforePostTask(TaskShutdownBehavior shutdown_behavior); | 94 bool BeforePostTask(TaskShutdownBehavior shutdown_behavior); |
| 90 | 95 |
| 91 // Called before a task with |shutdown_behavior| is run by RunTask(). Updates | 96 // Called before a task with |shutdown_behavior| is run by RunTask(). Updates |
| 92 // |num_tasks_blocking_shutdown_| if necessary and returns true if the current | 97 // |num_tasks_blocking_shutdown_| if necessary and returns true if the current |
| 93 // shutdown state allows the task to be run. | 98 // shutdown state allows the task to be run. |
| 94 bool BeforeRunTask(TaskShutdownBehavior shutdown_behavior); | 99 bool BeforeRunTask(TaskShutdownBehavior shutdown_behavior); |
| 95 | 100 |
| 96 // Called after a task with |shutdown_behavior| has been run by RunTask(). | 101 // Called after a task with |shutdown_behavior| has been run by RunTask(). |
| 97 // Updates |num_tasks_blocking_shutdown_| and signals |shutdown_cv_| if | 102 // Updates |num_tasks_blocking_shutdown_| and signals |shutdown_cv_| if |
| 98 // necessary. | 103 // necessary. |
| 99 void AfterRunTask(TaskShutdownBehavior shutdown_behavior); | 104 void AfterRunTask(TaskShutdownBehavior shutdown_behavior); |
| 100 | 105 |
| 101 // Called when the number of tasks blocking shutdown becomes zero after | 106 // Called when the number of tasks blocking shutdown becomes zero after |
| 102 // shutdown has started. | 107 // shutdown has started. |
| 103 void OnBlockingShutdownTasksComplete(); | 108 void OnBlockingShutdownTasksComplete(); |
| 104 | 109 |
| 105 // Decrements the number of pending undelayed tasks and signals |flush_cv_| if | 110 // Decrements the number of pending undelayed tasks and signals |flush_cv_| if |
| 106 // it reaches zero. | 111 // it reaches zero. |
| 107 void DecrementNumPendingUndelayedTasks(); | 112 void DecrementNumPendingUndelayedTasks(); |
| 108 | 113 |
| 114 // Records the TaskScheduler.TaskLatency.[task priority].[may block] histogram | |
| 115 // for |task|. | |
| 116 void RecordTaskLatencyHistogram(Task* task); | |
| 117 | |
| 109 // Number of tasks blocking shutdown and boolean indicating whether shutdown | 118 // Number of tasks blocking shutdown and boolean indicating whether shutdown |
| 110 // has started. | 119 // has started. |
| 111 const std::unique_ptr<State> state_; | 120 const std::unique_ptr<State> state_; |
| 112 | 121 |
| 113 // Number of undelayed tasks that haven't completed their execution. Is | 122 // Number of undelayed tasks that haven't completed their execution. Is |
| 114 // incremented and decremented without a barrier. When it reaches zero, | 123 // incremented and decremented without a barrier. When it reaches zero, |
| 115 // |flush_lock_| is acquired (forcing memory synchronization) and |flush_cv_| | 124 // |flush_lock_| is acquired (forcing memory synchronization) and |flush_cv_| |
| 116 // is signaled. | 125 // is signaled. |
| 117 subtle::Atomic32 num_pending_undelayed_tasks_ = 0; | 126 subtle::Atomic32 num_pending_undelayed_tasks_ = 0; |
| 118 | 127 |
| 119 // Lock associated with |flush_cv_|. Partially synchronizes access to | 128 // Lock associated with |flush_cv_|. Partially synchronizes access to |
| 120 // |num_pending_undelayed_tasks_|. Full synchronization isn't needed because | 129 // |num_pending_undelayed_tasks_|. Full synchronization isn't needed because |
| 121 // it's atomic, but synchronization is needed to coordinate waking and | 130 // it's atomic, but synchronization is needed to coordinate waking and |
| 122 // sleeping at the right time. | 131 // sleeping at the right time. |
| 123 mutable SchedulerLock flush_lock_; | 132 mutable SchedulerLock flush_lock_; |
| 124 | 133 |
| 125 // Signaled when |num_pending_undelayed_tasks_| is zero or when shutdown | 134 // Signaled when |num_pending_undelayed_tasks_| is zero or when shutdown |
| 126 // completes. | 135 // completes. |
| 127 const std::unique_ptr<ConditionVariable> flush_cv_; | 136 const std::unique_ptr<ConditionVariable> flush_cv_; |
| 128 | 137 |
| 129 // Synchronizes access to shutdown related members below. | 138 // Synchronizes access to shutdown related members below. |
| 130 mutable SchedulerLock shutdown_lock_; | 139 mutable SchedulerLock shutdown_lock_; |
| 131 | 140 |
| 132 // Event instantiated when shutdown starts and signaled when shutdown | 141 // Event instantiated when shutdown starts and signaled when shutdown |
| 133 // completes. | 142 // completes. |
| 134 std::unique_ptr<WaitableEvent> shutdown_event_; | 143 std::unique_ptr<WaitableEvent> shutdown_event_; |
| 135 | 144 |
| 145 // TaskScheduler.TaskLatency.[task priority].[may block] histograms. The first | |
| 146 // index is a TaskPriority. The second index is 0 for non-blocking tasks, 1 | |
| 147 // for blocking tasks. Intentionally leaked. | |
| 148 HistogramBase* const | |
| 149 task_latency_histograms_[static_cast<int>(TaskPriority::HIGHEST) + 1][2]; | |
| 150 | |
| 136 // Number of BLOCK_SHUTDOWN tasks posted during shutdown. | 151 // Number of BLOCK_SHUTDOWN tasks posted during shutdown. |
| 137 HistogramBase::Sample num_block_shutdown_tasks_posted_during_shutdown_ = 0; | 152 HistogramBase::Sample num_block_shutdown_tasks_posted_during_shutdown_ = 0; |
| 138 | 153 |
| 139 DISALLOW_COPY_AND_ASSIGN(TaskTracker); | 154 DISALLOW_COPY_AND_ASSIGN(TaskTracker); |
| 140 }; | 155 }; |
| 141 | 156 |
| 142 } // namespace internal | 157 } // namespace internal |
| 143 } // namespace base | 158 } // namespace base |
| 144 | 159 |
| 145 #endif // BASE_TASK_SCHEDULER_TASK_TRACKER_H_ | 160 #endif // BASE_TASK_SCHEDULER_TASK_TRACKER_H_ |
| OLD | NEW |