Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Side by Side Diff: base/trace_event/memory_profiler_allocation_context.cc

Issue 1391933004: [Tracing] Add hook to PartitionAlloc for heap profiling (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/memory_profiler_allocation_context.h" 5 #include "base/trace_event/memory_profiler_allocation_context.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/threading/thread_local_storage.h" 9 #include "base/threading/thread_local_storage.h"
10 10
11 namespace base { 11 namespace base {
12 namespace trace_event { 12 namespace trace_event {
13 13
14 subtle::Atomic32 AllocationContextTracker::capture_enabled_ = 0; 14 subtle::Atomic32 AllocationContextTracker::capture_enabled_ = 0;
15 15
16 namespace { 16 namespace {
17 ThreadLocalStorage::StaticSlot g_tls_alloc_ctx_tracker = TLS_INITIALIZER; 17 ThreadLocalStorage::StaticSlot g_tls_alloc_ctx_tracker = TLS_INITIALIZER;
18
19 // Returns a pointer past the end of the fixed-size array |array| of |T| of
20 // length |N|, identical to C++11 |std::end|.
21 template <typename T, int N>
22 T* End(T(&array)[N]) {
23 return array + N;
18 } 24 }
19 25
26 } // namespace
27
20 AllocationStack::AllocationStack() {} 28 AllocationStack::AllocationStack() {}
21 AllocationStack::~AllocationStack() {} 29 AllocationStack::~AllocationStack() {}
22 30
31 AllocationContext AllocationContext::Empty() {
32 AllocationContext context;
33
34 for (StackFrame* i = context.backtrace.frames;
35 i != End(context.backtrace.frames); i++)
Primiano Tucci (use gerrit) 2015/10/12 16:36:45 You can make this simpler, shorter and more readab
Ruud van Asseldonk 2015/10/13 10:42:18 Done.
36 *i = nullptr;
37
38 for (auto* i = context.fields; i != End(context.fields); i++)
Primiano Tucci (use gerrit) 2015/10/12 16:36:45 ditto
Ruud van Asseldonk 2015/10/13 10:42:18 Done.
39 i->first = nullptr;
40
41 return context;
42 }
43
23 // This function is added to the TLS slot to clean up the instance when the 44 // This function is added to the TLS slot to clean up the instance when the
24 // thread exits. 45 // thread exits.
25 void DestructAllocationContextTracker(void* alloc_ctx_tracker) { 46 void DestructAllocationContextTracker(void* alloc_ctx_tracker) {
26 delete static_cast<AllocationContextTracker*>(alloc_ctx_tracker); 47 delete static_cast<AllocationContextTracker*>(alloc_ctx_tracker);
27 } 48 }
28 49
29 AllocationContextTracker* AllocationContextTracker::GetThreadLocalTracker() { 50 AllocationContextTracker* AllocationContextTracker::GetThreadLocalTracker() {
30 auto tracker = 51 auto tracker =
31 static_cast<AllocationContextTracker*>(g_tls_alloc_ctx_tracker.Get()); 52 static_cast<AllocationContextTracker*>(g_tls_alloc_ctx_tracker.Get());
32 53
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 auto tracker = AllocationContextTracker::GetThreadLocalTracker(); 95 auto tracker = AllocationContextTracker::GetThreadLocalTracker();
75 tracker->context_[key] = value; 96 tracker->context_[key] = value;
76 } 97 }
77 98
78 // static 99 // static
79 void AllocationContextTracker::UnsetContextField(const char* key) { 100 void AllocationContextTracker::UnsetContextField(const char* key) {
80 auto tracker = AllocationContextTracker::GetThreadLocalTracker(); 101 auto tracker = AllocationContextTracker::GetThreadLocalTracker();
81 tracker->context_.erase(key); 102 tracker->context_.erase(key);
82 } 103 }
83 104
84 // Returns a pointer past the end of the fixed-size array |array| of |T| of
85 // length |N|, identical to C++11 |std::end|.
86 template <typename T, int N>
87 T* End(T(&array)[N]) {
88 return array + N;
89 }
90
91 // static 105 // static
92 AllocationContext AllocationContextTracker::GetContextSnapshot() { 106 AllocationContext AllocationContextTracker::GetContextSnapshot() {
93 AllocationContextTracker* tracker = GetThreadLocalTracker(); 107 AllocationContextTracker* tracker = GetThreadLocalTracker();
94 AllocationContext ctx; 108 AllocationContext ctx;
95 109
96 // Fill the backtrace. 110 // Fill the backtrace.
97 { 111 {
98 auto src = tracker->pseudo_stack_.bottom(); 112 auto src = tracker->pseudo_stack_.bottom();
99 auto dst = ctx.backtrace.frames; 113 auto dst = ctx.backtrace.frames;
100 auto src_end = tracker->pseudo_stack_.top(); 114 auto src_end = tracker->pseudo_stack_.top();
(...skipping 22 matching lines...) Expand all
123 // If there is room for more, fill the remaining slots with nullptr keys. 137 // If there is room for more, fill the remaining slots with nullptr keys.
124 for (; dst != dst_end; dst++) 138 for (; dst != dst_end; dst++)
125 dst->first = nullptr; 139 dst->first = nullptr;
126 } 140 }
127 141
128 return ctx; 142 return ctx;
129 } 143 }
130 144
131 } // namespace trace_event 145 } // namespace trace_event
132 } // namespace base 146 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698