Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: components/metrics/leak_detector/leak_detector.h

Issue 1665553002: metrics: Connect leak detector to allocator (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Simplify alloc hook registration; Enclose more stuff in CrOS #ifdef Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 ~LeakDetector(); 81 ~LeakDetector();
80 82
81 // Add |observer| to the list of stored Observers, i.e. |observers_|, to which 83 // Add |observer| to the list of stored Observers, i.e. |observers_|, to which
82 // the leak detector will report leaks. 84 // the leak detector will report leaks.
83 void AddObserver(Observer* observer); 85 void AddObserver(Observer* observer);
84 86
85 // Remove |observer| from |observers_|. 87 // Remove |observer| from |observers_|.
86 void RemoveObserver(Observer* observer); 88 void RemoveObserver(Observer* observer);
87 89
88 private: 90 private:
91 FRIEND_TEST_ALL_PREFIXES(LeakDetectorTest, AllocatorHooks);
89 FRIEND_TEST_ALL_PREFIXES(LeakDetectorTest, NotifyObservers); 92 FRIEND_TEST_ALL_PREFIXES(LeakDetectorTest, NotifyObservers);
90 93
94 // Allocator hook function that processes each alloc. Performs sampling and
95 // unwinds call stack if necessary. Passes the allocated memory |ptr| and
96 // allocation size |size| along with call stack info to RecordAlloc().
97 static void AllocHook(const void* ptr, size_t size);
98
99 // Allocator hook function that processes each free. Performs sampling and
100 // passes the allocation address |ptr| to |impl_|.
101 static void FreeHook(const void* ptr);
102
103 // Give an pointer |ptr|, computes a hash of the pointer value and compares it
104 // against |sampling_factor_| to determine if it should be sampled. This
105 // allows the same pointer to be sampled during both alloc and free.
106 bool ShouldSample(const void* ptr) const;
107
108 // Given an allocation |ptr| with size |size|, and a call stack |call_stack|
109 // with depth of |depth| frames, passes it all to |impl_|. Then performs leak
110 // analysis if a sufficient interval of allocated bytes has passed since the
111 // previous analysis.
112 void RecordAlloc(const void* ptr, size_t size, int depth, void** call_stack);
113
91 // Notifies all Observers in |observers_| with the given vector of leak 114 // Notifies all Observers in |observers_| with the given vector of leak
92 // reports. 115 // reports.
93 void NotifyObservers(const std::vector<LeakReport>& reports); 116 void NotifyObservers(const std::vector<LeakReport>& reports);
94 117
95 // List of observers to notify when there's a leak report. 118 // List of observers to notify when there's a leak report.
96 base::ObserverList<Observer> observers_; 119 base::ObserverList<Observer> observers_;
97 120
121 // Handles leak detection logic. Must be called under lock as LeakDetectorImpl
122 // uses shared resources.
123 scoped_ptr<leak_detector::LeakDetectorImpl> impl_;
124
98 // For thread safety. 125 // For thread safety.
99 base::ThreadChecker thread_checker_; 126 base::ThreadChecker thread_checker_;
100 127
128 // Mapping size and address of Chrome binary in memory.
129 uintptr_t binary_mapping_addr_;
130 size_t binary_mapping_size_;
131
132 // Total number of bytes allocated, computed before sampling.
133 size_t total_alloc_size_;
134
135 // The value of |total_alloc_size_| the last time there was a leak analysis,
136 // rounded down to the nearest multiple of |analysis_interval_bytes_|.
137 size_t last_analysis_alloc_size_;
138
139 // Perform a leak analysis each time this many bytes have been allocated since
140 // the previous analysis.
141 size_t analysis_interval_bytes_;
142
143 // When unwinding call stacks, unwind no more than this number of frames.
144 size_t max_call_stack_unwind_depth_;
145
146 // Sampling factor used by ShouldSample(). It's full range of values
147 // corresponds to the allowable range of |sampling_rate| passed in during
148 // initialization: [0.0f, 1.0f] -> [0, UINT64_MAX].
149 uint64_t sampling_factor_;
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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698