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