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 #include "base/task_scheduler/task_tracker.h" | 5 #include "base/task_scheduler/task_tracker.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/debug/task_annotator.h" | 10 #include "base/debug/task_annotator.h" |
11 #include "base/json/json_writer.h" | |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/ptr_util.h" | |
12 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
13 #include "base/sequence_token.h" | 15 #include "base/sequence_token.h" |
14 #include "base/synchronization/condition_variable.h" | 16 #include "base/synchronization/condition_variable.h" |
15 #include "base/threading/sequenced_task_runner_handle.h" | 17 #include "base/threading/sequenced_task_runner_handle.h" |
16 #include "base/threading/thread_restrictions.h" | 18 #include "base/threading/thread_restrictions.h" |
17 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
18 #include "base/trace_event/trace_event.h" | 20 #include "base/trace_event/trace_event.h" |
21 #include "base/values.h" | |
19 | 22 |
20 namespace base { | 23 namespace base { |
21 namespace internal { | 24 namespace internal { |
22 | 25 |
23 namespace { | 26 namespace { |
24 | 27 |
28 // An immutable copy of a scheduler task's info required by tracing. | |
29 class TaskTracingInfo : public trace_event::ConvertableToTraceFormat { | |
30 public: | |
31 TaskTracingInfo(const TaskTraits& task_traits, | |
32 ExecutionMode execution_mode, | |
33 const SequenceToken& sequence_token) | |
34 : task_traits_(task_traits), | |
35 execution_mode_(execution_mode), | |
36 sequence_token_(sequence_token) {} | |
37 | |
38 // Overridden from trace_event::ConvertableToTraceFormat: | |
danakj
2016/10/05 23:26:52
nit: normally written as "trace_event::Convertable
gab
2016/10/06 19:13:53
Done.
| |
39 void AppendAsTraceFormat(std::string* out) const override; | |
40 | |
41 private: | |
42 const TaskTraits task_traits_; | |
43 const ExecutionMode execution_mode_; | |
44 const SequenceToken sequence_token_; | |
45 | |
46 DISALLOW_COPY_AND_ASSIGN(TaskTracingInfo); | |
47 }; | |
48 | |
49 void TaskTracingInfo::AppendAsTraceFormat(std::string* out) const { | |
50 DictionaryValue dict; | |
51 | |
52 dict.SetString("TaskPriority", | |
danakj
2016/10/05 23:26:52
aren't tracing values usually like_this instead of
gab
2016/10/06 19:13:53
Done.
| |
53 base::TaskPriorityToString(task_traits_.priority())); | |
54 dict.SetString("ExecutionMode", base::ExecutionModeToString(execution_mode_)); | |
55 if (execution_mode_ != ExecutionMode::PARALLEL) | |
56 dict.SetInteger("SequenceToken", sequence_token_.ToInternalValue()); | |
57 | |
58 std::string tmp; | |
59 JSONWriter::Write(dict, &tmp); | |
60 out->append(tmp); | |
61 } | |
62 | |
25 const char kQueueFunctionName[] = "base::PostTask"; | 63 const char kQueueFunctionName[] = "base::PostTask"; |
26 | 64 |
27 // This name conveys that a Task is run by the task scheduler without revealing | 65 // This name conveys that a Task is run by the task scheduler without revealing |
28 // its implementation details. | 66 // its implementation details. |
29 const char kRunFunctionName[] = "TaskSchedulerRunTask"; | 67 const char kRunFunctionName[] = "TaskSchedulerRunTask"; |
30 | 68 |
31 // Upper bound for the | 69 // Upper bound for the |
32 // TaskScheduler.BlockShutdownTasksPostedDuringShutdown histogram. | 70 // TaskScheduler.BlockShutdownTasksPostedDuringShutdown histogram. |
33 const HistogramBase::Sample kMaxBlockShutdownTasksPostedDuringShutdown = 1000; | 71 const HistogramBase::Sample kMaxBlockShutdownTasksPostedDuringShutdown = 1000; |
34 | 72 |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
196 if (task->sequenced_task_runner_ref) { | 234 if (task->sequenced_task_runner_ref) { |
197 sequenced_task_runner_handle.reset( | 235 sequenced_task_runner_handle.reset( |
198 new SequencedTaskRunnerHandle(task->sequenced_task_runner_ref)); | 236 new SequencedTaskRunnerHandle(task->sequenced_task_runner_ref)); |
199 } else if (task->single_thread_task_runner_ref) { | 237 } else if (task->single_thread_task_runner_ref) { |
200 single_thread_task_runner_handle.reset( | 238 single_thread_task_runner_handle.reset( |
201 new ThreadTaskRunnerHandle(task->single_thread_task_runner_ref)); | 239 new ThreadTaskRunnerHandle(task->single_thread_task_runner_ref)); |
202 } | 240 } |
203 | 241 |
204 TRACE_TASK_EXECUTION(kRunFunctionName, *task); | 242 TRACE_TASK_EXECUTION(kRunFunctionName, *task); |
205 | 243 |
244 const ExecutionMode execution_mode = | |
245 task->single_thread_task_runner_ref | |
246 ? ExecutionMode::SINGLE_THREADED | |
247 : (task->sequenced_task_runner_ref ? ExecutionMode::SEQUENCED | |
248 : ExecutionMode::PARALLEL); | |
249 // TODO(gab): In a better world this would be tacked on as an extra arg | |
250 // to the trace event generated above. This is not possible however until | |
251 // http://crbug.com/652692 is resolved. | |
252 TRACE_EVENT1("task_scheduler", "TaskTracker::RunTask", "task_info", | |
253 MakeUnique<TaskTracingInfo>(task->traits, execution_mode, | |
254 sequence_token)); | |
255 | |
206 debug::TaskAnnotator task_annotator; | 256 debug::TaskAnnotator task_annotator; |
207 task_annotator.RunTask(kQueueFunctionName, *task); | 257 task_annotator.RunTask(kQueueFunctionName, *task); |
208 } | 258 } |
209 | 259 |
210 AfterRunTask(shutdown_behavior); | 260 AfterRunTask(shutdown_behavior); |
211 } | 261 } |
212 | 262 |
213 if (task->delayed_run_time.is_null()) | 263 if (task->delayed_run_time.is_null()) |
214 DecrementNumPendingUndelayedTasks(); | 264 DecrementNumPendingUndelayedTasks(); |
215 | 265 |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
384 subtle::NoBarrier_AtomicIncrement(&num_pending_undelayed_tasks_, -1); | 434 subtle::NoBarrier_AtomicIncrement(&num_pending_undelayed_tasks_, -1); |
385 DCHECK_GE(new_num_pending_undelayed_tasks, 0); | 435 DCHECK_GE(new_num_pending_undelayed_tasks, 0); |
386 if (new_num_pending_undelayed_tasks == 0) { | 436 if (new_num_pending_undelayed_tasks == 0) { |
387 AutoSchedulerLock auto_lock(flush_lock_); | 437 AutoSchedulerLock auto_lock(flush_lock_); |
388 flush_cv_->Signal(); | 438 flush_cv_->Signal(); |
389 } | 439 } |
390 } | 440 } |
391 | 441 |
392 } // namespace internal | 442 } // namespace internal |
393 } // namespace base | 443 } // namespace base |
OLD | NEW |