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 | 8 |
9 #include "platform/assert.h" | 9 #include "platform/assert.h" |
10 #include "vm/class_finalizer.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/symbols.h" |
14 #include "vm/unit_test.h" | 14 #include "vm/unit_test.h" |
15 | 15 |
16 namespace dart { | 16 namespace dart { |
17 | 17 |
18 static void MallocHookTestBufferInitializer(volatile char* buffer, | 18 static void MallocHookTestBufferInitializer(volatile char* buffer, |
19 uintptr_t size) { | 19 uintptr_t size) { |
20 // Run through the buffer and do something. If we don't do this and the memory | 20 // 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. | 21 // in buffer isn't touched, the tcmalloc hooks won't be called. |
22 for (uintptr_t i = 0; i < size; ++i) { | 22 for (uintptr_t i = 0; i < size; ++i) { |
23 buffer[i] = i; | 23 buffer[i] = i; |
24 } | 24 } |
25 } | 25 } |
26 | 26 |
27 | 27 |
28 UNIT_TEST_CASE(BasicMallocHookTest) { | 28 UNIT_TEST_CASE(BasicMallocHookTest) { |
| 29 bool enable_malloc_hooks_saved = FLAG_enable_malloc_hooks; |
| 30 FLAG_enable_malloc_hooks = true; |
| 31 |
29 MallocHooks::InitOnce(); | 32 MallocHooks::InitOnce(); |
30 MallocHooks::ResetStats(); | 33 MallocHooks::ResetStats(); |
31 EXPECT_EQ(0L, MallocHooks::allocation_count()); | 34 EXPECT_EQ(0L, MallocHooks::allocation_count()); |
32 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); | 35 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); |
33 const intptr_t buffer_size = 10; | 36 const intptr_t buffer_size = 10; |
34 char* buffer = new char[buffer_size]; | 37 char* buffer = new char[buffer_size]; |
35 MallocHookTestBufferInitializer(buffer, buffer_size); | 38 MallocHookTestBufferInitializer(buffer, buffer_size); |
36 | 39 |
37 EXPECT_EQ(1L, MallocHooks::allocation_count()); | 40 EXPECT_EQ(1L, MallocHooks::allocation_count()); |
38 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), | 41 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), |
39 MallocHooks::heap_allocated_memory_in_bytes()); | 42 MallocHooks::heap_allocated_memory_in_bytes()); |
40 | 43 |
41 delete[] buffer; | 44 delete[] buffer; |
42 EXPECT_EQ(0L, MallocHooks::allocation_count()); | 45 EXPECT_EQ(0L, MallocHooks::allocation_count()); |
43 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); | 46 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); |
44 MallocHooks::TearDown(); | 47 MallocHooks::TearDown(); |
| 48 |
| 49 FLAG_enable_malloc_hooks = enable_malloc_hooks_saved; |
45 } | 50 } |
46 | 51 |
47 | 52 |
48 UNIT_TEST_CASE(FreeUnseenMemoryMallocHookTest) { | 53 UNIT_TEST_CASE(FreeUnseenMemoryMallocHookTest) { |
| 54 bool enable_malloc_hooks_saved = FLAG_enable_malloc_hooks; |
| 55 FLAG_enable_malloc_hooks = true; |
| 56 |
49 MallocHooks::InitOnce(); | 57 MallocHooks::InitOnce(); |
50 const intptr_t pre_hook_buffer_size = 3; | 58 const intptr_t pre_hook_buffer_size = 3; |
51 char* pre_hook_buffer = new char[pre_hook_buffer_size]; | 59 char* pre_hook_buffer = new char[pre_hook_buffer_size]; |
52 MallocHookTestBufferInitializer(pre_hook_buffer, pre_hook_buffer_size); | 60 MallocHookTestBufferInitializer(pre_hook_buffer, pre_hook_buffer_size); |
53 | 61 |
54 MallocHooks::ResetStats(); | 62 MallocHooks::ResetStats(); |
55 EXPECT_EQ(0L, MallocHooks::allocation_count()); | 63 EXPECT_EQ(0L, MallocHooks::allocation_count()); |
56 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); | 64 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); |
57 | 65 |
58 const intptr_t buffer_size = 10; | 66 const intptr_t buffer_size = 10; |
59 volatile char* buffer = new char[buffer_size]; | 67 volatile char* buffer = new char[buffer_size]; |
60 MallocHookTestBufferInitializer(buffer, buffer_size); | 68 MallocHookTestBufferInitializer(buffer, buffer_size); |
61 | 69 |
62 EXPECT_EQ(1L, MallocHooks::allocation_count()); | 70 EXPECT_EQ(1L, MallocHooks::allocation_count()); |
63 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), | 71 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), |
64 MallocHooks::heap_allocated_memory_in_bytes()); | 72 MallocHooks::heap_allocated_memory_in_bytes()); |
65 | 73 |
66 delete[] pre_hook_buffer; | 74 delete[] pre_hook_buffer; |
67 EXPECT_EQ(1L, MallocHooks::allocation_count()); | 75 EXPECT_EQ(1L, MallocHooks::allocation_count()); |
68 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), | 76 EXPECT_EQ(static_cast<intptr_t>(sizeof(char) * buffer_size), |
69 MallocHooks::heap_allocated_memory_in_bytes()); | 77 MallocHooks::heap_allocated_memory_in_bytes()); |
70 | 78 |
71 | 79 |
72 delete[] buffer; | 80 delete[] buffer; |
73 EXPECT_EQ(0L, MallocHooks::allocation_count()); | 81 EXPECT_EQ(0L, MallocHooks::allocation_count()); |
74 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); | 82 EXPECT_EQ(0L, MallocHooks::heap_allocated_memory_in_bytes()); |
75 MallocHooks::TearDown(); | 83 MallocHooks::TearDown(); |
| 84 |
| 85 FLAG_enable_malloc_hooks = enable_malloc_hooks_saved; |
76 } | 86 } |
77 | 87 |
78 }; // namespace dart | 88 }; // namespace dart |
79 | 89 |
80 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) | 90 #endif // defined(DART_USE_TCMALLOC) && !defined(PRODUCT) |
OLD | NEW |