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