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 #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: | |
| 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 const char* priority_str = nullptr; | |
| 53 switch (task_traits_.priority()) { | |
|
fdoray
2016/10/05 12:39:40
Add a char* ToString(TaskPriority priority) functi
gab
2016/10/05 20:12:43
I had debated that and weakly opted against it, bu
| |
| 54 case TaskPriority::BACKGROUND: | |
| 55 priority_str = "BACKGROUND"; | |
| 56 break; | |
| 57 case TaskPriority::USER_VISIBLE: | |
| 58 priority_str = "USER_VISIBLE"; | |
| 59 break; | |
| 60 case TaskPriority::USER_BLOCKING: | |
| 61 priority_str = "USER_BLOCKING"; | |
| 62 break; | |
| 63 } | |
| 64 dict.SetString("TaskPriority", priority_str); | |
| 65 | |
| 66 const char* execution_mode_str = nullptr; | |
| 67 switch (execution_mode_) { | |
|
fdoray
2016/10/05 12:39:40
Same as above
gab
2016/10/05 20:12:43
Done.
| |
| 68 case ExecutionMode::PARALLEL: | |
| 69 execution_mode_str = "PARALLEL"; | |
| 70 break; | |
| 71 case ExecutionMode::SEQUENCED: | |
| 72 execution_mode_str = "SEQUENCED"; | |
| 73 break; | |
| 74 case ExecutionMode::SINGLE_THREADED: | |
| 75 execution_mode_str = "SINGLE_THREADED"; | |
| 76 break; | |
| 77 } | |
| 78 dict.SetString("ExecutionMode", execution_mode_str); | |
| 79 | |
| 80 if (execution_mode_ != ExecutionMode::PARALLEL) | |
| 81 dict.SetString("SequenceToken", sequence_token_.ToString()); | |
|
fdoray
2016/10/05 12:39:40
why not
dict.SetInteger("SequenceToken", sequence_
gab
2016/10/05 20:12:43
Great idea, done.
| |
| 82 | |
| 83 std::string tmp; | |
| 84 JSONWriter::Write(dict, &tmp); | |
| 85 out->append(tmp); | |
| 86 } | |
| 87 | |
| 25 const char kQueueFunctionName[] = "base::PostTask"; | 88 const char kQueueFunctionName[] = "base::PostTask"; |
| 26 | 89 |
| 27 // This name conveys that a Task is run by the task scheduler without revealing | 90 // This name conveys that a Task is run by the task scheduler without revealing |
| 28 // its implementation details. | 91 // its implementation details. |
| 29 const char kRunFunctionName[] = "TaskSchedulerRunTask"; | 92 const char kRunFunctionName[] = "TaskSchedulerRunTask"; |
| 30 | 93 |
| 31 // Upper bound for the | 94 // Upper bound for the |
| 32 // TaskScheduler.BlockShutdownTasksPostedDuringShutdown histogram. | 95 // TaskScheduler.BlockShutdownTasksPostedDuringShutdown histogram. |
| 33 const HistogramBase::Sample kMaxBlockShutdownTasksPostedDuringShutdown = 1000; | 96 const HistogramBase::Sample kMaxBlockShutdownTasksPostedDuringShutdown = 1000; |
| 34 | 97 |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 196 if (task->sequenced_task_runner_ref) { | 259 if (task->sequenced_task_runner_ref) { |
| 197 sequenced_task_runner_handle.reset( | 260 sequenced_task_runner_handle.reset( |
| 198 new SequencedTaskRunnerHandle(task->sequenced_task_runner_ref)); | 261 new SequencedTaskRunnerHandle(task->sequenced_task_runner_ref)); |
| 199 } else if (task->single_thread_task_runner_ref) { | 262 } else if (task->single_thread_task_runner_ref) { |
| 200 single_thread_task_runner_handle.reset( | 263 single_thread_task_runner_handle.reset( |
| 201 new ThreadTaskRunnerHandle(task->single_thread_task_runner_ref)); | 264 new ThreadTaskRunnerHandle(task->single_thread_task_runner_ref)); |
| 202 } | 265 } |
| 203 | 266 |
| 204 TRACE_TASK_EXECUTION(kRunFunctionName, *task); | 267 TRACE_TASK_EXECUTION(kRunFunctionName, *task); |
| 205 | 268 |
| 269 ExecutionMode execution_mode = | |
|
fdoray
2016/10/05 12:39:40
const ExecutionMode execution_mode =
gab
2016/10/05 20:12:43
Done.
| |
| 270 task->single_thread_task_runner_ref | |
| 271 ? ExecutionMode::SINGLE_THREADED | |
| 272 : (task->sequenced_task_runner_ref ? ExecutionMode::SEQUENCED | |
| 273 : ExecutionMode::PARALLEL); | |
| 274 // TODO(gab): In a better world this would be tacked on as an extra arg | |
| 275 // to the trace event generated above. This is not possible however until | |
| 276 // http://crbug.com/652692 is resolved. | |
| 277 TRACE_EVENT1("task_scheduler", "TaskTracker::RunTask", "task_info", | |
| 278 MakeUnique<TaskTracingInfo>( | |
| 279 task->traits, execution_mode, sequence_token)); | |
| 280 | |
| 206 debug::TaskAnnotator task_annotator; | 281 debug::TaskAnnotator task_annotator; |
| 207 task_annotator.RunTask(kQueueFunctionName, *task); | 282 task_annotator.RunTask(kQueueFunctionName, *task); |
| 208 } | 283 } |
| 209 | 284 |
| 210 AfterRunTask(shutdown_behavior); | 285 AfterRunTask(shutdown_behavior); |
| 211 } | 286 } |
| 212 | 287 |
| 213 if (task->delayed_run_time.is_null()) | 288 if (task->delayed_run_time.is_null()) |
| 214 DecrementNumPendingUndelayedTasks(); | 289 DecrementNumPendingUndelayedTasks(); |
| 215 | 290 |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 384 subtle::NoBarrier_AtomicIncrement(&num_pending_undelayed_tasks_, -1); | 459 subtle::NoBarrier_AtomicIncrement(&num_pending_undelayed_tasks_, -1); |
| 385 DCHECK_GE(new_num_pending_undelayed_tasks, 0); | 460 DCHECK_GE(new_num_pending_undelayed_tasks, 0); |
| 386 if (new_num_pending_undelayed_tasks == 0) { | 461 if (new_num_pending_undelayed_tasks == 0) { |
| 387 AutoSchedulerLock auto_lock(flush_lock_); | 462 AutoSchedulerLock auto_lock(flush_lock_); |
| 388 flush_cv_->Signal(); | 463 flush_cv_->Signal(); |
| 389 } | 464 } |
| 390 } | 465 } |
| 391 | 466 |
| 392 } // namespace internal | 467 } // namespace internal |
| 393 } // namespace base | 468 } // namespace base |
| OLD | NEW |