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

Side by Side Diff: base/debug/scoped_thread_heap_usage.cc

Issue 2163783003: Implement a ScopedThreadHeapUsage class to allow profiling per-thread heap usage. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@shim-default
Patch Set: Change Init implementation to return a bool status. ::Init is now also tested when the shim is disa… Created 4 years, 3 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
« no previous file with comments | « base/debug/scoped_thread_heap_usage.h ('k') | base/debug/scoped_thread_heap_usage_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/debug/scoped_thread_heap_usage.h"
6
7 #include <stdint.h>
8 #include <algorithm>
9
10 #include "base/allocator/allocator_shim.h"
11 #include "base/allocator/features.h"
12 #include "base/threading/thread_local_storage.h"
13 #include "build/build_config.h"
14
15 #if defined(OS_MACOSX) || defined(OS_IOS)
16 #include <malloc/malloc.h>
17 #else
18 #include <malloc.h>
19 #endif
20
21 namespace base {
22 namespace debug {
23
24 namespace {
25
26 using base::allocator::AllocatorDispatch;
27
28 ThreadLocalStorage::StaticSlot g_thread_allocator_usage = TLS_INITIALIZER;
29
30 ScopedThreadHeapUsage::ThreadAllocatorUsage* const kInitializingSentinel =
31 reinterpret_cast<ScopedThreadHeapUsage::ThreadAllocatorUsage*>(-1);
32
33 // Forward declared as it needs to delegate memory allocation to the next
34 // lower shim.
35 ScopedThreadHeapUsage::ThreadAllocatorUsage* GetOrCreateThreadUsage();
36
37 size_t GetAllocSizeEstimate(const AllocatorDispatch* next, void* ptr) {
38 if (ptr == nullptr || !next->get_size_estimate_function)
39 return 0U;
40
41 return next->get_size_estimate_function(next, ptr);
42 }
43
44 void RecordAlloc(const AllocatorDispatch* next, void* ptr, size_t size) {
45 ScopedThreadHeapUsage::ThreadAllocatorUsage* usage = GetOrCreateThreadUsage();
46 if (usage == nullptr)
47 return;
48
49 usage->alloc_ops++;
50 size_t estimate = GetAllocSizeEstimate(next, ptr);
51 if (size && estimate) {
52 usage->alloc_bytes += estimate;
53 usage->alloc_overhead_bytes += estimate - size;
54
55 // Only keep track of the net number of bytes allocated in the scope if the
56 // size estimate function returns sane values, e.g. non-zero.
57 uint64_t allocated_bytes = usage->alloc_bytes - usage->free_bytes;
58 if (allocated_bytes > usage->max_allocated_bytes)
59 usage->max_allocated_bytes = allocated_bytes;
60 } else {
61 usage->alloc_bytes += size;
62 }
63 }
64
65 void RecordFree(const AllocatorDispatch* next, void* ptr) {
66 ScopedThreadHeapUsage::ThreadAllocatorUsage* usage = GetOrCreateThreadUsage();
67 if (usage == nullptr)
68 return;
69
70 size_t estimate = GetAllocSizeEstimate(next, ptr);
71 usage->free_ops++;
72 usage->free_bytes += estimate;
73 }
74
75 void* AllocFn(const AllocatorDispatch* self, size_t size) {
76 void* ret = self->next->alloc_function(self->next, size);
77 if (ret != nullptr)
78 RecordAlloc(self->next, ret, size);
79
80 return ret;
81 }
82
83 void* AllocZeroInitializedFn(const AllocatorDispatch* self,
84 size_t n,
85 size_t size) {
86 void* ret = self->next->alloc_zero_initialized_function(self->next, n, size);
87 if (ret != nullptr)
88 RecordAlloc(self->next, ret, size);
89
90 return ret;
91 }
92
93 void* AllocAlignedFn(const AllocatorDispatch* self,
94 size_t alignment,
95 size_t size) {
96 void* ret = self->next->alloc_aligned_function(self->next, alignment, size);
97 if (ret != nullptr)
98 RecordAlloc(self->next, ret, size);
99
100 return ret;
101 }
102
103 void* ReallocFn(const AllocatorDispatch* self, void* address, size_t size) {
104 if (address != nullptr)
105 RecordFree(self->next, address);
106
107 void* ret = self->next->realloc_function(self->next, address, size);
108 if (ret != nullptr && size != 0)
109 RecordAlloc(self->next, ret, size);
110
111 return ret;
112 }
113
114 void FreeFn(const AllocatorDispatch* self, void* address) {
115 if (address != nullptr)
116 RecordFree(self->next, address);
117 self->next->free_function(self->next, address);
118 }
119
120 size_t GetSizeEstimateFn(const AllocatorDispatch* self, void* address) {
121 return self->next->get_size_estimate_function(self->next, address);
122 }
123
124 // The dispatch for the intercept used.
125 AllocatorDispatch allocator_dispatch = {
126 &AllocFn, &AllocZeroInitializedFn, &AllocAlignedFn, &ReallocFn,
127 &FreeFn, &GetSizeEstimateFn, nullptr};
128
129 ScopedThreadHeapUsage::ThreadAllocatorUsage* GetOrCreateThreadUsage() {
130 ScopedThreadHeapUsage::ThreadAllocatorUsage* allocator_usage =
131 static_cast<ScopedThreadHeapUsage::ThreadAllocatorUsage*>(
132 g_thread_allocator_usage.Get());
133 if (allocator_usage == kInitializingSentinel)
134 return nullptr; // Re-entrancy case.
135
136 if (!allocator_usage) {
137 // Prevent reentrancy due to the allocation below.
138 g_thread_allocator_usage.Set(kInitializingSentinel);
139
140 allocator_usage = new ScopedThreadHeapUsage::ThreadAllocatorUsage;
141 memset(allocator_usage, 0, sizeof(*allocator_usage));
142 g_thread_allocator_usage.Set(allocator_usage);
143 }
144
145 return allocator_usage;
146 }
147
148 } // namespace
149
150 ScopedThreadHeapUsage::ScopedThreadHeapUsage() {
151 ThreadAllocatorUsage* usage = GetOrCreateThreadUsage();
152 usage_at_creation_ = *usage;
153
154 // Reset the stats for our current scope.
155 // The per-thread usage instance now tracks this scope's usage, while this
156 // instance persists the outer scope's usage stats. On destruction, this
157 // instance will restore the outer scope's usage stats with this scopes usage
158 // added.
159 memset(usage, 0, sizeof(*usage));
Primiano Tucci (use gerrit) 2016/09/06 19:33:12 maybe add also a static_assert(is_trivially_const
Sigurður Ásgeirsson 2016/09/07 15:49:26 Good idea. I added std::is_pod, which should do th
160 }
161
162 ScopedThreadHeapUsage::~ScopedThreadHeapUsage() {
163 DCHECK(thread_checker_.CalledOnValidThread());
164
165 ThreadAllocatorUsage* usage = GetOrCreateThreadUsage();
166
167 // Update the outer max.
168 if (usage->max_allocated_bytes) {
169 uint64_t outer_net_alloc_bytes =
170 usage_at_creation_.alloc_bytes - usage_at_creation_.free_bytes;
171
172 usage->max_allocated_bytes =
173 std::max(usage_at_creation_.max_allocated_bytes,
174 outer_net_alloc_bytes + usage->max_allocated_bytes);
175 }
176
177 usage->alloc_ops += usage_at_creation_.alloc_ops;
178 usage->alloc_bytes += usage_at_creation_.alloc_bytes;
179 usage->alloc_overhead_bytes += usage_at_creation_.alloc_overhead_bytes;
180 usage->free_ops += usage_at_creation_.free_ops;
181 usage->free_bytes += usage_at_creation_.free_bytes;
182 }
183
184 ScopedThreadHeapUsage::ThreadAllocatorUsage ScopedThreadHeapUsage::Now() {
185 ThreadAllocatorUsage* usage = GetOrCreateThreadUsage();
186 return *usage;
187 }
188
189 bool ScopedThreadHeapUsage::Initialize() {
190 EnsureTLSInitialized();
Primiano Tucci (use gerrit) 2016/09/06 19:33:12 you can probaby add a bool g_initialized with a CH
Sigurður Ásgeirsson 2016/09/07 15:49:26 Done.
191
192 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM)
193 InsertAllocatorDispatch(&allocator_dispatch);
194 return true;
195 #else
196 return false;
197 #endif // BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM)
198 }
199
200 void ScopedThreadHeapUsage::EnsureTLSInitialized() {
201 if (!g_thread_allocator_usage.initialized()) {
202 g_thread_allocator_usage.Initialize([](void* allocator_usage) {
203 delete static_cast<ScopedThreadHeapUsage::ThreadAllocatorUsage*>(
204 allocator_usage);
205 });
206 }
207 }
208
209 void ScopedThreadHeapUsage::TearDownForTesting() {
210 #if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM)
211 RemoveAllocatorDispatchForTesting(&allocator_dispatch);
212 #endif // BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM)
213 }
214
215 base::allocator::AllocatorDispatch*
216 ScopedThreadHeapUsage::GetDispatchForTesting() {
217 return &allocator_dispatch;
218 }
219
220 void ScopedThreadHeapUsage::EnsureTLSInitializedForTesting() {
221 EnsureTLSInitialized();
222 }
223
224 } // namespace debug
225 } // namespace base
OLDNEW
« no previous file with comments | « base/debug/scoped_thread_heap_usage.h ('k') | base/debug/scoped_thread_heap_usage_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698