| 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 <memory> | 12 #include <memory> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/feature_list.h" | 15 #include "base/feature_list.h" |
| 16 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
| 17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/ref_counted.h" |
| 18 #include "base/observer_list.h" | 19 #include "base/observer_list.h" |
| 19 #include "base/synchronization/lock.h" | 20 #include "base/synchronization/lock.h" |
| 21 #include "base/task_runner.h" |
| 20 #include "base/threading/thread_checker.h" | 22 #include "base/threading/thread_checker.h" |
| 21 #include "components/metrics/proto/memory_leak_report.pb.h" | 23 #include "components/metrics/proto/memory_leak_report.pb.h" |
| 22 | 24 |
| 23 namespace base { | 25 namespace base { |
| 24 template <typename T> | 26 template <typename T> |
| 25 struct DefaultLazyInstanceTraits; | 27 struct DefaultLazyInstanceTraits; |
| 26 } | 28 } |
| 27 | 29 |
| 28 namespace metrics { | 30 namespace metrics { |
| 29 | 31 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 55 virtual ~Observer() {} | 57 virtual ~Observer() {} |
| 56 | 58 |
| 57 // Called by leak detector to report leaks. | 59 // Called by leak detector to report leaks. |
| 58 virtual void OnLeaksFound( | 60 virtual void OnLeaksFound( |
| 59 const std::vector<MemoryLeakReportProto>& reports) = 0; | 61 const std::vector<MemoryLeakReportProto>& reports) = 0; |
| 60 }; | 62 }; |
| 61 | 63 |
| 62 // Returns the sole instance, or creates it if it hasn't already been created. | 64 // Returns the sole instance, or creates it if it hasn't already been created. |
| 63 static LeakDetector* GetInstance(); | 65 static LeakDetector* GetInstance(); |
| 64 | 66 |
| 65 // Initializes leak detector with a set of params given by |params|. See the | 67 // Initializes leak detector. Args: |
| 66 // definition of MemoryLeakReportProto::Params for info about individual | 68 // - |params| is a set of parameters used by the leak detector. See definition |
| 67 // parameters. | 69 // of MemoryLeakReportProto::Params for info about individual parameters. |
| 68 void Init(const MemoryLeakReportProto::Params& params); | 70 // - |task_runner| is a TaskRunner to which NotifyObservers() should be |
| 71 // posted, if it is initally called from a different thread than the one on |
| 72 // which |task_runner| runs. |
| 73 void Init(const MemoryLeakReportProto::Params& params, |
| 74 scoped_refptr<base::TaskRunner> task_runner); |
| 69 | 75 |
| 70 // Add |observer| to the list of stored Observers, i.e. |observers_|, to which | 76 // Add |observer| to the list of stored Observers, i.e. |observers_|, to which |
| 71 // the leak detector will report leaks. | 77 // the leak detector will report leaks. |
| 72 void AddObserver(Observer* observer); | 78 void AddObserver(Observer* observer); |
| 73 | 79 |
| 74 // Remove |observer| from |observers_|. | 80 // Remove |observer| from |observers_|. |
| 75 void RemoveObserver(Observer* observer); | 81 void RemoveObserver(Observer* observer); |
| 76 | 82 |
| 77 private: | 83 private: |
| 78 friend base::DefaultLazyInstanceTraits<LeakDetector>; | 84 friend base::DefaultLazyInstanceTraits<LeakDetector>; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 91 // Allocator hook function that processes each free. Performs sampling and | 97 // Allocator hook function that processes each free. Performs sampling and |
| 92 // passes the allocation address |ptr| to |impl_|. | 98 // passes the allocation address |ptr| to |impl_|. |
| 93 static void FreeHook(const void* ptr); | 99 static void FreeHook(const void* ptr); |
| 94 | 100 |
| 95 // Give an pointer |ptr|, computes a hash of the pointer value and compares it | 101 // Give an pointer |ptr|, computes a hash of the pointer value and compares it |
| 96 // against |sampling_factor_| to determine if it should be sampled. This | 102 // against |sampling_factor_| to determine if it should be sampled. This |
| 97 // allows the same pointer to be sampled during both alloc and free. | 103 // allows the same pointer to be sampled during both alloc and free. |
| 98 bool ShouldSample(const void* ptr) const; | 104 bool ShouldSample(const void* ptr) const; |
| 99 | 105 |
| 100 // Notifies all Observers in |observers_| with the given vector of leak | 106 // Notifies all Observers in |observers_| with the given vector of leak |
| 101 // report protobufs. | 107 // report protobufs. If it is not currently on the thread that corresponds to |
| 108 // |task_runner_|, it will post the call as task to |task_runner_|. |
| 102 void NotifyObservers(const std::vector<MemoryLeakReportProto>& reports); | 109 void NotifyObservers(const std::vector<MemoryLeakReportProto>& reports); |
| 103 | 110 |
| 104 // List of observers to notify when there's a leak report. | 111 // List of observers to notify when there's a leak report. |
| 105 // TODO(sque): Consider using ObserverListThreadSafe instead. | 112 // TODO(sque): Consider using ObserverListThreadSafe instead. |
| 106 base::ObserverList<Observer> observers_; | 113 base::ObserverList<Observer> observers_; |
| 107 | 114 |
| 108 // For atomic access to |observers_|. | 115 // For atomic access to |observers_|. |
| 109 base::Lock observers_lock_; | 116 base::Lock observers_lock_; |
| 110 | 117 |
| 111 // Handles leak detection logic. Must be called under lock as LeakDetectorImpl | 118 // Handles leak detection logic. Must be called under lock as LeakDetectorImpl |
| 112 // uses shared resources. | 119 // uses shared resources. |
| 113 std::unique_ptr<leak_detector::LeakDetectorImpl> impl_; | 120 std::unique_ptr<leak_detector::LeakDetectorImpl> impl_; |
| 114 | 121 |
| 115 // For thread safety. | 122 // For thread safety. |
| 116 base::ThreadChecker thread_checker_; | 123 base::ThreadChecker thread_checker_; |
| 117 | 124 |
| 125 // For posting leak report notifications. |
| 126 scoped_refptr<base::TaskRunner> task_runner_; |
| 127 |
| 118 // Total number of bytes allocated, computed before sampling. | 128 // Total number of bytes allocated, computed before sampling. |
| 119 size_t total_alloc_size_; | 129 size_t total_alloc_size_; |
| 120 | 130 |
| 121 // The value of |total_alloc_size_| the last time there was a leak analysis, | 131 // The value of |total_alloc_size_| the last time there was a leak analysis, |
| 122 // rounded down to the nearest multiple of |analysis_interval_bytes_|. | 132 // rounded down to the nearest multiple of |analysis_interval_bytes_|. |
| 123 size_t last_analysis_alloc_size_; | 133 size_t last_analysis_alloc_size_; |
| 124 | 134 |
| 125 // For atomic access to |impl_|, |total_alloc_size_| and | 135 // For atomic access to |impl_|, |total_alloc_size_| and |
| 126 // |last_analysis_alloc_size_|. | 136 // |last_analysis_alloc_size_|. |
| 127 base::Lock recording_lock_; | 137 base::Lock recording_lock_; |
| 128 | 138 |
| 129 // Perform a leak analysis each time this many bytes have been allocated since | 139 // Perform a leak analysis each time this many bytes have been allocated since |
| 130 // the previous analysis. | 140 // the previous analysis. |
| 131 size_t analysis_interval_bytes_; | 141 size_t analysis_interval_bytes_; |
| 132 | 142 |
| 133 // When unwinding call stacks, unwind no more than this number of frames. | 143 // When unwinding call stacks, unwind no more than this number of frames. |
| 134 size_t max_call_stack_unwind_depth_; | 144 size_t max_call_stack_unwind_depth_; |
| 135 | 145 |
| 136 // Sampling factor used by ShouldSample(). It's full range of values | 146 // Sampling factor used by ShouldSample(). It's full range of values |
| 137 // corresponds to the allowable range of |sampling_rate| passed in during | 147 // corresponds to the allowable range of |sampling_rate| passed in during |
| 138 // initialization: [0.0f, 1.0f] -> [0, UINT64_MAX]. | 148 // initialization: [0.0f, 1.0f] -> [0, UINT64_MAX]. |
| 139 uint64_t sampling_factor_; | 149 uint64_t sampling_factor_; |
| 140 | 150 |
| 141 DISALLOW_COPY_AND_ASSIGN(LeakDetector); | 151 DISALLOW_COPY_AND_ASSIGN(LeakDetector); |
| 142 }; | 152 }; |
| 143 | 153 |
| 144 } // namespace metrics | 154 } // namespace metrics |
| 145 | 155 |
| 146 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ | 156 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ |
| OLD | NEW |