| 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) && \ | 7 #if defined(DART_USE_TCMALLOC) && !defined(PRODUCT) && !defined(TARGET_ARCH_DBC) |
| 8 !defined(TARGET_ARCH_DBC) && !defined(HOST_OS_FUCHSIA) | |
| 9 | 8 |
| 10 #include "platform/assert.h" | 9 #include "platform/assert.h" |
| 11 #include "vm/globals.h" | 10 #include "vm/globals.h" |
| 12 #include "vm/malloc_hooks.h" | 11 #include "vm/malloc_hooks.h" |
| 13 #include "vm/os.h" | 12 #include "vm/os.h" |
| 14 #include "vm/profiler.h" | 13 #include "vm/profiler.h" |
| 15 #include "vm/profiler_service.h" | 14 #include "vm/profiler_service.h" |
| 16 #include "vm/unit_test.h" | 15 #include "vm/unit_test.h" |
| 17 | 16 |
| 18 namespace dart { | 17 namespace dart { |
| 19 | 18 |
| 20 static void MallocHookTestBufferInitializer(volatile char* buffer, | 19 static void MallocHookTestBufferInitializer(volatile char* buffer, |
| 21 uintptr_t size) { | 20 uintptr_t size) { |
| 22 // 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 |
| 23 // 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. |
| 24 for (uintptr_t i = 0; i < size; ++i) { | 23 for (uintptr_t i = 0; i < size; ++i) { |
| 25 buffer[i] = i; | 24 buffer[i] = i; |
| 26 } | 25 } |
| 27 } | 26 } |
| 28 | 27 |
| 29 | 28 |
| 30 UNIT_TEST_CASE(BasicMallocHookTest) { | 29 UNIT_TEST_CASE(BasicMallocHookTest) { |
| 31 bool enable_malloc_hooks_saved = FLAG_enable_malloc_hooks; | 30 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; |
| 32 FLAG_enable_malloc_hooks = true; | 31 FLAG_profiler_native_memory = true; |
| 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_enable_malloc_hooks = enable_malloc_hooks_saved; | 50 FLAG_profiler_native_memory = enable_malloc_hooks_saved; |
| 52 } | 51 } |
| 53 | 52 |
| 54 | 53 |
| 55 UNIT_TEST_CASE(FreeUnseenMemoryMallocHookTest) { | 54 UNIT_TEST_CASE(FreeUnseenMemoryMallocHookTest) { |
| 56 bool enable_malloc_hooks_saved = FLAG_enable_malloc_hooks; | 55 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; |
| 57 FLAG_enable_malloc_hooks = true; | 56 FLAG_profiler_native_memory = true; |
| 58 | 57 |
| 59 MallocHooks::InitOnce(); | 58 MallocHooks::InitOnce(); |
| 60 const intptr_t pre_hook_buffer_size = 3; | 59 const intptr_t pre_hook_buffer_size = 3; |
| 61 char* pre_hook_buffer = new char[pre_hook_buffer_size]; | 60 char* pre_hook_buffer = new char[pre_hook_buffer_size]; |
| 62 MallocHookTestBufferInitializer(pre_hook_buffer, pre_hook_buffer_size); | 61 MallocHookTestBufferInitializer(pre_hook_buffer, pre_hook_buffer_size); |
| 63 | 62 |
| 64 MallocHooks::ResetStats(); | 63 MallocHooks::ResetStats(); |
| 65 EXPECT_EQ(0L, MallocHooks::allocation_count()); | 64 EXPECT_EQ(0L, MallocHooks::allocation_count()); |
| 66 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); | 65 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); |
| 67 | 66 |
| 68 const intptr_t buffer_size = 10; | 67 const intptr_t buffer_size = 10; |
| 69 char* buffer = new char[buffer_size]; | 68 char* buffer = new char[buffer_size]; |
| 70 MallocHookTestBufferInitializer(buffer, buffer_size); | 69 MallocHookTestBufferInitializer(buffer, buffer_size); |
| 71 | 70 |
| 72 EXPECT_EQ(1L, MallocHooks::allocation_count()); | 71 EXPECT_EQ(1L, MallocHooks::allocation_count()); |
| 73 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), | 72 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), |
| 74 MallocHooks::heap_allocated_memory_in_bytes()); | 73 MallocHooks::heap_allocated_memory_in_bytes()); |
| 75 | 74 |
| 76 delete[] pre_hook_buffer; | 75 delete[] pre_hook_buffer; |
| 77 EXPECT_EQ(1L, MallocHooks::allocation_count()); | 76 EXPECT_EQ(1L, MallocHooks::allocation_count()); |
| 78 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), | 77 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), |
| 79 MallocHooks::heap_allocated_memory_in_bytes()); | 78 MallocHooks::heap_allocated_memory_in_bytes()); |
| 80 | 79 |
| 81 | 80 |
| 82 delete[] buffer; | 81 delete[] buffer; |
| 83 EXPECT_EQ(0L, MallocHooks::allocation_count()); | 82 EXPECT_EQ(0L, MallocHooks::allocation_count()); |
| 84 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); | 83 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); |
| 85 MallocHooks::TearDown(); | 84 MallocHooks::TearDown(); |
| 86 | 85 |
| 87 FLAG_enable_malloc_hooks = enable_malloc_hooks_saved; | 86 FLAG_profiler_native_memory = enable_malloc_hooks_saved; |
| 88 } | 87 } |
| 89 | 88 |
| 90 | 89 |
| 91 VM_UNIT_TEST_CASE(StackTraceMallocHookSimpleTest) { | 90 VM_UNIT_TEST_CASE(StackTraceMallocHookSimpleTest) { |
| 92 bool enable_malloc_hooks_saved = FLAG_enable_malloc_hooks; | 91 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; |
| 93 FLAG_enable_malloc_hooks = true; | 92 FLAG_profiler_native_memory = true; |
| 94 | 93 |
| 95 MallocHooks::InitOnce(); | 94 MallocHooks::InitOnce(); |
| 96 MallocHooks::ResetStats(); | 95 MallocHooks::ResetStats(); |
| 97 | 96 |
| 98 bool enable_stack_traces_saved = | 97 bool enable_stack_traces_saved = |
| 99 MallocHooks::stack_trace_collection_enabled(); | 98 MallocHooks::stack_trace_collection_enabled(); |
| 100 MallocHooks::set_stack_trace_collection_enabled(true); | 99 MallocHooks::set_stack_trace_collection_enabled(true); |
| 101 | 100 |
| 102 char* var = static_cast<char*>(malloc(16 * sizeof(char))); | 101 char* var = static_cast<char*>(malloc(16 * sizeof(char))); |
| 103 Sample* sample = MallocHooks::GetSample(var); | 102 Sample* sample = MallocHooks::GetSample(var); |
| 104 EXPECT(sample != NULL); | 103 EXPECT(sample != NULL); |
| 105 | 104 |
| 106 free(var); | 105 free(var); |
| 107 sample = MallocHooks::GetSample(var); | 106 sample = MallocHooks::GetSample(var); |
| 108 EXPECT(sample == NULL); | 107 EXPECT(sample == NULL); |
| 109 MallocHooks::TearDown(); | 108 MallocHooks::TearDown(); |
| 110 MallocHooks::set_stack_trace_collection_enabled(enable_stack_traces_saved); | 109 MallocHooks::set_stack_trace_collection_enabled(enable_stack_traces_saved); |
| 111 FLAG_enable_malloc_hooks = enable_malloc_hooks_saved; | 110 FLAG_profiler_native_memory = enable_malloc_hooks_saved; |
| 112 } | 111 } |
| 113 | 112 |
| 114 | 113 |
| 115 static char* DART_NOINLINE StackTraceLengthHelper(uintptr_t* end_address) { | 114 static char* DART_NOINLINE StackTraceLengthHelper(uintptr_t* end_address) { |
| 116 char* var = static_cast<char*>(malloc(16 * sizeof(char))); | 115 char* var = static_cast<char*>(malloc(16 * sizeof(char))); |
| 117 *end_address = OS::GetProgramCounter(); | 116 *end_address = OS::GetProgramCounter(); |
| 118 return var; | 117 return var; |
| 119 } | 118 } |
| 120 | 119 |
| 121 | 120 |
| 122 VM_UNIT_TEST_CASE(StackTraceMallocHookLengthTest) { | 121 VM_UNIT_TEST_CASE(StackTraceMallocHookLengthTest) { |
| 123 bool enable_malloc_hooks_saved = FLAG_enable_malloc_hooks; | 122 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; |
| 124 FLAG_enable_malloc_hooks = true; | 123 FLAG_profiler_native_memory = true; |
| 125 | 124 |
| 126 uintptr_t test_start_address = | 125 uintptr_t test_start_address = |
| 127 reinterpret_cast<uintptr_t>(Dart_TestStackTraceMallocHookLengthTest); | 126 reinterpret_cast<uintptr_t>(Dart_TestStackTraceMallocHookLengthTest); |
| 128 uintptr_t helper_start_address = | 127 uintptr_t helper_start_address = |
| 129 reinterpret_cast<uintptr_t>(StackTraceLengthHelper); | 128 reinterpret_cast<uintptr_t>(StackTraceLengthHelper); |
| 130 uintptr_t helper_end_address = 0; | 129 uintptr_t helper_end_address = 0; |
| 131 | 130 |
| 132 MallocHooks::InitOnce(); | 131 MallocHooks::InitOnce(); |
| 133 MallocHooks::ResetStats(); | 132 MallocHooks::ResetStats(); |
| 134 | 133 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 162 "is stored in the kSkipCount constant.\n"); | 161 "is stored in the kSkipCount constant.\n"); |
| 163 OS::PrintErr("First result: %d Second Result: %d\n", first_result, | 162 OS::PrintErr("First result: %d Second Result: %d\n", first_result, |
| 164 second_result); | 163 second_result); |
| 165 OS::PrintErr("Dumping sample stack trace:\n"); | 164 OS::PrintErr("Dumping sample stack trace:\n"); |
| 166 sample->DumpStackTrace(); | 165 sample->DumpStackTrace(); |
| 167 } | 166 } |
| 168 | 167 |
| 169 free(var); | 168 free(var); |
| 170 MallocHooks::TearDown(); | 169 MallocHooks::TearDown(); |
| 171 MallocHooks::set_stack_trace_collection_enabled(enable_stack_traces_saved); | 170 MallocHooks::set_stack_trace_collection_enabled(enable_stack_traces_saved); |
| 172 FLAG_enable_malloc_hooks = enable_malloc_hooks_saved; | 171 FLAG_profiler_native_memory = enable_malloc_hooks_saved; |
| 173 } | 172 } |
| 174 | 173 |
| 175 | 174 |
| 176 ISOLATE_UNIT_TEST_CASE(StackTraceMallocHookSimpleJSONTest) { | 175 ISOLATE_UNIT_TEST_CASE(StackTraceMallocHookSimpleJSONTest) { |
| 177 bool enable_malloc_hooks_saved = FLAG_enable_malloc_hooks; | 176 bool enable_malloc_hooks_saved = FLAG_profiler_native_memory; |
| 178 FLAG_enable_malloc_hooks = true; | 177 FLAG_profiler_native_memory = true; |
| 179 | 178 |
| 180 MallocHooks::InitOnce(); | 179 MallocHooks::InitOnce(); |
| 181 MallocHooks::ResetStats(); | 180 MallocHooks::ResetStats(); |
| 182 | 181 |
| 183 bool enable_stack_traces_saved = | 182 bool enable_stack_traces_saved = |
| 184 MallocHooks::stack_trace_collection_enabled(); | 183 MallocHooks::stack_trace_collection_enabled(); |
| 185 MallocHooks::set_stack_trace_collection_enabled(true); | 184 MallocHooks::set_stack_trace_collection_enabled(true); |
| 186 | 185 |
| 187 ClearProfileVisitor cpv(Isolate::Current()); | 186 ClearProfileVisitor cpv(Isolate::Current()); |
| 188 Profiler::sample_buffer()->VisitSamples(&cpv); | 187 Profiler::sample_buffer()->VisitSamples(&cpv); |
| 189 | 188 |
| 190 char* var = static_cast<char*>(malloc(16 * sizeof(char))); | 189 char* var = static_cast<char*>(malloc(16 * sizeof(char))); |
| 191 JSONStream js; | 190 JSONStream js; |
| 192 ProfilerService::PrintNativeAllocationJSON(&js, Profile::kNoTags, -1, -1); | 191 ProfilerService::PrintNativeAllocationJSON(&js, Profile::kNoTags, -1, -1); |
| 193 const char* json = js.ToCString(); | 192 const char* json = js.ToCString(); |
| 194 | 193 |
| 195 // Check that all the stack frames from the current down to main are actually | 194 // Check that all the stack frames from the current down to main are actually |
| 196 // present in the profile. This is just a simple sanity check to make sure | 195 // present in the profile. This is just a simple sanity check to make sure |
| 197 // that the ProfileTrie has a representation of the stack trace collected when | 196 // that the ProfileTrie has a representation of the stack trace collected when |
| 198 // var is allocated. More intense testing is already done in profiler_test.cc. | 197 // var is allocated. More intense testing is already done in profiler_test.cc. |
| 199 EXPECT_SUBSTRING("\"dart::Dart_TestStackTraceMallocHookSimpleJSONTest()\"", | 198 EXPECT_SUBSTRING("\"dart::Dart_TestStackTraceMallocHookSimpleJSONTest()\"", |
| 200 json); | 199 json); |
| 201 EXPECT_SUBSTRING("\"dart::TestCase::Run()\"", json); | 200 EXPECT_SUBSTRING("\"dart::TestCase::Run()\"", json); |
| 202 EXPECT_SUBSTRING("\"dart::TestCaseBase::RunTest()\"", json); | 201 EXPECT_SUBSTRING("\"dart::TestCaseBase::RunTest()\"", json); |
| 203 EXPECT_SUBSTRING("\"main\"", json); | 202 EXPECT_SUBSTRING("\"main\"", json); |
| 204 | 203 |
| 205 free(var); | 204 free(var); |
| 206 MallocHooks::TearDown(); | 205 MallocHooks::TearDown(); |
| 207 MallocHooks::set_stack_trace_collection_enabled(enable_stack_traces_saved); | 206 MallocHooks::set_stack_trace_collection_enabled(enable_stack_traces_saved); |
| 208 FLAG_enable_malloc_hooks = enable_malloc_hooks_saved; | 207 FLAG_profiler_native_memory = enable_malloc_hooks_saved; |
| 209 } | 208 } |
| 210 | 209 |
| 211 }; // namespace dart | 210 }; // namespace dart |
| 212 | 211 |
| 213 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) | 212 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) |
| OLD | NEW |