| 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/weak_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/observer_list.h" | 18 #include "base/observer_list.h" |
| 19 #include "base/synchronization/lock.h" |
| 19 #include "base/threading/thread_checker.h" | 20 #include "base/threading/thread_checker.h" |
| 20 | 21 |
| 22 namespace base { |
| 23 template <typename T> struct DefaultLazyInstanceTraits; |
| 24 } |
| 25 |
| 21 namespace metrics { | 26 namespace metrics { |
| 22 | 27 |
| 28 namespace leak_detector { |
| 29 class LeakDetectorImpl; |
| 30 } |
| 31 |
| 23 // LeakDetector is an interface layer that connects the allocator | 32 // LeakDetector is an interface layer that connects the allocator |
| 24 // (base::allocator), the leak detector logic (LeakDetectorImpl), and any | 33 // (base::allocator), the leak detector logic (LeakDetectorImpl), and any |
| 25 // external classes interested in receiving leak reports (extend the Observer | 34 // external classes interested in receiving leak reports (extend the Observer |
| 26 // class). | 35 // class). |
| 27 // | 36 // |
| 28 // Currently it is stubbed out and only provides an interface for registering | 37 // Only one instance of this class can exist. Access this instance using |
| 29 // observers to receive leak reports. | 38 // GetInstance(). Do not create an instance of this class directly. |
| 30 // TODO(sque): Add the full functionality and allow only one instance. | |
| 31 // | 39 // |
| 32 // This class is not thread-safe, and it should always be called on the same | 40 // These member functions are thread-safe: |
| 33 // thread that instantiated it. | 41 // - AllocHook |
| 42 // - FreeHook |
| 43 // - AddObserver |
| 44 // - RemoveObserver |
| 45 // |
| 46 // All other functions must always be called from the same thread. This is |
| 47 // enforced with a DCHECK. |
| 34 class LeakDetector { | 48 class LeakDetector { |
| 35 public: | 49 public: |
| 36 // Contains a report of a detected memory leak. | 50 // Contains a report of a detected memory leak. |
| 37 struct LeakReport { | 51 struct LeakReport { |
| 38 LeakReport(); | 52 LeakReport(); |
| 39 ~LeakReport(); | 53 ~LeakReport(); |
| 40 | 54 |
| 41 size_t alloc_size_bytes; | 55 size_t alloc_size_bytes; |
| 42 | 56 |
| 43 // Unlike the CallStack struct, which consists of addresses, this call stack | 57 // Unlike the CallStack struct, which consists of addresses, this call stack |
| 44 // will contain offsets in the executable binary. | 58 // will contain offsets in the executable binary. |
| 45 std::vector<uintptr_t> call_stack; | 59 std::vector<uintptr_t> call_stack; |
| 46 }; | 60 }; |
| 47 | 61 |
| 48 // Interface for receiving leak reports. | 62 // Interface for receiving leak reports. |
| 49 class Observer { | 63 class Observer { |
| 50 public: | 64 public: |
| 51 virtual ~Observer() {} | 65 virtual ~Observer() {} |
| 52 | 66 |
| 53 // Called by leak detector to report a leak. | 67 // Called by leak detector to report a leak. |
| 54 virtual void OnLeakFound(const LeakReport& report) = 0; | 68 virtual void OnLeakFound(const LeakReport& report) = 0; |
| 55 }; | 69 }; |
| 56 | 70 |
| 57 // Constructor arguments: | 71 // Returns the sole instance, or creates it if it hasn't already been created. |
| 72 static LeakDetector* GetInstance(); |
| 73 |
| 74 // Initializer arguments: |
| 58 // sampling_rate: | 75 // sampling_rate: |
| 59 // Pseudorandomly sample a fraction of the incoming allocations and frees, | 76 // Pseudorandomly sample a fraction of the incoming allocations and frees, |
| 60 // based on hash values. Setting to 0 means no allocs/frees are sampled. | 77 // based on hash values. Setting to 0 means no allocs/frees are sampled. |
| 61 // Setting to 1.0 or more means all allocs/frees are sampled. Anything in | 78 // Setting to 1.0 or more means all allocs/frees are sampled. Anything in |
| 62 // between will result in an approximately that fraction of allocs/frees | 79 // between will result in an approximately that fraction of allocs/frees |
| 63 // being sampled. | 80 // being sampled. |
| 64 // max_call_stack_unwind_depth: | 81 // max_call_stack_unwind_depth: |
| 65 // The max number of call stack frames to unwind. | 82 // The max number of call stack frames to unwind. |
| 66 // analysis_interval_bytes: | 83 // analysis_interval_bytes: |
| 67 // Perform a leak analysis each time this many bytes have been allocated | 84 // Perform a leak analysis each time this many bytes have been allocated |
| 68 // since the previous analysis. | 85 // since the previous analysis. |
| 69 // size_suspicion_threshold, call_stack_suspicion_threshold: | 86 // size_suspicion_threshold, call_stack_suspicion_threshold: |
| 70 // A possible leak should be suspected this many times to take action on i | 87 // A possible leak should be suspected this many times to take action on i |
| 71 // For size analysis, the action is to start profiling by call stack. | 88 // For size analysis, the action is to start profiling by call stack. |
| 72 // For call stack analysis, the action is to generate a leak report. | 89 // For call stack analysis, the action is to generate a leak report. |
| 73 LeakDetector(float sampling_rate, | 90 void Init(float sampling_rate, |
| 74 size_t max_call_stack_unwind_depth, | 91 size_t max_call_stack_unwind_depth, |
| 75 uint64_t analysis_interval_bytes, | 92 uint64_t analysis_interval_bytes, |
| 76 uint32_t size_suspicion_threshold, | 93 uint32_t size_suspicion_threshold, |
| 77 uint32_t call_stack_suspicion_threshold); | 94 uint32_t call_stack_suspicion_threshold); |
| 78 | |
| 79 ~LeakDetector(); | |
| 80 | 95 |
| 81 // Add |observer| to the list of stored Observers, i.e. |observers_|, to which | 96 // Add |observer| to the list of stored Observers, i.e. |observers_|, to which |
| 82 // the leak detector will report leaks. | 97 // the leak detector will report leaks. |
| 83 void AddObserver(Observer* observer); | 98 void AddObserver(Observer* observer); |
| 84 | 99 |
| 85 // Remove |observer| from |observers_|. | 100 // Remove |observer| from |observers_|. |
| 86 void RemoveObserver(Observer* observer); | 101 void RemoveObserver(Observer* observer); |
| 87 | 102 |
| 88 private: | 103 private: |
| 104 friend base::DefaultLazyInstanceTraits<LeakDetector>; |
| 89 FRIEND_TEST_ALL_PREFIXES(LeakDetectorTest, NotifyObservers); | 105 FRIEND_TEST_ALL_PREFIXES(LeakDetectorTest, NotifyObservers); |
| 90 | 106 |
| 107 // Keep these private, as this class is meant to be initialized only through |
| 108 // the lazy instance, and never destroyed. |
| 109 LeakDetector(); |
| 110 ~LeakDetector(); |
| 111 |
| 112 // Allocator hook function that processes each alloc. Performs sampling and |
| 113 // unwinds call stack if necessary. Passes the allocated memory |ptr| and |
| 114 // allocation size |size| along with call stack info to RecordAlloc(). |
| 115 static void AllocHook(const void* ptr, size_t size); |
| 116 |
| 117 // Allocator hook function that processes each free. Performs sampling and |
| 118 // passes the allocation address |ptr| to |impl_|. |
| 119 static void FreeHook(const void* ptr); |
| 120 |
| 121 // Give an pointer |ptr|, computes a hash of the pointer value and compares it |
| 122 // against |sampling_factor_| to determine if it should be sampled. This |
| 123 // allows the same pointer to be sampled during both alloc and free. |
| 124 bool ShouldSample(const void* ptr) const; |
| 125 |
| 91 // Notifies all Observers in |observers_| with the given vector of leak | 126 // Notifies all Observers in |observers_| with the given vector of leak |
| 92 // reports. | 127 // reports. |
| 93 void NotifyObservers(const std::vector<LeakReport>& reports); | 128 void NotifyObservers(const std::vector<LeakReport>& reports); |
| 94 | 129 |
| 95 // List of observers to notify when there's a leak report. | 130 // List of observers to notify when there's a leak report. |
| 96 base::ObserverList<Observer> observers_; | 131 base::ObserverList<Observer> observers_; |
| 97 | 132 |
| 133 // For atomic access to |observers_|. |
| 134 base::Lock observers_lock_; |
| 135 |
| 136 // Handles leak detection logic. Must be called under lock as LeakDetectorImpl |
| 137 // uses shared resources. |
| 138 scoped_ptr<leak_detector::LeakDetectorImpl> impl_; |
| 139 |
| 140 // For atomic access to |impl_|. |
| 141 base::Lock impl_lock_; |
| 142 |
| 98 // For thread safety. | 143 // For thread safety. |
| 99 base::ThreadChecker thread_checker_; | 144 base::ThreadChecker thread_checker_; |
| 100 | 145 |
| 101 // For generating closures containing objects of this class. | 146 // Total number of bytes allocated, computed before sampling. |
| 102 base::WeakPtrFactory<LeakDetector> weak_factory_; | 147 size_t total_alloc_size_; |
| 148 |
| 149 // The value of |total_alloc_size_| the last time there was a leak analysis, |
| 150 // rounded down to the nearest multiple of |analysis_interval_bytes_|. |
| 151 size_t last_analysis_alloc_size_; |
| 152 |
| 153 // For atomic access to |total_alloc_size_| and |last_analysis_alloc_size_|. |
| 154 base::Lock alloc_size_lock_; |
| 155 |
| 156 // Perform a leak analysis each time this many bytes have been allocated since |
| 157 // the previous analysis. |
| 158 size_t analysis_interval_bytes_; |
| 159 |
| 160 // When unwinding call stacks, unwind no more than this number of frames. |
| 161 size_t max_call_stack_unwind_depth_; |
| 162 |
| 163 // Sampling factor used by ShouldSample(). It's full range of values |
| 164 // corresponds to the allowable range of |sampling_rate| passed in during |
| 165 // initialization: [0.0f, 1.0f] -> [0, UINT64_MAX]. |
| 166 uint64_t sampling_factor_; |
| 103 | 167 |
| 104 DISALLOW_COPY_AND_ASSIGN(LeakDetector); | 168 DISALLOW_COPY_AND_ASSIGN(LeakDetector); |
| 105 }; | 169 }; |
| 106 | 170 |
| 107 } // namespace metrics | 171 } // namespace metrics |
| 108 | 172 |
| 109 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ | 173 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ |
| OLD | NEW |