Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/metrics/leak_detector/leak_detector_impl.h" | 5 #include "components/metrics/leak_detector/leak_detector_impl.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <complex> | 11 #include <complex> |
| 12 #include <new> | 12 #include <new> |
| 13 #include <set> | 13 #include <set> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "components/metrics/leak_detector/custom_allocator.h" | 18 #include "components/metrics/leak_detector/custom_allocator.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 namespace metrics { | 21 namespace metrics { |
| 22 namespace leak_detector { | 22 namespace leak_detector { |
| 23 | 23 |
| 24 using InternalLeakReport = LeakDetectorImpl::LeakReport; | |
| 25 template <typename T> | |
| 26 using InternalVector = LeakDetectorImpl::InternalVector<T>; | |
| 27 | |
| 28 namespace { | 24 namespace { |
| 29 | 25 |
| 30 // Makes working with complex numbers easier. | 26 // Makes working with complex numbers easier. |
| 31 using Complex = std::complex<double>; | 27 using Complex = std::complex<double>; |
| 32 | 28 |
| 33 // The mapping location in memory for a fictional executable. | 29 // The mapping location in memory for a fictional executable. |
| 34 const uintptr_t kMappingAddr = 0x800000; | 30 const uintptr_t kMappingAddr = 0x800000; |
| 35 const size_t kMappingSize = 0x200000; | 31 const size_t kMappingSize = 0x200000; |
| 36 | 32 |
| 37 // Some call stacks within the fictional executable. | 33 // Some call stacks within the fictional executable. |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 132 alloced_ptrs_.clear(); | 128 alloced_ptrs_.clear(); |
| 133 | 129 |
| 134 // Must destroy all objects that use CustomAllocator before shutting down. | 130 // Must destroy all objects that use CustomAllocator before shutting down. |
| 135 detector_.reset(); | 131 detector_.reset(); |
| 136 stored_reports_.clear(); | 132 stored_reports_.clear(); |
| 137 | 133 |
| 138 EXPECT_TRUE(CustomAllocator::Shutdown()); | 134 EXPECT_TRUE(CustomAllocator::Shutdown()); |
| 139 } | 135 } |
| 140 | 136 |
| 141 protected: | 137 protected: |
| 138 using InternalLeakReport = LeakDetectorImpl::LeakReport; | |
| 139 template <typename T> | |
| 140 using InternalVector = LeakDetectorImpl::InternalVector<T>; | |
| 141 using AllocationBreakdown = LeakDetectorImpl::LeakReport::AllocationBreakdown; | |
| 142 | |
| 142 // Alloc and free functions that allocate and free heap memory and | 143 // Alloc and free functions that allocate and free heap memory and |
| 143 // automatically pass alloc/free info to |detector_|. They emulate the | 144 // automatically pass alloc/free info to |detector_|. They emulate the |
| 144 // alloc/free hook functions that would call into LeakDetectorImpl in | 145 // alloc/free hook functions that would call into LeakDetectorImpl in |
| 145 // real-life usage. They also keep track of individual allocations locally, so | 146 // real-life usage. They also keep track of individual allocations locally, so |
| 146 // any leaked memory could be cleaned up. | 147 // any leaked memory could be cleaned up. |
| 147 // | 148 // |
| 148 // |stack| is just a nominal call stack object to identify the call site. It | 149 // |stack| is just a nominal call stack object to identify the call site. It |
| 149 // doesn't have to contain the stack trace of the actual call stack. | 150 // doesn't have to contain the stack trace of the actual call stack. |
| 150 void* Alloc(size_t size, const TestCallStack& stack) { | 151 void* Alloc(size_t size, const TestCallStack& stack) { |
| 151 void* ptr = new char[size]; | 152 void* ptr = new char[size]; |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 525 // - Each inner loop iteration allocates a net of 1x 32 bytes and 1x 48 bytes. | 526 // - Each inner loop iteration allocates a net of 1x 32 bytes and 1x 48 bytes. |
| 526 // - Each outer loop iteration allocates a net of 32x 32 bytes and 32x 48 | 527 // - Each outer loop iteration allocates a net of 32x 32 bytes and 32x 48 |
| 527 // bytes. | 528 // bytes. |
| 528 // - However, the leak analysis happens after the allocs but before the frees | 529 // - However, the leak analysis happens after the allocs but before the frees |
| 529 // that come right after. So it should count the two extra allocs made at | 530 // that come right after. So it should count the two extra allocs made at |
| 530 // call sites |kStack3| and |kStack4|. The formula is |(i + 1) * 32 + 2|, | 531 // call sites |kStack3| and |kStack4|. The formula is |(i + 1) * 32 + 2|, |
| 531 // where |i| is the iteration index. | 532 // where |i| is the iteration index. |
| 532 // There should have been one leak analysis per outer loop iteration, for a | 533 // There should have been one leak analysis per outer loop iteration, for a |
| 533 // total of 20 history records (|kNumOuterIterations|) per report. | 534 // total of 20 history records (|kNumOuterIterations|) per report. |
| 534 | 535 |
| 535 const auto& report1_size_history = report1.size_breakdown_history(); | 536 const auto& report1_history = report1.alloc_breakdown_history(); |
| 536 EXPECT_EQ(20U, report1_size_history.size()); | 537 EXPECT_EQ(20U, report1_history.size()); |
| 537 | 538 |
| 538 size_t index = 0; | 539 size_t index = 0; |
| 539 for (const InternalVector<uint32_t>& entry : report1_size_history) { | 540 for (const AllocationBreakdown& entry : report1_history) { |
| 540 ASSERT_GT(entry.size(), SizeToIndex(48)); | 541 const InternalVector<uint32_t>& counts_by_size = entry.counts_by_size; |
| 542 ASSERT_GT(counts_by_size.size(), SizeToIndex(48)); | |
| 541 | 543 |
| 542 // Check the two leaky sizes, 32 and 48. | 544 // Check the two leaky sizes, 32 and 48. |
| 543 EXPECT_EQ((index + 1) * 32 + 2, entry[SizeToIndex(32)]); | 545 EXPECT_EQ((index + 1) * 32 + 2, counts_by_size[SizeToIndex(32)]); |
| 544 EXPECT_EQ((index + 1) * 32 + 2, entry[SizeToIndex(48)]); | 546 EXPECT_EQ((index + 1) * 32 + 2, counts_by_size[SizeToIndex(48)]); |
| 545 | 547 |
| 546 // Not related to the leaks, but there should be a dangling 16-byte | 548 // Not related to the leaks, but there should be a dangling 16-byte |
| 547 // allocation during each leak analysis, because it hasn't yet been freed. | 549 // allocation during each leak analysis, because it hasn't yet been freed. |
| 548 EXPECT_EQ(1U, entry[SizeToIndex(16)]); | 550 EXPECT_EQ(1U, counts_by_size[SizeToIndex(16)]); |
| 549 ++index; | 551 ++index; |
| 550 } | 552 } |
|
Simon Que
2016/04/09 00:15:58
Still need to check the call stack breakdown.
Simon Que
2016/04/09 01:04:32
Done.
| |
| 551 | 553 |
| 552 // |report2| should have the same size history as |report1|. | 554 // |report2| should have the same size history and call stack history as |
| 553 const auto& report2_size_history = report2.size_breakdown_history(); | 555 // |report1|. |
| 554 EXPECT_TRUE(std::equal(report1_size_history.begin(), | 556 const auto& report2_history = report2.alloc_breakdown_history(); |
| 555 report1_size_history.end(), | 557 auto compare_history_func = |
| 556 report2_size_history.begin())); | 558 [](AllocationBreakdown a, AllocationBreakdown b) -> bool { |
| 559 return std::equal(a.counts_by_size.begin(), a.counts_by_size.end(), | |
| 560 b.counts_by_size.begin()) && | |
| 561 a.count_for_call_stack == b.count_for_call_stack; | |
| 562 }; | |
| 563 EXPECT_TRUE(std::equal(report1_history.begin(), report1_history.end(), | |
| 564 report2_history.begin(), compare_history_func)); | |
| 557 } | 565 } |
| 558 | 566 |
| 559 TEST_F(LeakDetectorImplTest, JuliaSetNoLeak) { | 567 TEST_F(LeakDetectorImplTest, JuliaSetNoLeak) { |
| 560 JuliaSet(false /* enable_leaks */); | 568 JuliaSet(false /* enable_leaks */); |
| 561 | 569 |
| 562 // JuliaSet() should have run cleanly without leaking. | 570 // JuliaSet() should have run cleanly without leaking. |
| 563 EXPECT_EQ(total_num_allocs_, total_num_frees_); | 571 EXPECT_EQ(total_num_allocs_, total_num_frees_); |
| 564 EXPECT_EQ(0U, alloced_ptrs_.size()); | 572 EXPECT_EQ(0U, alloced_ptrs_.size()); |
| 565 ASSERT_EQ(0U, stored_reports_.size()); | 573 ASSERT_EQ(0U, stored_reports_.size()); |
| 566 } | 574 } |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 591 // |kStack4|. | 599 // |kStack4|. |
| 592 const InternalLeakReport& report2 = *(++stored_reports_.begin()); | 600 const InternalLeakReport& report2 = *(++stored_reports_.begin()); |
| 593 EXPECT_EQ(sizeof(Complex) + 52, report2.alloc_size_bytes()); | 601 EXPECT_EQ(sizeof(Complex) + 52, report2.alloc_size_bytes()); |
| 594 ASSERT_EQ(kStack4.depth, report2.call_stack().size()); | 602 ASSERT_EQ(kStack4.depth, report2.call_stack().size()); |
| 595 for (size_t i = 0; i < kStack4.depth; ++i) { | 603 for (size_t i = 0; i < kStack4.depth; ++i) { |
| 596 EXPECT_EQ(GetOffsetInMapping(kStack4.stack[i]), | 604 EXPECT_EQ(GetOffsetInMapping(kStack4.stack[i]), |
| 597 report2.call_stack()[i]) << i; | 605 report2.call_stack()[i]) << i; |
| 598 } | 606 } |
| 599 | 607 |
| 600 // Check |report1|'s historical data. | 608 // Check |report1|'s historical data. |
| 601 const auto& report1_size_history = report1.size_breakdown_history(); | 609 const auto& report1_history = report1.alloc_breakdown_history(); |
| 602 // Computing the exact number of leak analyses is not trivial, but we know it | 610 // Computing the exact number of leak analyses is not trivial, but we know it |
| 603 // must be at least |kSizeSuspicionThreshold + kCallStackSuspicionThreshold| | 611 // must be at least |kSizeSuspicionThreshold + kCallStackSuspicionThreshold| |
| 604 // in order to have generated a report. | 612 // in order to have generated a report. |
| 605 EXPECT_GT(report1_size_history.size(), | 613 EXPECT_GT(report1_history.size(), |
| 606 kSizeSuspicionThreshold + kCallStackSuspicionThreshold); | 614 kSizeSuspicionThreshold + kCallStackSuspicionThreshold); |
| 607 | 615 |
| 608 // Make sure that the final allocation counts for the leaky sizes are larger | 616 // Make sure that the final allocation counts for the leaky sizes are larger |
| 609 // than that of the non-leaky size by at least an order of magnitude. | 617 // than that of the non-leaky size by at least an order of magnitude. |
| 610 const InternalVector<uint32_t>& final_entry = *report1_size_history.rbegin(); | 618 const AllocationBreakdown& final_entry = *report1_history.rbegin(); |
| 619 const InternalVector<uint32_t>& counts_by_size = final_entry.counts_by_size; | |
| 611 uint32_t size_0_index = SizeToIndex(sizeof(Complex) + 24); | 620 uint32_t size_0_index = SizeToIndex(sizeof(Complex) + 24); |
| 612 uint32_t size_1_index = SizeToIndex(sizeof(Complex) + 40); | 621 uint32_t size_1_index = SizeToIndex(sizeof(Complex) + 40); |
| 613 uint32_t size_2_index = SizeToIndex(sizeof(Complex) + 52); | 622 uint32_t size_2_index = SizeToIndex(sizeof(Complex) + 52); |
| 614 ASSERT_LT(size_0_index, final_entry.size()); | 623 ASSERT_LT(size_0_index, counts_by_size.size()); |
| 615 ASSERT_LT(size_1_index, final_entry.size()); | 624 ASSERT_LT(size_1_index, counts_by_size.size()); |
| 616 ASSERT_LT(size_2_index, final_entry.size()); | 625 ASSERT_LT(size_2_index, counts_by_size.size()); |
| 617 | 626 |
| 618 EXPECT_GT(final_entry[size_1_index], final_entry[size_0_index] * 10); | 627 EXPECT_GT(counts_by_size[size_1_index], counts_by_size[size_0_index] * 10); |
| 619 EXPECT_GT(final_entry[size_2_index], final_entry[size_0_index] * 10); | 628 EXPECT_GT(counts_by_size[size_2_index], counts_by_size[size_0_index] * 10); |
| 620 | 629 |
| 621 // |report2| should have the same size history as |report1|. | 630 // |report2| should have the same size history as |report1|, but not the same |
| 622 const auto& report2_size_history = report2.size_breakdown_history(); | 631 // call stack history. |
| 623 EXPECT_TRUE(std::equal(report1_size_history.begin(), | 632 const auto& report2_history = report2.alloc_breakdown_history(); |
| 624 report1_size_history.end(), | 633 auto compare_size_history_func = |
| 625 report2_size_history.begin())); | 634 [](AllocationBreakdown a, AllocationBreakdown b) -> bool { |
| 635 return std::equal(a.counts_by_size.begin(), a.counts_by_size.end(), | |
| 636 b.counts_by_size.begin()); | |
| 637 }; | |
| 638 EXPECT_TRUE(std::equal(report1_history.begin(), report1_history.end(), | |
| 639 report2_history.begin(), compare_size_history_func)); | |
| 626 } | 640 } |
| 627 | 641 |
| 628 } // namespace leak_detector | 642 } // namespace leak_detector |
| 629 } // namespace metrics | 643 } // namespace metrics |
| OLD | NEW |