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

Side by Side Diff: runtime/vm/malloc_hooks.cc

Issue 2625613002: Implemented basic heap memory allocation tracking in MallocHooks using hooks registered with tcmall… (Closed)
Patch Set: Created 3 years, 11 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 | « runtime/vm/malloc_hooks.h ('k') | runtime/vm/malloc_hooks_unsupported.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #if defined(DART_USE_TCMALLOC) 5 #if defined(DART_USE_TCMALLOC)
6 6
7 #include "vm/malloc_hooks.h" 7 #include "vm/malloc_hooks.h"
8 8
9 #include "platform/assert.h"
10
11 #include "base/spinlock.h"
12 #include "gperftools/malloc_hook.h"
13
9 namespace dart { 14 namespace dart {
10 15
16 // Use a simple spinlock to avoid race conditions while also avoiding use of
17 // malloc/new.
18 static SpinLock malloc_hooks_spinlock_(base::LINKER_INITIALIZED);
19
20 // MallocHooks static member variables.
21 bool MallocHooks::initialized_ = false;
22 LowLevelAlloc::Arena* MallocHooks::malloc_hooks_memory_ = NULL;
23 uintptr_t MallocHooks::heap_allocated_memory_in_bytes_ = 0;
24 AddressMap<uintptr_t>* MallocHooks::allocation_map_ = NULL;
25
26
11 void MallocHooks::Init() { 27 void MallocHooks::Init() {
12 // TODO(bkonyi): Implement 28 SpinLockHolder l(&malloc_hooks_spinlock_);
29
30 if (initialized_) {
31 return;
32 }
33 initialized_ = true;
34
35 // Allocate any memory needed for the MallocHooks datastructures
36 // before we connect the hooks to avoid dependencies on malloc/free.
37 malloc_hooks_memory_ =
38 LowLevelAlloc::NewArena(0, LowLevelAlloc::DefaultArena());
39 allocation_map_ =
40 new AddressMap<uintptr_t>(MallocHooksMalloc, MallocHooksFree);
41
42 heap_allocated_memory_in_bytes_ = 0;
43
44 // Register malloc hooks.
45 ASSERT(MallocHook::AddNewHook(&RecordAllocHook));
46 ASSERT(MallocHook::AddDeleteHook(&RecordFreeHook));
47 }
48
49
50 void MallocHooks::TearDown() {
51 SpinLockHolder l(&malloc_hooks_spinlock_);
52 ASSERT(MallocHook::RemoveNewHook(&RecordAllocHook));
53 ASSERT(MallocHook::RemoveDeleteHook(&RecordFreeHook));
54 initialized_ = false;
55 }
56
57
58 void MallocHooks::RecordAllocHook(const void* ptr, size_t size) {
59 SpinLockHolder l(&malloc_hooks_spinlock_);
60 if (ptr != NULL) {
61 heap_allocated_memory_in_bytes_ += size;
62 allocation_map_->Insert(ptr, size);
63 }
64 }
65
66
67 void MallocHooks::RecordFreeHook(const void* ptr) {
68 SpinLockHolder l(&malloc_hooks_spinlock_);
69 if (ptr != NULL) {
70 uintptr_t value = 0;
71 ASSERT(allocation_map_->FindAndRemove(ptr, &value));
72 ASSERT(heap_allocated_memory_in_bytes_ >= value);
73 heap_allocated_memory_in_bytes_ -= value;
74 }
13 } 75 }
14 76
15 } // namespace dart 77 } // namespace dart
16 78
17 #endif // defined(DART_USE_TCMALLOC) 79 #endif // defined(DART_USE_TCMALLOC)
OLDNEW
« no previous file with comments | « runtime/vm/malloc_hooks.h ('k') | runtime/vm/malloc_hooks_unsupported.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698