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_count_(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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 void AllocationContextTracker::PopCurrentTaskContext(const char* context) { | 114 void AllocationContextTracker::PopCurrentTaskContext(const char* context) { |
113 DCHECK_EQ(context, task_contexts_.back()) | 115 DCHECK_EQ(context, task_contexts_.back()) |
114 << "Encountered an unmatched context end"; | 116 << "Encountered an unmatched context end"; |
115 task_contexts_.pop_back(); | 117 task_contexts_.pop_back(); |
116 } | 118 } |
117 | 119 |
118 // static | 120 // static |
119 AllocationContext AllocationContextTracker::GetContextSnapshot() { | 121 AllocationContext AllocationContextTracker::GetContextSnapshot() { |
120 AllocationContext ctx; | 122 AllocationContext ctx; |
121 | 123 |
124 if (ignore_scope_count_) { | |
125 ctx = AllocationContext::Empty(); | |
Primiano Tucci (use gerrit)
2016/04/20 16:34:57
I think either you or dskiba need to rebase on top
ssid
2016/04/21 01:07:46
Yes was waiting for it to land. Done.
| |
126 ctx.backtrace.frames[0] = kTracingOverhead; | |
127 ctx.type_name = kTracingOverhead; | |
128 return ctx; | |
129 } | |
130 | |
122 // Fill the backtrace. | 131 // Fill the backtrace. |
123 { | 132 { |
124 auto src = pseudo_stack_.begin(); | 133 auto src = pseudo_stack_.begin(); |
125 auto dst = std::begin(ctx.backtrace.frames); | 134 auto dst = std::begin(ctx.backtrace.frames); |
126 auto src_end = pseudo_stack_.end(); | 135 auto src_end = pseudo_stack_.end(); |
127 auto dst_end = std::end(ctx.backtrace.frames); | 136 auto dst_end = std::end(ctx.backtrace.frames); |
128 | 137 |
129 // Add the thread name as the first enrty in the backtrace. | 138 // Add the thread name as the first enrty in the backtrace. |
130 if (thread_name_) { | 139 if (thread_name_) { |
131 DCHECK(dst < dst_end); | 140 DCHECK(dst < dst_end); |
(...skipping 12 matching lines...) Expand all Loading... | |
144 | 153 |
145 // TODO(ssid): Fix crbug.com/594803 to add file name as 3rd dimension | 154 // TODO(ssid): Fix crbug.com/594803 to add file name as 3rd dimension |
146 // (component name) in the heap profiler and not piggy back on the type name. | 155 // (component name) in the heap profiler and not piggy back on the type name. |
147 ctx.type_name = task_contexts_.empty() ? nullptr : task_contexts_.back(); | 156 ctx.type_name = task_contexts_.empty() ? nullptr : task_contexts_.back(); |
148 | 157 |
149 return ctx; | 158 return ctx; |
150 } | 159 } |
151 | 160 |
152 } // namespace trace_event | 161 } // namespace trace_event |
153 } // namespace base | 162 } // namespace base |
OLD | NEW |