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) && \ |
| 8 !defined(TARGET_ARCH_DBC) && !defined(TARGET_OS_FUCHSIA) |
8 | 9 |
9 #include "platform/assert.h" | 10 #include "platform/assert.h" |
10 #include "vm/class_finalizer.h" | |
11 #include "vm/globals.h" | 11 #include "vm/globals.h" |
12 #include "vm/malloc_hooks.h" | 12 #include "vm/malloc_hooks.h" |
13 #include "vm/symbols.h" | 13 #include "vm/os.h" |
| 14 #include "vm/profiler.h" |
| 15 #include "vm/profiler_service.h" |
14 #include "vm/unit_test.h" | 16 #include "vm/unit_test.h" |
15 | 17 |
16 namespace dart { | 18 namespace dart { |
17 | 19 |
18 static void MallocHookTestBufferInitializer(volatile char* buffer, | 20 static void MallocHookTestBufferInitializer(volatile char* buffer, |
19 uintptr_t size) { | 21 uintptr_t size) { |
20 // Run through the buffer and do something. If we don't do this and the memory | 22 // Run through the buffer and do something. If we don't do this and the memory |
21 // in buffer isn't touched, the tcmalloc hooks won't be called. | 23 // in buffer isn't touched, the tcmalloc hooks won't be called. |
22 for (uintptr_t i = 0; i < size; ++i) { | 24 for (uintptr_t i = 0; i < size; ++i) { |
23 buffer[i] = i; | 25 buffer[i] = i; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 MallocHooks::InitOnce(); | 59 MallocHooks::InitOnce(); |
58 const intptr_t pre_hook_buffer_size = 3; | 60 const intptr_t pre_hook_buffer_size = 3; |
59 char* pre_hook_buffer = new char[pre_hook_buffer_size]; | 61 char* pre_hook_buffer = new char[pre_hook_buffer_size]; |
60 MallocHookTestBufferInitializer(pre_hook_buffer, pre_hook_buffer_size); | 62 MallocHookTestBufferInitializer(pre_hook_buffer, pre_hook_buffer_size); |
61 | 63 |
62 MallocHooks::ResetStats(); | 64 MallocHooks::ResetStats(); |
63 EXPECT_EQ(0L, MallocHooks::allocation_count()); | 65 EXPECT_EQ(0L, MallocHooks::allocation_count()); |
64 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); | 66 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); |
65 | 67 |
66 const intptr_t buffer_size = 10; | 68 const intptr_t buffer_size = 10; |
67 volatile char* buffer = new char[buffer_size]; | 69 char* buffer = new char[buffer_size]; |
68 MallocHookTestBufferInitializer(buffer, buffer_size); | 70 MallocHookTestBufferInitializer(buffer, buffer_size); |
69 | 71 |
70 EXPECT_EQ(1L, MallocHooks::allocation_count()); | 72 EXPECT_EQ(1L, MallocHooks::allocation_count()); |
71 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), | 73 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), |
72 MallocHooks::heap_allocated_memory_in_bytes()); | 74 MallocHooks::heap_allocated_memory_in_bytes()); |
73 | 75 |
74 delete[] pre_hook_buffer; | 76 delete[] pre_hook_buffer; |
75 EXPECT_EQ(1L, MallocHooks::allocation_count()); | 77 EXPECT_EQ(1L, MallocHooks::allocation_count()); |
76 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), | 78 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), |
77 MallocHooks::heap_allocated_memory_in_bytes()); | 79 MallocHooks::heap_allocated_memory_in_bytes()); |
78 | 80 |
79 | 81 |
80 delete[] buffer; | 82 delete[] buffer; |
81 EXPECT_EQ(0L, MallocHooks::allocation_count()); | 83 EXPECT_EQ(0L, MallocHooks::allocation_count()); |
82 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); | 84 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); |
83 MallocHooks::TearDown(); | 85 MallocHooks::TearDown(); |
84 | 86 |
85 FLAG_enable_malloc_hooks = enable_malloc_hooks_saved; | 87 FLAG_enable_malloc_hooks = enable_malloc_hooks_saved; |
86 } | 88 } |
87 | 89 |
| 90 |
| 91 VM_UNIT_TEST_CASE(StackTraceMallocHookSimpleTest) { |
| 92 bool enable_malloc_hooks_saved = FLAG_enable_malloc_hooks; |
| 93 FLAG_enable_malloc_hooks = true; |
| 94 |
| 95 MallocHooks::InitOnce(); |
| 96 MallocHooks::ResetStats(); |
| 97 |
| 98 bool enable_stack_traces_saved = |
| 99 MallocHooks::stack_trace_collection_enabled(); |
| 100 MallocHooks::set_stack_trace_collection_enabled(true); |
| 101 |
| 102 char* var = static_cast<char*>(malloc(16 * sizeof(char))); |
| 103 Sample* sample = MallocHooks::GetSample(var); |
| 104 EXPECT(sample != NULL); |
| 105 |
| 106 free(var); |
| 107 sample = MallocHooks::GetSample(var); |
| 108 EXPECT(sample == NULL); |
| 109 MallocHooks::TearDown(); |
| 110 MallocHooks::set_stack_trace_collection_enabled(enable_stack_traces_saved); |
| 111 FLAG_enable_malloc_hooks = enable_malloc_hooks_saved; |
| 112 } |
| 113 |
| 114 |
| 115 static char* DART_NOINLINE StackTraceLengthHelper(uintptr_t* end_address) { |
| 116 char* var = static_cast<char*>(malloc(16 * sizeof(char))); |
| 117 *end_address = OS::GetProgramCounter(); |
| 118 return var; |
| 119 } |
| 120 |
| 121 |
| 122 VM_UNIT_TEST_CASE(StackTraceMallocHookLengthTest) { |
| 123 bool enable_malloc_hooks_saved = FLAG_enable_malloc_hooks; |
| 124 FLAG_enable_malloc_hooks = true; |
| 125 |
| 126 uintptr_t test_start_address = |
| 127 reinterpret_cast<uintptr_t>(Dart_TestStackTraceMallocHookLengthTest); |
| 128 uintptr_t helper_start_address = |
| 129 reinterpret_cast<uintptr_t>(StackTraceLengthHelper); |
| 130 uintptr_t helper_end_address = 0; |
| 131 |
| 132 MallocHooks::InitOnce(); |
| 133 MallocHooks::ResetStats(); |
| 134 |
| 135 bool enable_stack_traces_saved = |
| 136 MallocHooks::stack_trace_collection_enabled(); |
| 137 MallocHooks::set_stack_trace_collection_enabled(true); |
| 138 |
| 139 char* var = StackTraceLengthHelper(&helper_end_address); |
| 140 Sample* sample = MallocHooks::GetSample(var); |
| 141 EXPECT(sample != NULL); |
| 142 uintptr_t test_end_address = OS::GetProgramCounter(); |
| 143 |
| 144 // Ensure that all stack frames are where we expect them to be in the sample. |
| 145 // If they aren't, the kSkipCount constant in malloc_hooks.cc is likely |
| 146 // incorrect. |
| 147 uword address = sample->At(0); |
| 148 bool first_result = |
| 149 (helper_start_address <= address) && (helper_end_address >= address); |
| 150 EXPECT(first_result); |
| 151 address = sample->At(1); |
| 152 bool second_result = |
| 153 (test_start_address <= address) && (test_end_address >= address); |
| 154 EXPECT(second_result); |
| 155 |
| 156 if (!(first_result && second_result)) { |
| 157 OS::PrintErr( |
| 158 "If this test is failing, it's likely that the value set for" |
| 159 "the number of frames to skip in malloc_hooks.cc is " |
| 160 "incorrect for this configuration/platform. This value can be" |
| 161 " found in malloc_hooks.cc in the AllocationInfo class, and " |
| 162 "is stored in the kSkipCount constant."); |
| 163 } |
| 164 |
| 165 free(var); |
| 166 MallocHooks::TearDown(); |
| 167 MallocHooks::set_stack_trace_collection_enabled(enable_stack_traces_saved); |
| 168 FLAG_enable_malloc_hooks = enable_malloc_hooks_saved; |
| 169 } |
| 170 |
| 171 |
| 172 ISOLATE_UNIT_TEST_CASE(StackTraceMallocHookSimpleJSONTest) { |
| 173 bool enable_malloc_hooks_saved = FLAG_enable_malloc_hooks; |
| 174 FLAG_enable_malloc_hooks = true; |
| 175 |
| 176 MallocHooks::InitOnce(); |
| 177 MallocHooks::ResetStats(); |
| 178 |
| 179 bool enable_stack_traces_saved = |
| 180 MallocHooks::stack_trace_collection_enabled(); |
| 181 MallocHooks::set_stack_trace_collection_enabled(true); |
| 182 |
| 183 ClearProfileVisitor cpv(Isolate::Current()); |
| 184 Profiler::sample_buffer()->VisitSamples(&cpv); |
| 185 |
| 186 char* var = static_cast<char*>(malloc(16 * sizeof(char))); |
| 187 JSONStream js; |
| 188 ProfilerService::PrintNativeAllocationJSON(&js, Profile::kNoTags, -1, -1); |
| 189 const char* json = js.ToCString(); |
| 190 |
| 191 // Check that all the stack frames from the current down to main are actually |
| 192 // present in the profile. This is just a simple sanity check to make sure |
| 193 // that the ProfileTrie has a representation of the stack trace collected when |
| 194 // var is allocated. More intense testing is already done in profiler_test.cc. |
| 195 EXPECT_SUBSTRING("\"dart::Dart_TestStackTraceMallocHookSimpleJSONTest()\"", |
| 196 json); |
| 197 EXPECT_SUBSTRING("\"dart::TestCase::Run()\"", json); |
| 198 EXPECT_SUBSTRING("\"dart::TestCaseBase::RunTest()\"", json); |
| 199 EXPECT_SUBSTRING("\"main\"", json); |
| 200 |
| 201 free(var); |
| 202 MallocHooks::TearDown(); |
| 203 MallocHooks::set_stack_trace_collection_enabled(enable_stack_traces_saved); |
| 204 FLAG_enable_malloc_hooks = enable_malloc_hooks_saved; |
| 205 } |
| 206 |
88 }; // namespace dart | 207 }; // namespace dart |
89 | 208 |
90 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) | 209 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) |
OLD | NEW |