| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdint.h> | 5 #include <stdint.h> |
| 6 #include <string.h> | 6 #include <string.h> |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "tools/android/heap_profiler/heap_profiler.h" | 10 #include "tools/android/heap_profiler/heap_profiler.h" |
| 12 | 11 |
| 13 namespace { | 12 namespace { |
| 14 | 13 |
| 15 class HeapProfilerTest : public testing::Test { | 14 class HeapProfilerTest : public testing::Test { |
| 16 public: | 15 public: |
| 17 void SetUp() override { heap_profiler_init(&stats_); } | 16 void SetUp() override { heap_profiler_init(&stats_); } |
| 18 | 17 |
| 19 void TearDown() override { | 18 void TearDown() override { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 continue; | 66 continue; |
| 68 ++allocs_seen; | 67 ++allocs_seen; |
| 69 stacktrace_bytes_by_alloc[alloc->st] += alloc->end - alloc->start + 1; | 68 stacktrace_bytes_by_alloc[alloc->st] += alloc->end - alloc->start + 1; |
| 70 } | 69 } |
| 71 | 70 |
| 72 for (uint32_t i = 0; i < stats_.max_stack_traces; ++i) { | 71 for (uint32_t i = 0; i < stats_.max_stack_traces; ++i) { |
| 73 StacktraceEntry* st = &stats_.stack_traces[i]; | 72 StacktraceEntry* st = &stats_.stack_traces[i]; |
| 74 if (st->alloc_bytes == 0) | 73 if (st->alloc_bytes == 0) |
| 75 continue; | 74 continue; |
| 76 ++stack_traces_seen; | 75 ++stack_traces_seen; |
| 77 EXPECT_EQ(1, stacktrace_bytes_by_alloc.count(st)); | 76 EXPECT_EQ(1u, stacktrace_bytes_by_alloc.count(st)); |
| 78 EXPECT_EQ(stacktrace_bytes_by_alloc[st], st->alloc_bytes); | 77 EXPECT_EQ(stacktrace_bytes_by_alloc[st], st->alloc_bytes); |
| 79 } | 78 } |
| 80 | 79 |
| 81 EXPECT_EQ(allocs_seen, stats_.num_allocs); | 80 EXPECT_EQ(allocs_seen, stats_.num_allocs); |
| 82 EXPECT_EQ(stack_traces_seen, stats_.num_stack_traces); | 81 EXPECT_EQ(stack_traces_seen, stats_.num_stack_traces); |
| 83 } | 82 } |
| 84 | 83 |
| 85 HeapStats stats_; | 84 HeapStats stats_; |
| 86 }; | 85 }; |
| 87 | 86 |
| 88 TEST_F(HeapProfilerTest, SimpleAlloc) { | 87 TEST_F(HeapProfilerTest, SimpleAlloc) { |
| 89 StackTrace st1 = GenStackTrace(8, 0x0); | 88 StackTrace st1 = GenStackTrace(8, 0x0); |
| 90 heap_profiler_alloc((void*)0x1000, 1024, st1.frames, st1.depth, 0); | 89 heap_profiler_alloc((void*)0x1000, 1024, st1.frames, st1.depth, 0); |
| 91 heap_profiler_alloc((void*)0x2000, 2048, st1.frames, st1.depth, 0); | 90 heap_profiler_alloc((void*)0x2000, 2048, st1.frames, st1.depth, 0); |
| 92 | 91 |
| 93 EXPECT_EQ(2, stats_.num_allocs); | 92 EXPECT_EQ(2u, stats_.num_allocs); |
| 94 EXPECT_EQ(1, stats_.num_stack_traces); | 93 EXPECT_EQ(1u, stats_.num_stack_traces); |
| 95 EXPECT_EQ(1024 + 2048, stats_.total_alloc_bytes); | 94 EXPECT_EQ(1024u + 2048, stats_.total_alloc_bytes); |
| 96 ExpectAlloc(0x1000, 0x13ff, st1, 0); | 95 ExpectAlloc(0x1000, 0x13ff, st1, 0); |
| 97 ExpectAlloc(0x2000, 0x27ff, st1, 0); | 96 ExpectAlloc(0x2000, 0x27ff, st1, 0); |
| 98 } | 97 } |
| 99 | 98 |
| 100 TEST_F(HeapProfilerTest, AllocMultipleStacks) { | 99 TEST_F(HeapProfilerTest, AllocMultipleStacks) { |
| 101 StackTrace st1 = GenStackTrace(8, 0x0); | 100 StackTrace st1 = GenStackTrace(8, 0x0); |
| 102 StackTrace st2 = GenStackTrace(4, 0x1000); | 101 StackTrace st2 = GenStackTrace(4, 0x1000); |
| 103 heap_profiler_alloc((void*)0x1000, 1024, st1.frames, st1.depth, 0); | 102 heap_profiler_alloc((void*)0x1000, 1024, st1.frames, st1.depth, 0); |
| 104 heap_profiler_alloc((void*)0x2000, 2048, st2.frames, st2.depth, 0); | 103 heap_profiler_alloc((void*)0x2000, 2048, st2.frames, st2.depth, 0); |
| 105 heap_profiler_alloc((void*)0x3000, 32, st1.frames, st1.depth, 0); | 104 heap_profiler_alloc((void*)0x3000, 32, st1.frames, st1.depth, 0); |
| 106 | 105 |
| 107 EXPECT_EQ(3, stats_.num_allocs); | 106 EXPECT_EQ(3u, stats_.num_allocs); |
| 108 EXPECT_EQ(2, stats_.num_stack_traces); | 107 EXPECT_EQ(2u, stats_.num_stack_traces); |
| 109 EXPECT_EQ(1024 + 2048 + 32, stats_.total_alloc_bytes); | 108 EXPECT_EQ(1024u + 2048 + 32, stats_.total_alloc_bytes); |
| 110 ExpectAlloc(0x1000, 0x13ff, st1, 0); | 109 ExpectAlloc(0x1000, 0x13ff, st1, 0); |
| 111 ExpectAlloc(0x2000, 0x27ff, st2, 0); | 110 ExpectAlloc(0x2000, 0x27ff, st2, 0); |
| 112 ExpectAlloc(0x3000, 0x301f, st1, 0); | 111 ExpectAlloc(0x3000, 0x301f, st1, 0); |
| 113 } | 112 } |
| 114 | 113 |
| 115 TEST_F(HeapProfilerTest, SimpleAllocAndFree) { | 114 TEST_F(HeapProfilerTest, SimpleAllocAndFree) { |
| 116 StackTrace st1 = GenStackTrace(8, 0x0); | 115 StackTrace st1 = GenStackTrace(8, 0x0); |
| 117 heap_profiler_alloc((void*)0x1000, 1024, st1.frames, st1.depth, 0); | 116 heap_profiler_alloc((void*)0x1000, 1024, st1.frames, st1.depth, 0); |
| 118 heap_profiler_free((void*)0x1000, 1024, NULL); | 117 heap_profiler_free((void*)0x1000, 1024, NULL); |
| 119 | 118 |
| 120 EXPECT_EQ(0, stats_.num_allocs); | 119 EXPECT_EQ(0u, stats_.num_allocs); |
| 121 EXPECT_EQ(0, stats_.num_stack_traces); | 120 EXPECT_EQ(0u, stats_.num_stack_traces); |
| 122 EXPECT_EQ(0, stats_.total_alloc_bytes); | 121 EXPECT_EQ(0u, stats_.total_alloc_bytes); |
| 123 } | 122 } |
| 124 | 123 |
| 125 TEST_F(HeapProfilerTest, Realloc) { | 124 TEST_F(HeapProfilerTest, Realloc) { |
| 126 StackTrace st1 = GenStackTrace(8, 0); | 125 StackTrace st1 = GenStackTrace(8, 0); |
| 127 heap_profiler_alloc((void*)0, 32, st1.frames, st1.depth, 0); | 126 heap_profiler_alloc((void*)0, 32, st1.frames, st1.depth, 0); |
| 128 heap_profiler_alloc((void*)0, 32, st1.frames, st1.depth, 0); | 127 heap_profiler_alloc((void*)0, 32, st1.frames, st1.depth, 0); |
| 129 } | 128 } |
| 130 | 129 |
| 131 TEST_F(HeapProfilerTest, AllocAndFreeMultipleStacks) { | 130 TEST_F(HeapProfilerTest, AllocAndFreeMultipleStacks) { |
| 132 StackTrace st1 = GenStackTrace(8, 0x0); | 131 StackTrace st1 = GenStackTrace(8, 0x0); |
| 133 StackTrace st2 = GenStackTrace(6, 0x1000); | 132 StackTrace st2 = GenStackTrace(6, 0x1000); |
| 134 heap_profiler_alloc((void*)0x1000, 1024, st1.frames, st1.depth, 0); | 133 heap_profiler_alloc((void*)0x1000, 1024, st1.frames, st1.depth, 0); |
| 135 heap_profiler_alloc((void*)0x2000, 2048, st1.frames, st1.depth, 0); | 134 heap_profiler_alloc((void*)0x2000, 2048, st1.frames, st1.depth, 0); |
| 136 heap_profiler_alloc((void*)0x3000, 32, st2.frames, st2.depth, 0); | 135 heap_profiler_alloc((void*)0x3000, 32, st2.frames, st2.depth, 0); |
| 137 heap_profiler_alloc((void*)0x4000, 64, st2.frames, st2.depth, 0); | 136 heap_profiler_alloc((void*)0x4000, 64, st2.frames, st2.depth, 0); |
| 138 | 137 |
| 139 heap_profiler_free((void*)0x1000, 1024, NULL); | 138 heap_profiler_free((void*)0x1000, 1024, NULL); |
| 140 heap_profiler_free((void*)0x3000, 32, NULL); | 139 heap_profiler_free((void*)0x3000, 32, NULL); |
| 141 | 140 |
| 142 EXPECT_EQ(2, stats_.num_allocs); | 141 EXPECT_EQ(2u, stats_.num_allocs); |
| 143 EXPECT_EQ(2, stats_.num_stack_traces); | 142 EXPECT_EQ(2u, stats_.num_stack_traces); |
| 144 EXPECT_EQ(2048 + 64, stats_.total_alloc_bytes); | 143 EXPECT_EQ(2048u + 64, stats_.total_alloc_bytes); |
| 145 ExpectAlloc(0x2000, 0x27ff, st1, 0); | 144 ExpectAlloc(0x2000, 0x27ff, st1, 0); |
| 146 ExpectAlloc(0x4000, 0x403f, st2, 0); | 145 ExpectAlloc(0x4000, 0x403f, st2, 0); |
| 147 } | 146 } |
| 148 | 147 |
| 149 TEST_F(HeapProfilerTest, AllocAndFreeAll) { | 148 TEST_F(HeapProfilerTest, AllocAndFreeAll) { |
| 150 StackTrace st1 = GenStackTrace(8, 0x0); | 149 StackTrace st1 = GenStackTrace(8, 0x0); |
| 151 StackTrace st2 = GenStackTrace(6, 0x1000); | 150 StackTrace st2 = GenStackTrace(6, 0x1000); |
| 152 heap_profiler_alloc((void*)0x1000, 1024, st1.frames, st1.depth, 0); | 151 heap_profiler_alloc((void*)0x1000, 1024, st1.frames, st1.depth, 0); |
| 153 heap_profiler_alloc((void*)0x2000, 2048, st1.frames, st1.depth, 0); | 152 heap_profiler_alloc((void*)0x2000, 2048, st1.frames, st1.depth, 0); |
| 154 heap_profiler_alloc((void*)0x3000, 32, st2.frames, st2.depth, 0); | 153 heap_profiler_alloc((void*)0x3000, 32, st2.frames, st2.depth, 0); |
| 155 heap_profiler_alloc((void*)0x4000, 64, st2.frames, st2.depth, 0); | 154 heap_profiler_alloc((void*)0x4000, 64, st2.frames, st2.depth, 0); |
| 156 | 155 |
| 157 heap_profiler_free((void*)0x1000, 1024, NULL); | 156 heap_profiler_free((void*)0x1000, 1024, NULL); |
| 158 heap_profiler_free((void*)0x2000, 2048, NULL); | 157 heap_profiler_free((void*)0x2000, 2048, NULL); |
| 159 heap_profiler_free((void*)0x3000, 32, NULL); | 158 heap_profiler_free((void*)0x3000, 32, NULL); |
| 160 heap_profiler_free((void*)0x4000, 64, NULL); | 159 heap_profiler_free((void*)0x4000, 64, NULL); |
| 161 | 160 |
| 162 EXPECT_EQ(0, stats_.num_allocs); | 161 EXPECT_EQ(0u, stats_.num_allocs); |
| 163 EXPECT_EQ(0, stats_.num_stack_traces); | 162 EXPECT_EQ(0u, stats_.num_stack_traces); |
| 164 EXPECT_EQ(0, stats_.total_alloc_bytes); | 163 EXPECT_EQ(0u, stats_.total_alloc_bytes); |
| 165 } | 164 } |
| 166 | 165 |
| 167 TEST_F(HeapProfilerTest, AllocAndFreeWithZeroSize) { | 166 TEST_F(HeapProfilerTest, AllocAndFreeWithZeroSize) { |
| 168 StackTrace st1 = GenStackTrace(8, 0x0); | 167 StackTrace st1 = GenStackTrace(8, 0x0); |
| 169 StackTrace st2 = GenStackTrace(6, 0x1000); | 168 StackTrace st2 = GenStackTrace(6, 0x1000); |
| 170 heap_profiler_alloc((void*)0x1000, 1024, st1.frames, st1.depth, 0); | 169 heap_profiler_alloc((void*)0x1000, 1024, st1.frames, st1.depth, 0); |
| 171 heap_profiler_alloc((void*)0x2000, 2048, st2.frames, st2.depth, 0); | 170 heap_profiler_alloc((void*)0x2000, 2048, st2.frames, st2.depth, 0); |
| 172 | 171 |
| 173 heap_profiler_free((void*)0x1000, 0, NULL); | 172 heap_profiler_free((void*)0x1000, 0, NULL); |
| 174 | 173 |
| 175 EXPECT_EQ(1, stats_.num_allocs); | 174 EXPECT_EQ(1u, stats_.num_allocs); |
| 176 EXPECT_EQ(1, stats_.num_stack_traces); | 175 EXPECT_EQ(1u, stats_.num_stack_traces); |
| 177 EXPECT_EQ(2048, stats_.total_alloc_bytes); | 176 EXPECT_EQ(2048u, stats_.total_alloc_bytes); |
| 178 } | 177 } |
| 179 | 178 |
| 180 TEST_F(HeapProfilerTest, AllocAndFreeContiguous) { | 179 TEST_F(HeapProfilerTest, AllocAndFreeContiguous) { |
| 181 StackTrace st1 = GenStackTrace(8, 0x0); | 180 StackTrace st1 = GenStackTrace(8, 0x0); |
| 182 StackTrace st2 = GenStackTrace(6, 0x1000); | 181 StackTrace st2 = GenStackTrace(6, 0x1000); |
| 183 heap_profiler_alloc((void*)0x1000, 4096, st1.frames, st1.depth, 0); | 182 heap_profiler_alloc((void*)0x1000, 4096, st1.frames, st1.depth, 0); |
| 184 heap_profiler_alloc((void*)0x2000, 4096, st2.frames, st2.depth, 0); | 183 heap_profiler_alloc((void*)0x2000, 4096, st2.frames, st2.depth, 0); |
| 185 | 184 |
| 186 heap_profiler_free((void*)0x1000, 8192, NULL); | 185 heap_profiler_free((void*)0x1000, 8192, NULL); |
| 187 | 186 |
| 188 EXPECT_EQ(0, stats_.num_allocs); | 187 EXPECT_EQ(0u, stats_.num_allocs); |
| 189 EXPECT_EQ(0, stats_.num_stack_traces); | 188 EXPECT_EQ(0u, stats_.num_stack_traces); |
| 190 EXPECT_EQ(0, stats_.total_alloc_bytes); | 189 EXPECT_EQ(0u, stats_.total_alloc_bytes); |
| 191 } | 190 } |
| 192 | 191 |
| 193 TEST_F(HeapProfilerTest, SparseAllocsOneLargeOuterFree) { | 192 TEST_F(HeapProfilerTest, SparseAllocsOneLargeOuterFree) { |
| 194 StackTrace st1 = GenStackTrace(8, 0x0); | 193 StackTrace st1 = GenStackTrace(8, 0x0); |
| 195 StackTrace st2 = GenStackTrace(6, 0x1000); | 194 StackTrace st2 = GenStackTrace(6, 0x1000); |
| 196 | 195 |
| 197 heap_profiler_alloc((void*)0x1010, 1, st1.frames, st1.depth, 0); | 196 heap_profiler_alloc((void*)0x1010, 1, st1.frames, st1.depth, 0); |
| 198 heap_profiler_alloc((void*)0x1400, 2, st2.frames, st2.depth, 0); | 197 heap_profiler_alloc((void*)0x1400, 2, st2.frames, st2.depth, 0); |
| 199 heap_profiler_alloc((void*)0x1600, 5, st1.frames, st1.depth, 0); | 198 heap_profiler_alloc((void*)0x1600, 5, st1.frames, st1.depth, 0); |
| 200 heap_profiler_alloc((void*)0x9000, 4096, st2.frames, st2.depth, 0); | 199 heap_profiler_alloc((void*)0x9000, 4096, st2.frames, st2.depth, 0); |
| 201 | 200 |
| 202 heap_profiler_free((void*)0x0800, 8192, NULL); | 201 heap_profiler_free((void*)0x0800, 8192, NULL); |
| 203 EXPECT_EQ(1, stats_.num_allocs); | 202 EXPECT_EQ(1u, stats_.num_allocs); |
| 204 EXPECT_EQ(1, stats_.num_stack_traces); | 203 EXPECT_EQ(1u, stats_.num_stack_traces); |
| 205 EXPECT_EQ(4096, stats_.total_alloc_bytes); | 204 EXPECT_EQ(4096u, stats_.total_alloc_bytes); |
| 206 ExpectAlloc(0x9000, 0x9fff, st2, 0); | 205 ExpectAlloc(0x9000, 0x9fff, st2, 0); |
| 207 } | 206 } |
| 208 | 207 |
| 209 TEST_F(HeapProfilerTest, SparseAllocsOneLargePartiallyOverlappingFree) { | 208 TEST_F(HeapProfilerTest, SparseAllocsOneLargePartiallyOverlappingFree) { |
| 210 StackTrace st1 = GenStackTrace(8, 0x0); | 209 StackTrace st1 = GenStackTrace(8, 0x0); |
| 211 StackTrace st2 = GenStackTrace(6, 0x1000); | 210 StackTrace st2 = GenStackTrace(6, 0x1000); |
| 212 StackTrace st3 = GenStackTrace(4, 0x2000); | 211 StackTrace st3 = GenStackTrace(4, 0x2000); |
| 213 | 212 |
| 214 // This will be untouched. | 213 // This will be untouched. |
| 215 heap_profiler_alloc((void*)0x1000, 1024, st1.frames, st1.depth, 0); | 214 heap_profiler_alloc((void*)0x1000, 1024, st1.frames, st1.depth, 0); |
| 216 | 215 |
| 217 // These will be partially freed in one shot (% 64 a bytes "margin"). | 216 // These will be partially freed in one shot (% 64 a bytes "margin"). |
| 218 heap_profiler_alloc((void*)0x2000, 128, st2.frames, st2.depth, 0); | 217 heap_profiler_alloc((void*)0x2000, 128, st2.frames, st2.depth, 0); |
| 219 heap_profiler_alloc((void*)0x2400, 128, st2.frames, st2.depth, 0); | 218 heap_profiler_alloc((void*)0x2400, 128, st2.frames, st2.depth, 0); |
| 220 heap_profiler_alloc((void*)0x2f80, 128, st2.frames, st2.depth, 0); | 219 heap_profiler_alloc((void*)0x2f80, 128, st2.frames, st2.depth, 0); |
| 221 | 220 |
| 222 // This will be untouched. | 221 // This will be untouched. |
| 223 heap_profiler_alloc((void*)0x3000, 1024, st3.frames, st3.depth, 0); | 222 heap_profiler_alloc((void*)0x3000, 1024, st3.frames, st3.depth, 0); |
| 224 | 223 |
| 225 heap_profiler_free((void*)0x2040, 4096 - 64 - 64, NULL); | 224 heap_profiler_free((void*)0x2040, 4096 - 64 - 64, NULL); |
| 226 EXPECT_EQ(4, stats_.num_allocs); | 225 EXPECT_EQ(4u, stats_.num_allocs); |
| 227 EXPECT_EQ(3, stats_.num_stack_traces); | 226 EXPECT_EQ(3u, stats_.num_stack_traces); |
| 228 EXPECT_EQ(1024 + 64 + 64 + 1024, stats_.total_alloc_bytes); | 227 EXPECT_EQ(1024u + 64 + 64 + 1024, stats_.total_alloc_bytes); |
| 229 | 228 |
| 230 ExpectAlloc(0x1000, 0x13ff, st1, 0); | 229 ExpectAlloc(0x1000, 0x13ff, st1, 0); |
| 231 ExpectAlloc(0x2000, 0x203f, st2, 0); | 230 ExpectAlloc(0x2000, 0x203f, st2, 0); |
| 232 ExpectAlloc(0x2fc0, 0x2fff, st2, 0); | 231 ExpectAlloc(0x2fc0, 0x2fff, st2, 0); |
| 233 ExpectAlloc(0x3000, 0x33ff, st3, 0); | 232 ExpectAlloc(0x3000, 0x33ff, st3, 0); |
| 234 } | 233 } |
| 235 | 234 |
| 236 TEST_F(HeapProfilerTest, AllocAndFreeScattered) { | 235 TEST_F(HeapProfilerTest, AllocAndFreeScattered) { |
| 237 StackTrace st1 = GenStackTrace(8, 0x0); | 236 StackTrace st1 = GenStackTrace(8, 0x0); |
| 238 heap_profiler_alloc((void*)0x1000, 4096, st1.frames, st1.depth, 0); | 237 heap_profiler_alloc((void*)0x1000, 4096, st1.frames, st1.depth, 0); |
| 239 heap_profiler_alloc((void*)0x2000, 4096, st1.frames, st1.depth, 0); | 238 heap_profiler_alloc((void*)0x2000, 4096, st1.frames, st1.depth, 0); |
| 240 heap_profiler_alloc((void*)0x3000, 4096, st1.frames, st1.depth, 0); | 239 heap_profiler_alloc((void*)0x3000, 4096, st1.frames, st1.depth, 0); |
| 241 heap_profiler_alloc((void*)0x4000, 4096, st1.frames, st1.depth, 0); | 240 heap_profiler_alloc((void*)0x4000, 4096, st1.frames, st1.depth, 0); |
| 242 | 241 |
| 243 heap_profiler_free((void*)0x800, 4096, NULL); | 242 heap_profiler_free((void*)0x800, 4096, NULL); |
| 244 EXPECT_EQ(4, stats_.num_allocs); | 243 EXPECT_EQ(4u, stats_.num_allocs); |
| 245 EXPECT_EQ(2048 + 4096 + 4096 + 4096, stats_.total_alloc_bytes); | 244 EXPECT_EQ(2048u + 4096 + 4096 + 4096, stats_.total_alloc_bytes); |
| 246 | 245 |
| 247 heap_profiler_free((void*)0x1800, 4096, NULL); | 246 heap_profiler_free((void*)0x1800, 4096, NULL); |
| 248 EXPECT_EQ(3, stats_.num_allocs); | 247 EXPECT_EQ(3u, stats_.num_allocs); |
| 249 EXPECT_EQ(2048 + 4096 + 4096, stats_.total_alloc_bytes); | 248 EXPECT_EQ(2048u + 4096 + 4096, stats_.total_alloc_bytes); |
| 250 | 249 |
| 251 heap_profiler_free((void*)0x2800, 4096, NULL); | 250 heap_profiler_free((void*)0x2800, 4096, NULL); |
| 252 EXPECT_EQ(2, stats_.num_allocs); | 251 EXPECT_EQ(2u, stats_.num_allocs); |
| 253 EXPECT_EQ(2048 + 4096, stats_.total_alloc_bytes); | 252 EXPECT_EQ(2048u + 4096, stats_.total_alloc_bytes); |
| 254 | 253 |
| 255 heap_profiler_free((void*)0x3800, 4096, NULL); | 254 heap_profiler_free((void*)0x3800, 4096, NULL); |
| 256 EXPECT_EQ(1, stats_.num_allocs); | 255 EXPECT_EQ(1u, stats_.num_allocs); |
| 257 EXPECT_EQ(2048, stats_.total_alloc_bytes); | 256 EXPECT_EQ(2048u, stats_.total_alloc_bytes); |
| 258 | 257 |
| 259 heap_profiler_free((void*)0x4800, 4096, NULL); | 258 heap_profiler_free((void*)0x4800, 4096, NULL); |
| 260 EXPECT_EQ(0, stats_.num_allocs); | 259 EXPECT_EQ(0u, stats_.num_allocs); |
| 261 EXPECT_EQ(0, stats_.num_stack_traces); | 260 EXPECT_EQ(0u, stats_.num_stack_traces); |
| 262 EXPECT_EQ(0, stats_.total_alloc_bytes); | 261 EXPECT_EQ(0u, stats_.total_alloc_bytes); |
| 263 } | 262 } |
| 264 | 263 |
| 265 TEST_F(HeapProfilerTest, AllocAndOverFreeContiguous) { | 264 TEST_F(HeapProfilerTest, AllocAndOverFreeContiguous) { |
| 266 StackTrace st1 = GenStackTrace(8, 0x0); | 265 StackTrace st1 = GenStackTrace(8, 0x0); |
| 267 StackTrace st2 = GenStackTrace(6, 0x1000); | 266 StackTrace st2 = GenStackTrace(6, 0x1000); |
| 268 heap_profiler_alloc((void*)0x1000, 4096, st1.frames, st1.depth, 0); | 267 heap_profiler_alloc((void*)0x1000, 4096, st1.frames, st1.depth, 0); |
| 269 heap_profiler_alloc((void*)0x2000, 4096, st2.frames, st2.depth, 0); | 268 heap_profiler_alloc((void*)0x2000, 4096, st2.frames, st2.depth, 0); |
| 270 | 269 |
| 271 heap_profiler_free((void*)0, 16834, NULL); | 270 heap_profiler_free((void*)0, 16834, NULL); |
| 272 | 271 |
| 273 EXPECT_EQ(0, stats_.num_allocs); | 272 EXPECT_EQ(0u, stats_.num_allocs); |
| 274 EXPECT_EQ(0, stats_.num_stack_traces); | 273 EXPECT_EQ(0u, stats_.num_stack_traces); |
| 275 EXPECT_EQ(0, stats_.total_alloc_bytes); | 274 EXPECT_EQ(0u, stats_.total_alloc_bytes); |
| 276 } | 275 } |
| 277 | 276 |
| 278 TEST_F(HeapProfilerTest, AllocContiguousAndPunchHole) { | 277 TEST_F(HeapProfilerTest, AllocContiguousAndPunchHole) { |
| 279 StackTrace st1 = GenStackTrace(8, 0x0); | 278 StackTrace st1 = GenStackTrace(8, 0x0); |
| 280 StackTrace st2 = GenStackTrace(6, 0x1000); | 279 StackTrace st2 = GenStackTrace(6, 0x1000); |
| 281 heap_profiler_alloc((void*)0x1000, 4096, st1.frames, st1.depth, 0); | 280 heap_profiler_alloc((void*)0x1000, 4096, st1.frames, st1.depth, 0); |
| 282 heap_profiler_alloc((void*)0x2000, 4096, st2.frames, st2.depth, 0); | 281 heap_profiler_alloc((void*)0x2000, 4096, st2.frames, st2.depth, 0); |
| 283 | 282 |
| 284 // Punch a 4k hole in the middle of the two contiguous 4k allocs. | 283 // Punch a 4k hole in the middle of the two contiguous 4k allocs. |
| 285 heap_profiler_free((void*)0x1800, 4096, NULL); | 284 heap_profiler_free((void*)0x1800, 4096, NULL); |
| 286 | 285 |
| 287 EXPECT_EQ(2, stats_.num_allocs); | 286 EXPECT_EQ(2u, stats_.num_allocs); |
| 288 EXPECT_EQ(2, stats_.num_stack_traces); | 287 EXPECT_EQ(2u, stats_.num_stack_traces); |
| 289 EXPECT_EQ(4096, stats_.total_alloc_bytes); | 288 EXPECT_EQ(4096u, stats_.total_alloc_bytes); |
| 290 } | 289 } |
| 291 | 290 |
| 292 TEST_F(HeapProfilerTest, AllocAndPartialFree) { | 291 TEST_F(HeapProfilerTest, AllocAndPartialFree) { |
| 293 StackTrace st1 = GenStackTrace(8, 0x0); | 292 StackTrace st1 = GenStackTrace(8, 0x0); |
| 294 StackTrace st2 = GenStackTrace(6, 0x1000); | 293 StackTrace st2 = GenStackTrace(6, 0x1000); |
| 295 StackTrace st3 = GenStackTrace(7, 0x2000); | 294 StackTrace st3 = GenStackTrace(7, 0x2000); |
| 296 StackTrace st4 = GenStackTrace(9, 0x3000); | 295 StackTrace st4 = GenStackTrace(9, 0x3000); |
| 297 heap_profiler_alloc((void*)0x1000, 1024, st1.frames, st1.depth, 0); | 296 heap_profiler_alloc((void*)0x1000, 1024, st1.frames, st1.depth, 0); |
| 298 heap_profiler_alloc((void*)0x2000, 1024, st2.frames, st2.depth, 0); | 297 heap_profiler_alloc((void*)0x2000, 1024, st2.frames, st2.depth, 0); |
| 299 heap_profiler_alloc((void*)0x3000, 1024, st3.frames, st3.depth, 0); | 298 heap_profiler_alloc((void*)0x3000, 1024, st3.frames, st3.depth, 0); |
| 300 heap_profiler_alloc((void*)0x4000, 1024, st4.frames, st4.depth, 0); | 299 heap_profiler_alloc((void*)0x4000, 1024, st4.frames, st4.depth, 0); |
| 301 | 300 |
| 302 heap_profiler_free((void*)0x1000, 128, NULL); // Shrink left by 128B. | 301 heap_profiler_free((void*)0x1000, 128, NULL); // Shrink left by 128B. |
| 303 heap_profiler_free((void*)0x2380, 128, NULL); // Shrink right by 128B. | 302 heap_profiler_free((void*)0x2380, 128, NULL); // Shrink right by 128B. |
| 304 heap_profiler_free((void*)0x3100, 512, NULL); // 512B hole in the middle. | 303 heap_profiler_free((void*)0x3100, 512, NULL); // 512B hole in the middle. |
| 305 heap_profiler_free((void*)0x4000, 512, NULL); // Free up the 4th alloc... | 304 heap_profiler_free((void*)0x4000, 512, NULL); // Free up the 4th alloc... |
| 306 heap_profiler_free((void*)0x4200, 512, NULL); // ...but do it in two halves. | 305 heap_profiler_free((void*)0x4200, 512, NULL); // ...but do it in two halves. |
| 307 | 306 |
| 308 EXPECT_EQ(4, stats_.num_allocs); // 1 + 2 + two sides around the hole 3. | 307 EXPECT_EQ(4u, stats_.num_allocs); // 1 + 2 + two sides around the hole 3. |
| 309 EXPECT_EQ(3, stats_.num_stack_traces); // st4 should be gone. | 308 EXPECT_EQ(3u, stats_.num_stack_traces); // st4 should be gone. |
| 310 EXPECT_EQ(896 + 896 + 512, stats_.total_alloc_bytes); | 309 EXPECT_EQ(896u + 896 + 512, stats_.total_alloc_bytes); |
| 311 } | 310 } |
| 312 | 311 |
| 313 TEST_F(HeapProfilerTest, RandomIndividualAllocAndFrees) { | 312 TEST_F(HeapProfilerTest, RandomIndividualAllocAndFrees) { |
| 314 static const size_t NUM_ST = 128; | 313 static const size_t NUM_ST = 128; |
| 315 static const size_t NUM_OPS = 1000; | 314 static const size_t NUM_OPS = 1000; |
| 316 | 315 |
| 317 StackTrace st[NUM_ST]; | 316 StackTrace st[NUM_ST]; |
| 318 for (uint32_t i = 0; i < NUM_ST; ++i) | 317 for (uint32_t i = 0; i < NUM_ST; ++i) |
| 319 st[i] = GenStackTrace((i % 10) + 2, i * 128); | 318 st[i] = GenStackTrace((i % 10) + 2, i * 128); |
| 320 | 319 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 TEST_F(HeapProfilerTest, UnwindStackTooLargeShouldSaturate) { | 356 TEST_F(HeapProfilerTest, UnwindStackTooLargeShouldSaturate) { |
| 358 StackTrace st1 = GenStackTrace(HEAP_PROFILER_MAX_DEPTH, 0x0); | 357 StackTrace st1 = GenStackTrace(HEAP_PROFILER_MAX_DEPTH, 0x0); |
| 359 uintptr_t many_frames[100] = {}; | 358 uintptr_t many_frames[100] = {}; |
| 360 memcpy(many_frames, st1.frames, sizeof(uintptr_t) * st1.depth); | 359 memcpy(many_frames, st1.frames, sizeof(uintptr_t) * st1.depth); |
| 361 heap_profiler_alloc((void*)0x1000, 1024, many_frames, 100, 0); | 360 heap_profiler_alloc((void*)0x1000, 1024, many_frames, 100, 0); |
| 362 ExpectAlloc(0x1000, 0x13ff, st1, 0); | 361 ExpectAlloc(0x1000, 0x13ff, st1, 0); |
| 363 } | 362 } |
| 364 | 363 |
| 365 TEST_F(HeapProfilerTest, NoUnwindShouldNotCrashButNoop) { | 364 TEST_F(HeapProfilerTest, NoUnwindShouldNotCrashButNoop) { |
| 366 heap_profiler_alloc((void*)0x1000, 1024, NULL, 0, 0); | 365 heap_profiler_alloc((void*)0x1000, 1024, NULL, 0, 0); |
| 367 EXPECT_EQ(0, stats_.num_allocs); | 366 EXPECT_EQ(0u, stats_.num_allocs); |
| 368 EXPECT_EQ(0, stats_.num_stack_traces); | 367 EXPECT_EQ(0u, stats_.num_stack_traces); |
| 369 EXPECT_EQ(0, stats_.total_alloc_bytes); | 368 EXPECT_EQ(0u, stats_.total_alloc_bytes); |
| 370 } | 369 } |
| 371 | 370 |
| 372 TEST_F(HeapProfilerTest, FreeNonExisting) { | 371 TEST_F(HeapProfilerTest, FreeNonExisting) { |
| 373 StackTrace st1 = GenStackTrace(5, 0x0); | 372 StackTrace st1 = GenStackTrace(5, 0x0); |
| 374 heap_profiler_free((void*)0x1000, 1024, NULL); | 373 heap_profiler_free((void*)0x1000, 1024, NULL); |
| 375 heap_profiler_free((void*)0x1400, 1024, NULL); | 374 heap_profiler_free((void*)0x1400, 1024, NULL); |
| 376 EXPECT_EQ(0, stats_.num_allocs); | 375 EXPECT_EQ(0u, stats_.num_allocs); |
| 377 EXPECT_EQ(0, stats_.num_stack_traces); | 376 EXPECT_EQ(0u, stats_.num_stack_traces); |
| 378 EXPECT_EQ(0, stats_.total_alloc_bytes); | 377 EXPECT_EQ(0u, stats_.total_alloc_bytes); |
| 379 heap_profiler_alloc((void*)0x1000, 1024, st1.frames, st1.depth, 0); | 378 heap_profiler_alloc((void*)0x1000, 1024, st1.frames, st1.depth, 0); |
| 380 EXPECT_EQ(1, stats_.num_allocs); | 379 EXPECT_EQ(1u, stats_.num_allocs); |
| 381 EXPECT_EQ(1024, stats_.total_alloc_bytes); | 380 EXPECT_EQ(1024u, stats_.total_alloc_bytes); |
| 382 } | 381 } |
| 383 | 382 |
| 384 TEST_F(HeapProfilerTest, FlagsConsistency) { | 383 TEST_F(HeapProfilerTest, FlagsConsistency) { |
| 385 StackTrace st1 = GenStackTrace(8, 0x0); | 384 StackTrace st1 = GenStackTrace(8, 0x0); |
| 386 uint32_t flags = 0; | 385 uint32_t flags = 0; |
| 387 heap_profiler_alloc((void*)0x1000, 4096, st1.frames, st1.depth, 42); | 386 heap_profiler_alloc((void*)0x1000, 4096, st1.frames, st1.depth, 42); |
| 388 heap_profiler_alloc((void*)0x2000, 4096, st1.frames, st1.depth, 142); | 387 heap_profiler_alloc((void*)0x2000, 4096, st1.frames, st1.depth, 142); |
| 389 | 388 |
| 390 ExpectAlloc(0x1000, 0x1fff, st1, 42); | 389 ExpectAlloc(0x1000, 0x1fff, st1, 42); |
| 391 ExpectAlloc(0x2000, 0x2fff, st1, 142); | 390 ExpectAlloc(0x2000, 0x2fff, st1, 142); |
| 392 | 391 |
| 393 // Punch a 4k hole in the middle of the two contiguous 4k allocs. | 392 // Punch a 4k hole in the middle of the two contiguous 4k allocs. |
| 394 heap_profiler_free((void*)0x1800, 4096, NULL); | 393 heap_profiler_free((void*)0x1800, 4096, NULL); |
| 395 | 394 |
| 396 ExpectAlloc(0x1000, 0x17ff, st1, 42); | 395 ExpectAlloc(0x1000, 0x17ff, st1, 42); |
| 397 heap_profiler_free((void*)0x1000, 2048, &flags); | 396 heap_profiler_free((void*)0x1000, 2048, &flags); |
| 398 EXPECT_EQ(42, flags); | 397 EXPECT_EQ(42u, flags); |
| 399 | 398 |
| 400 ExpectAlloc(0x2800, 0x2fff, st1, 142); | 399 ExpectAlloc(0x2800, 0x2fff, st1, 142); |
| 401 heap_profiler_free((void*)0x2800, 2048, &flags); | 400 heap_profiler_free((void*)0x2800, 2048, &flags); |
| 402 EXPECT_EQ(142, flags); | 401 EXPECT_EQ(142u, flags); |
| 403 } | 402 } |
| 404 | 403 |
| 405 TEST_F(HeapProfilerTest, BeConsistentOnOOM) { | 404 TEST_F(HeapProfilerTest, BeConsistentOnOOM) { |
| 406 static const size_t NUM_ALLOCS = 512 * 1024; | 405 static const size_t NUM_ALLOCS = 512 * 1024; |
| 407 uintptr_t frames[1]; | 406 uintptr_t frames[1]; |
| 408 | 407 |
| 409 for (uintptr_t i = 0; i < NUM_ALLOCS; ++i) { | 408 for (uintptr_t i = 0; i < NUM_ALLOCS; ++i) { |
| 410 frames[0] = i; | 409 frames[0] = i; |
| 411 heap_profiler_alloc((void*)(i * 32), 32, frames, 1, 0); | 410 heap_profiler_alloc((void*)(i * 32), 32, frames, 1, 0); |
| 412 } | 411 } |
| 413 | 412 |
| 414 CheckAllocVsStacktaceConsistency(); | 413 CheckAllocVsStacktaceConsistency(); |
| 415 // Check that we're saturating, otherwise this entire test is pointless. | 414 // Check that we're saturating, otherwise this entire test is pointless. |
| 416 EXPECT_LT(stats_.num_allocs, NUM_ALLOCS); | 415 EXPECT_LT(stats_.num_allocs, NUM_ALLOCS); |
| 417 EXPECT_LT(stats_.num_stack_traces, NUM_ALLOCS); | 416 EXPECT_LT(stats_.num_stack_traces, NUM_ALLOCS); |
| 418 | 417 |
| 419 for (uintptr_t i = 0; i < NUM_ALLOCS; ++i) | 418 for (uintptr_t i = 0; i < NUM_ALLOCS; ++i) |
| 420 heap_profiler_free((void*)(i * 32), 32, NULL); | 419 heap_profiler_free((void*)(i * 32), 32, NULL); |
| 421 | 420 |
| 422 EXPECT_EQ(0, stats_.num_allocs); | 421 EXPECT_EQ(0u, stats_.num_allocs); |
| 423 EXPECT_EQ(0, stats_.total_alloc_bytes); | 422 EXPECT_EQ(0u, stats_.total_alloc_bytes); |
| 424 EXPECT_EQ(0, stats_.num_stack_traces); | 423 EXPECT_EQ(0u, stats_.num_stack_traces); |
| 425 } | 424 } |
| 426 | 425 |
| 427 #ifdef __LP64__ | 426 #ifdef __LP64__ |
| 428 TEST_F(HeapProfilerTest, Test64Bit) { | 427 TEST_F(HeapProfilerTest, Test64Bit) { |
| 429 StackTrace st1 = GenStackTrace(8, 0x0); | 428 StackTrace st1 = GenStackTrace(8, 0x0); |
| 430 StackTrace st2 = GenStackTrace(10, 0x7fffffff70000000L); | 429 StackTrace st2 = GenStackTrace(10, 0x7fffffff70000000L); |
| 431 StackTrace st3 = GenStackTrace(10, 0xffffffff70000000L); | 430 StackTrace st3 = GenStackTrace(10, 0xffffffff70000000L); |
| 432 heap_profiler_alloc((void*)0x1000, 4096, st1.frames, st1.depth, 0); | 431 heap_profiler_alloc((void*)0x1000, 4096, st1.frames, st1.depth, 0); |
| 433 heap_profiler_alloc( | 432 heap_profiler_alloc( |
| 434 (void*)0x7ffffffffffff000L, 4096, st2.frames, st2.depth, 0); | 433 (void*)0x7ffffffffffff000L, 4096, st2.frames, st2.depth, 0); |
| 435 heap_profiler_alloc( | 434 heap_profiler_alloc( |
| 436 (void*)0xfffffffffffff000L, 4096, st3.frames, st3.depth, 0); | 435 (void*)0xfffffffffffff000L, 4096, st3.frames, st3.depth, 0); |
| 437 EXPECT_EQ(3, stats_.num_allocs); | 436 EXPECT_EQ(3, stats_.num_allocs); |
| 438 EXPECT_EQ(3, stats_.num_stack_traces); | 437 EXPECT_EQ(3, stats_.num_stack_traces); |
| 439 EXPECT_EQ(4096 + 4096 + 4096, stats_.total_alloc_bytes); | 438 EXPECT_EQ(4096u + 4096 + 4096, stats_.total_alloc_bytes); |
| 440 | 439 |
| 441 heap_profiler_free((void*)0x1000, 4096, NULL); | 440 heap_profiler_free((void*)0x1000, 4096, NULL); |
| 442 EXPECT_EQ(2, stats_.num_allocs); | 441 EXPECT_EQ(2u, stats_.num_allocs); |
| 443 EXPECT_EQ(2, stats_.num_stack_traces); | 442 EXPECT_EQ(2u, stats_.num_stack_traces); |
| 444 EXPECT_EQ(4096 + 4096, stats_.total_alloc_bytes); | 443 EXPECT_EQ(4096u + 4096, stats_.total_alloc_bytes); |
| 445 | 444 |
| 446 heap_profiler_free((void*)0x7ffffffffffff000L, 4096, NULL); | 445 heap_profiler_free((void*)0x7ffffffffffff000L, 4096, NULL); |
| 447 EXPECT_EQ(1, stats_.num_allocs); | 446 EXPECT_EQ(1u, stats_.num_allocs); |
| 448 EXPECT_EQ(1, stats_.num_stack_traces); | 447 EXPECT_EQ(1u, stats_.num_stack_traces); |
| 449 EXPECT_EQ(4096, stats_.total_alloc_bytes); | 448 EXPECT_EQ(4096u, stats_.total_alloc_bytes); |
| 450 | 449 |
| 451 heap_profiler_free((void*)0xfffffffffffff000L, 4096, NULL); | 450 heap_profiler_free((void*)0xfffffffffffff000L, 4096, NULL); |
| 452 EXPECT_EQ(0, stats_.num_allocs); | 451 EXPECT_EQ(0u, stats_.num_allocs); |
| 453 EXPECT_EQ(0, stats_.num_stack_traces); | 452 EXPECT_EQ(0u, stats_.num_stack_traces); |
| 454 EXPECT_EQ(0, stats_.total_alloc_bytes); | 453 EXPECT_EQ(0u, stats_.total_alloc_bytes); |
| 455 } | 454 } |
| 456 #endif | 455 #endif |
| 457 | 456 |
| 458 } // namespace | 457 } // namespace |
| OLD | NEW |