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 | |
| 16 // The top level leak detector is an interface layer that connects the allocator | |
|
Alexei Svitkine (slow)
2016/02/10 20:16:58
I don't understand what "top level" means in this
Simon Que
2016/02/11 01:45:38
Done.
| |
| 17 // the leak detector logic, and the leak reporting pipeline. | |
| 18 // | |
| 19 // Currently it is stubbed out and only provides an interface for registering | |
| 20 // observers to receive leak reports. | |
| 21 // TODO(sque): Add the full functionality and allow only one instance. | |
|
Alexei Svitkine (slow)
2016/02/10 20:16:58
Nit: Put this whole comment above the class declar
Simon Que
2016/02/11 01:45:38
Done.
| |
| 22 | |
| 23 namespace metrics { | |
| 24 | |
| 25 class LeakDetector { | |
| 26 public: | |
| 27 // Contains a report of a detected memory leak. | |
| 28 struct LeakReport { | |
| 29 LeakReport(); | |
| 30 ~LeakReport(); | |
| 31 | |
| 32 size_t alloc_size_bytes; | |
| 33 | |
| 34 // Unlike the CallStack struct, which consists of addresses, this call stack | |
| 35 // will contain offsets in the executable binary. | |
| 36 std::vector<uintptr_t> call_stack; | |
| 37 | |
| 38 bool operator<(const LeakReport& other) const { | |
|
Alexei Svitkine (slow)
2016/02/10 20:16:58
Why is this needed?
Simon Que
2016/02/11 01:45:37
It's used in the unit test. I've moved it to there
| |
| 39 if (alloc_size_bytes != other.alloc_size_bytes) | |
| 40 return alloc_size_bytes < other.alloc_size_bytes; | |
| 41 | |
| 42 return call_stack < other.call_stack; | |
| 43 } | |
| 44 }; | |
| 45 | |
| 46 // Interface for receiving leak reports. | |
| 47 class Observer { | |
| 48 public: | |
| 49 virtual ~Observer() {} | |
| 50 | |
| 51 // Called by leak detector to report a leak. | |
| 52 virtual void OnLeakFound(const LeakReport& report) = 0; | |
| 53 }; | |
| 54 | |
| 55 // Constructor arguments: | |
| 56 // sampling_rate: | |
| 57 // Pseudorandomly sample a fraction of the incoming allocations and frees, | |
| 58 // based on hash values. Setting to 0 means no allocs/frees are sampled. | |
| 59 // Setting to 1.0 or more means all allocs/frees are sampled. Anything in | |
| 60 // between will result in an approximately that fraction of allocs/frees | |
| 61 // being sampled. | |
| 62 // max_stack_depth: | |
| 63 // The max number of call stack frames to unwind. | |
| 64 // analysis_interval_bytes: | |
| 65 // Perform a leak analysis each time this many bytes have been allocated | |
| 66 // since the previous analysis. | |
| 67 // size_suspicion_threshold, call_stack_suspicion_threshold: | |
| 68 // A possible leak should be suspected this many times to take action on i | |
| 69 // For size analysis, the action is to start profiling by call stack. | |
| 70 // For call stack analysis, the action is to generate a leak report. | |
| 71 LeakDetector(float sampling_rate, | |
| 72 int max_stack_depth, | |
| 73 uint64_t analysis_interval_bytes, | |
| 74 int size_suspicion_threshold, | |
| 75 int call_stack_suspicion_threshold); | |
| 76 | |
| 77 ~LeakDetector(); | |
| 78 | |
| 79 // Add |observer| to the list of stored LeakDetectorObservers to which the | |
| 80 // leak detector will report leaks. Returns true on success. | |
| 81 bool AddObserver(Observer* observer); | |
| 82 | |
| 83 // Remove |observer| from the list of stored LeakDetectorObservers. If it is | |
| 84 // not in the list, this function has no effect but returns false. Returns | |
| 85 // true on success. | |
| 86 bool RemoveObserver(Observer* observer); | |
| 87 | |
| 88 // Notifies all Observers in |observers_| with the given vector of leak | |
| 89 // reports. | |
| 90 void NotifyObservers(const std::vector<LeakReport>& reports); | |
| 91 | |
| 92 private: | |
| 93 // List of observers to notify when there's a leak report. | |
| 94 std::list<Observer*> observers_; | |
|
Alexei Svitkine (slow)
2016/02/10 20:16:58
Consider using base/observer_list.h
Simon Que
2016/02/11 01:45:38
Done.
| |
| 95 | |
| 96 DISALLOW_COPY_AND_ASSIGN(LeakDetector); | |
| 97 }; | |
| 98 | |
| 99 } // namespace metrics | |
| 100 | |
| 101 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ | |
| OLD | NEW |