OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 | 6 |
7 #if defined(DART_USE_TCMALLOC) && !defined(PRODUCT) | 7 #if defined(DART_USE_TCMALLOC) && !defined(PRODUCT) |
8 | 8 |
9 #include "vm/malloc_hooks.h" | 9 #include "vm/malloc_hooks.h" |
10 | 10 |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 intptr_t MallocHooksState::original_pid_ = MallocHooksState::kInvalidPid; | 196 intptr_t MallocHooksState::original_pid_ = MallocHooksState::kInvalidPid; |
197 Mutex* MallocHooksState::malloc_hook_mutex_ = new Mutex(); | 197 Mutex* MallocHooksState::malloc_hook_mutex_ = new Mutex(); |
198 | 198 |
199 // Memory allocation state information. | 199 // Memory allocation state information. |
200 intptr_t MallocHooksState::allocation_count_ = 0; | 200 intptr_t MallocHooksState::allocation_count_ = 0; |
201 intptr_t MallocHooksState::heap_allocated_memory_in_bytes_ = 0; | 201 intptr_t MallocHooksState::heap_allocated_memory_in_bytes_ = 0; |
202 AddressMap* MallocHooksState::address_map_ = NULL; | 202 AddressMap* MallocHooksState::address_map_ = NULL; |
203 | 203 |
204 | 204 |
205 void MallocHooks::InitOnce() { | 205 void MallocHooks::InitOnce() { |
| 206 if (!FLAG_enable_malloc_hooks) { |
| 207 return; |
| 208 } |
206 MutexLocker ml(MallocHooksState::malloc_hook_mutex()); | 209 MutexLocker ml(MallocHooksState::malloc_hook_mutex()); |
207 ASSERT(!MallocHooksState::Active()); | 210 ASSERT(!MallocHooksState::Active()); |
208 | 211 |
209 MallocHookScope::InitMallocHookFlag(); | 212 MallocHookScope::InitMallocHookFlag(); |
210 MallocHooksState::Init(); | 213 MallocHooksState::Init(); |
211 | 214 |
212 // Register malloc hooks. | 215 // Register malloc hooks. |
213 bool success = false; | 216 bool success = false; |
214 success = MallocHook::AddNewHook(&MallocHooksState::RecordAllocHook); | 217 success = MallocHook::AddNewHook(&MallocHooksState::RecordAllocHook); |
215 ASSERT(success); | 218 ASSERT(success); |
216 success = MallocHook::AddDeleteHook(&MallocHooksState::RecordFreeHook); | 219 success = MallocHook::AddDeleteHook(&MallocHooksState::RecordFreeHook); |
217 ASSERT(success); | 220 ASSERT(success); |
218 } | 221 } |
219 | 222 |
220 | 223 |
221 void MallocHooks::TearDown() { | 224 void MallocHooks::TearDown() { |
| 225 if (!FLAG_enable_malloc_hooks) { |
| 226 return; |
| 227 } |
222 MutexLocker ml(MallocHooksState::malloc_hook_mutex()); | 228 MutexLocker ml(MallocHooksState::malloc_hook_mutex()); |
223 ASSERT(MallocHooksState::Active()); | 229 ASSERT(MallocHooksState::Active()); |
224 | 230 |
225 // Remove malloc hooks. | 231 // Remove malloc hooks. |
226 bool success = false; | 232 bool success = false; |
227 success = MallocHook::RemoveNewHook(&MallocHooksState::RecordAllocHook); | 233 success = MallocHook::RemoveNewHook(&MallocHooksState::RecordAllocHook); |
228 ASSERT(success); | 234 ASSERT(success); |
229 success = MallocHook::RemoveDeleteHook(&MallocHooksState::RecordFreeHook); | 235 success = MallocHook::RemoveDeleteHook(&MallocHooksState::RecordFreeHook); |
230 ASSERT(success); | 236 ASSERT(success); |
231 | 237 |
232 MallocHooksState::TearDown(); | 238 MallocHooksState::TearDown(); |
233 MallocHookScope::DestroyMallocHookFlag(); | 239 MallocHookScope::DestroyMallocHookFlag(); |
234 } | 240 } |
235 | 241 |
236 | 242 |
237 void MallocHooks::ResetStats() { | 243 void MallocHooks::ResetStats() { |
| 244 if (!FLAG_enable_malloc_hooks) { |
| 245 return; |
| 246 } |
238 MutexLocker ml(MallocHooksState::malloc_hook_mutex()); | 247 MutexLocker ml(MallocHooksState::malloc_hook_mutex()); |
239 if (MallocHooksState::Active()) { | 248 if (MallocHooksState::Active()) { |
240 MallocHooksState::ResetStats(); | 249 MallocHooksState::ResetStats(); |
241 } | 250 } |
242 } | 251 } |
243 | 252 |
244 | 253 |
245 bool MallocHooks::Active() { | 254 bool MallocHooks::Active() { |
| 255 if (!FLAG_enable_malloc_hooks) { |
| 256 return false; |
| 257 } |
246 ASSERT(MallocHooksState::malloc_hook_mutex()->IsOwnedByCurrentThread()); | 258 ASSERT(MallocHooksState::malloc_hook_mutex()->IsOwnedByCurrentThread()); |
247 return MallocHooksState::Active(); | 259 return MallocHooksState::Active(); |
248 } | 260 } |
249 | 261 |
250 | 262 |
251 void MallocHooks::PrintToJSONObject(JSONObject* jsobj) { | 263 void MallocHooks::PrintToJSONObject(JSONObject* jsobj) { |
| 264 if (!FLAG_enable_malloc_hooks) { |
| 265 return; |
| 266 } |
252 intptr_t allocated_memory = 0; | 267 intptr_t allocated_memory = 0; |
253 intptr_t allocation_count = 0; | 268 intptr_t allocation_count = 0; |
254 bool add_usage = false; | 269 bool add_usage = false; |
255 // AddProperty may call malloc which would result in an attempt | 270 // AddProperty may call malloc which would result in an attempt |
256 // to acquire the lock recursively so we extract the values first | 271 // to acquire the lock recursively so we extract the values first |
257 // and then add the JSON properties. | 272 // and then add the JSON properties. |
258 { | 273 { |
259 MutexLocker ml(MallocHooksState::malloc_hook_mutex()); | 274 MutexLocker ml(MallocHooksState::malloc_hook_mutex()); |
260 if (Active()) { | 275 if (Active()) { |
261 allocated_memory = MallocHooksState::heap_allocated_memory_in_bytes(); | 276 allocated_memory = MallocHooksState::heap_allocated_memory_in_bytes(); |
262 allocation_count = MallocHooksState::allocation_count(); | 277 allocation_count = MallocHooksState::allocation_count(); |
263 add_usage = true; | 278 add_usage = true; |
264 } | 279 } |
265 } | 280 } |
266 if (add_usage) { | 281 if (add_usage) { |
267 jsobj->AddProperty("_heapAllocatedMemoryUsage", allocated_memory); | 282 jsobj->AddProperty("_heapAllocatedMemoryUsage", allocated_memory); |
268 jsobj->AddProperty("_heapAllocationCount", allocation_count); | 283 jsobj->AddProperty("_heapAllocationCount", allocation_count); |
269 } | 284 } |
270 } | 285 } |
271 | 286 |
272 | 287 |
273 intptr_t MallocHooks::allocation_count() { | 288 intptr_t MallocHooks::allocation_count() { |
| 289 if (!FLAG_enable_malloc_hooks) { |
| 290 return 0; |
| 291 } |
274 MutexLocker ml(MallocHooksState::malloc_hook_mutex()); | 292 MutexLocker ml(MallocHooksState::malloc_hook_mutex()); |
275 return MallocHooksState::allocation_count(); | 293 return MallocHooksState::allocation_count(); |
276 } | 294 } |
277 | 295 |
278 | 296 |
279 intptr_t MallocHooks::heap_allocated_memory_in_bytes() { | 297 intptr_t MallocHooks::heap_allocated_memory_in_bytes() { |
| 298 if (!FLAG_enable_malloc_hooks) { |
| 299 return 0; |
| 300 } |
280 MutexLocker ml(MallocHooksState::malloc_hook_mutex()); | 301 MutexLocker ml(MallocHooksState::malloc_hook_mutex()); |
281 return MallocHooksState::heap_allocated_memory_in_bytes(); | 302 return MallocHooksState::heap_allocated_memory_in_bytes(); |
282 } | 303 } |
283 | 304 |
284 | 305 |
285 void MallocHooksState::RecordAllocHook(const void* ptr, size_t size) { | 306 void MallocHooksState::RecordAllocHook(const void* ptr, size_t size) { |
286 if (MallocHookScope::IsInHook() || !MallocHooksState::IsOriginalProcess()) { | 307 if (MallocHookScope::IsInHook() || !MallocHooksState::IsOriginalProcess()) { |
287 return; | 308 return; |
288 } | 309 } |
289 | 310 |
(...skipping 24 matching lines...) Expand all Loading... |
314 if (MallocHooksState::address_map()->Lookup(ptr, &size)) { | 335 if (MallocHooksState::address_map()->Lookup(ptr, &size)) { |
315 MallocHooksState::DecrementHeapAllocatedMemoryInBytes(size); | 336 MallocHooksState::DecrementHeapAllocatedMemoryInBytes(size); |
316 MallocHooksState::address_map()->Remove(ptr); | 337 MallocHooksState::address_map()->Remove(ptr); |
317 } | 338 } |
318 } | 339 } |
319 } | 340 } |
320 | 341 |
321 } // namespace dart | 342 } // namespace dart |
322 | 343 |
323 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) | 344 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) |
OLD | NEW |