| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/debug/task_annotator.h" | 5 #include "base/debug/task_annotator.h" |
| 6 | 6 |
| 7 #include "base/debug/alias.h" | 7 #include "base/debug/alias.h" |
| 8 #include "base/pending_task.h" | 8 #include "base/pending_task.h" |
| 9 #include "base/trace_event/trace_event.h" | 9 #include "base/trace_event/trace_event.h" |
| 10 #include "base/tracked_objects.h" | 10 #include "base/tracked_objects.h" |
| 11 | 11 |
| 12 namespace base { | 12 namespace base { |
| 13 namespace debug { | 13 namespace debug { |
| 14 | 14 |
| 15 TaskAnnotator::TaskAnnotator() { | 15 TaskAnnotator::TaskAnnotator() { |
| 16 } | 16 } |
| 17 | 17 |
| 18 TaskAnnotator::~TaskAnnotator() { | 18 TaskAnnotator::~TaskAnnotator() { |
| 19 } | 19 } |
| 20 | 20 |
| 21 void TaskAnnotator::DidQueueTask(const char* queue_function, | 21 void TaskAnnotator::DidQueueTask(const char* queue_function, |
| 22 const PendingTask& pending_task) { | 22 const PendingTask& pending_task) { |
| 23 TRACE_EVENT_WITH_FLOW0("toplevel", "TaskAnnotator::DidQueueTask", |
| 24 TRACE_ID_MANGLE(GetTaskTraceID(pending_task)), |
| 25 TRACE_EVENT_FLAG_FLOW_OUT); |
| 23 TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("toplevel.flow"), | 26 TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("toplevel.flow"), |
| 24 queue_function, | 27 queue_function, |
| 25 TRACE_ID_MANGLE(GetTaskTraceID(pending_task))); | 28 TRACE_ID_MANGLE(GetTaskTraceID(pending_task))); |
| 26 } | 29 } |
| 27 | 30 |
| 28 void TaskAnnotator::RunTask(const char* queue_function, | 31 void TaskAnnotator::RunTask(const char* queue_function, |
| 29 const char* run_function, | 32 const char* run_function, |
| 30 const PendingTask& pending_task) { | 33 const PendingTask& pending_task) { |
| 31 tracked_objects::TaskStopwatch stopwatch; | 34 tracked_objects::TaskStopwatch stopwatch; |
| 32 stopwatch.Start(); | 35 stopwatch.Start(); |
| 33 tracked_objects::Duration queue_duration = | 36 tracked_objects::Duration queue_duration = |
| 34 stopwatch.StartTime() - pending_task.EffectiveTimePosted(); | 37 stopwatch.StartTime() - pending_task.EffectiveTimePosted(); |
| 35 | 38 |
| 39 // When tracing memory for posted tasks it's more valuable to attribute the |
| 40 // memory allocations to the source function than generically to the task |
| 41 // runner. |
| 42 TRACE_EVENT_WITH_MEMORY_TAG_WITH_FLOW2( |
| 43 "toplevel", run_function, |
| 44 pending_task.posted_from.function_name(), // Name for memory tracking. |
| 45 TRACE_ID_MANGLE(GetTaskTraceID(pending_task)), TRACE_EVENT_FLAG_FLOW_IN, |
| 46 "src_file", pending_task.posted_from.file_name(), "src_func", |
| 47 pending_task.posted_from.function_name()); |
| 48 |
| 36 TRACE_EVENT_FLOW_END1(TRACE_DISABLED_BY_DEFAULT("toplevel.flow"), | 49 TRACE_EVENT_FLOW_END1(TRACE_DISABLED_BY_DEFAULT("toplevel.flow"), |
| 37 queue_function, | 50 queue_function, |
| 38 TRACE_ID_MANGLE(GetTaskTraceID(pending_task)), | 51 TRACE_ID_MANGLE(GetTaskTraceID(pending_task)), |
| 39 "queue_duration", | 52 "queue_duration", queue_duration.InMilliseconds()); |
| 40 queue_duration.InMilliseconds()); | |
| 41 | |
| 42 // When tracing memory for posted tasks it's more valuable to attribute the | |
| 43 // memory allocations to the source function than generically to the task | |
| 44 // runner. | |
| 45 TRACE_EVENT_WITH_MEMORY_TAG2( | |
| 46 "toplevel", | |
| 47 run_function, | |
| 48 pending_task.posted_from.function_name(), // Name for memory tracking. | |
| 49 "src_file", | |
| 50 pending_task.posted_from.file_name(), | |
| 51 "src_func", | |
| 52 pending_task.posted_from.function_name()); | |
| 53 | 53 |
| 54 // Before running the task, store the program counter where it was posted | 54 // Before running the task, store the program counter where it was posted |
| 55 // and deliberately alias it to ensure it is on the stack if the task | 55 // and deliberately alias it to ensure it is on the stack if the task |
| 56 // crashes. Be careful not to assume that the variable itself will have the | 56 // crashes. Be careful not to assume that the variable itself will have the |
| 57 // expected value when displayed by the optimizer in an optimized build. | 57 // expected value when displayed by the optimizer in an optimized build. |
| 58 // Look at a memory dump of the stack. | 58 // Look at a memory dump of the stack. |
| 59 const void* program_counter = pending_task.posted_from.program_counter(); | 59 const void* program_counter = pending_task.posted_from.program_counter(); |
| 60 debug::Alias(&program_counter); | 60 debug::Alias(&program_counter); |
| 61 | 61 |
| 62 pending_task.task.Run(); | 62 pending_task.task.Run(); |
| 63 | 63 |
| 64 stopwatch.Stop(); | 64 stopwatch.Stop(); |
| 65 tracked_objects::ThreadData::TallyRunOnNamedThreadIfTracking( | 65 tracked_objects::ThreadData::TallyRunOnNamedThreadIfTracking( |
| 66 pending_task, stopwatch); | 66 pending_task, stopwatch); |
| 67 } | 67 } |
| 68 | 68 |
| 69 uint64 TaskAnnotator::GetTaskTraceID(const PendingTask& task) const { | 69 uint64 TaskAnnotator::GetTaskTraceID(const PendingTask& task) const { |
| 70 return (static_cast<uint64>(task.sequence_num) << 32) | | 70 return (static_cast<uint64>(task.sequence_num) << 32) | |
| 71 ((static_cast<uint64>(reinterpret_cast<intptr_t>(this)) << 32) >> 32); | 71 ((static_cast<uint64>(reinterpret_cast<intptr_t>(this)) << 32) >> 32); |
| 72 } | 72 } |
| 73 | 73 |
| 74 } // namespace debug | 74 } // namespace debug |
| 75 } // namespace base | 75 } // namespace base |
| OLD | NEW |