OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/trace_event/heap_profiler_allocation_context_tracker.h" | 5 #include "base/trace_event/heap_profiler_allocation_context_tracker.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 | 9 |
10 #include "base/atomicops.h" | 10 #include "base/atomicops.h" |
11 #include "base/threading/thread_local_storage.h" | 11 #include "base/threading/thread_local_storage.h" |
12 #include "base/trace_event/heap_profiler_allocation_context.h" | 12 #include "base/trace_event/heap_profiler_allocation_context.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 namespace trace_event { | 15 namespace trace_event { |
16 | 16 |
17 subtle::Atomic32 AllocationContextTracker::capture_enabled_ = 0; | 17 subtle::Atomic32 AllocationContextTracker::capture_enabled_ = 0; |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 const size_t kMaxStackDepth = 128u; | 21 const size_t kMaxStackDepth = 128u; |
22 const size_t kMaxTaskDepth = 16u; | 22 const size_t kMaxTaskDepth = 16u; |
23 AllocationContextTracker* const kInitializingSentinel = | 23 AllocationContextTracker* const kInitializingSentinel = |
24 reinterpret_cast<AllocationContextTracker*>(-1); | 24 reinterpret_cast<AllocationContextTracker*>(-1); |
| 25 const char kTracingOverhead[] = "tracing_overhead"; |
25 | 26 |
26 ThreadLocalStorage::StaticSlot g_tls_alloc_ctx_tracker = TLS_INITIALIZER; | 27 ThreadLocalStorage::StaticSlot g_tls_alloc_ctx_tracker = TLS_INITIALIZER; |
27 | 28 |
28 // This function is added to the TLS slot to clean up the instance when the | 29 // This function is added to the TLS slot to clean up the instance when the |
29 // thread exits. | 30 // thread exits. |
30 void DestructAllocationContextTracker(void* alloc_ctx_tracker) { | 31 void DestructAllocationContextTracker(void* alloc_ctx_tracker) { |
31 delete static_cast<AllocationContextTracker*>(alloc_ctx_tracker); | 32 delete static_cast<AllocationContextTracker*>(alloc_ctx_tracker); |
32 } | 33 } |
33 | 34 |
34 } // namespace | 35 } // namespace |
35 | 36 |
36 // static | 37 // static |
37 AllocationContextTracker* | 38 AllocationContextTracker* |
38 AllocationContextTracker::GetInstanceForCurrentThread() { | 39 AllocationContextTracker::GetInstanceForCurrentThread() { |
39 AllocationContextTracker* tracker = | 40 AllocationContextTracker* tracker = |
40 static_cast<AllocationContextTracker*>(g_tls_alloc_ctx_tracker.Get()); | 41 static_cast<AllocationContextTracker*>(g_tls_alloc_ctx_tracker.Get()); |
41 if (tracker == kInitializingSentinel) | 42 if (tracker == kInitializingSentinel) |
42 return nullptr; // Re-entrancy case. | 43 return nullptr; // Re-entrancy case. |
43 | 44 |
44 if (!tracker) { | 45 if (!tracker) { |
45 g_tls_alloc_ctx_tracker.Set(kInitializingSentinel); | 46 g_tls_alloc_ctx_tracker.Set(kInitializingSentinel); |
46 tracker = new AllocationContextTracker(); | 47 tracker = new AllocationContextTracker(); |
47 g_tls_alloc_ctx_tracker.Set(tracker); | 48 g_tls_alloc_ctx_tracker.Set(tracker); |
48 } | 49 } |
49 | 50 |
50 return tracker; | 51 return tracker; |
51 } | 52 } |
52 | 53 |
53 AllocationContextTracker::AllocationContextTracker() : thread_name_(nullptr) { | 54 AllocationContextTracker::AllocationContextTracker() |
| 55 : thread_name_(nullptr), ignore_scope_depth_(0) { |
54 pseudo_stack_.reserve(kMaxStackDepth); | 56 pseudo_stack_.reserve(kMaxStackDepth); |
55 task_contexts_.reserve(kMaxTaskDepth); | 57 task_contexts_.reserve(kMaxTaskDepth); |
56 } | 58 } |
57 AllocationContextTracker::~AllocationContextTracker() {} | 59 AllocationContextTracker::~AllocationContextTracker() {} |
58 | 60 |
59 // static | 61 // static |
60 void AllocationContextTracker::SetCurrentThreadName(const char* name) { | 62 void AllocationContextTracker::SetCurrentThreadName(const char* name) { |
61 if (name && capture_enabled()) { | 63 if (name && capture_enabled()) { |
62 GetInstanceForCurrentThread()->thread_name_ = name; | 64 GetInstanceForCurrentThread()->thread_name_ = name; |
63 } | 65 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 void AllocationContextTracker::PopCurrentTaskContext(const char* context) { | 115 void AllocationContextTracker::PopCurrentTaskContext(const char* context) { |
114 DCHECK_EQ(context, task_contexts_.back()) | 116 DCHECK_EQ(context, task_contexts_.back()) |
115 << "Encountered an unmatched context end"; | 117 << "Encountered an unmatched context end"; |
116 task_contexts_.pop_back(); | 118 task_contexts_.pop_back(); |
117 } | 119 } |
118 | 120 |
119 // static | 121 // static |
120 AllocationContext AllocationContextTracker::GetContextSnapshot() { | 122 AllocationContext AllocationContextTracker::GetContextSnapshot() { |
121 AllocationContext ctx; | 123 AllocationContext ctx; |
122 | 124 |
| 125 if (ignore_scope_depth_) { |
| 126 ctx.backtrace.frames[0] = StackFrame::FromTraceEventName(kTracingOverhead); |
| 127 ctx.type_name = kTracingOverhead; |
| 128 ctx.backtrace.frame_count = 1; |
| 129 return ctx; |
| 130 } |
| 131 |
123 // Fill the backtrace. | 132 // Fill the backtrace. |
124 { | 133 { |
125 auto backtrace = std::begin(ctx.backtrace.frames); | 134 auto backtrace = std::begin(ctx.backtrace.frames); |
126 auto backtrace_end = std::end(ctx.backtrace.frames); | 135 auto backtrace_end = std::end(ctx.backtrace.frames); |
127 | 136 |
128 // Add the thread name as the first entry | 137 // Add the thread name as the first entry |
129 if (thread_name_) { | 138 if (thread_name_) { |
130 *backtrace++ = StackFrame::FromThreadName(thread_name_); | 139 *backtrace++ = StackFrame::FromThreadName(thread_name_); |
131 } | 140 } |
132 | 141 |
133 for (const char* event_name: pseudo_stack_) { | 142 for (const char* event_name: pseudo_stack_) { |
134 if (backtrace == backtrace_end) { | 143 if (backtrace == backtrace_end) { |
135 break; | 144 break; |
136 } | 145 } |
137 *backtrace++ = StackFrame::FromTraceEventName(event_name); | 146 *backtrace++ = StackFrame::FromTraceEventName(event_name); |
138 } | 147 } |
139 | 148 |
140 ctx.backtrace.frame_count = backtrace - std::begin(ctx.backtrace.frames); | 149 ctx.backtrace.frame_count = backtrace - std::begin(ctx.backtrace.frames); |
141 } | 150 } |
142 | 151 |
143 // TODO(ssid): Fix crbug.com/594803 to add file name as 3rd dimension | 152 // TODO(ssid): Fix crbug.com/594803 to add file name as 3rd dimension |
144 // (component name) in the heap profiler and not piggy back on the type name. | 153 // (component name) in the heap profiler and not piggy back on the type name. |
145 ctx.type_name = task_contexts_.empty() ? nullptr : task_contexts_.back(); | 154 ctx.type_name = task_contexts_.empty() ? nullptr : task_contexts_.back(); |
146 | 155 |
147 return ctx; | 156 return ctx; |
148 } | 157 } |
149 | 158 |
150 } // namespace trace_event | 159 } // namespace trace_event |
151 } // namespace base | 160 } // namespace base |
OLD | NEW |