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

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

Issue 1868193003: Store alloc history data in memory leak report protobuf (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@leak-history
Patch Set: Add back algorithm include; remove dependency on other patch set Created 4 years, 8 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/scoped_ptr.h"
18 #include "base/observer_list.h" 18 #include "base/observer_list.h"
19 #include "base/synchronization/lock.h" 19 #include "base/synchronization/lock.h"
20 #include "base/threading/thread_checker.h" 20 #include "base/threading/thread_checker.h"
21 #include "components/metrics/proto/memory_leak_report.pb.h"
21 22
22 namespace base { 23 namespace base {
23 template <typename T> 24 template <typename T>
24 struct DefaultLazyInstanceTraits; 25 struct DefaultLazyInstanceTraits;
25 } 26 }
26 27
27 namespace metrics { 28 namespace metrics {
28 29
29 namespace leak_detector { 30 namespace leak_detector {
30 class LeakDetectorImpl; 31 class LeakDetectorImpl;
(...skipping 10 matching lines...) Expand all
41 // These member functions are thread-safe: 42 // These member functions are thread-safe:
42 // - AllocHook 43 // - AllocHook
43 // - FreeHook 44 // - FreeHook
44 // - AddObserver 45 // - AddObserver
45 // - RemoveObserver 46 // - RemoveObserver
46 // 47 //
47 // All other functions must always be called from the same thread. This is 48 // All other functions must always be called from the same thread. This is
48 // enforced with a DCHECK. 49 // enforced with a DCHECK.
49 class LeakDetector { 50 class LeakDetector {
50 public: 51 public:
51 // Contains a report of a detected memory leak.
52 struct LeakReport {
53 LeakReport();
54 LeakReport(const LeakReport& other);
55 ~LeakReport();
56
57 size_t alloc_size_bytes;
58
59 // Unlike the CallStack struct, which consists of addresses, this call stack
60 // will contain offsets in the executable binary.
61 std::vector<uintptr_t> call_stack;
62 };
63
64 // Interface for receiving leak reports. 52 // Interface for receiving leak reports.
65 class Observer { 53 class Observer {
66 public: 54 public:
67 virtual ~Observer() {} 55 virtual ~Observer() {}
68 56
69 // Called by leak detector to report a leak. 57 // Called by leak detector to report leaks.
70 virtual void OnLeakFound(const LeakReport& report) = 0; 58 virtual void OnLeaksFound(
59 const std::vector<MemoryLeakReportProto>& reports) = 0;
71 }; 60 };
72 61
73 // Returns the sole instance, or creates it if it hasn't already been created. 62 // Returns the sole instance, or creates it if it hasn't already been created.
74 static LeakDetector* GetInstance(); 63 static LeakDetector* GetInstance();
75 64
76 // Initializer arguments: 65 // Initializer arguments:
77 // sampling_rate: 66 // sampling_rate:
78 // Pseudorandomly sample a fraction of the incoming allocations and frees, 67 // Pseudorandomly sample a fraction of the incoming allocations and frees,
79 // based on hash values. Setting to 0 means no allocs/frees are sampled. 68 // based on hash values. Setting to 0 means no allocs/frees are sampled.
80 // Setting to 1.0 or more means all allocs/frees are sampled. Anything in 69 // Setting to 1.0 or more means all allocs/frees are sampled. Anything in
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 // Allocator hook function that processes each free. Performs sampling and 108 // Allocator hook function that processes each free. Performs sampling and
120 // passes the allocation address |ptr| to |impl_|. 109 // passes the allocation address |ptr| to |impl_|.
121 static void FreeHook(const void* ptr); 110 static void FreeHook(const void* ptr);
122 111
123 // Give an pointer |ptr|, computes a hash of the pointer value and compares it 112 // Give an pointer |ptr|, computes a hash of the pointer value and compares it
124 // against |sampling_factor_| to determine if it should be sampled. This 113 // against |sampling_factor_| to determine if it should be sampled. This
125 // allows the same pointer to be sampled during both alloc and free. 114 // allows the same pointer to be sampled during both alloc and free.
126 bool ShouldSample(const void* ptr) const; 115 bool ShouldSample(const void* ptr) const;
127 116
128 // Notifies all Observers in |observers_| with the given vector of leak 117 // Notifies all Observers in |observers_| with the given vector of leak
129 // reports. 118 // report protobufs.
130 void NotifyObservers(const std::vector<LeakReport>& reports); 119 void NotifyObservers(const std::vector<MemoryLeakReportProto>& reports);
131 120
132 // List of observers to notify when there's a leak report. 121 // List of observers to notify when there's a leak report.
133 // TODO(sque): Consider using ObserverListThreadSafe instead. 122 // TODO(sque): Consider using ObserverListThreadSafe instead.
134 base::ObserverList<Observer> observers_; 123 base::ObserverList<Observer> observers_;
135 124
136 // For atomic access to |observers_|. 125 // For atomic access to |observers_|.
137 base::Lock observers_lock_; 126 base::Lock observers_lock_;
138 127
139 // Handles leak detection logic. Must be called under lock as LeakDetectorImpl 128 // Handles leak detection logic. Must be called under lock as LeakDetectorImpl
140 // uses shared resources. 129 // uses shared resources.
(...skipping 24 matching lines...) Expand all
165 // corresponds to the allowable range of |sampling_rate| passed in during 154 // corresponds to the allowable range of |sampling_rate| passed in during
166 // initialization: [0.0f, 1.0f] -> [0, UINT64_MAX]. 155 // initialization: [0.0f, 1.0f] -> [0, UINT64_MAX].
167 uint64_t sampling_factor_; 156 uint64_t sampling_factor_;
168 157
169 DISALLOW_COPY_AND_ASSIGN(LeakDetector); 158 DISALLOW_COPY_AND_ASSIGN(LeakDetector);
170 }; 159 };
171 160
172 } // namespace metrics 161 } // namespace metrics
173 162
174 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ 163 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698