Chromium Code Reviews| 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" |
| 19 #include "base/threading/thread_checker.h" | 20 #include "base/threading/thread_checker.h" |
| 21 #include "components/metrics/leak_detector/leak_detector_impl.h" | |
|
Primiano Tucci (use gerrit)
2016/03/03 16:23:44
Are you adding this just for the scoped_ptr? I thi
Simon Que
2016/03/04 01:59:18
Done.
| |
| 20 | 22 |
| 21 namespace metrics { | 23 namespace metrics { |
| 22 | 24 |
| 23 // LeakDetector is an interface layer that connects the allocator | 25 // LeakDetector is an interface layer that connects the allocator |
| 24 // (base::allocator), the leak detector logic (LeakDetectorImpl), and any | 26 // (base::allocator), the leak detector logic (LeakDetectorImpl), and any |
| 25 // external classes interested in receiving leak reports (extend the Observer | 27 // external classes interested in receiving leak reports (extend the Observer |
| 26 // class). | 28 // class). |
| 27 // | 29 // |
| 28 // Currently it is stubbed out and only provides an interface for registering | 30 // Currently it is stubbed out and only provides an interface for registering |
| 29 // observers to receive leak reports. | 31 // observers to receive leak reports. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 // being sampled. | 65 // being sampled. |
| 64 // max_call_stack_unwind_depth: | 66 // max_call_stack_unwind_depth: |
| 65 // The max number of call stack frames to unwind. | 67 // The max number of call stack frames to unwind. |
| 66 // analysis_interval_bytes: | 68 // analysis_interval_bytes: |
| 67 // Perform a leak analysis each time this many bytes have been allocated | 69 // Perform a leak analysis each time this many bytes have been allocated |
| 68 // since the previous analysis. | 70 // since the previous analysis. |
| 69 // size_suspicion_threshold, call_stack_suspicion_threshold: | 71 // size_suspicion_threshold, call_stack_suspicion_threshold: |
| 70 // A possible leak should be suspected this many times to take action on i | 72 // 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. | 73 // 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. | 74 // For call stack analysis, the action is to generate a leak report. |
| 75 // | |
| 76 // The non-const, non-static member functions of this class are not thread- | |
| 77 // safe and should be either called under lock or from the same thread. | |
| 73 LeakDetector(float sampling_rate, | 78 LeakDetector(float sampling_rate, |
| 74 size_t max_call_stack_unwind_depth, | 79 size_t max_call_stack_unwind_depth, |
| 75 uint64_t analysis_interval_bytes, | 80 uint64_t analysis_interval_bytes, |
| 76 uint32_t size_suspicion_threshold, | 81 uint32_t size_suspicion_threshold, |
| 77 uint32_t call_stack_suspicion_threshold); | 82 uint32_t call_stack_suspicion_threshold); |
| 78 | 83 |
| 79 ~LeakDetector(); | 84 ~LeakDetector(); |
| 80 | 85 |
| 81 // Add |observer| to the list of stored Observers, i.e. |observers_|, to which | 86 // Add |observer| to the list of stored Observers, i.e. |observers_|, to which |
| 82 // the leak detector will report leaks. | 87 // the leak detector will report leaks. |
| 83 void AddObserver(Observer* observer); | 88 void AddObserver(Observer* observer); |
| 84 | 89 |
| 85 // Remove |observer| from |observers_|. | 90 // Remove |observer| from |observers_|. |
| 86 void RemoveObserver(Observer* observer); | 91 void RemoveObserver(Observer* observer); |
| 87 | 92 |
| 88 private: | 93 private: |
| 89 FRIEND_TEST_ALL_PREFIXES(LeakDetectorTest, NotifyObservers); | 94 FRIEND_TEST_ALL_PREFIXES(LeakDetectorTest, NotifyObservers); |
| 90 | 95 |
| 96 // Allocator hook function that processes each alloc. Performs sampling and | |
| 97 // unwinds call stack if necessary. Passes the allocated memory |ptr| and | |
|
Primiano Tucci (use gerrit)
2016/03/03 16:23:44
These functions seem internal only and hence no ne
Simon Que
2016/03/04 01:59:18
These functions need to access the contents of the
| |
| 98 // allocation size |size| along with call stack info to RecordAlloc(). | |
| 99 static void AllocHook(const void* ptr, size_t size); | |
| 100 | |
| 101 // Allocator hook function that processes each free. Performs sampling and | |
| 102 // passes the allocation address |ptr| to |impl_|. | |
| 103 static void FreeHook(const void* ptr); | |
| 104 | |
| 105 // Give an pointer |ptr|, computes a hash of the pointer value and compares it | |
| 106 // against |sampling_factor_| to determine if it should be sampled. This | |
| 107 // allows the same pointer to be sampled during both alloc and free. | |
| 108 bool ShouldSample(const void* ptr) const; | |
| 109 | |
| 110 // Given an allocation |ptr| with size |size|, and a call stack |call_stack| | |
| 111 // with depth of |depth| frames, passes it all to |impl_|. Then performs leak | |
| 112 // analysis if a sufficient interval of allocated bytes has passed since the | |
| 113 // previous analysis. | |
| 114 void RecordAlloc(const void* ptr, size_t size, int depth, void** call_stack); | |
| 115 | |
| 91 // Notifies all Observers in |observers_| with the given vector of leak | 116 // Notifies all Observers in |observers_| with the given vector of leak |
| 92 // reports. | 117 // reports. |
| 93 void NotifyObservers(const std::vector<LeakReport>& reports); | 118 void NotifyObservers(const std::vector<LeakReport>& reports); |
| 94 | 119 |
| 95 // List of observers to notify when there's a leak report. | 120 // List of observers to notify when there's a leak report. |
| 96 base::ObserverList<Observer> observers_; | 121 base::ObserverList<Observer> observers_; |
| 97 | 122 |
| 123 // Handles leak detection logic. Must be called under lock as LeakDetectorImpl | |
| 124 // uses shared resources. | |
| 125 scoped_ptr<leak_detector::LeakDetectorImpl> impl_; | |
| 126 | |
| 98 // For thread safety. | 127 // For thread safety. |
| 99 base::ThreadChecker thread_checker_; | 128 base::ThreadChecker thread_checker_; |
| 100 | 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(). It's full range of values | |
| 149 // corresponds to the allowable range of |sampling_rate| passed in during | |
| 150 // initialization: [0.0f, 1.0f] -> [0, UINT64_MAX]. | |
| 151 uint64_t sampling_factor_; | |
| 152 | |
| 101 // For generating closures containing objects of this class. | 153 // For generating closures containing objects of this class. |
| 102 base::WeakPtrFactory<LeakDetector> weak_factory_; | 154 base::WeakPtrFactory<LeakDetector> weak_factory_; |
| 103 | 155 |
| 104 DISALLOW_COPY_AND_ASSIGN(LeakDetector); | 156 DISALLOW_COPY_AND_ASSIGN(LeakDetector); |
| 105 }; | 157 }; |
| 106 | 158 |
| 107 } // namespace metrics | 159 } // namespace metrics |
| 108 | 160 |
| 109 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ | 161 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ |
| OLD | NEW |