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 #ifndef COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ | 5 #ifndef COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ |
6 #define COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ | 6 #define COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <list> | 11 #include <list> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/feature_list.h" | 14 #include "base/feature_list.h" |
15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/scoped_ptr.h" |
17 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
18 #include "base/observer_list.h" | 19 #include "base/observer_list.h" |
| 20 #include "base/synchronization/lock.h" |
19 #include "base/threading/thread_checker.h" | 21 #include "base/threading/thread_checker.h" |
| 22 #include "components/metrics/leak_detector/leak_detector_impl.h" |
20 | 23 |
21 namespace metrics { | 24 namespace metrics { |
22 | 25 |
23 // LeakDetector is an interface layer that connects the allocator | 26 // LeakDetector is an interface layer that connects the allocator |
24 // (base::allocator), the leak detector logic (LeakDetectorImpl), and any | 27 // (base::allocator), the leak detector logic (LeakDetectorImpl), and any |
25 // external classes interested in receiving leak reports (extend the Observer | 28 // external classes interested in receiving leak reports (extend the Observer |
26 // class). | 29 // class). |
27 // | 30 // |
28 // Currently it is stubbed out and only provides an interface for registering | 31 // Currently it is stubbed out and only provides an interface for registering |
29 // observers to receive leak reports. | 32 // observers to receive leak reports. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 ~LeakDetector(); | 82 ~LeakDetector(); |
80 | 83 |
81 // Add |observer| to the list of stored Observers, i.e. |observers_|, to which | 84 // Add |observer| to the list of stored Observers, i.e. |observers_|, to which |
82 // the leak detector will report leaks. | 85 // the leak detector will report leaks. |
83 void AddObserver(Observer* observer); | 86 void AddObserver(Observer* observer); |
84 | 87 |
85 // Remove |observer| from |observers_|. | 88 // Remove |observer| from |observers_|. |
86 void RemoveObserver(Observer* observer); | 89 void RemoveObserver(Observer* observer); |
87 | 90 |
88 private: | 91 private: |
| 92 FRIEND_TEST_ALL_PREFIXES(LeakDetectorTest, AllocatorHooks); |
89 FRIEND_TEST_ALL_PREFIXES(LeakDetectorTest, NotifyObservers); | 93 FRIEND_TEST_ALL_PREFIXES(LeakDetectorTest, NotifyObservers); |
90 | 94 |
| 95 // Allocator hook function that processes each alloc. Performs sampling and |
| 96 // unwinds call stack if necessary. Passes the allocated memory |ptr| and |
| 97 // allocation size |size| along with call stack info to RecordAlloc(). |
| 98 static void AllocHook(const void* ptr, size_t size); |
| 99 |
| 100 // Allocator hook function that processes each free. Performs sampling and |
| 101 // passes the allocation address |ptr| to |impl_|. |
| 102 static void FreeHook(const void* ptr); |
| 103 |
| 104 // Give an pointer |ptr|, computes a hash of the pointer value and compares it |
| 105 // against |sampling_factor_| to determine if it should be sampled. This |
| 106 // allows the same pointer to be sampled during both alloc and free. |
| 107 bool ShouldSample(const void* ptr) const; |
| 108 |
| 109 // Given an allocation |ptr| with size |size|, and a call stack |call_stack| |
| 110 // with depth of |depth| frames, passes it all to |impl_|. Then performs leak |
| 111 // analysis if a sufficient interval of allocated bytes has passed since the |
| 112 // previous analysis. |
| 113 void RecordAlloc(const void* ptr, size_t size, int depth, void** call_stack); |
| 114 |
91 // Notifies all Observers in |observers_| with the given vector of leak | 115 // Notifies all Observers in |observers_| with the given vector of leak |
92 // reports. | 116 // reports. |
93 void NotifyObservers(const std::vector<LeakReport>& reports); | 117 void NotifyObservers(const std::vector<LeakReport>& reports); |
94 | 118 |
95 // List of observers to notify when there's a leak report. | 119 // List of observers to notify when there's a leak report. |
96 base::ObserverList<Observer> observers_; | 120 base::ObserverList<Observer> observers_; |
97 | 121 |
| 122 // Handles leak detection logic. Must be called under lock as LeakDetectorImpl |
| 123 // uses shared resources. |
| 124 scoped_ptr<leak_detector::LeakDetectorImpl> impl_; |
| 125 |
98 // For thread safety. | 126 // For thread safety. |
99 base::ThreadChecker thread_checker_; | 127 base::ThreadChecker thread_checker_; |
| 128 base::Lock lock_; |
| 129 |
| 130 // Mapping size and address of Chrome binary in memory. |
| 131 uintptr_t binary_mapping_addr_; |
| 132 size_t binary_mapping_size_; |
| 133 |
| 134 // Total number of bytes allocated, computed before sampling. |
| 135 size_t total_alloc_size_; |
| 136 |
| 137 // The value of |total_alloc_size_| the last time there was a leak analysis, |
| 138 // rounded down to the nearest multiple of |analysis_interval_bytes_|. |
| 139 size_t last_analysis_alloc_size_; |
| 140 |
| 141 // Perform a leak analysis each time this many bytes have been allocated since |
| 142 // the previous analysis. |
| 143 size_t analysis_interval_bytes_; |
| 144 |
| 145 // When unwinding call stacks, unwind no more than this number of frames. |
| 146 size_t max_call_stack_unwind_depth_; |
| 147 |
| 148 // Sampling factor used by ShouldSample(). |
| 149 uint32_t sampling_factor_; |
100 | 150 |
101 // For generating closures containing objects of this class. | 151 // For generating closures containing objects of this class. |
102 base::WeakPtrFactory<LeakDetector> weak_factory_; | 152 base::WeakPtrFactory<LeakDetector> weak_factory_; |
103 | 153 |
104 DISALLOW_COPY_AND_ASSIGN(LeakDetector); | 154 DISALLOW_COPY_AND_ASSIGN(LeakDetector); |
105 }; | 155 }; |
106 | 156 |
107 } // namespace metrics | 157 } // namespace metrics |
108 | 158 |
109 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ | 159 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ |
OLD | NEW |