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 |
| 16 // The top level leak detector is an interface layer that connects the |
| 17 // allocator, the leak detector logic, and the leak reporting pipeline. |
| 18 |
| 19 namespace metrics { |
| 20 |
| 21 class LeakDetector { |
| 22 public: |
| 23 // Contains a report of a detected memory leak. |
| 24 struct LeakReport { |
| 25 LeakReport(); |
| 26 ~LeakReport(); |
| 27 |
| 28 size_t alloc_size_bytes; |
| 29 |
| 30 // Unlike the CallStack struct, which consists of addresses, this call stack |
| 31 // will contain offsets in the executable binary. |
| 32 std::vector<uintptr_t> call_stack; |
| 33 |
| 34 bool operator<(const LeakReport& other) const { |
| 35 if (alloc_size_bytes != other.alloc_size_bytes) |
| 36 return alloc_size_bytes < other.alloc_size_bytes; |
| 37 |
| 38 return call_stack < other.call_stack; |
| 39 } |
| 40 }; |
| 41 |
| 42 // Interface for receiving leak reports. |
| 43 class Observer { |
| 44 public: |
| 45 virtual ~Observer() {} |
| 46 |
| 47 // Called by leak detector to report a leak. |
| 48 virtual void OnLeakFound(const LeakReport& report) = 0; |
| 49 }; |
| 50 |
| 51 // Constructor arguments: |
| 52 // sampling_factor: |
| 53 // Pseudorandomly sample a fraction of the incoming allocations and frees, |
| 54 // based on hash values. Setting to 0 means no allocs/frees are sampled. |
| 55 // Setting to 1.0 or more means all allocs/frees are sampled. Anything in |
| 56 // between will result in an approximately that fraction of allocs/frees |
| 57 // being sampled. |
| 58 // max_stack_depth: |
| 59 // The max number of call stack frames to unwind. |
| 60 // analysis_interval_bytes: |
| 61 // Perform a leak analysis each time this many bytes have been allocated |
| 62 // since the previous analysis. |
| 63 // size_suspicion_threshold, call_stack_suspicion_threshold: |
| 64 // A possible leak should be suspected this many times to take action on i |
| 65 // For size analysis, the action is to start profiling by call stack. |
| 66 // For call stack analysis, the action is to generate a leak report. |
| 67 LeakDetector(double sampling_factor, |
| 68 int max_stack_depth, |
| 69 uint64_t analysis_interval_bytes, |
| 70 int size_suspicion_threshold, |
| 71 int call_stack_suspicion_threshold); |
| 72 |
| 73 // Initialize with default arguments. See constructor definition in |
| 74 // leak_detector.cc |
| 75 LeakDetector(); |
| 76 |
| 77 ~LeakDetector(); |
| 78 |
| 79 // Returns true if the leak detector was successfully initialized. |
| 80 bool IsInitialized() const { return init_success_; } |
| 81 |
| 82 // Add |observer| to the list of stored LeakDetectorObservers to which the |
| 83 // leak detector will report leaks. Returns true on success. |
| 84 bool AddObserver(Observer* observer); |
| 85 |
| 86 // Remove |observer| from the list of stored LeakDetectorObservers. If it is |
| 87 // not in the list, this function has no effect but returns false. Returns |
| 88 // true on success. |
| 89 bool RemoveObserver(Observer* observer); |
| 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 private: |
| 96 // Flag indicating that initialization was successful. |
| 97 bool init_success_; |
| 98 |
| 99 // List of observers to notify when there's a leak report. |
| 100 std::list<Observer*> observers_; |
| 101 |
| 102 DISALLOW_COPY_AND_ASSIGN(LeakDetector); |
| 103 }; |
| 104 |
| 105 } // namespace metrics |
| 106 |
| 107 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ |
OLD | NEW |