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

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

Issue 2680213002: Updated MallocHooks to collect stack traces when memory is allocated. (Closed)
Patch Set: Rough patch: addressed initial comments + converted sample filters from using Isolates to Dart_Port… Created 3 years, 10 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 (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 <execinfo.h> TODO(bkonyi) remove before final patch.
zra 2017/02/08 18:14:14 If this is always going to be on Linux, I'd be fin
bkonyi 2017/02/09 21:22:46 This should basically always be on Linux since the
10
9 #include "platform/assert.h" 11 #include "platform/assert.h"
10 #include "vm/class_finalizer.h" 12 #include "vm/class_finalizer.h"
11 #include "vm/globals.h" 13 #include "vm/globals.h"
12 #include "vm/malloc_hooks.h" 14 #include "vm/malloc_hooks.h"
15 #include "vm/native_symbol.h"
16 #include "vm/profiler.h"
13 #include "vm/symbols.h" 17 #include "vm/symbols.h"
14 #include "vm/unit_test.h" 18 #include "vm/unit_test.h"
15 19
16 namespace dart { 20 namespace dart {
17 21
18 static void MallocHookTestBufferInitializer(volatile char* buffer, 22 static void MallocHookTestBufferInitializer(volatile char* buffer,
19 uintptr_t size) { 23 uintptr_t size) {
20 // Run through the buffer and do something. If we don't do this and the memory 24 // Run through the buffer and do something. If we don't do this and the memory
21 // in buffer isn't touched, the tcmalloc hooks won't be called. 25 // in buffer isn't touched, the tcmalloc hooks won't be called.
22 for (uintptr_t i = 0; i < size; ++i) { 26 for (uintptr_t i = 0; i < size; ++i) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), 72 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size),
69 MallocHooks::heap_allocated_memory_in_bytes()); 73 MallocHooks::heap_allocated_memory_in_bytes());
70 74
71 75
72 delete[] buffer; 76 delete[] buffer;
73 EXPECT_EQ(0L, MallocHooks::allocation_count()); 77 EXPECT_EQ(0L, MallocHooks::allocation_count());
74 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); 78 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes());
75 MallocHooks::TearDown(); 79 MallocHooks::TearDown();
76 } 80 }
77 81
82 /* TODO(bkonyi) Remove before final patch.
83 // Helper used to add another frame to the stack before allocating.
84 char* AllocationHelper(void* stack_trace[32]) {
85 backtrace(stack_trace, 32);
86 return static_cast<char*>(malloc(16 * sizeof(char)));
87 }
88
89 VM_UNIT_TEST_CASE(StackTraceMallocHookTest) {
90 MallocHooks::ResetStats();
91 ASSERT(OSThread::DoesCurrentThreadExist());
92
93 const intptr_t first_frame_count = 6;
94 void* stack_trace[32];
95
96 // Perform the memory allocation and grab the stack trace created by
97 // backtrace() for comparison purposes.
98 char* var = AllocationHelper(stack_trace);
99
100 // Update top pc of the stack trace to match the location that malloc was
101 // called in AllocationHelper. The rest of the pcs should match without any
102 // changes.
103 stack_trace[0] =
104 reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(stack_trace[0]) + 15);
105
106 Sample* sample = MallocHooks::GetSample(var);
107 ASSERT(sample != NULL);
108
109 intptr_t frame_count = 0;
110 while (sample != NULL) {
111 for (uintptr_t i = 0; i < 8; ++i) {
112 // Cease comparisons after we pass the frame for main.
113 if (sample->At(i) == 0) {
114 break;
115 }
116
117 // The first 6 frames in the stack trace we generate are all from the
118 // memory allocation. The frame for AllocationHelper is the 7th element,
119 // so we skip checking all the frames before that.
120 if (frame_count >= first_frame_count) {
121 EXPECT_EQ(reinterpret_cast<uword>(
122 stack_trace[frame_count - first_frame_count]),
123 sample->At(i));
124 }
125 ++frame_count;
126 }
127
128 if (!sample->is_continuation_sample()) {
129 break;
130 }
131 sample = Profiler::sample_buffer()->At(sample->continuation_index());
132 }
133 }*/
134
78 }; // namespace dart 135 }; // namespace dart
79 136
80 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) 137 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698