| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.h" | 5 #include "components/metrics/leak_detector/leak_detector.h" |
| 6 | 6 |
| 7 #include <algorithm> |
| 7 #include <set> | 8 #include <set> |
| 8 | 9 |
| 9 #include "base/allocator/allocator_extension.h" | 10 #include "base/allocator/allocator_extension.h" |
| 10 #include "base/macros.h" | 11 #include "base/macros.h" |
| 11 #include "content/public/test/test_browser_thread_bundle.h" | 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 14 |
| 14 namespace metrics { | 15 namespace metrics { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // Default values for LeakDetector params. See header file for the meaning of | 19 // Default values for LeakDetector params. See header file for the meaning of |
| 19 // each parameter. | 20 // each parameter. |
| 20 const float kDefaultSamplingRate = 1.0f; | 21 const float kDefaultSamplingRate = 1.0f; |
| 21 const size_t kDefaultMaxCallStackUnwindDepth = 4; | 22 const size_t kDefaultMaxCallStackUnwindDepth = 4; |
| 22 const uint64_t kDefaultAnalysisIntervalBytes = 32 * 1024 * 1024; // 32 MiB. | 23 const uint64_t kDefaultAnalysisIntervalBytes = 32 * 1024 * 1024; // 32 MiB. |
| 23 const uint32_t kDefaultSizeSuspicionThreshold = 4; | 24 const uint32_t kDefaultSizeSuspicionThreshold = 4; |
| 24 const uint32_t kDefaultCallStackSuspicionThreshold = 4; | 25 const uint32_t kDefaultCallStackSuspicionThreshold = 4; |
| 25 | 26 |
| 26 using LeakReport = LeakDetector::LeakReport; | |
| 27 | |
| 28 // Observer class that receives leak reports and stores them in |reports_|. | 27 // Observer class that receives leak reports and stores them in |reports_|. |
| 29 // Only one copy of each unique report will be stored. | 28 // Only one copy of each unique report will be stored. |
| 30 class TestObserver : public LeakDetector::Observer { | 29 class TestObserver : public LeakDetector::Observer { |
| 31 public: | 30 public: |
| 32 // Contains a comparator function used to compare LeakReports for uniqueness. | 31 // Contains a comparator function used to compare MemoryLeakReportProtos for |
| 32 // uniqueness. |
| 33 struct Comparator { | 33 struct Comparator { |
| 34 bool operator()(const LeakReport& a, const LeakReport& b) const { | 34 bool operator()(const MemoryLeakReportProto& a, |
| 35 if (a.alloc_size_bytes != b.alloc_size_bytes) | 35 const MemoryLeakReportProto& b) const { |
| 36 return a.alloc_size_bytes < b.alloc_size_bytes; | 36 if (a.size_bytes() != b.size_bytes()) |
| 37 return a.size_bytes() < b.size_bytes(); |
| 37 | 38 |
| 38 return a.call_stack < b.call_stack; | 39 return std::lexicographical_compare(a.call_stack().begin(), |
| 40 a.call_stack().end(), |
| 41 b.call_stack().begin(), |
| 42 b.call_stack().end()); |
| 39 } | 43 } |
| 40 }; | 44 }; |
| 41 | 45 |
| 42 TestObserver() {} | 46 TestObserver() {} |
| 43 | 47 |
| 44 void OnLeakFound(const LeakReport& report) override { | 48 void OnLeaksFound( |
| 45 reports_.insert(report); | 49 const std::vector<MemoryLeakReportProto>& reports) override { |
| 50 reports_.insert(reports.begin(), reports.end()); |
| 46 } | 51 } |
| 47 | 52 |
| 48 const std::set<LeakReport, Comparator>& reports() const { return reports_; } | 53 const std::set<MemoryLeakReportProto, Comparator>& reports() const { |
| 54 return reports_; |
| 55 } |
| 49 | 56 |
| 50 private: | 57 private: |
| 51 // Container for all leak reports received through OnLeakFound(). Stores only | 58 // Container for all leak reports received through OnLeaksFound(). Stores only |
| 52 // one copy of each unique report. | 59 // one copy of each unique report. |
| 53 std::set<LeakReport, Comparator> reports_; | 60 std::set<MemoryLeakReportProto, Comparator> reports_; |
| 54 | 61 |
| 55 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 62 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 56 }; | 63 }; |
| 57 | 64 |
| 58 } // namespace | 65 } // namespace |
| 59 | 66 |
| 60 class LeakDetectorTest : public ::testing::Test { | 67 class LeakDetectorTest : public ::testing::Test { |
| 61 public: | 68 public: |
| 62 LeakDetectorTest() : detector_(LeakDetector::GetInstance()) { | 69 LeakDetectorTest() : detector_(LeakDetector::GetInstance()) { |
| 63 detector_->Init(kDefaultSamplingRate, kDefaultMaxCallStackUnwindDepth, | 70 detector_->Init(kDefaultSamplingRate, kDefaultMaxCallStackUnwindDepth, |
| 64 kDefaultAnalysisIntervalBytes, | 71 kDefaultAnalysisIntervalBytes, |
| 65 kDefaultSizeSuspicionThreshold, | 72 kDefaultSizeSuspicionThreshold, |
| 66 kDefaultCallStackSuspicionThreshold); | 73 kDefaultCallStackSuspicionThreshold); |
| 67 } | 74 } |
| 68 | 75 |
| 69 protected: | 76 protected: |
| 70 // Points to the instance of LeakDetector returned by GetInstance(). | 77 // Points to the instance of LeakDetector returned by GetInstance(). |
| 71 LeakDetector* detector_; | 78 LeakDetector* detector_; |
| 72 | 79 |
| 73 private: | 80 private: |
| 74 // For supporting content::BrowserThread operations. | 81 // For supporting content::BrowserThread operations. |
| 75 content::TestBrowserThreadBundle thread_bundle_; | 82 content::TestBrowserThreadBundle thread_bundle_; |
| 76 | 83 |
| 77 DISALLOW_COPY_AND_ASSIGN(LeakDetectorTest); | 84 DISALLOW_COPY_AND_ASSIGN(LeakDetectorTest); |
| 78 }; | 85 }; |
| 79 | 86 |
| 80 TEST_F(LeakDetectorTest, NotifyObservers) { | 87 TEST_F(LeakDetectorTest, NotifyObservers) { |
| 81 // Generate two sets of leak reports. | 88 // Generate two sets of leak reports. |
| 82 std::vector<LeakReport> reports1(3); | 89 std::vector<MemoryLeakReportProto> reports1(3); |
| 83 reports1[0].alloc_size_bytes = 8; | 90 reports1[0].set_size_bytes(8); |
| 84 reports1[0].call_stack = {1, 2, 3, 4}; | 91 for (uint64_t entry : {1, 2, 3, 4}) |
| 85 reports1[1].alloc_size_bytes = 16; | 92 reports1[0].add_call_stack(entry); |
| 86 reports1[1].call_stack = {5, 6, 7, 8}; | 93 reports1[1].set_size_bytes(16); |
| 87 reports1[2].alloc_size_bytes = 24; | 94 for (uint64_t entry : {5, 6, 7, 8}) |
| 88 reports1[2].call_stack = {9, 10, 11, 12}; | 95 reports1[1].add_call_stack(entry); |
| 96 reports1[2].set_size_bytes(24); |
| 97 for (uint64_t entry : {9, 10, 11, 12}) |
| 98 reports1[1].add_call_stack(entry); |
| 89 | 99 |
| 90 std::vector<LeakReport> reports2(3); | 100 std::vector<MemoryLeakReportProto> reports2(3); |
| 91 reports2[0].alloc_size_bytes = 32; | 101 reports2[0].set_size_bytes(32); |
| 92 reports2[0].call_stack = {1, 2, 4, 8}; | 102 for (uint64_t entry : {1, 2, 4, 8}) |
| 93 reports2[1].alloc_size_bytes = 40; | 103 reports2[0].add_call_stack(entry); |
| 94 reports2[1].call_stack = {16, 32, 64, 128}; | 104 reports2[1].set_size_bytes(40); |
| 95 reports2[2].alloc_size_bytes = 48; | 105 for (uint64_t entry : {16, 32, 64, 128}) |
| 96 reports2[2].call_stack = {256, 512, 1024, 2048}; | 106 reports2[1].add_call_stack(entry); |
| 107 reports2[2].set_size_bytes(48); |
| 108 for (uint64_t entry : {256, 512, 1024, 2048}) |
| 109 reports2[2].add_call_stack(entry); |
| 97 | 110 |
| 98 // Register three observers; | 111 // Register three observers; |
| 99 TestObserver obs1, obs2, obs3; | 112 TestObserver obs1, obs2, obs3; |
| 100 detector_->AddObserver(&obs1); | 113 detector_->AddObserver(&obs1); |
| 101 detector_->AddObserver(&obs2); | 114 detector_->AddObserver(&obs2); |
| 102 detector_->AddObserver(&obs3); | 115 detector_->AddObserver(&obs3); |
| 103 | 116 |
| 104 // Pass both sets of reports to the leak detector. | 117 // Pass both sets of reports to the leak detector. |
| 105 detector_->NotifyObservers(reports1); | 118 detector_->NotifyObservers(reports1); |
| 106 detector_->NotifyObservers(reports2); | 119 detector_->NotifyObservers(reports2); |
| 107 | 120 |
| 108 // Check that all three observers got both sets of reports, passed in | 121 // Check that all three observers got both sets of reports, passed in |
| 109 // separately. | 122 // separately. |
| 110 for (const TestObserver* obs : {&obs1, &obs2, &obs3}) { | 123 for (const TestObserver* obs : {&obs1, &obs2, &obs3}) { |
| 111 EXPECT_EQ(6U, obs->reports().size()); | 124 EXPECT_EQ(6U, obs->reports().size()); |
| 112 for (const auto& report : {reports1[0], reports1[1], reports1[2], | 125 for (const auto& report : {reports1[0], reports1[1], reports1[2], |
| 113 reports2[0], reports2[1], reports2[2]}) { | 126 reports2[0], reports2[1], reports2[2]}) { |
| 114 EXPECT_TRUE(obs->reports().find(report) != obs->reports().end()); | 127 EXPECT_TRUE(obs->reports().find(report) != obs->reports().end()); |
| 115 } | 128 } |
| 116 } | 129 } |
| 117 } | 130 } |
| 118 | 131 |
| 119 } // namespace metrics | 132 } // namespace metrics |
| OLD | NEW |