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

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

Issue 2989093002: [vm] Don't enable the profiler by default. Enable the profiler at startup with --observe, or later … (Closed)
Patch Set: . Created 3 years, 4 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) && !defined(TARGET_ARCH_DBC) 7 #if defined(DART_USE_TCMALLOC) && !defined(PRODUCT) && !defined(TARGET_ARCH_DBC)
8 8
9 #include "platform/assert.h" 9 #include "platform/assert.h"
10 #include "vm/globals.h" 10 #include "vm/globals.h"
11 #include "vm/malloc_hooks.h" 11 #include "vm/malloc_hooks.h"
12 #include "vm/os.h" 12 #include "vm/os.h"
13 #include "vm/profiler.h" 13 #include "vm/profiler.h"
14 #include "vm/profiler_service.h" 14 #include "vm/profiler_service.h"
15 #include "vm/unit_test.h" 15 #include "vm/unit_test.h"
16 16
17 namespace dart { 17 namespace dart {
18 18
19 static void MallocHookTestBufferInitializer(volatile char* buffer, 19 static void MallocHookTestBufferInitializer(volatile char* buffer,
20 uintptr_t size) { 20 uintptr_t size) {
21 // Run through the buffer and do something. If we don't do this and the memory 21 // Run through the buffer and do something. If we don't do this and the memory
22 // in buffer isn't touched, the tcmalloc hooks won't be called. 22 // in buffer isn't touched, the tcmalloc hooks won't be called.
23 for (uintptr_t i = 0; i < size; ++i) { 23 for (uintptr_t i = 0; i < size; ++i) {
24 buffer[i] = i; 24 buffer[i] = i;
25 } 25 }
26 } 26 }
27 27
28 class EnableMallocHooksScope : public ValueObject {
29 public:
30 EnableMallocHooksScope() {
31 saved_enable_malloc_hooks_ = FLAG_profiler_native_memory;
32 FLAG_profiler_native_memory = true;
33 Profiler::InitAllocationSampleBuffer();
34 MallocHooks::InitOnce();
35 MallocHooks::ResetStats();
36 }
37
38 ~EnableMallocHooksScope() {
39 MallocHooks::TearDown();
40 FLAG_profiler_native_memory = saved_enable_malloc_hooks_;
41 }
42
43 private:
44 bool saved_enable_malloc_hooks_;
45 };
46
47 class EnableMallocHooksAndStacksScope : public EnableMallocHooksScope {
48 public:
49 EnableMallocHooksAndStacksScope() {
50 saved_enable_stack_traces_ = MallocHooks::stack_trace_collection_enabled();
51 MallocHooks::set_stack_trace_collection_enabled(true);
52 Profiler::EnsureEnabled();
53 }
54
55 ~EnableMallocHooksAndStacksScope() {
56 MallocHooks::set_stack_trace_collection_enabled(saved_enable_stack_traces_);
57 }
58
59 private:
60 bool saved_enable_stack_traces_;
61 };
62
28 UNIT_TEST_CASE(BasicMallocHookTest) { 63 UNIT_TEST_CASE(BasicMallocHookTest) {
29 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; 64 EnableMallocHooksScope scope;
30 FLAG_profiler_native_memory = true;
31 Profiler::InitAllocationSampleBuffer();
32 65
33 MallocHooks::InitOnce();
34 MallocHooks::ResetStats();
35 EXPECT_EQ(0L, MallocHooks::allocation_count()); 66 EXPECT_EQ(0L, MallocHooks::allocation_count());
36 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); 67 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes());
37 const intptr_t buffer_size = 10; 68 const intptr_t buffer_size = 10;
38 char* buffer = new char[buffer_size]; 69 char* buffer = new char[buffer_size];
39 MallocHookTestBufferInitializer(buffer, buffer_size); 70 MallocHookTestBufferInitializer(buffer, buffer_size);
40 71
41 EXPECT_EQ(1L, MallocHooks::allocation_count()); 72 EXPECT_EQ(1L, MallocHooks::allocation_count());
42 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), 73 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size),
43 MallocHooks::heap_allocated_memory_in_bytes()); 74 MallocHooks::heap_allocated_memory_in_bytes());
44 75
45 delete[] buffer; 76 delete[] buffer;
46 EXPECT_EQ(0L, MallocHooks::allocation_count()); 77 EXPECT_EQ(0L, MallocHooks::allocation_count());
47 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); 78 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes());
48 MallocHooks::TearDown();
49
50 FLAG_profiler_native_memory = enable_malloc_hooks_saved;
51 } 79 }
52 80
53 UNIT_TEST_CASE(FreeUnseenMemoryMallocHookTest) { 81 UNIT_TEST_CASE(FreeUnseenMemoryMallocHookTest) {
54 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; 82 EnableMallocHooksScope scope;
55 FLAG_profiler_native_memory = true;
56 Profiler::InitAllocationSampleBuffer();
57 83
58 MallocHooks::InitOnce();
59 const intptr_t pre_hook_buffer_size = 3; 84 const intptr_t pre_hook_buffer_size = 3;
60 char* pre_hook_buffer = new char[pre_hook_buffer_size]; 85 char* pre_hook_buffer = new char[pre_hook_buffer_size];
61 MallocHookTestBufferInitializer(pre_hook_buffer, pre_hook_buffer_size); 86 MallocHookTestBufferInitializer(pre_hook_buffer, pre_hook_buffer_size);
62 87
63 MallocHooks::ResetStats(); 88 MallocHooks::ResetStats();
64 EXPECT_EQ(0L, MallocHooks::allocation_count()); 89 EXPECT_EQ(0L, MallocHooks::allocation_count());
65 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); 90 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes());
66 91
67 const intptr_t buffer_size = 10; 92 const intptr_t buffer_size = 10;
68 char* buffer = new char[buffer_size]; 93 char* buffer = new char[buffer_size];
69 MallocHookTestBufferInitializer(buffer, buffer_size); 94 MallocHookTestBufferInitializer(buffer, buffer_size);
70 95
71 EXPECT_EQ(1L, MallocHooks::allocation_count()); 96 EXPECT_EQ(1L, MallocHooks::allocation_count());
72 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), 97 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size),
73 MallocHooks::heap_allocated_memory_in_bytes()); 98 MallocHooks::heap_allocated_memory_in_bytes());
74 99
75 delete[] pre_hook_buffer; 100 delete[] pre_hook_buffer;
76 EXPECT_EQ(1L, MallocHooks::allocation_count()); 101 EXPECT_EQ(1L, MallocHooks::allocation_count());
77 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), 102 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size),
78 MallocHooks::heap_allocated_memory_in_bytes()); 103 MallocHooks::heap_allocated_memory_in_bytes());
79 104
80 delete[] buffer; 105 delete[] buffer;
81 EXPECT_EQ(0L, MallocHooks::allocation_count()); 106 EXPECT_EQ(0L, MallocHooks::allocation_count());
82 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); 107 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes());
83 MallocHooks::TearDown();
84
85 FLAG_profiler_native_memory = enable_malloc_hooks_saved;
86 } 108 }
87 109
88 VM_UNIT_TEST_CASE(StackTraceMallocHookSimpleTest) { 110 VM_UNIT_TEST_CASE(StackTraceMallocHookSimpleTest) {
89 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; 111 EnableMallocHooksAndStacksScope scope;
90 FLAG_profiler_native_memory = true;
91 Profiler::InitAllocationSampleBuffer();
92
93 MallocHooks::InitOnce();
94 MallocHooks::ResetStats();
95
96 bool enable_stack_traces_saved =
97 MallocHooks::stack_trace_collection_enabled();
98 MallocHooks::set_stack_trace_collection_enabled(true);
99 112
100 char* var = static_cast<char*>(malloc(16 * sizeof(char))); 113 char* var = static_cast<char*>(malloc(16 * sizeof(char)));
101 Sample* sample = MallocHooks::GetSample(var); 114 Sample* sample = MallocHooks::GetSample(var);
102 EXPECT(sample != NULL); 115 EXPECT(sample != NULL);
103 116
104 free(var); 117 free(var);
105 sample = MallocHooks::GetSample(var); 118 sample = MallocHooks::GetSample(var);
106 EXPECT(sample == NULL); 119 EXPECT(sample == NULL);
107 MallocHooks::TearDown();
108 MallocHooks::set_stack_trace_collection_enabled(enable_stack_traces_saved);
109 FLAG_profiler_native_memory = enable_malloc_hooks_saved;
110 } 120 }
111 121
112 static char* DART_NOINLINE StackTraceLengthHelper(uintptr_t* end_address) { 122 static char* DART_NOINLINE StackTraceLengthHelper(uintptr_t* end_address) {
113 char* var = static_cast<char*>(malloc(16 * sizeof(char))); 123 char* var = static_cast<char*>(malloc(16 * sizeof(char)));
114 *end_address = OS::GetProgramCounter(); 124 *end_address = OS::GetProgramCounter();
115 return var; 125 return var;
116 } 126 }
117 127
118 VM_UNIT_TEST_CASE(StackTraceMallocHookLengthTest) { 128 VM_UNIT_TEST_CASE(StackTraceMallocHookLengthTest) {
119 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; 129 EnableMallocHooksAndStacksScope scope;
120 FLAG_profiler_native_memory = true;
121 Profiler::InitAllocationSampleBuffer();
122 130
123 uintptr_t test_start_address = 131 uintptr_t test_start_address =
124 reinterpret_cast<uintptr_t>(Dart_TestStackTraceMallocHookLengthTest); 132 reinterpret_cast<uintptr_t>(Dart_TestStackTraceMallocHookLengthTest);
125 uintptr_t helper_start_address = 133 uintptr_t helper_start_address =
126 reinterpret_cast<uintptr_t>(StackTraceLengthHelper); 134 reinterpret_cast<uintptr_t>(StackTraceLengthHelper);
127 uintptr_t helper_end_address = 0; 135 uintptr_t helper_end_address = 0;
128 136
129 MallocHooks::InitOnce();
130 MallocHooks::ResetStats();
131
132 bool enable_stack_traces_saved =
133 MallocHooks::stack_trace_collection_enabled();
134 MallocHooks::set_stack_trace_collection_enabled(true);
135
136 char* var = StackTraceLengthHelper(&helper_end_address); 137 char* var = StackTraceLengthHelper(&helper_end_address);
137 Sample* sample = MallocHooks::GetSample(var); 138 Sample* sample = MallocHooks::GetSample(var);
138 EXPECT(sample != NULL); 139 EXPECT(sample != NULL);
139 uintptr_t test_end_address = OS::GetProgramCounter(); 140 uintptr_t test_end_address = OS::GetProgramCounter();
140 141
141 // Ensure that all stack frames are where we expect them to be in the sample. 142 // Ensure that all stack frames are where we expect them to be in the sample.
142 // If they aren't, the kSkipCount constant in malloc_hooks.cc is likely 143 // If they aren't, the kSkipCount constant in malloc_hooks.cc is likely
143 // incorrect. 144 // incorrect.
144 uword address = sample->At(0); 145 uword address = sample->At(0);
145 bool first_result = 146 bool first_result =
(...skipping 11 matching lines...) Expand all
157 "incorrect for this configuration/platform. This value can be" 158 "incorrect for this configuration/platform. This value can be"
158 " found in malloc_hooks.cc in the AllocationInfo class, and " 159 " found in malloc_hooks.cc in the AllocationInfo class, and "
159 "is stored in the kSkipCount constant.\n"); 160 "is stored in the kSkipCount constant.\n");
160 OS::PrintErr("First result: %d Second Result: %d\n", first_result, 161 OS::PrintErr("First result: %d Second Result: %d\n", first_result,
161 second_result); 162 second_result);
162 OS::PrintErr("Dumping sample stack trace:\n"); 163 OS::PrintErr("Dumping sample stack trace:\n");
163 sample->DumpStackTrace(); 164 sample->DumpStackTrace();
164 } 165 }
165 166
166 free(var); 167 free(var);
167 MallocHooks::TearDown();
168 MallocHooks::set_stack_trace_collection_enabled(enable_stack_traces_saved);
169 FLAG_profiler_native_memory = enable_malloc_hooks_saved;
170 } 168 }
171 169
172 ISOLATE_UNIT_TEST_CASE(StackTraceMallocHookSimpleJSONTest) { 170 ISOLATE_UNIT_TEST_CASE(StackTraceMallocHookSimpleJSONTest) {
173 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; 171 EnableMallocHooksAndStacksScope scope;
174 FLAG_profiler_native_memory = true;
175 Profiler::InitAllocationSampleBuffer();
176
177 MallocHooks::InitOnce();
178 MallocHooks::ResetStats();
179
180 bool enable_stack_traces_saved =
181 MallocHooks::stack_trace_collection_enabled();
182 MallocHooks::set_stack_trace_collection_enabled(true);
183 172
184 ClearProfileVisitor cpv(Isolate::Current()); 173 ClearProfileVisitor cpv(Isolate::Current());
185 Profiler::sample_buffer()->VisitSamples(&cpv); 174 Profiler::sample_buffer()->VisitSamples(&cpv);
186 175
187 char* var = static_cast<char*>(malloc(16 * sizeof(char))); 176 char* var = static_cast<char*>(malloc(16 * sizeof(char)));
188 JSONStream js; 177 JSONStream js;
189 ProfilerService::PrintNativeAllocationJSON(&js, Profile::kNoTags, -1, -1); 178 ProfilerService::PrintNativeAllocationJSON(&js, Profile::kNoTags, -1, -1);
190 const char* json = js.ToCString(); 179 const char* json = js.ToCString();
191 180
192 // Check that all the stack frames from the current down to main are actually 181 // Check that all the stack frames from the current down to main are actually
193 // present in the profile. This is just a simple sanity check to make sure 182 // present in the profile. This is just a simple sanity check to make sure
194 // that the ProfileTrie has a representation of the stack trace collected when 183 // that the ProfileTrie has a representation of the stack trace collected when
195 // var is allocated. More intense testing is already done in profiler_test.cc. 184 // var is allocated. More intense testing is already done in profiler_test.cc.
196 EXPECT_SUBSTRING("\"dart::Dart_TestStackTraceMallocHookSimpleJSONTest()\"", 185 EXPECT_SUBSTRING("\"dart::Dart_TestStackTraceMallocHookSimpleJSONTest()\"",
197 json); 186 json);
198 EXPECT_SUBSTRING("\"dart::TestCase::Run()\"", json); 187 EXPECT_SUBSTRING("\"dart::TestCase::Run()\"", json);
199 EXPECT_SUBSTRING("\"dart::TestCaseBase::RunTest()\"", json); 188 EXPECT_SUBSTRING("\"dart::TestCaseBase::RunTest()\"", json);
200 EXPECT_SUBSTRING("\"main\"", json); 189 EXPECT_SUBSTRING("\"main\"", json);
201 190
202 free(var); 191 free(var);
203 MallocHooks::TearDown();
204 MallocHooks::set_stack_trace_collection_enabled(enable_stack_traces_saved);
205 FLAG_profiler_native_memory = enable_malloc_hooks_saved;
206 } 192 }
207 193
208 }; // namespace dart 194 }; // namespace dart
209 195
210 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) 196 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698