Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(991)

Side by Side Diff: components/metrics/leak_detector/leak_detector_impl_unittest.cc

Issue 1878863004: Revert of Record call site history in LeakDetectorImpl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
24 namespace { 28 namespace {
25 29
26 // Makes working with complex numbers easier. 30 // Makes working with complex numbers easier.
27 using Complex = std::complex<double>; 31 using Complex = std::complex<double>;
28 32
29 // The mapping location in memory for a fictional executable. 33 // The mapping location in memory for a fictional executable.
30 const uintptr_t kMappingAddr = 0x800000; 34 const uintptr_t kMappingAddr = 0x800000;
31 const size_t kMappingSize = 0x200000; 35 const size_t kMappingSize = 0x200000;
32 36
33 // Some call stacks within the fictional executable. 37 // Some call stacks within the fictional executable.
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 alloced_ptrs_.clear(); 132 alloced_ptrs_.clear();
129 133
130 // Must destroy all objects that use CustomAllocator before shutting down. 134 // Must destroy all objects that use CustomAllocator before shutting down.
131 detector_.reset(); 135 detector_.reset();
132 stored_reports_.clear(); 136 stored_reports_.clear();
133 137
134 EXPECT_TRUE(CustomAllocator::Shutdown()); 138 EXPECT_TRUE(CustomAllocator::Shutdown());
135 } 139 }
136 140
137 protected: 141 protected:
138 using InternalLeakReport = LeakDetectorImpl::LeakReport;
139 template <typename T>
140 using InternalVector = LeakDetectorImpl::InternalVector<T>;
141 using AllocationBreakdown = LeakDetectorImpl::LeakReport::AllocationBreakdown;
142
143 // Alloc and free functions that allocate and free heap memory and 142 // Alloc and free functions that allocate and free heap memory and
144 // automatically pass alloc/free info to |detector_|. They emulate the 143 // automatically pass alloc/free info to |detector_|. They emulate the
145 // alloc/free hook functions that would call into LeakDetectorImpl in 144 // alloc/free hook functions that would call into LeakDetectorImpl in
146 // real-life usage. They also keep track of individual allocations locally, so 145 // real-life usage. They also keep track of individual allocations locally, so
147 // any leaked memory could be cleaned up. 146 // any leaked memory could be cleaned up.
148 // 147 //
149 // |stack| is just a nominal call stack object to identify the call site. It 148 // |stack| is just a nominal call stack object to identify the call site. It
150 // doesn't have to contain the stack trace of the actual call stack. 149 // doesn't have to contain the stack trace of the actual call stack.
151 void* Alloc(size_t size, const TestCallStack& stack) { 150 void* Alloc(size_t size, const TestCallStack& stack) {
152 void* ptr = new char[size]; 151 void* ptr = new char[size];
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
526 // - Each inner loop iteration allocates a net of 1x 32 bytes and 1x 48 bytes. 525 // - Each inner loop iteration allocates a net of 1x 32 bytes and 1x 48 bytes.
527 // - Each outer loop iteration allocates a net of 32x 32 bytes and 32x 48 526 // - Each outer loop iteration allocates a net of 32x 32 bytes and 32x 48
528 // bytes. 527 // bytes.
529 // - However, the leak analysis happens after the allocs but before the frees 528 // - However, the leak analysis happens after the allocs but before the frees
530 // that come right after. So it should count the two extra allocs made at 529 // that come right after. So it should count the two extra allocs made at
531 // call sites |kStack3| and |kStack4|. The formula is |(i + 1) * 32 + 2|, 530 // call sites |kStack3| and |kStack4|. The formula is |(i + 1) * 32 + 2|,
532 // where |i| is the iteration index. 531 // where |i| is the iteration index.
533 // There should have been one leak analysis per outer loop iteration, for a 532 // There should have been one leak analysis per outer loop iteration, for a
534 // total of 20 history records (|kNumOuterIterations|) per report. 533 // total of 20 history records (|kNumOuterIterations|) per report.
535 534
536 const auto& report1_history = report1.alloc_breakdown_history(); 535 const auto& report1_size_history = report1.size_breakdown_history();
537 EXPECT_EQ(20U, report1_history.size()); 536 EXPECT_EQ(20U, report1_size_history.size());
538 537
539 for (size_t i = 0; i < report1_history.size(); ++i) { 538 size_t index = 0;
540 const AllocationBreakdown& entry = report1_history[i]; 539 for (const InternalVector<uint32_t>& entry : report1_size_history) {
541 540 ASSERT_GT(entry.size(), SizeToIndex(48));
542 const InternalVector<uint32_t>& counts_by_size = entry.counts_by_size;
543 ASSERT_GT(counts_by_size.size(), SizeToIndex(48));
544 541
545 // Check the two leaky sizes, 32 and 48. 542 // Check the two leaky sizes, 32 and 48.
546 uint32_t expected_leaky_count = (i + 1) * 32 + 2; 543 EXPECT_EQ((index + 1) * 32 + 2, entry[SizeToIndex(32)]);
547 EXPECT_EQ(expected_leaky_count, counts_by_size[SizeToIndex(32)]); 544 EXPECT_EQ((index + 1) * 32 + 2, entry[SizeToIndex(48)]);
548 EXPECT_EQ(expected_leaky_count, counts_by_size[SizeToIndex(48)]);
549 545
550 // Not related to the leaks, but there should be a dangling 16-byte 546 // Not related to the leaks, but there should be a dangling 16-byte
551 // allocation during each leak analysis, because it hasn't yet been freed. 547 // allocation during each leak analysis, because it hasn't yet been freed.
552 EXPECT_EQ(1U, counts_by_size[SizeToIndex(16)]); 548 EXPECT_EQ(1U, entry[SizeToIndex(16)]);
549 ++index;
553 } 550 }
554 551
555 // Check call site count over time. 552 // |report2| should have the same size history as |report1|.
556 ASSERT_LT(kSizeSuspicionThreshold, report1_history.size()); 553 const auto& report2_size_history = report2.size_breakdown_history();
557 // Initially, there has been no call site tracking. 554 EXPECT_TRUE(std::equal(report1_size_history.begin(),
558 for (size_t i = 0; i < kSizeSuspicionThreshold; ++i) 555 report1_size_history.end(),
559 EXPECT_EQ(0U, report1_history[i].count_for_call_stack); 556 report2_size_history.begin()));
560
561 // Once |kSizeSuspicionThreshold| has been reached and call site tracking has
562 // begun, the number of allocations for the suspected call site should
563 // increase by 32 each frame. See comments above.
564 uint32_t expected_call_stack_count = 0;
565 for (size_t i = kSizeSuspicionThreshold; i < report1_history.size(); ++i) {
566 EXPECT_EQ(expected_call_stack_count,
567 report1_history[i].count_for_call_stack);
568 expected_call_stack_count += 32;
569 }
570
571 // |report2| should have the same size history and call stack history as
572 // |report1|.
573 const auto& report2_history = report2.alloc_breakdown_history();
574 auto compare_history_func =
575 [](AllocationBreakdown a, AllocationBreakdown b) -> bool {
576 return std::equal(a.counts_by_size.begin(), a.counts_by_size.end(),
577 b.counts_by_size.begin()) &&
578 a.count_for_call_stack == b.count_for_call_stack;
579 };
580 EXPECT_TRUE(std::equal(report1_history.begin(), report1_history.end(),
581 report2_history.begin(), compare_history_func));
582 } 557 }
583 558
584 TEST_F(LeakDetectorImplTest, JuliaSetNoLeak) { 559 TEST_F(LeakDetectorImplTest, JuliaSetNoLeak) {
585 JuliaSet(false /* enable_leaks */); 560 JuliaSet(false /* enable_leaks */);
586 561
587 // JuliaSet() should have run cleanly without leaking. 562 // JuliaSet() should have run cleanly without leaking.
588 EXPECT_EQ(total_num_allocs_, total_num_frees_); 563 EXPECT_EQ(total_num_allocs_, total_num_frees_);
589 EXPECT_EQ(0U, alloced_ptrs_.size()); 564 EXPECT_EQ(0U, alloced_ptrs_.size());
590 ASSERT_EQ(0U, stored_reports_.size()); 565 ASSERT_EQ(0U, stored_reports_.size());
591 } 566 }
(...skipping 24 matching lines...) Expand all
616 // |kStack4|. 591 // |kStack4|.
617 const InternalLeakReport& report2 = *(++stored_reports_.begin()); 592 const InternalLeakReport& report2 = *(++stored_reports_.begin());
618 EXPECT_EQ(sizeof(Complex) + 52, report2.alloc_size_bytes()); 593 EXPECT_EQ(sizeof(Complex) + 52, report2.alloc_size_bytes());
619 ASSERT_EQ(kStack4.depth, report2.call_stack().size()); 594 ASSERT_EQ(kStack4.depth, report2.call_stack().size());
620 for (size_t i = 0; i < kStack4.depth; ++i) { 595 for (size_t i = 0; i < kStack4.depth; ++i) {
621 EXPECT_EQ(GetOffsetInMapping(kStack4.stack[i]), 596 EXPECT_EQ(GetOffsetInMapping(kStack4.stack[i]),
622 report2.call_stack()[i]) << i; 597 report2.call_stack()[i]) << i;
623 } 598 }
624 599
625 // Check |report1|'s historical data. 600 // Check |report1|'s historical data.
626 const auto& report1_history = report1.alloc_breakdown_history(); 601 const auto& report1_size_history = report1.size_breakdown_history();
627 // Computing the exact number of leak analyses is not trivial, but we know it 602 // Computing the exact number of leak analyses is not trivial, but we know it
628 // must be at least |kSizeSuspicionThreshold + kCallStackSuspicionThreshold| 603 // must be at least |kSizeSuspicionThreshold + kCallStackSuspicionThreshold|
629 // in order to have generated a report. 604 // in order to have generated a report.
630 EXPECT_GT(report1_history.size(), 605 EXPECT_GT(report1_size_history.size(),
631 kSizeSuspicionThreshold + kCallStackSuspicionThreshold); 606 kSizeSuspicionThreshold + kCallStackSuspicionThreshold);
632 607
633 // Make sure that the final allocation counts for the leaky sizes are larger 608 // Make sure that the final allocation counts for the leaky sizes are larger
634 // than that of the non-leaky size by at least an order of magnitude. 609 // than that of the non-leaky size by at least an order of magnitude.
635 const AllocationBreakdown& final_entry = *report1_history.rbegin(); 610 const InternalVector<uint32_t>& final_entry = *report1_size_history.rbegin();
636 const InternalVector<uint32_t>& counts_by_size = final_entry.counts_by_size;
637 uint32_t size_0_index = SizeToIndex(sizeof(Complex) + 24); 611 uint32_t size_0_index = SizeToIndex(sizeof(Complex) + 24);
638 uint32_t size_1_index = SizeToIndex(sizeof(Complex) + 40); 612 uint32_t size_1_index = SizeToIndex(sizeof(Complex) + 40);
639 uint32_t size_2_index = SizeToIndex(sizeof(Complex) + 52); 613 uint32_t size_2_index = SizeToIndex(sizeof(Complex) + 52);
640 ASSERT_LT(size_0_index, counts_by_size.size()); 614 ASSERT_LT(size_0_index, final_entry.size());
641 ASSERT_LT(size_1_index, counts_by_size.size()); 615 ASSERT_LT(size_1_index, final_entry.size());
642 ASSERT_LT(size_2_index, counts_by_size.size()); 616 ASSERT_LT(size_2_index, final_entry.size());
643 617
644 EXPECT_GT(counts_by_size[size_1_index], counts_by_size[size_0_index] * 10); 618 EXPECT_GT(final_entry[size_1_index], final_entry[size_0_index] * 10);
645 EXPECT_GT(counts_by_size[size_2_index], counts_by_size[size_0_index] * 10); 619 EXPECT_GT(final_entry[size_2_index], final_entry[size_0_index] * 10);
646 620
647 // |report2| should have the same size history as |report1|, but not the same 621 // |report2| should have the same size history as |report1|.
648 // call stack history. 622 const auto& report2_size_history = report2.size_breakdown_history();
649 const auto& report2_history = report2.alloc_breakdown_history(); 623 EXPECT_TRUE(std::equal(report1_size_history.begin(),
650 auto compare_size_history_func = 624 report1_size_history.end(),
651 [](AllocationBreakdown a, AllocationBreakdown b) -> bool { 625 report2_size_history.begin()));
652 return std::equal(a.counts_by_size.begin(), a.counts_by_size.end(),
653 b.counts_by_size.begin());
654 };
655 EXPECT_TRUE(std::equal(report1_history.begin(), report1_history.end(),
656 report2_history.begin(), compare_size_history_func));
657 } 626 }
658 627
659 } // namespace leak_detector 628 } // namespace leak_detector
660 } // namespace metrics 629 } // namespace metrics
OLDNEW
« no previous file with comments | « components/metrics/leak_detector/leak_detector_impl.cc ('k') | components/metrics/leak_detector/ranked_set.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698