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/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 not thread-safe, and it should always be called on the same |
| 36 // thread that instantiated it. |
| 37 class LeakDetector { |
| 38 public: |
| 39 // Contains a report of a detected memory leak. |
| 40 struct LeakReport { |
| 41 LeakReport(); |
| 42 ~LeakReport(); |
| 43 |
| 44 size_t alloc_size_bytes; |
| 45 |
| 46 // Unlike the CallStack struct, which consists of addresses, this call stack |
| 47 // will contain offsets in the executable binary. |
| 48 std::vector<uintptr_t> call_stack; |
| 49 }; |
| 50 |
| 51 // Interface for receiving leak reports. |
| 52 class Observer { |
| 53 public: |
| 54 virtual ~Observer() {} |
| 55 |
| 56 // Called by leak detector to report a leak. |
| 57 virtual void OnLeakFound(const LeakReport& report) = 0; |
| 58 }; |
| 59 |
| 60 // Constructor arguments: |
| 61 // sampling_rate: |
| 62 // Pseudorandomly sample a fraction of the incoming allocations and frees, |
| 63 // based on hash values. Setting to 0 means no allocs/frees are sampled. |
| 64 // Setting to 1.0 or more means all allocs/frees are sampled. Anything in |
| 65 // between will result in an approximately that fraction of allocs/frees |
| 66 // being sampled. |
| 67 // max_stack_depth: |
| 68 // The max number of call stack frames to unwind. |
| 69 // analysis_interval_bytes: |
| 70 // Perform a leak analysis each time this many bytes have been allocated |
| 71 // since the previous analysis. |
| 72 // size_suspicion_threshold, call_stack_suspicion_threshold: |
| 73 // A possible leak should be suspected this many times to take action on i |
| 74 // For size analysis, the action is to start profiling by call stack. |
| 75 // For call stack analysis, the action is to generate a leak report. |
| 76 LeakDetector(float sampling_rate, |
| 77 int max_stack_depth, |
| 78 uint64_t analysis_interval_bytes, |
| 79 int size_suspicion_threshold, |
| 80 int call_stack_suspicion_threshold); |
| 81 |
| 82 // Initialize with default parameters. |
| 83 LeakDetector(); |
| 84 |
| 85 ~LeakDetector(); |
| 86 |
| 87 // Add |observer| to the list of stored Observers, i.e. |observers_|, to which |
| 88 // the leak detector will report leaks. |
| 89 void AddObserver(Observer* observer); |
| 90 |
| 91 // Remove |observer| from |observers_|. |
| 92 void RemoveObserver(Observer* observer); |
| 93 |
| 94 protected: |
| 95 // Notifies all Observers in |observers_| with the given vector of leak |
| 96 // reports. |
| 97 void NotifyObservers(const std::vector<LeakReport>& reports); |
| 98 |
| 99 private: |
| 100 // List of observers to notify when there's a leak report. |
| 101 base::ObserverList<Observer> observers_; |
| 102 |
| 103 // For thread safety. |
| 104 base::ThreadChecker thread_checker_; |
| 105 |
| 106 // For generating closures containing objects of this class. |
| 107 base::WeakPtrFactory<LeakDetector> weak_factory_; |
| 108 |
| 109 DISALLOW_COPY_AND_ASSIGN(LeakDetector); |
| 110 }; |
| 111 |
| 112 } // namespace metrics |
| 113 |
| 114 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ |
OLD | NEW |