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