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 <array> |
| 8 |
7 #include "base/debug/activity_tracker.h" | 9 #include "base/debug/activity_tracker.h" |
8 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
9 #include "base/pending_task.h" | 11 #include "base/pending_task.h" |
10 #include "base/trace_event/trace_event.h" | 12 #include "base/trace_event/trace_event.h" |
11 #include "base/tracked_objects.h" | 13 #include "base/tracked_objects.h" |
12 | 14 |
13 namespace base { | 15 namespace base { |
14 namespace debug { | 16 namespace debug { |
15 | 17 |
16 TaskAnnotator::TaskAnnotator() { | 18 TaskAnnotator::TaskAnnotator() { |
(...skipping 17 matching lines...) Expand all Loading... |
34 tracked_objects::TaskStopwatch stopwatch; | 36 tracked_objects::TaskStopwatch stopwatch; |
35 stopwatch.Start(); | 37 stopwatch.Start(); |
36 tracked_objects::Duration queue_duration = | 38 tracked_objects::Duration queue_duration = |
37 stopwatch.StartTime() - pending_task->EffectiveTimePosted(); | 39 stopwatch.StartTime() - pending_task->EffectiveTimePosted(); |
38 | 40 |
39 TRACE_EVENT_WITH_FLOW1( | 41 TRACE_EVENT_WITH_FLOW1( |
40 TRACE_DISABLED_BY_DEFAULT("toplevel.flow"), queue_function, | 42 TRACE_DISABLED_BY_DEFAULT("toplevel.flow"), queue_function, |
41 TRACE_ID_MANGLE(GetTaskTraceID(*pending_task)), TRACE_EVENT_FLAG_FLOW_IN, | 43 TRACE_ID_MANGLE(GetTaskTraceID(*pending_task)), TRACE_EVENT_FLAG_FLOW_IN, |
42 "queue_duration", queue_duration.InMilliseconds()); | 44 "queue_duration", queue_duration.InMilliseconds()); |
43 | 45 |
44 // Before running the task, store the program counter where it was posted | 46 // Before running the task, store the task backtrace with the chain of |
45 // and deliberately alias it to ensure it is on the stack if the task | 47 // PostTasks that resulted in this call and deliberately alias it to ensure |
46 // crashes. Be careful not to assume that the variable itself will have the | 48 // it is on the stack if the task crashes. Be careful not to assume that the |
47 // expected value when displayed by the optimizer in an optimized build. | 49 // variable itself will have the expected value when displayed by the |
48 // Look at a memory dump of the stack. | 50 // optimizer in an optimized build. Look at a memory dump of the stack. |
49 const void* program_counter = pending_task->posted_from.program_counter(); | 51 static constexpr int kStackTaskTraceSnapshotSize = |
50 debug::Alias(&program_counter); | 52 std::tuple_size<decltype(pending_task->task_backtrace)>::value + 1; |
| 53 std::array<const void*, kStackTaskTraceSnapshotSize> task_backtrace; |
| 54 task_backtrace[0] = pending_task->posted_from.program_counter(); |
| 55 std::copy(pending_task->task_backtrace.begin(), |
| 56 pending_task->task_backtrace.end(), task_backtrace.begin() + 1); |
| 57 debug::Alias(&task_backtrace); |
51 | 58 |
52 std::move(pending_task->task).Run(); | 59 std::move(pending_task->task).Run(); |
53 | 60 |
54 stopwatch.Stop(); | 61 stopwatch.Stop(); |
55 tracked_objects::ThreadData::TallyRunOnNamedThreadIfTracking(*pending_task, | 62 tracked_objects::ThreadData::TallyRunOnNamedThreadIfTracking(*pending_task, |
56 stopwatch); | 63 stopwatch); |
57 } | 64 } |
58 | 65 |
59 uint64_t TaskAnnotator::GetTaskTraceID(const PendingTask& task) const { | 66 uint64_t TaskAnnotator::GetTaskTraceID(const PendingTask& task) const { |
60 return (static_cast<uint64_t>(task.sequence_num) << 32) | | 67 return (static_cast<uint64_t>(task.sequence_num) << 32) | |
61 ((static_cast<uint64_t>(reinterpret_cast<intptr_t>(this)) << 32) >> | 68 ((static_cast<uint64_t>(reinterpret_cast<intptr_t>(this)) << 32) >> |
62 32); | 69 32); |
63 } | 70 } |
64 | 71 |
65 } // namespace debug | 72 } // namespace debug |
66 } // namespace base | 73 } // namespace base |
OLD | NEW |