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

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

Issue 1681263003: metrics: Add leak detector controller in Chrome OS metrics system (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make LeakDetector thread-safe Created 4 years, 10 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_
6 #define COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_
7
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include <list>
12 #include <vector>
13
14 #include "base/feature_list.h"
15 #include "base/macros.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "base/threading/thread_checker.h"
19
20 namespace metrics {
21
22 // Runtime flag that indicates whether this leak detector should be enabled in
23 // the current instance of Chrome.
24 extern const base::Feature kEnableRuntimeMemoryLeakDetector;
25
26 // LeakDetector is an interface layer that connects the allocator
27 // (base::allocator), the leak detector logic (LeakDetectorImpl), and any
28 // external classes interested in receiving leak reports (extend the Observer
29 // class).
30 //
31 // Currently it is stubbed out and only provides an interface for registering
32 // observers to receive leak reports.
33 // TODO(sque): Add the full functionality and allow only one instance.
34 //
35 // This class is thread-safe.
Will Harris 2016/02/20 08:54:05 This class is not thread-safe, and it should alway
Simon Que 2016/02/22 23:26:18 Done.
36 class LeakDetector {
37 public:
38 // Contains a report of a detected memory leak.
39 struct LeakReport {
40 LeakReport();
41 ~LeakReport();
42
43 size_t alloc_size_bytes;
44
45 // Unlike the CallStack struct, which consists of addresses, this call stack
46 // will contain offsets in the executable binary.
47 std::vector<uintptr_t> call_stack;
48 };
49
50 // Interface for receiving leak reports.
51 class Observer {
52 public:
53 virtual ~Observer() {}
54
55 // Called by leak detector to report a leak.
56 virtual void OnLeakFound(const LeakReport& report) = 0;
57 };
58
59 // Constructor arguments:
60 // sampling_rate:
61 // Pseudorandomly sample a fraction of the incoming allocations and frees,
62 // based on hash values. Setting to 0 means no allocs/frees are sampled.
63 // Setting to 1.0 or more means all allocs/frees are sampled. Anything in
64 // between will result in an approximately that fraction of allocs/frees
65 // being sampled.
66 // max_stack_depth:
67 // The max number of call stack frames to unwind.
68 // analysis_interval_bytes:
69 // Perform a leak analysis each time this many bytes have been allocated
70 // since the previous analysis.
71 // size_suspicion_threshold, call_stack_suspicion_threshold:
72 // A possible leak should be suspected this many times to take action on i
73 // For size analysis, the action is to start profiling by call stack.
74 // For call stack analysis, the action is to generate a leak report.
75 LeakDetector(float sampling_rate,
76 int max_stack_depth,
77 uint64_t analysis_interval_bytes,
78 int size_suspicion_threshold,
79 int call_stack_suspicion_threshold);
80
81 // Initialize with default parameters.
82 LeakDetector();
83
84 ~LeakDetector();
85
86 // Add |observer| to the list of stored Observers, i.e. |observers_|, to which
87 // the leak detector will report leaks.
88 void AddObserver(Observer* observer);
89
90 // Remove |observer| from |observers_|.
91 void RemoveObserver(Observer* observer);
92
93 protected:
94 // Notifies all Observers in |observers_| with the given vector of leak
95 // reports.
96 void NotifyObservers(const std::vector<LeakReport>& reports);
Will Harris 2016/02/20 08:54:05 what calls (or will call) this method, other than
Simon Que 2016/02/22 23:26:18 It will be called from the hook function, so this
Will Harris 2016/02/23 20:48:26 I still don't get why this is public. The code bei
Simon Que 2016/02/23 23:19:23 Done.
97
98 private:
99 // List of observers to notify when there's a leak report.
100 base::ObserverList<Observer> observers_;
101
102 // For thread safety.
103 base::ThreadChecker thread_checker_;
104
105 // For generating closures containing objects of this class.
106 base::WeakPtrFactory<LeakDetector> weak_factory_;
107
108 DISALLOW_COPY_AND_ASSIGN(LeakDetector);
109 };
110
111 } // namespace metrics
112
113 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698