OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/task_scheduler/task_tracker.h" | |
6 | |
7 #include "base/callback.h" | |
8 #include "base/debug/task_annotator.h" | |
9 #include "base/metrics/histogram_macros.h" | |
10 | |
11 namespace base { | |
12 namespace internal { | |
13 | |
14 namespace { | |
15 | |
16 const char kQueueFunctionName[] = "base::PostTask"; | |
17 | |
18 // Upper bound for the | |
19 // TaskScheduler.BlockShutdownTasksPostedDuringShutdown histogram. | |
20 const size_t kMaxBlockShutdownTasksPostedDuringShutdown = 10000; | |
21 | |
22 void RecordNumBlockShutdownTasksPostedDuringShutdown(size_t value) { | |
23 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
24 "TaskScheduler.BlockShutdownTasksPostedDuringShutdown", value, 1, | |
25 kMaxBlockShutdownTasksPostedDuringShutdown, 50); | |
26 } | |
27 | |
28 } // namespace | |
29 | |
30 TaskTracker::TaskTracker() = default; | |
31 TaskTracker::~TaskTracker() = default; | |
32 | |
33 void TaskTracker::Shutdown() { | |
34 AutoSchedulerLock auto_lock(lock_); | |
35 | |
36 DCHECK(!shutdown_completed_ && !shutdown_cv_) | |
danakj
2016/03/22 18:56:56
Don't use && in DCHECK. Write 2 DCHECKs instead so
fdoray
2016/03/22 20:19:43
Done.
| |
37 << "TaskTracker::Shutdown() should only be invoked once."; | |
38 | |
39 shutdown_cv_ = lock_.CreateConditionVariable(); | |
40 | |
41 // Wait until the number of tasks blocking shutdown is zero. | |
42 while (num_tasks_blocking_shutdown_ != 0) | |
43 shutdown_cv_->Wait(); | |
44 | |
45 shutdown_cv_.reset(); | |
46 shutdown_completed_ = true; | |
47 | |
48 // Record the TaskScheduler.BlockShutdownTasksPostedDuringShutdown if less | |
49 // than |kMaxBlockShutdownTasksPostedDuringShutdown| BLOCK_SHUTDOWN tasks have | |
50 // been posted during shutdown. Otherwise, the histogram has already been | |
51 // recorded in BeforePostTask(). | |
52 if (num_block_shutdown_tasks_posted_during_shutdown_ < | |
53 kMaxBlockShutdownTasksPostedDuringShutdown) { | |
54 RecordNumBlockShutdownTasksPostedDuringShutdown( | |
55 num_block_shutdown_tasks_posted_during_shutdown_); | |
56 } | |
57 } | |
58 | |
59 void TaskTracker::PostTask( | |
60 const Callback<void(scoped_ptr<Task>)>& post_task_callback, | |
61 scoped_ptr<Task> task) { | |
62 DCHECK(!post_task_callback.is_null()); | |
63 DCHECK(task); | |
64 | |
65 if (!BeforePostTask(task->traits.shutdown_behavior())) | |
66 return; | |
67 | |
68 debug::TaskAnnotator task_annotator; | |
69 task_annotator.DidQueueTask(kQueueFunctionName, *task); | |
70 | |
71 post_task_callback.Run(std::move(task)); | |
72 } | |
73 | |
74 void TaskTracker::RunTask(const Task* task) { | |
75 DCHECK(task); | |
76 | |
77 const TaskShutdownBehavior shutdown_behavior = | |
78 task->traits.shutdown_behavior(); | |
79 | |
80 DCHECK(shutdown_behavior != TaskShutdownBehavior::BLOCK_SHUTDOWN || | |
81 num_tasks_blocking_shutdown_ > 0U); | |
gab
2016/03/21 19:53:25
Need to hold lock for |num_tasks_blocking_shutdown
danakj
2016/03/22 18:56:56
+1
Also it's easier to read as:
if (shutdown_beh
fdoray
2016/03/22 20:19:43
This check was already in BeforeRunTask(). I just
| |
82 | |
83 if (!BeforeRunTask(shutdown_behavior)) | |
84 return; | |
85 | |
86 debug::TaskAnnotator task_annotator; | |
87 task_annotator.RunTask(kQueueFunctionName, *task); | |
88 | |
89 AfterRunTask(shutdown_behavior); | |
90 } | |
91 | |
92 bool TaskTracker::IsShuttingDownForTesting() const { | |
93 AutoSchedulerLock auto_lock(lock_); | |
94 return !!shutdown_cv_; | |
95 } | |
96 | |
97 bool TaskTracker::BeforePostTask(TaskShutdownBehavior shutdown_behavior) { | |
98 AutoSchedulerLock auto_lock(lock_); | |
99 | |
100 if (shutdown_completed_) { | |
101 // A BLOCK_SHUTDOWN task posted after shutdown has completed is an ordering | |
102 // bug. This DCHECK aims to catch those early. | |
103 DCHECK_NE(TaskShutdownBehavior::BLOCK_SHUTDOWN, shutdown_behavior); | |
danakj
2016/03/22 18:56:56
nit: write this in the order you would write a !=
fdoray
2016/03/22 20:19:43
Done.
gab
2016/03/24 13:00:39
I agree that I prefer to write DCHECK_NE(actual, e
| |
104 | |
105 // No task is allowed to be posted after shutdown. | |
106 return false; | |
107 } | |
108 | |
109 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN) { | |
110 // BLOCK_SHUTDOWN tasks block shutdown between the moment they are posted | |
111 // and the moment they complete their execution. | |
112 ++num_tasks_blocking_shutdown_; | |
113 | |
114 if (shutdown_cv_) { | |
115 ++num_block_shutdown_tasks_posted_during_shutdown_; | |
116 | |
117 if (num_block_shutdown_tasks_posted_during_shutdown_ == | |
118 kMaxBlockShutdownTasksPostedDuringShutdown) { | |
119 // Record the TaskScheduler.BlockShutdownTasksPostedDuringShutdown | |
120 // histogram as soon as its upper bound is hit. That way, a value will | |
121 // be recorded even if an infinite number of BLOCK_SHUTDOWN tasks is | |
122 // posted, preventing shutdown to complete. | |
123 RecordNumBlockShutdownTasksPostedDuringShutdown( | |
124 num_block_shutdown_tasks_posted_during_shutdown_); | |
125 } | |
126 } | |
127 | |
128 // A BLOCK_SHUTDOWN task is allowed to be posted iff shutdown hasn't | |
129 // completed. | |
130 return true; | |
131 } | |
132 | |
133 // A non BLOCK_SHUTDOWN task is allowed to be posted iff shutdown hasn't | |
134 // started. | |
135 return !shutdown_cv_; | |
136 } | |
137 | |
138 bool TaskTracker::BeforeRunTask(TaskShutdownBehavior shutdown_behavior) { | |
139 AutoSchedulerLock auto_lock(lock_); | |
140 | |
141 if (shutdown_completed_) { | |
142 // Trying to run a BLOCK_SHUTDOWN task after shutdown has completed is | |
143 // unexpected as it either shouldn't been posted if shutdown completed or | |
gab
2016/03/21 19:53:25
s/shouldn't been posted/shouldn't have been posted
fdoray
2016/03/22 20:19:43
Done.
| |
144 // should be blocking shutdown if was posted before it did. | |
145 DCHECK_NE(TaskShutdownBehavior::BLOCK_SHUTDOWN, shutdown_behavior); | |
146 | |
147 // A WorkerThread might extract a non BLOCK_SHUTDOWN task from a | |
148 // PriorityQueue after shutdown. It shouldn't be allowed to run it. | |
149 return false; | |
150 } | |
151 | |
152 switch (shutdown_behavior) { | |
153 case TaskShutdownBehavior::BLOCK_SHUTDOWN: | |
154 DCHECK_GT(num_tasks_blocking_shutdown_, 0U); | |
155 return true; | |
156 | |
157 case TaskShutdownBehavior::SKIP_ON_SHUTDOWN: | |
158 if (shutdown_cv_) | |
159 return false; | |
160 | |
161 // SKIP_ON_SHUTDOWN tasks block shutdown while they are running. | |
162 ++num_tasks_blocking_shutdown_; | |
163 return true; | |
164 | |
165 case TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN: | |
166 return !shutdown_cv_; | |
167 } | |
168 | |
169 NOTREACHED(); | |
170 return false; | |
171 } | |
172 | |
173 void TaskTracker::AfterRunTask(TaskShutdownBehavior shutdown_behavior) { | |
174 if (shutdown_behavior == TaskShutdownBehavior::BLOCK_SHUTDOWN || | |
175 shutdown_behavior == TaskShutdownBehavior::SKIP_ON_SHUTDOWN) { | |
176 AutoSchedulerLock auto_lock(lock_); | |
177 DCHECK_GT(num_tasks_blocking_shutdown_, 0U); | |
178 --num_tasks_blocking_shutdown_; | |
179 if (num_tasks_blocking_shutdown_ == 0 && shutdown_cv_) | |
180 shutdown_cv_->Signal(); | |
181 } | |
182 } | |
183 | |
184 } // namespace internal | |
185 } // namespace base | |
OLD | NEW |