Index: components/metrics/leak_detector/leak_detector.h |
diff --git a/components/metrics/leak_detector/leak_detector.h b/components/metrics/leak_detector/leak_detector.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..455b32363e76cf83e360afd788fdda88273d08a9 |
--- /dev/null |
+++ b/components/metrics/leak_detector/leak_detector.h |
@@ -0,0 +1,103 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ |
+#define COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ |
+ |
+#include <stddef.h> |
+#include <stdint.h> |
+ |
+#include <list> |
+#include <vector> |
+ |
+#include "base/macros.h" |
+ |
+// The top level leak detector is an interface layer that connects the allocator |
+// the leak detector logic, and the leak reporting pipeline. |
+// |
+// This class can only have one instance created at a time. |
+ |
+namespace metrics { |
+ |
+class LeakDetector { |
+ public: |
+ // Contains a report of a detected memory leak. |
+ struct LeakReport { |
+ LeakReport(); |
+ ~LeakReport(); |
+ |
+ size_t alloc_size_bytes; |
+ |
+ // Unlike the CallStack struct, which consists of addresses, this call stack |
+ // will contain offsets in the executable binary. |
+ std::vector<uintptr_t> call_stack; |
+ |
+ bool operator<(const LeakReport& other) const { |
+ if (alloc_size_bytes != other.alloc_size_bytes) |
+ return alloc_size_bytes < other.alloc_size_bytes; |
+ |
+ return call_stack < other.call_stack; |
+ } |
+ }; |
+ |
+ // Interface for receiving leak reports. |
+ class Observer { |
+ public: |
+ virtual ~Observer() {} |
+ |
+ // Called by leak detector to report a leak. |
+ virtual void OnLeakFound(const LeakReport& report) = 0; |
+ }; |
+ |
+ // Constructor arguments: |
+ // sampling_factor: |
+ // Pseudorandomly sample a fraction of the incoming allocations and frees, |
+ // based on hash values. Setting to 0 means no allocs/frees are sampled. |
+ // Setting to 1.0 or more means all allocs/frees are sampled. Anything in |
+ // between will result in an approximately that fraction of allocs/frees |
+ // being sampled. |
+ // max_stack_depth: |
+ // The max number of call stack frames to unwind. |
+ // analysis_interval_bytes: |
+ // Perform a leak analysis each time this many bytes have been allocated |
+ // since the previous analysis. |
+ // size_suspicion_threshold, call_stack_suspicion_threshold: |
+ // A possible leak should be suspected this many times to take action on i |
+ // For size analysis, the action is to start profiling by call stack. |
+ // For call stack analysis, the action is to generate a leak report. |
+ LeakDetector(double sampling_factor, |
+ int max_stack_depth, |
+ uint64_t analysis_interval_bytes, |
+ int size_suspicion_threshold, |
+ int call_stack_suspicion_threshold); |
+ |
+ // Initialize with default arguments. See constructor definition in |
+ // leak_detector.cc |
+ LeakDetector(); |
+ |
+ ~LeakDetector(); |
+ |
+ // Add |observer| to the list of stored LeakDetectorObservers to which the |
+ // leak detector will report leaks. Returns true on success. |
+ bool AddObserver(Observer* observer); |
+ |
+ // Remove |observer| from the list of stored LeakDetectorObservers. If it is |
+ // not in the list, this function has no effect but returns false. Returns |
+ // true on success. |
+ bool RemoveObserver(Observer* observer); |
+ |
+ // Notifies all Observers in |observers_| with the given vector of leak |
+ // reports. |
+ void NotifyObservers(const std::vector<LeakReport>& reports); |
+ |
+ private: |
+ // List of observers to notify when there's a leak report. |
+ std::list<Observer*> observers_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(LeakDetector); |
+}; |
+ |
+} // namespace metrics |
+ |
+#endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ |