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