Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/macros.h" | |
| 15 #include "base/observer_list.h" | |
| 16 | |
| 17 namespace metrics { | |
| 18 | |
| 19 // LeakDetector is an interface layer that connects the allocator | |
| 20 // (base::allocator), the leak detector logic (LeakDetectorImpl), and any | |
| 21 // external classes interested in receiving leak reports (extend the Observer | |
| 22 // class). | |
| 23 // | |
| 24 // Currently it is stubbed out and only provides an interface for registering | |
| 25 // observers to receive leak reports. | |
| 26 // TODO(sque): Add the full functionality and allow only one instance. | |
| 27 class LeakDetector { | |
| 28 public: | |
| 29 // Contains a report of a detected memory leak. | |
| 30 struct LeakReport { | |
| 31 LeakReport(); | |
| 32 ~LeakReport(); | |
| 33 | |
| 34 size_t alloc_size_bytes; | |
| 35 | |
| 36 // Unlike the CallStack struct, which consists of addresses, this call stack | |
| 37 // will contain offsets in the executable binary. | |
| 38 std::vector<uintptr_t> call_stack; | |
| 39 }; | |
| 40 | |
| 41 // Interface for receiving leak reports. | |
| 42 class Observer { | |
| 43 public: | |
| 44 virtual ~Observer() {} | |
| 45 | |
| 46 // Called by leak detector to report a leak. | |
| 47 virtual void OnLeakFound(const LeakReport& report) = 0; | |
| 48 }; | |
| 49 | |
| 50 // Constructor arguments: | |
| 51 // sampling_rate: | |
| 52 // Pseudorandomly sample a fraction of the incoming allocations and frees, | |
| 53 // based on hash values. Setting to 0 means no allocs/frees are sampled. | |
| 54 // Setting to 1.0 or more means all allocs/frees are sampled. Anything in | |
| 55 // between will result in an approximately that fraction of allocs/frees | |
| 56 // being sampled. | |
| 57 // max_stack_depth: | |
| 58 // The max number of call stack frames to unwind. | |
| 59 // analysis_interval_bytes: | |
| 60 // Perform a leak analysis each time this many bytes have been allocated | |
| 61 // since the previous analysis. | |
| 62 // size_suspicion_threshold, call_stack_suspicion_threshold: | |
| 63 // A possible leak should be suspected this many times to take action on i | |
| 64 // For size analysis, the action is to start profiling by call stack. | |
| 65 // For call stack analysis, the action is to generate a leak report. | |
| 66 LeakDetector(float sampling_rate, | |
| 67 int max_stack_depth, | |
| 68 uint64_t analysis_interval_bytes, | |
| 69 int size_suspicion_threshold, | |
| 70 int call_stack_suspicion_threshold); | |
| 71 | |
| 72 // Initialize with default parameters. | |
| 73 LeakDetector(); | |
| 74 | |
| 75 ~LeakDetector(); | |
| 76 | |
| 77 // Add |observer| to the list of stored Observers, i.e. |observers_|, to which | |
| 78 // the leak detector will report leaks. | |
| 79 void AddObserver(Observer* observer); | |
| 80 | |
| 81 // Remove |observer| from |observers_|. | |
| 82 void RemoveObserver(Observer* observer); | |
| 83 | |
| 84 // Notifies all Observers in |observers_| with the given vector of leak | |
| 85 // reports. | |
| 86 void NotifyObservers(const std::vector<LeakReport>& reports); | |
|
Alexei Svitkine (slow)
2016/02/12 16:17:42
Does this need to be public? Kind of strange to ha
Simon Que
2016/02/12 19:00:07
This will be called from the memory allocator hook
Alexei Svitkine (slow)
2016/02/17 16:06:30
If having the hook be a static member of the class
Simon Que
2016/02/17 20:49:33
Done.
| |
| 87 | |
| 88 private: | |
| 89 // List of observers to notify when there's a leak report. | |
| 90 base::ObserverList<Observer> observers_; | |
| 91 | |
| 92 DISALLOW_COPY_AND_ASSIGN(LeakDetector); | |
| 93 }; | |
| 94 | |
| 95 } // namespace metrics | |
| 96 | |
| 97 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ | |
| OLD | NEW |