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" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 42 |
43 if (!tracker) { | 43 if (!tracker) { |
44 g_tls_alloc_ctx_tracker.Set(kInitializingSentinel); | 44 g_tls_alloc_ctx_tracker.Set(kInitializingSentinel); |
45 tracker = new AllocationContextTracker(); | 45 tracker = new AllocationContextTracker(); |
46 g_tls_alloc_ctx_tracker.Set(tracker); | 46 g_tls_alloc_ctx_tracker.Set(tracker); |
47 } | 47 } |
48 | 48 |
49 return tracker; | 49 return tracker; |
50 } | 50 } |
51 | 51 |
52 AllocationContextTracker::AllocationContextTracker() { | 52 AllocationContextTracker::AllocationContextTracker() : thread_name_(nullptr) { |
53 pseudo_stack_.reserve(kMaxStackDepth); | 53 pseudo_stack_.reserve(kMaxStackDepth); |
54 } | 54 } |
55 AllocationContextTracker::~AllocationContextTracker() {} | 55 AllocationContextTracker::~AllocationContextTracker() {} |
56 | 56 |
57 // static | 57 // static |
| 58 void AllocationContextTracker::SetCurrentThreadName(const char* name) { |
| 59 if (name && capture_enabled()) { |
| 60 GetInstanceForCurrentThread()->thread_name_ = name; |
| 61 } |
| 62 } |
| 63 |
| 64 // static |
58 void AllocationContextTracker::SetCaptureEnabled(bool enabled) { | 65 void AllocationContextTracker::SetCaptureEnabled(bool enabled) { |
59 // When enabling capturing, also initialize the TLS slot. This does not create | 66 // When enabling capturing, also initialize the TLS slot. This does not create |
60 // a TLS instance yet. | 67 // a TLS instance yet. |
61 if (enabled && !g_tls_alloc_ctx_tracker.initialized()) | 68 if (enabled && !g_tls_alloc_ctx_tracker.initialized()) |
62 g_tls_alloc_ctx_tracker.Initialize(DestructAllocationContextTracker); | 69 g_tls_alloc_ctx_tracker.Initialize(DestructAllocationContextTracker); |
63 | 70 |
64 // Release ordering ensures that when a thread observes |capture_enabled_| to | 71 // Release ordering ensures that when a thread observes |capture_enabled_| to |
65 // be true through an acquire load, the TLS slot has been initialized. | 72 // be true through an acquire load, the TLS slot has been initialized. |
66 subtle::Release_Store(&capture_enabled_, enabled); | 73 subtle::Release_Store(&capture_enabled_, enabled); |
67 } | 74 } |
(...skipping 28 matching lines...) Expand all Loading... |
96 AllocationContext AllocationContextTracker::GetContextSnapshot() { | 103 AllocationContext AllocationContextTracker::GetContextSnapshot() { |
97 AllocationContext ctx; | 104 AllocationContext ctx; |
98 | 105 |
99 // Fill the backtrace. | 106 // Fill the backtrace. |
100 { | 107 { |
101 auto src = pseudo_stack_.begin(); | 108 auto src = pseudo_stack_.begin(); |
102 auto dst = std::begin(ctx.backtrace.frames); | 109 auto dst = std::begin(ctx.backtrace.frames); |
103 auto src_end = pseudo_stack_.end(); | 110 auto src_end = pseudo_stack_.end(); |
104 auto dst_end = std::end(ctx.backtrace.frames); | 111 auto dst_end = std::end(ctx.backtrace.frames); |
105 | 112 |
| 113 // Add the thread name as the first enrty in the backtrace. |
| 114 if (thread_name_) { |
| 115 DCHECK(dst < dst_end); |
| 116 *dst = thread_name_; |
| 117 ++dst; |
| 118 } |
| 119 |
106 // Copy as much of the bottom of the pseudo stack into the backtrace as | 120 // Copy as much of the bottom of the pseudo stack into the backtrace as |
107 // possible. | 121 // possible. |
108 for (; src != src_end && dst != dst_end; src++, dst++) | 122 for (; src != src_end && dst != dst_end; src++, dst++) |
109 *dst = *src; | 123 *dst = *src; |
110 | 124 |
111 // If there is room for more, fill the remaining slots with empty frames. | 125 // If there is room for more, fill the remaining slots with empty frames. |
112 std::fill(dst, dst_end, nullptr); | 126 std::fill(dst, dst_end, nullptr); |
113 } | 127 } |
114 | 128 |
115 ctx.type_name = nullptr; | 129 ctx.type_name = nullptr; |
116 | 130 |
117 return ctx; | 131 return ctx; |
118 } | 132 } |
119 | 133 |
120 } // namespace trace_event | 134 } // namespace trace_event |
121 } // namespace base | 135 } // namespace base |
OLD | NEW |