| 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/trace_event/trace_event_impl.h" |
| 22 #include "base/values.h" |
| 19 | 23 |
| 20 namespace base { | 24 namespace base { |
| 21 namespace internal { | 25 namespace internal { |
| 22 | 26 |
| 23 namespace { | 27 namespace { |
| 24 | 28 |
| 29 // An immutable copy of a scheduler task's info required by tracing. |
| 30 class TaskTracingInfo : public trace_event::ConvertableToTraceFormat { |
| 31 public: |
| 32 TaskTracingInfo(const TaskTraits& task_traits, |
| 33 ExecutionMode execution_mode, |
| 34 const SequenceToken& sequence_token) |
| 35 : task_traits_(task_traits), |
| 36 execution_mode_(execution_mode), |
| 37 sequence_token_(sequence_token) {} |
| 38 |
| 39 // Overridden from trace_event::ConvertableToTraceFormat: |
| 40 void AppendAsTraceFormat(std::string* out) const override; |
| 41 |
| 42 private: |
| 43 const TaskTraits task_traits_; |
| 44 const ExecutionMode execution_mode_; |
| 45 const SequenceToken sequence_token_; |
| 46 |
| 47 DISALLOW_COPY_AND_ASSIGN(TaskTracingInfo); |
| 48 }; |
| 49 |
| 50 void TaskTracingInfo::AppendAsTraceFormat(std::string* out) const { |
| 51 DictionaryValue dict; |
| 52 |
| 53 bool task_scheduler_tracing_enabled = false; |
| 54 TRACE_EVENT_CATEGORY_GROUP_ENABLED( |
| 55 TRACE_DISABLED_BY_DEFAULT("taskscheduler"), |
| 56 &task_scheduler_tracing_enabled); |
| 57 if (task_scheduler_tracing_enabled) { |
| 58 const char* priority_str = nullptr; |
| 59 switch (task_traits_.priority()) { |
| 60 case TaskPriority::BACKGROUND: |
| 61 priority_str = "BACKGROUND"; |
| 62 break; |
| 63 case TaskPriority::USER_VISIBLE: |
| 64 priority_str = "USER_VISIBLE"; |
| 65 break; |
| 66 case TaskPriority::USER_BLOCKING: |
| 67 priority_str = "USER_BLOCKING"; |
| 68 break; |
| 69 } |
| 70 dict.SetString("TaskPriority", priority_str); |
| 71 |
| 72 const char* execution_mode_str = nullptr; |
| 73 switch (execution_mode_) { |
| 74 case ExecutionMode::PARALLEL: |
| 75 execution_mode_str = "PARALLEL"; |
| 76 break; |
| 77 case ExecutionMode::SEQUENCED: |
| 78 execution_mode_str = "SEQUENCED"; |
| 79 break; |
| 80 case ExecutionMode::SINGLE_THREADED: |
| 81 execution_mode_str = "SINGLE_THREADED"; |
| 82 break; |
| 83 } |
| 84 dict.SetString("ExecutionMode", execution_mode_str); |
| 85 |
| 86 if (execution_mode_ != ExecutionMode::PARALLEL) |
| 87 dict.SetString("SequenceToken", sequence_token_.ToString()); |
| 88 } |
| 89 |
| 90 // Even when the taskscheduler tracing category is disabled, a valid (empty) |
| 91 // JSON object still needs to be emitted. |
| 92 std::string tmp; |
| 93 JSONWriter::Write(dict, &tmp); |
| 94 out->append(tmp); |
| 95 } |
| 96 |
| 25 const char kQueueFunctionName[] = "base::PostTask"; | 97 const char kQueueFunctionName[] = "base::PostTask"; |
| 26 | 98 |
| 27 // This name conveys that a Task is run by the task scheduler without revealing | 99 // This name conveys that a Task is run by the task scheduler without revealing |
| 28 // its implementation details. | 100 // its implementation details. |
| 29 const char kRunFunctionName[] = "TaskSchedulerRunTask"; | 101 const char kRunFunctionName[] = "TaskSchedulerRunTask"; |
| 30 | 102 |
| 31 // Upper bound for the | 103 // Upper bound for the |
| 32 // TaskScheduler.BlockShutdownTasksPostedDuringShutdown histogram. | 104 // TaskScheduler.BlockShutdownTasksPostedDuringShutdown histogram. |
| 33 const HistogramBase::Sample kMaxBlockShutdownTasksPostedDuringShutdown = 1000; | 105 const HistogramBase::Sample kMaxBlockShutdownTasksPostedDuringShutdown = 1000; |
| 34 | 106 |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 DCHECK(!task->sequenced_task_runner_ref || | 266 DCHECK(!task->sequenced_task_runner_ref || |
| 195 !task->single_thread_task_runner_ref); | 267 !task->single_thread_task_runner_ref); |
| 196 if (task->sequenced_task_runner_ref) { | 268 if (task->sequenced_task_runner_ref) { |
| 197 sequenced_task_runner_handle.reset( | 269 sequenced_task_runner_handle.reset( |
| 198 new SequencedTaskRunnerHandle(task->sequenced_task_runner_ref)); | 270 new SequencedTaskRunnerHandle(task->sequenced_task_runner_ref)); |
| 199 } else if (task->single_thread_task_runner_ref) { | 271 } else if (task->single_thread_task_runner_ref) { |
| 200 single_thread_task_runner_handle.reset( | 272 single_thread_task_runner_handle.reset( |
| 201 new ThreadTaskRunnerHandle(task->single_thread_task_runner_ref)); | 273 new ThreadTaskRunnerHandle(task->single_thread_task_runner_ref)); |
| 202 } | 274 } |
| 203 | 275 |
| 204 TRACE_TASK_EXECUTION(kRunFunctionName, *task); | 276 ExecutionMode execution_mode = |
| 277 task->single_thread_task_runner_ref |
| 278 ? ExecutionMode::SINGLE_THREADED |
| 279 : (task->sequenced_task_runner_ref ? ExecutionMode::SEQUENCED |
| 280 : ExecutionMode::PARALLEL); |
| 281 |
| 282 TRACE_TASK_EXECUTION1(kRunFunctionName, *task, "TaskSchedulerInfo", |
| 283 MakeUnique<TaskTracingInfo>( |
| 284 task->traits, execution_mode, sequence_token)); |
| 205 | 285 |
| 206 debug::TaskAnnotator task_annotator; | 286 debug::TaskAnnotator task_annotator; |
| 207 task_annotator.RunTask(kQueueFunctionName, *task); | 287 task_annotator.RunTask(kQueueFunctionName, *task); |
| 208 } | 288 } |
| 209 | 289 |
| 210 AfterRunTask(shutdown_behavior); | 290 AfterRunTask(shutdown_behavior); |
| 211 } | 291 } |
| 212 | 292 |
| 213 if (task->delayed_run_time.is_null()) | 293 if (task->delayed_run_time.is_null()) |
| 214 DecrementNumPendingUndelayedTasks(); | 294 DecrementNumPendingUndelayedTasks(); |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 subtle::NoBarrier_AtomicIncrement(&num_pending_undelayed_tasks_, -1); | 464 subtle::NoBarrier_AtomicIncrement(&num_pending_undelayed_tasks_, -1); |
| 385 DCHECK_GE(new_num_pending_undelayed_tasks, 0); | 465 DCHECK_GE(new_num_pending_undelayed_tasks, 0); |
| 386 if (new_num_pending_undelayed_tasks == 0) { | 466 if (new_num_pending_undelayed_tasks == 0) { |
| 387 AutoSchedulerLock auto_lock(flush_lock_); | 467 AutoSchedulerLock auto_lock(flush_lock_); |
| 388 flush_cv_->Signal(); | 468 flush_cv_->Signal(); |
| 389 } | 469 } |
| 390 } | 470 } |
| 391 | 471 |
| 392 } // namespace internal | 472 } // namespace internal |
| 393 } // namespace base | 473 } // namespace base |
| OLD | NEW |