OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <stdint.h> |
| 6 #include <string.h> |
| 7 #include <map> |
| 8 |
| 9 #include "base/compiler_specific.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "tools/android/heap_profiler/heap_profiler.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 class HeapProfilerTest : public testing::Test { |
| 16 public: |
| 17 virtual void SetUp() OVERRIDE { |
| 18 heap_profiler_init(&stats_); |
| 19 } |
| 20 |
| 21 virtual void TearDown() OVERRIDE { |
| 22 CheckVMAvsStacktaceConsistency(); |
| 23 heap_profiler_cleanup(); |
| 24 } |
| 25 |
| 26 protected: |
| 27 struct StackTrace { |
| 28 uintptr_t frames[HEAP_PROFILER_MAX_DEPTH]; |
| 29 size_t depth; |
| 30 }; |
| 31 |
| 32 StackTrace GenStackTrace(size_t depth, uintptr_t base) { |
| 33 assert(depth <= HEAP_PROFILER_MAX_DEPTH); |
| 34 StackTrace st; |
| 35 for (size_t i = 0; i < depth; ++i) |
| 36 st.frames[i] = base + i * 0x10UL; |
| 37 st.depth = depth; |
| 38 return st; |
| 39 } |
| 40 |
| 41 void ExpectVMA( |
| 42 uintptr_t start, uintptr_t end, const StackTrace& st, uint32_t flags) { |
| 43 for (uint32_t i = 0; i < stats_.max_allocs; ++i) { |
| 44 const VMA& vma = stats_.allocs[i]; |
| 45 if (vma.start != start || vma.end != end) |
| 46 continue; |
| 47 // Check that the stack trace match. |
| 48 for (uint32_t j = 0; j < st.depth; ++j) { |
| 49 EXPECT_EQ(st.frames[j], vma.st->frames[j]) << |
| 50 "Stacktrace not matching @ depth " << j; |
| 51 } |
| 52 EXPECT_EQ(flags, vma.flags); |
| 53 return; |
| 54 } |
| 55 |
| 56 FAIL() << "Could not find VMA [" << std::hex << start << "," << end << "]"; |
| 57 } |
| 58 |
| 59 void CheckVMAvsStacktaceConsistency() { |
| 60 uint32_t allocs_seen = 0; |
| 61 uint32_t stack_traces_seen = 0; |
| 62 std::map<StacktraceEntry*, uintptr_t> stacktrace_bytes_seen_by_vma; |
| 63 |
| 64 for (uint32_t i = 0; i < stats_.max_allocs; ++i) { |
| 65 VMA* vma = &stats_.allocs[i]; |
| 66 if (vma->start == 0 && vma->end == 0) |
| 67 continue; |
| 68 ++allocs_seen; |
| 69 stacktrace_bytes_seen_by_vma[vma->st] += vma->end - vma->start + 1; |
| 70 } |
| 71 |
| 72 for (uint32_t i = 0; i < stats_.max_stack_traces; ++i) { |
| 73 StacktraceEntry* st = &stats_.stack_traces[i]; |
| 74 if (st->alloc_bytes == 0) |
| 75 continue; |
| 76 ++stack_traces_seen; |
| 77 EXPECT_EQ(1, stacktrace_bytes_seen_by_vma.count(st)); |
| 78 EXPECT_EQ(stacktrace_bytes_seen_by_vma[st], st->alloc_bytes); |
| 79 } |
| 80 |
| 81 EXPECT_EQ(allocs_seen, stats_.num_allocs); |
| 82 EXPECT_EQ(stack_traces_seen, stats_.num_stack_traces); |
| 83 } |
| 84 |
| 85 HeapStats stats_; |
| 86 }; |
| 87 |
| 88 |
| 89 TEST_F(HeapProfilerTest, SimpleAlloc) { |
| 90 StackTrace st1 = GenStackTrace(8, 0x0); |
| 91 heap_profiler_alloc((void*) 0x1000, 1024, st1.frames, st1.depth, 0); |
| 92 heap_profiler_alloc((void*) 0x2000, 2048, st1.frames, st1.depth, 0); |
| 93 |
| 94 EXPECT_EQ(2, stats_.num_allocs); |
| 95 EXPECT_EQ(1, stats_.num_stack_traces); |
| 96 EXPECT_EQ(1024 + 2048, stats_.total_alloc_bytes); |
| 97 ExpectVMA(0x1000, 0x13ff, st1, 0); |
| 98 ExpectVMA(0x2000, 0x27ff, st1, 0); |
| 99 } |
| 100 |
| 101 TEST_F(HeapProfilerTest, AllocMultipleStacks) { |
| 102 StackTrace st1 = GenStackTrace(8, 0x0); |
| 103 StackTrace st2 = GenStackTrace(4, 0x1000); |
| 104 heap_profiler_alloc((void*) 0x1000, 1024, st1.frames, st1.depth, 0); |
| 105 heap_profiler_alloc((void*) 0x2000, 2048, st2.frames, st2.depth, 0); |
| 106 heap_profiler_alloc((void*) 0x3000, 32, st1.frames, st1.depth, 0); |
| 107 |
| 108 EXPECT_EQ(3, stats_.num_allocs); |
| 109 EXPECT_EQ(2, stats_.num_stack_traces); |
| 110 EXPECT_EQ(1024 + 2048 + 32, stats_.total_alloc_bytes); |
| 111 ExpectVMA(0x1000, 0x13ff, st1, 0); |
| 112 ExpectVMA(0x2000, 0x27ff, st2, 0); |
| 113 ExpectVMA(0x3000, 0x301f, st1, 0); |
| 114 } |
| 115 |
| 116 TEST_F(HeapProfilerTest, SimpleAllocAndFree) { |
| 117 StackTrace st1 = GenStackTrace(8, 0x0); |
| 118 heap_profiler_alloc((void*) 0x1000, 1024, st1.frames, st1.depth, 0); |
| 119 heap_profiler_free((void*) 0x1000, 1024, NULL); |
| 120 |
| 121 EXPECT_EQ(0, stats_.num_allocs); |
| 122 EXPECT_EQ(0, stats_.num_stack_traces); |
| 123 EXPECT_EQ(0, stats_.total_alloc_bytes); |
| 124 } |
| 125 |
| 126 TEST_F(HeapProfilerTest, Realloc) { |
| 127 StackTrace st1 = GenStackTrace(8, 0); |
| 128 heap_profiler_alloc((void*) 0, 32, st1.frames, st1.depth, 0); |
| 129 heap_profiler_alloc((void*) 0, 32, st1.frames, st1.depth, 0); |
| 130 } |
| 131 |
| 132 TEST_F(HeapProfilerTest, AllocAndFreeMultipleStacks) { |
| 133 StackTrace st1 = GenStackTrace(8, 0x0); |
| 134 StackTrace st2 = GenStackTrace(6, 0x1000); |
| 135 heap_profiler_alloc((void*) 0x1000, 1024, st1.frames, st1.depth, 0); |
| 136 heap_profiler_alloc((void*) 0x2000, 2048, st1.frames, st1.depth, 0); |
| 137 heap_profiler_alloc((void*) 0x3000, 32, st2.frames, st2.depth, 0); |
| 138 heap_profiler_alloc((void*) 0x4000, 64, st2.frames, st2.depth, 0); |
| 139 |
| 140 heap_profiler_free((void*) 0x1000, 1024, NULL); |
| 141 heap_profiler_free((void*) 0x3000, 32, NULL); |
| 142 |
| 143 EXPECT_EQ(2, stats_.num_allocs); |
| 144 EXPECT_EQ(2, stats_.num_stack_traces); |
| 145 EXPECT_EQ(2048 + 64, stats_.total_alloc_bytes); |
| 146 ExpectVMA(0x2000, 0x27ff, st1, 0); |
| 147 ExpectVMA(0x4000, 0x403f, st2, 0); |
| 148 } |
| 149 |
| 150 TEST_F(HeapProfilerTest, AllocAndFreeAll) { |
| 151 StackTrace st1 = GenStackTrace(8, 0x0); |
| 152 StackTrace st2 = GenStackTrace(6, 0x1000); |
| 153 heap_profiler_alloc((void*) 0x1000, 1024, st1.frames, st1.depth, 0); |
| 154 heap_profiler_alloc((void*) 0x2000, 2048, st1.frames, st1.depth, 0); |
| 155 heap_profiler_alloc((void*) 0x3000, 32, st2.frames, st2.depth, 0); |
| 156 heap_profiler_alloc((void*) 0x4000, 64, st2.frames, st2.depth, 0); |
| 157 |
| 158 heap_profiler_free((void*) 0x1000, 1024, NULL); |
| 159 heap_profiler_free((void*) 0x2000, 2048, NULL); |
| 160 heap_profiler_free((void*) 0x3000, 32, NULL); |
| 161 heap_profiler_free((void*) 0x4000, 64, NULL); |
| 162 |
| 163 EXPECT_EQ(0, stats_.num_allocs); |
| 164 EXPECT_EQ(0, stats_.num_stack_traces); |
| 165 EXPECT_EQ(0, stats_.total_alloc_bytes); |
| 166 } |
| 167 |
| 168 TEST_F(HeapProfilerTest, AllocAndFreeWithZeroSize) { |
| 169 StackTrace st1 = GenStackTrace(8, 0x0); |
| 170 StackTrace st2 = GenStackTrace(6, 0x1000); |
| 171 heap_profiler_alloc((void*) 0x1000, 1024, st1.frames, st1.depth, 0); |
| 172 heap_profiler_alloc((void*) 0x2000, 2048, st2.frames, st2.depth, 0); |
| 173 |
| 174 heap_profiler_free((void*) 0x1000, 0, NULL); |
| 175 |
| 176 EXPECT_EQ(1, stats_.num_allocs); |
| 177 EXPECT_EQ(1, stats_.num_stack_traces); |
| 178 EXPECT_EQ(2048, stats_.total_alloc_bytes); |
| 179 } |
| 180 |
| 181 TEST_F(HeapProfilerTest, AllocAndFreeContiguous) { |
| 182 StackTrace st1 = GenStackTrace(8, 0x0); |
| 183 StackTrace st2 = GenStackTrace(6, 0x1000); |
| 184 heap_profiler_alloc((void*) 0x1000, 4096, st1.frames, st1.depth, 0); |
| 185 heap_profiler_alloc((void*) 0x2000, 4096, st2.frames, st2.depth, 0); |
| 186 |
| 187 heap_profiler_free((void*) 0x1000, 8192, NULL); |
| 188 |
| 189 EXPECT_EQ(0, stats_.num_allocs); |
| 190 EXPECT_EQ(0, stats_.num_stack_traces); |
| 191 EXPECT_EQ(0, stats_.total_alloc_bytes); |
| 192 } |
| 193 |
| 194 TEST_F(HeapProfilerTest, SparseAllocsOneLargeOuterFree) { |
| 195 StackTrace st1 = GenStackTrace(8, 0x0); |
| 196 StackTrace st2 = GenStackTrace(6, 0x1000); |
| 197 |
| 198 heap_profiler_alloc((void*) 0x1010, 1, st1.frames, st1.depth, 0); |
| 199 heap_profiler_alloc((void*) 0x1400, 2, st2.frames, st2.depth, 0); |
| 200 heap_profiler_alloc((void*) 0x1600, 5, st1.frames, st1.depth, 0); |
| 201 heap_profiler_alloc((void*) 0x9000, 4096, st2.frames, st2.depth, 0); |
| 202 |
| 203 heap_profiler_free((void*) 0x0800, 8192, NULL); |
| 204 EXPECT_EQ(1, stats_.num_allocs); |
| 205 EXPECT_EQ(1, stats_.num_stack_traces); |
| 206 EXPECT_EQ(4096, stats_.total_alloc_bytes); |
| 207 ExpectVMA(0x9000, 0x9fff, st2, 0); |
| 208 } |
| 209 |
| 210 TEST_F(HeapProfilerTest, SparseAllocsOneLargePartiallyOverlappingFree) { |
| 211 StackTrace st1 = GenStackTrace(8, 0x0); |
| 212 StackTrace st2 = GenStackTrace(6, 0x1000); |
| 213 StackTrace st3 = GenStackTrace(4, 0x2000); |
| 214 |
| 215 // This will be untouched. |
| 216 heap_profiler_alloc((void*) 0x1000, 1024, st1.frames, st1.depth, 0); |
| 217 |
| 218 // These will be partially freed in one shot (% 64 a bytes "margin"). |
| 219 heap_profiler_alloc((void*) 0x2000, 128, st2.frames, st2.depth, 0); |
| 220 heap_profiler_alloc((void*) 0x2400, 128, st2.frames, st2.depth, 0); |
| 221 heap_profiler_alloc((void*) 0x2f80, 128, st2.frames, st2.depth, 0); |
| 222 |
| 223 // This will be untouched. |
| 224 heap_profiler_alloc((void*) 0x3000, 1024, st3.frames, st3.depth, 0); |
| 225 |
| 226 heap_profiler_free((void*) 0x2040, 4096 - 64 - 64, NULL); |
| 227 EXPECT_EQ(4, stats_.num_allocs); |
| 228 EXPECT_EQ(3, stats_.num_stack_traces); |
| 229 EXPECT_EQ(1024 + 64 + 64 + 1024, stats_.total_alloc_bytes); |
| 230 |
| 231 ExpectVMA(0x1000, 0x13ff, st1, 0); |
| 232 ExpectVMA(0x2000, 0x203f, st2, 0); |
| 233 ExpectVMA(0x2fc0, 0x2fff, st2, 0); |
| 234 ExpectVMA(0x3000, 0x33ff, st3, 0); |
| 235 } |
| 236 |
| 237 TEST_F(HeapProfilerTest, AllocAndFreeScattered) { |
| 238 StackTrace st1 = GenStackTrace(8, 0x0); |
| 239 heap_profiler_alloc((void*) 0x1000, 4096, st1.frames, st1.depth, 0); |
| 240 heap_profiler_alloc((void*) 0x2000, 4096, st1.frames, st1.depth, 0); |
| 241 heap_profiler_alloc((void*) 0x3000, 4096, st1.frames, st1.depth, 0); |
| 242 heap_profiler_alloc((void*) 0x4000, 4096, st1.frames, st1.depth, 0); |
| 243 |
| 244 heap_profiler_free((void*) 0x800, 4096, NULL); |
| 245 EXPECT_EQ(4, stats_.num_allocs); |
| 246 EXPECT_EQ(2048 + 4096 + 4096 + 4096, stats_.total_alloc_bytes); |
| 247 |
| 248 heap_profiler_free((void*) 0x1800, 4096, NULL); |
| 249 EXPECT_EQ(3, stats_.num_allocs); |
| 250 EXPECT_EQ(2048 + 4096 + 4096, stats_.total_alloc_bytes); |
| 251 |
| 252 heap_profiler_free((void*) 0x2800, 4096, NULL); |
| 253 EXPECT_EQ(2, stats_.num_allocs); |
| 254 EXPECT_EQ(2048 + 4096, stats_.total_alloc_bytes); |
| 255 |
| 256 heap_profiler_free((void*) 0x3800, 4096, NULL); |
| 257 EXPECT_EQ(1, stats_.num_allocs); |
| 258 EXPECT_EQ(2048, stats_.total_alloc_bytes); |
| 259 |
| 260 heap_profiler_free((void*) 0x4800, 4096, NULL); |
| 261 EXPECT_EQ(0, stats_.num_allocs); |
| 262 EXPECT_EQ(0, stats_.num_stack_traces); |
| 263 EXPECT_EQ(0, stats_.total_alloc_bytes); |
| 264 } |
| 265 |
| 266 TEST_F(HeapProfilerTest, AllocAndOverFreeContiguous) { |
| 267 StackTrace st1 = GenStackTrace(8, 0x0); |
| 268 StackTrace st2 = GenStackTrace(6, 0x1000); |
| 269 heap_profiler_alloc((void*) 0x1000, 4096, st1.frames, st1.depth, 0); |
| 270 heap_profiler_alloc((void*) 0x2000, 4096, st2.frames, st2.depth, 0); |
| 271 |
| 272 heap_profiler_free((void*) 0, 16834, NULL); |
| 273 |
| 274 EXPECT_EQ(0, stats_.num_allocs); |
| 275 EXPECT_EQ(0, stats_.num_stack_traces); |
| 276 EXPECT_EQ(0, stats_.total_alloc_bytes); |
| 277 } |
| 278 |
| 279 TEST_F(HeapProfilerTest, AllocContiguousAndPunchHole) { |
| 280 StackTrace st1 = GenStackTrace(8, 0x0); |
| 281 StackTrace st2 = GenStackTrace(6, 0x1000); |
| 282 heap_profiler_alloc((void*) 0x1000, 4096, st1.frames, st1.depth, 0); |
| 283 heap_profiler_alloc((void*) 0x2000, 4096, st2.frames, st2.depth, 0); |
| 284 |
| 285 // Punch a 4k hole in the middle of the two contiguous 4k allocs. |
| 286 heap_profiler_free((void*) 0x1800, 4096, NULL); |
| 287 |
| 288 EXPECT_EQ(2, stats_.num_allocs); |
| 289 EXPECT_EQ(2, stats_.num_stack_traces); |
| 290 EXPECT_EQ(4096, stats_.total_alloc_bytes); |
| 291 } |
| 292 |
| 293 TEST_F(HeapProfilerTest, AllocAndPartialFree) { |
| 294 StackTrace st1 = GenStackTrace(8, 0x0); |
| 295 StackTrace st2 = GenStackTrace(6, 0x1000); |
| 296 StackTrace st3 = GenStackTrace(7, 0x2000); |
| 297 StackTrace st4 = GenStackTrace(9, 0x3000); |
| 298 heap_profiler_alloc((void*) 0x1000, 1024, st1.frames, st1.depth, 0); |
| 299 heap_profiler_alloc((void*) 0x2000, 1024, st2.frames, st2.depth, 0); |
| 300 heap_profiler_alloc((void*) 0x3000, 1024, st3.frames, st3.depth, 0); |
| 301 heap_profiler_alloc((void*) 0x4000, 1024, st4.frames, st4.depth, 0); |
| 302 |
| 303 heap_profiler_free((void*) 0x1000, 128, NULL); // Shrink left by 128B. |
| 304 heap_profiler_free((void*) 0x2380, 128, NULL); // Shrink right by 128B. |
| 305 heap_profiler_free((void*) 0x3100, 512, NULL); // 512B hole in the middle. |
| 306 heap_profiler_free((void*) 0x4000, 512, NULL); // Free up the 4th vma... |
| 307 heap_profiler_free((void*) 0x4200, 512, NULL); // ...but do it in two halves. |
| 308 |
| 309 EXPECT_EQ(4, stats_.num_allocs); // 1 + 2 + two sides around the hole 3. |
| 310 EXPECT_EQ(3, stats_.num_stack_traces); // st4 should be gone. |
| 311 EXPECT_EQ(896 + 896 + 512, stats_.total_alloc_bytes); |
| 312 } |
| 313 |
| 314 TEST_F(HeapProfilerTest, RandomIndividualAllocAndFrees) { |
| 315 static const size_t NUM_ST = 128; |
| 316 static const size_t NUM_OPS = 1000; |
| 317 |
| 318 StackTrace st[NUM_ST]; |
| 319 for (uint32_t i = 0; i < NUM_ST; ++i) |
| 320 st[i] = GenStackTrace((i % 10) + 2, i * 128); |
| 321 |
| 322 for (size_t i = 0; i < NUM_OPS; ++i) { |
| 323 uintptr_t start = ((i + 7) << 8) & (0xffffff); |
| 324 size_t size = (start >> 16) & 0x0fff; |
| 325 if (i & 1) { |
| 326 StackTrace* s = &st[start % NUM_ST]; |
| 327 heap_profiler_alloc((void*) start, size, s->frames, s->depth, 0); |
| 328 } else { |
| 329 heap_profiler_free((void*) start, size, NULL); |
| 330 } |
| 331 CheckVMAvsStacktaceConsistency(); |
| 332 } |
| 333 } |
| 334 |
| 335 TEST_F(HeapProfilerTest, RandomAllocAndFreesBatches) { |
| 336 static const size_t NUM_ST = 128; |
| 337 static const size_t NUM_ALLOCS = 100; |
| 338 |
| 339 StackTrace st[NUM_ST]; |
| 340 for (size_t i = 0; i < NUM_ST; ++i) |
| 341 st[i] = GenStackTrace((i % 10) + 2, i * NUM_ST); |
| 342 |
| 343 for (int repeat = 0; repeat < 5; ++repeat) { |
| 344 for (size_t i = 0; i < NUM_ALLOCS; ++i) { |
| 345 StackTrace* s = &st[i % NUM_ST]; |
| 346 heap_profiler_alloc( |
| 347 (void*) (i * 4096), ((i + 1) * 32) % 4097, s->frames, s->depth, 0); |
| 348 CheckVMAvsStacktaceConsistency(); |
| 349 } |
| 350 |
| 351 for (size_t i = 0; i < NUM_ALLOCS; ++i) { |
| 352 heap_profiler_free((void*) (i * 1024), ((i + 1) * 64) % 16000, NULL); |
| 353 CheckVMAvsStacktaceConsistency(); |
| 354 } |
| 355 } |
| 356 } |
| 357 |
| 358 TEST_F(HeapProfilerTest, UnwindStackTooLargeShouldSaturate) { |
| 359 StackTrace st1 = GenStackTrace(HEAP_PROFILER_MAX_DEPTH, 0x0); |
| 360 uintptr_t many_frames[100] = {}; |
| 361 memcpy(many_frames, st1.frames, sizeof(uintptr_t) * st1.depth); |
| 362 heap_profiler_alloc((void*) 0x1000, 1024, many_frames, 100, 0); |
| 363 ExpectVMA(0x1000, 0x13ff, st1, 0); |
| 364 } |
| 365 |
| 366 TEST_F(HeapProfilerTest, NoUnwindShouldNotCrashButNoop) { |
| 367 heap_profiler_alloc((void*) 0x1000, 1024, NULL, 0, 0); |
| 368 EXPECT_EQ(0, stats_.num_allocs); |
| 369 EXPECT_EQ(0, stats_.num_stack_traces); |
| 370 EXPECT_EQ(0, stats_.total_alloc_bytes); |
| 371 } |
| 372 |
| 373 TEST_F(HeapProfilerTest, FreeNonExisting) { |
| 374 StackTrace st1 = GenStackTrace(5, 0x0); |
| 375 heap_profiler_free((void*) 0x1000, 1024, NULL); |
| 376 heap_profiler_free((void*) 0x1400, 1024, NULL); |
| 377 EXPECT_EQ(0, stats_.num_allocs); |
| 378 EXPECT_EQ(0, stats_.num_stack_traces); |
| 379 EXPECT_EQ(0, stats_.total_alloc_bytes); |
| 380 heap_profiler_alloc((void*) 0x1000, 1024, st1.frames, st1.depth, 0); |
| 381 EXPECT_EQ(1, stats_.num_allocs); |
| 382 EXPECT_EQ(1024, stats_.total_alloc_bytes); |
| 383 } |
| 384 |
| 385 TEST_F(HeapProfilerTest, FlagsConsistency) { |
| 386 StackTrace st1 = GenStackTrace(8, 0x0); |
| 387 uint32_t flags = 0; |
| 388 heap_profiler_alloc((void*) 0x1000, 4096, st1.frames, st1.depth, 42); |
| 389 heap_profiler_alloc((void*) 0x2000, 4096, st1.frames, st1.depth, 142); |
| 390 |
| 391 ExpectVMA(0x1000, 0x1fff, st1, 42); |
| 392 ExpectVMA(0x2000, 0x2fff, st1, 142); |
| 393 |
| 394 // Punch a 4k hole in the middle of the two contiguous 4k allocs. |
| 395 heap_profiler_free((void*) 0x1800, 4096, NULL); |
| 396 |
| 397 ExpectVMA(0x1000, 0x17ff, st1, 42); |
| 398 heap_profiler_free((void*) 0x1000, 2048, &flags); |
| 399 EXPECT_EQ(42, flags); |
| 400 |
| 401 ExpectVMA(0x2800, 0x2fff, st1, 142); |
| 402 heap_profiler_free((void*) 0x2800, 2048, &flags); |
| 403 EXPECT_EQ(142, flags); |
| 404 } |
| 405 |
| 406 TEST_F(HeapProfilerTest, BeConsistentOnOOM) { |
| 407 static const size_t NUM_ALLOCS = 512 * 1024; |
| 408 uintptr_t frames[1]; |
| 409 |
| 410 for (uintptr_t i = 0; i < NUM_ALLOCS; ++i) { |
| 411 frames[0] = i; |
| 412 heap_profiler_alloc((void*) (i * 32), 32, frames, 1, 0); |
| 413 } |
| 414 |
| 415 CheckVMAvsStacktaceConsistency(); |
| 416 // Check that we're saturating, otherwise this entire test is pointless. |
| 417 EXPECT_LT(stats_.num_allocs, NUM_ALLOCS); |
| 418 EXPECT_LT(stats_.num_stack_traces, NUM_ALLOCS); |
| 419 |
| 420 for (uintptr_t i = 0; i < NUM_ALLOCS; ++i) |
| 421 heap_profiler_free((void*) (i * 32), 32, NULL); |
| 422 |
| 423 EXPECT_EQ(0, stats_.num_allocs); |
| 424 EXPECT_EQ(0, stats_.total_alloc_bytes); |
| 425 EXPECT_EQ(0, stats_.num_stack_traces); |
| 426 } |
| 427 |
| 428 #ifdef __LP64__ |
| 429 TEST_F(HeapProfilerTest, Test64Bit) { |
| 430 StackTrace st1 = GenStackTrace(8, 0x0); |
| 431 StackTrace st2 = GenStackTrace(10, 0x7fffffff70000000LL); |
| 432 StackTrace st3 = GenStackTrace(10, 0xffffffff70000000LL); |
| 433 heap_profiler_alloc((void*) 0x1000, 4096, st1.frames, st1.depth, 0); |
| 434 heap_profiler_alloc( |
| 435 (void*) 0x7ffffffffffff000LL, 4096, st2.frames, st2.depth); |
| 436 heap_profiler_alloc( |
| 437 (void*) 0xfffffffffffff000LL, 4096, st3.frames, st3.depth); |
| 438 EXPECT_EQ(3, stats_.num_allocs); |
| 439 EXPECT_EQ(3, stats_.num_stack_traces); |
| 440 EXPECT_EQ(4096 + 4096 + 4096, stats_.total_alloc_bytes); |
| 441 |
| 442 heap_profiler_free((void*) 0x1000, 4096, NULL); |
| 443 EXPECT_EQ(2, stats_.num_allocs); |
| 444 EXPECT_EQ(2, stats_.num_stack_traces); |
| 445 EXPECT_EQ(4096 + 4096, stats_.total_alloc_bytes); |
| 446 |
| 447 heap_profiler_free((void*) 0x7ffffffffffff000LL, 4096, NULL); |
| 448 EXPECT_EQ(1, stats_.num_allocs); |
| 449 EXPECT_EQ(1, stats_.num_stack_traces); |
| 450 EXPECT_EQ(4096, stats_.total_alloc_bytes); |
| 451 |
| 452 heap_profiler_free((void*) 0xfffffffffffff000LL, 4096, NULL); |
| 453 EXPECT_EQ(0, stats_.num_allocs); |
| 454 EXPECT_EQ(0, stats_.num_stack_traces); |
| 455 EXPECT_EQ(0, stats_.total_alloc_bytes); |
| 456 } |
| 457 #endif |
| 458 |
| 459 } // namespace |
| 460 |
| 461 int main(int argc, char** argv) { |
| 462 testing::InitGoogleTest(&argc, argv); |
| 463 return RUN_ALL_TESTS(); |
| 464 } |
OLD | NEW |