| 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 | |
| 29 UNIT_TEST_CASE(BasicMallocHookTest) { | 28 UNIT_TEST_CASE(BasicMallocHookTest) { |
| 30 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; | 29 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; |
| 31 FLAG_profiler_native_memory = true; | 30 FLAG_profiler_native_memory = true; |
| 32 Profiler::InitAllocationSampleBuffer(); | 31 Profiler::InitAllocationSampleBuffer(); |
| 33 | 32 |
| 34 MallocHooks::InitOnce(); | 33 MallocHooks::InitOnce(); |
| 35 MallocHooks::ResetStats(); | 34 MallocHooks::ResetStats(); |
| 36 EXPECT_EQ(0L, MallocHooks::allocation_count()); | 35 EXPECT_EQ(0L, MallocHooks::allocation_count()); |
| 37 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); | 36 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); |
| 38 const intptr_t buffer_size = 10; | 37 const intptr_t buffer_size = 10; |
| 39 char* buffer = new char[buffer_size]; | 38 char* buffer = new char[buffer_size]; |
| 40 MallocHookTestBufferInitializer(buffer, buffer_size); | 39 MallocHookTestBufferInitializer(buffer, buffer_size); |
| 41 | 40 |
| 42 EXPECT_EQ(1L, MallocHooks::allocation_count()); | 41 EXPECT_EQ(1L, MallocHooks::allocation_count()); |
| 43 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), | 42 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), |
| 44 MallocHooks::heap_allocated_memory_in_bytes()); | 43 MallocHooks::heap_allocated_memory_in_bytes()); |
| 45 | 44 |
| 46 delete[] buffer; | 45 delete[] buffer; |
| 47 EXPECT_EQ(0L, MallocHooks::allocation_count()); | 46 EXPECT_EQ(0L, MallocHooks::allocation_count()); |
| 48 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); | 47 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); |
| 49 MallocHooks::TearDown(); | 48 MallocHooks::TearDown(); |
| 50 | 49 |
| 51 FLAG_profiler_native_memory = enable_malloc_hooks_saved; | 50 FLAG_profiler_native_memory = enable_malloc_hooks_saved; |
| 52 } | 51 } |
| 53 | 52 |
| 54 | |
| 55 UNIT_TEST_CASE(FreeUnseenMemoryMallocHookTest) { | 53 UNIT_TEST_CASE(FreeUnseenMemoryMallocHookTest) { |
| 56 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; | 54 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; |
| 57 FLAG_profiler_native_memory = true; | 55 FLAG_profiler_native_memory = true; |
| 58 Profiler::InitAllocationSampleBuffer(); | 56 Profiler::InitAllocationSampleBuffer(); |
| 59 | 57 |
| 60 MallocHooks::InitOnce(); | 58 MallocHooks::InitOnce(); |
| 61 const intptr_t pre_hook_buffer_size = 3; | 59 const intptr_t pre_hook_buffer_size = 3; |
| 62 char* pre_hook_buffer = new char[pre_hook_buffer_size]; | 60 char* pre_hook_buffer = new char[pre_hook_buffer_size]; |
| 63 MallocHookTestBufferInitializer(pre_hook_buffer, pre_hook_buffer_size); | 61 MallocHookTestBufferInitializer(pre_hook_buffer, pre_hook_buffer_size); |
| 64 | 62 |
| 65 MallocHooks::ResetStats(); | 63 MallocHooks::ResetStats(); |
| 66 EXPECT_EQ(0L, MallocHooks::allocation_count()); | 64 EXPECT_EQ(0L, MallocHooks::allocation_count()); |
| 67 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); | 65 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); |
| 68 | 66 |
| 69 const intptr_t buffer_size = 10; | 67 const intptr_t buffer_size = 10; |
| 70 char* buffer = new char[buffer_size]; | 68 char* buffer = new char[buffer_size]; |
| 71 MallocHookTestBufferInitializer(buffer, buffer_size); | 69 MallocHookTestBufferInitializer(buffer, buffer_size); |
| 72 | 70 |
| 73 EXPECT_EQ(1L, MallocHooks::allocation_count()); | 71 EXPECT_EQ(1L, MallocHooks::allocation_count()); |
| 74 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), | 72 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), |
| 75 MallocHooks::heap_allocated_memory_in_bytes()); | 73 MallocHooks::heap_allocated_memory_in_bytes()); |
| 76 | 74 |
| 77 delete[] pre_hook_buffer; | 75 delete[] pre_hook_buffer; |
| 78 EXPECT_EQ(1L, MallocHooks::allocation_count()); | 76 EXPECT_EQ(1L, MallocHooks::allocation_count()); |
| 79 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), | 77 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), |
| 80 MallocHooks::heap_allocated_memory_in_bytes()); | 78 MallocHooks::heap_allocated_memory_in_bytes()); |
| 81 | 79 |
| 82 | |
| 83 delete[] buffer; | 80 delete[] buffer; |
| 84 EXPECT_EQ(0L, MallocHooks::allocation_count()); | 81 EXPECT_EQ(0L, MallocHooks::allocation_count()); |
| 85 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); | 82 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); |
| 86 MallocHooks::TearDown(); | 83 MallocHooks::TearDown(); |
| 87 | 84 |
| 88 FLAG_profiler_native_memory = enable_malloc_hooks_saved; | 85 FLAG_profiler_native_memory = enable_malloc_hooks_saved; |
| 89 } | 86 } |
| 90 | 87 |
| 91 | |
| 92 VM_UNIT_TEST_CASE(StackTraceMallocHookSimpleTest) { | 88 VM_UNIT_TEST_CASE(StackTraceMallocHookSimpleTest) { |
| 93 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; | 89 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; |
| 94 FLAG_profiler_native_memory = true; | 90 FLAG_profiler_native_memory = true; |
| 95 Profiler::InitAllocationSampleBuffer(); | 91 Profiler::InitAllocationSampleBuffer(); |
| 96 | 92 |
| 97 MallocHooks::InitOnce(); | 93 MallocHooks::InitOnce(); |
| 98 MallocHooks::ResetStats(); | 94 MallocHooks::ResetStats(); |
| 99 | 95 |
| 100 bool enable_stack_traces_saved = | 96 bool enable_stack_traces_saved = |
| 101 MallocHooks::stack_trace_collection_enabled(); | 97 MallocHooks::stack_trace_collection_enabled(); |
| 102 MallocHooks::set_stack_trace_collection_enabled(true); | 98 MallocHooks::set_stack_trace_collection_enabled(true); |
| 103 | 99 |
| 104 char* var = static_cast<char*>(malloc(16 * sizeof(char))); | 100 char* var = static_cast<char*>(malloc(16 * sizeof(char))); |
| 105 Sample* sample = MallocHooks::GetSample(var); | 101 Sample* sample = MallocHooks::GetSample(var); |
| 106 EXPECT(sample != NULL); | 102 EXPECT(sample != NULL); |
| 107 | 103 |
| 108 free(var); | 104 free(var); |
| 109 sample = MallocHooks::GetSample(var); | 105 sample = MallocHooks::GetSample(var); |
| 110 EXPECT(sample == NULL); | 106 EXPECT(sample == NULL); |
| 111 MallocHooks::TearDown(); | 107 MallocHooks::TearDown(); |
| 112 MallocHooks::set_stack_trace_collection_enabled(enable_stack_traces_saved); | 108 MallocHooks::set_stack_trace_collection_enabled(enable_stack_traces_saved); |
| 113 FLAG_profiler_native_memory = enable_malloc_hooks_saved; | 109 FLAG_profiler_native_memory = enable_malloc_hooks_saved; |
| 114 } | 110 } |
| 115 | 111 |
| 116 | |
| 117 static char* DART_NOINLINE StackTraceLengthHelper(uintptr_t* end_address) { | 112 static char* DART_NOINLINE StackTraceLengthHelper(uintptr_t* end_address) { |
| 118 char* var = static_cast<char*>(malloc(16 * sizeof(char))); | 113 char* var = static_cast<char*>(malloc(16 * sizeof(char))); |
| 119 *end_address = OS::GetProgramCounter(); | 114 *end_address = OS::GetProgramCounter(); |
| 120 return var; | 115 return var; |
| 121 } | 116 } |
| 122 | 117 |
| 123 | |
| 124 VM_UNIT_TEST_CASE(StackTraceMallocHookLengthTest) { | 118 VM_UNIT_TEST_CASE(StackTraceMallocHookLengthTest) { |
| 125 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; | 119 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; |
| 126 FLAG_profiler_native_memory = true; | 120 FLAG_profiler_native_memory = true; |
| 127 Profiler::InitAllocationSampleBuffer(); | 121 Profiler::InitAllocationSampleBuffer(); |
| 128 | 122 |
| 129 uintptr_t test_start_address = | 123 uintptr_t test_start_address = |
| 130 reinterpret_cast<uintptr_t>(Dart_TestStackTraceMallocHookLengthTest); | 124 reinterpret_cast<uintptr_t>(Dart_TestStackTraceMallocHookLengthTest); |
| 131 uintptr_t helper_start_address = | 125 uintptr_t helper_start_address = |
| 132 reinterpret_cast<uintptr_t>(StackTraceLengthHelper); | 126 reinterpret_cast<uintptr_t>(StackTraceLengthHelper); |
| 133 uintptr_t helper_end_address = 0; | 127 uintptr_t helper_end_address = 0; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 OS::PrintErr("Dumping sample stack trace:\n"); | 162 OS::PrintErr("Dumping sample stack trace:\n"); |
| 169 sample->DumpStackTrace(); | 163 sample->DumpStackTrace(); |
| 170 } | 164 } |
| 171 | 165 |
| 172 free(var); | 166 free(var); |
| 173 MallocHooks::TearDown(); | 167 MallocHooks::TearDown(); |
| 174 MallocHooks::set_stack_trace_collection_enabled(enable_stack_traces_saved); | 168 MallocHooks::set_stack_trace_collection_enabled(enable_stack_traces_saved); |
| 175 FLAG_profiler_native_memory = enable_malloc_hooks_saved; | 169 FLAG_profiler_native_memory = enable_malloc_hooks_saved; |
| 176 } | 170 } |
| 177 | 171 |
| 178 | |
| 179 ISOLATE_UNIT_TEST_CASE(StackTraceMallocHookSimpleJSONTest) { | 172 ISOLATE_UNIT_TEST_CASE(StackTraceMallocHookSimpleJSONTest) { |
| 180 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; | 173 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; |
| 181 FLAG_profiler_native_memory = true; | 174 FLAG_profiler_native_memory = true; |
| 182 Profiler::InitAllocationSampleBuffer(); | 175 Profiler::InitAllocationSampleBuffer(); |
| 183 | 176 |
| 184 MallocHooks::InitOnce(); | 177 MallocHooks::InitOnce(); |
| 185 MallocHooks::ResetStats(); | 178 MallocHooks::ResetStats(); |
| 186 | 179 |
| 187 bool enable_stack_traces_saved = | 180 bool enable_stack_traces_saved = |
| 188 MallocHooks::stack_trace_collection_enabled(); | 181 MallocHooks::stack_trace_collection_enabled(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 208 | 201 |
| 209 free(var); | 202 free(var); |
| 210 MallocHooks::TearDown(); | 203 MallocHooks::TearDown(); |
| 211 MallocHooks::set_stack_trace_collection_enabled(enable_stack_traces_saved); | 204 MallocHooks::set_stack_trace_collection_enabled(enable_stack_traces_saved); |
| 212 FLAG_profiler_native_memory = enable_malloc_hooks_saved; | 205 FLAG_profiler_native_memory = enable_malloc_hooks_saved; |
| 213 } | 206 } |
| 214 | 207 |
| 215 }; // namespace dart | 208 }; // namespace dart |
| 216 | 209 |
| 217 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) | 210 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) |
| OLD | NEW |