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

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: Added tests and modified stack walker to allow for skipping an arbitrary number of frames before co… 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 #if defined(TARGET_OS_LINUX)
10 #include <execinfo.h>
11 #endif // defined(TARGET_OS_LINUX)
12
9 #include "platform/assert.h" 13 #include "platform/assert.h"
10 #include "vm/class_finalizer.h"
11 #include "vm/globals.h" 14 #include "vm/globals.h"
12 #include "vm/malloc_hooks.h" 15 #include "vm/malloc_hooks.h"
13 #include "vm/symbols.h" 16 #include "vm/profiler.h"
17 #include "vm/profiler_service.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) {
23 buffer[i] = i; 27 buffer[i] = i;
(...skipping 25 matching lines...) Expand all
49 MallocHooks::InitOnce(); 53 MallocHooks::InitOnce();
50 const intptr_t pre_hook_buffer_size = 3; 54 const intptr_t pre_hook_buffer_size = 3;
51 char* pre_hook_buffer = new char[pre_hook_buffer_size]; 55 char* pre_hook_buffer = new char[pre_hook_buffer_size];
52 MallocHookTestBufferInitializer(pre_hook_buffer, pre_hook_buffer_size); 56 MallocHookTestBufferInitializer(pre_hook_buffer, pre_hook_buffer_size);
53 57
54 MallocHooks::ResetStats(); 58 MallocHooks::ResetStats();
55 EXPECT_EQ(0L, MallocHooks::allocation_count()); 59 EXPECT_EQ(0L, MallocHooks::allocation_count());
56 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); 60 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes());
57 61
58 const intptr_t buffer_size = 10; 62 const intptr_t buffer_size = 10;
59 volatile char* buffer = new char[buffer_size]; 63 char* buffer = new char[buffer_size];
60 MallocHookTestBufferInitializer(buffer, buffer_size); 64 MallocHookTestBufferInitializer(buffer, buffer_size);
61 65
62 EXPECT_EQ(1L, MallocHooks::allocation_count()); 66 EXPECT_EQ(1L, MallocHooks::allocation_count());
63 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), 67 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size),
64 MallocHooks::heap_allocated_memory_in_bytes()); 68 MallocHooks::heap_allocated_memory_in_bytes());
65 69
66 delete[] pre_hook_buffer; 70 delete[] pre_hook_buffer;
67 EXPECT_EQ(1L, MallocHooks::allocation_count()); 71 EXPECT_EQ(1L, MallocHooks::allocation_count());
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
83 VM_UNIT_TEST_CASE(StackTraceMallocHookSimpleTest) {
84 MallocHooks::ResetStats();
85 ASSERT(MallocHooks::ProfilingEnabled());
86
87 char* var = static_cast<char*>(malloc(16 * sizeof(char)));
88 Sample* sample = MallocHooks::GetSample(var);
89 EXPECT(sample != NULL);
90
91 free(var);
92 sample = MallocHooks::GetSample(var);
93 EXPECT(sample == NULL);
94 }
95
96
97 ISOLATE_UNIT_TEST_CASE(StackTraceMallocHookSimpleJSONTest) {
98 MallocHooks::ResetStats();
99 ASSERT(MallocHooks::ProfilingEnabled());
100 ClearProfileVisitor cpv(Isolate::Current());
101 Profiler::sample_buffer()->VisitSamples(&cpv);
102
103 char* var = static_cast<char*>(malloc(16 * sizeof(char)));
104 JSONStream js;
105 ProfilerService::PrintNativeAllocationJSON(&js, Profile::kNoTags, -1, -1);
106 const char* json = js.ToCString();
107
108 // Check that all the stack frames from the current down to main are actually
109 // present in the profile. This is just a simple sanity check to make sure
110 // that the ProfileTrie has a representation of the stack trace collected when
111 // var is allocated. More intense testing is already done in profiler_test.cc.
112 EXPECT_SUBSTRING("\"dart::Dart_TestStackTraceMallocHookSimpleJSONTest()\"",
113 json);
114 EXPECT_SUBSTRING("\"dart::TestCase::Run()\"", json);
115 EXPECT_SUBSTRING("\"dart::TestCaseBase::RunTest()\"", json);
116 EXPECT_SUBSTRING("\"main\"", json);
117
118 free(var);
119 }
120
121
122 #if defined(TARGET_OS_LINUX)
Cutch 2017/02/15 08:21:00 Not sure if this OS_LINUX test is a good idea
bkonyi 2017/02/16 00:28:28 After reading the comments below, I agree with you
123 // Helper used to add another frame to the stack before allocating.
124 char* AllocationHelper(void* stack_trace[32]) {
125 backtrace(stack_trace, 32);
126 return static_cast<char*>(malloc(16 * sizeof(char)));
127 }
128
129
130 VM_UNIT_TEST_CASE(StackTraceMallocHookTest) {
131 MallocHooks::ResetStats();
132 ASSERT(MallocHooks::ProfilingEnabled());
133
134 void* stack_trace[32];
135
136 // Perform the memory allocation and grab the stack trace created by
137 // backtrace() for comparison purposes.
138 char* var = AllocationHelper(stack_trace);
139
140 // Update top pc of the stack trace to match the location that malloc was
141 // called in AllocationHelper. The rest of the pcs should match without any
142 // changes.
143 stack_trace[0] =
144 reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(stack_trace[0]) + 10);
Cutch 2017/02/15 08:21:00 this won't be the same across MIPS/ARM/ARM64/etc
145
146 Sample* sample = MallocHooks::GetSample(var);
147 ASSERT(sample != NULL);
148
149 while (sample != NULL) {
150 for (uintptr_t i = 0; i < 8; ++i) {
Cutch 2017/02/15 08:21:00 Where did 8 come from?
151 // Cease comparisons after we pass the frame for main.
152 if (sample->At(i) == 0) {
153 break;
154 }
155 EXPECT_EQ(reinterpret_cast<uword>(stack_trace[i]), sample->At(i));
156 }
157
158 if (!sample->is_continuation_sample()) {
159 break;
160 }
161 sample = Profiler::sample_buffer()->At(sample->continuation_index());
162 }
163 free(var);
164 }
165 #endif // defined(TARGET_OS_LINUX)
166
78 }; // namespace dart 167 }; // namespace dart
79 168
80 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) 169 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698