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

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: Add content/public/browser to GN deps 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
« no previous file with comments | « components/metrics/DEPS ('k') | components/metrics/leak_detector/leak_detector.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/gtest_prod_util.h"
16 #include "base/macros.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/observer_list.h"
19 #include "base/threading/thread_checker.h"
20
21 namespace metrics {
22
23 // LeakDetector is an interface layer that connects the allocator
24 // (base::allocator), the leak detector logic (LeakDetectorImpl), and any
25 // external classes interested in receiving leak reports (extend the Observer
26 // class).
27 //
28 // Currently it is stubbed out and only provides an interface for registering
29 // observers to receive leak reports.
30 // TODO(sque): Add the full functionality and allow only one instance.
31 //
32 // This class is not thread-safe, and it should always be called on the same
33 // thread that instantiated it.
34 class LeakDetector {
35 public:
36 // Contains a report of a detected memory leak.
37 struct LeakReport {
38 LeakReport();
39 ~LeakReport();
40
41 size_t alloc_size_bytes;
42
43 // Unlike the CallStack struct, which consists of addresses, this call stack
44 // will contain offsets in the executable binary.
45 std::vector<uintptr_t> call_stack;
46 };
47
48 // Interface for receiving leak reports.
49 class Observer {
50 public:
51 virtual ~Observer() {}
52
53 // Called by leak detector to report a leak.
54 virtual void OnLeakFound(const LeakReport& report) = 0;
55 };
56
57 // Constructor arguments:
58 // sampling_rate:
59 // Pseudorandomly sample a fraction of the incoming allocations and frees,
60 // based on hash values. Setting to 0 means no allocs/frees are sampled.
61 // Setting to 1.0 or more means all allocs/frees are sampled. Anything in
62 // between will result in an approximately that fraction of allocs/frees
63 // being sampled.
64 // max_call_stack_unwind_depth:
65 // The max number of call stack frames to unwind.
66 // analysis_interval_bytes:
67 // Perform a leak analysis each time this many bytes have been allocated
68 // since the previous analysis.
69 // size_suspicion_threshold, call_stack_suspicion_threshold:
70 // 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.
72 // For call stack analysis, the action is to generate a leak report.
73 LeakDetector(float sampling_rate,
74 size_t max_call_stack_unwind_depth,
75 uint64_t analysis_interval_bytes,
76 uint32_t size_suspicion_threshold,
77 uint32_t call_stack_suspicion_threshold);
78
79 ~LeakDetector();
80
81 // Add |observer| to the list of stored Observers, i.e. |observers_|, to which
82 // the leak detector will report leaks.
83 void AddObserver(Observer* observer);
84
85 // Remove |observer| from |observers_|.
86 void RemoveObserver(Observer* observer);
87
88 private:
89 FRIEND_TEST_ALL_PREFIXES(LeakDetectorTest, NotifyObservers);
90
91 // Notifies all Observers in |observers_| with the given vector of leak
92 // reports.
93 void NotifyObservers(const std::vector<LeakReport>& reports);
94
95 // List of observers to notify when there's a leak report.
96 base::ObserverList<Observer> observers_;
97
98 // For thread safety.
99 base::ThreadChecker thread_checker_;
100
101 // For generating closures containing objects of this class.
102 base::WeakPtrFactory<LeakDetector> weak_factory_;
103
104 DISALLOW_COPY_AND_ASSIGN(LeakDetector);
105 };
106
107 } // namespace metrics
108
109 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_
OLDNEW
« no previous file with comments | « components/metrics/DEPS ('k') | components/metrics/leak_detector/leak_detector.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698