Chromium Code Reviews| 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 *dst = thread_name_; | |
|
petrcermak
2016/03/29 13:07:46
I think that you should DCHECK that dst < dst_end
ssid
2016/03/29 18:19:11
Done.
| |
| 116 ++dst; | |
| 117 } | |
| 118 | |
| 106 // Copy as much of the bottom of the pseudo stack into the backtrace as | 119 // Copy as much of the bottom of the pseudo stack into the backtrace as |
| 107 // possible. | 120 // possible. |
| 108 for (; src != src_end && dst != dst_end; src++, dst++) | 121 for (; src != src_end && dst != dst_end; src++, dst++) |
| 109 *dst = *src; | 122 *dst = *src; |
| 110 | 123 |
| 111 // If there is room for more, fill the remaining slots with empty frames. | 124 // If there is room for more, fill the remaining slots with empty frames. |
| 112 std::fill(dst, dst_end, nullptr); | 125 std::fill(dst, dst_end, nullptr); |
| 113 } | 126 } |
| 114 | 127 |
| 115 ctx.type_name = nullptr; | 128 ctx.type_name = nullptr; |
| 116 | 129 |
| 117 return ctx; | 130 return ctx; |
| 118 } | 131 } |
| 119 | 132 |
| 120 } // namespace trace_event | 133 } // namespace trace_event |
| 121 } // namespace base | 134 } // namespace base |
| OLD | NEW |