Chromium Code Reviews| 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..da3f5d1fe3fdb05af17df4112ddf73f2f657fbfc |
| --- /dev/null |
| +++ b/components/metrics/leak_detector/leak_detector.h |
| @@ -0,0 +1,97 @@ |
| +// 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" |
| +#include "base/observer_list.h" |
| + |
| +namespace metrics { |
| + |
| +// LeakDetector is an interface layer that connects the allocator |
| +// (base::allocator), the leak detector logic (LeakDetectorImpl), and any |
| +// external classes interested in receiving leak reports (extend the Observer |
| +// class). |
| +// |
| +// Currently it is stubbed out and only provides an interface for registering |
| +// observers to receive leak reports. |
| +// TODO(sque): Add the full functionality and allow only one instance. |
| +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; |
| + }; |
| + |
| + // 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_rate: |
| + // 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(float sampling_rate, |
| + int max_stack_depth, |
| + uint64_t analysis_interval_bytes, |
| + int size_suspicion_threshold, |
| + int call_stack_suspicion_threshold); |
| + |
| + // Initialize with default parameters. |
| + LeakDetector(); |
| + |
| + ~LeakDetector(); |
| + |
| + // Add |observer| to the list of stored Observers, i.e. |observers_|, to which |
| + // the leak detector will report leaks. |
| + void AddObserver(Observer* observer); |
| + |
| + // Remove |observer| from |observers_|. |
| + void RemoveObserver(Observer* observer); |
| + |
| + // Notifies all Observers in |observers_| with the given vector of leak |
| + // reports. |
| + void NotifyObservers(const std::vector<LeakReport>& reports); |
|
Alexei Svitkine (slow)
2016/02/12 16:17:42
Does this need to be public? Kind of strange to ha
Simon Que
2016/02/12 19:00:07
This will be called from the memory allocator hook
Alexei Svitkine (slow)
2016/02/17 16:06:30
If having the hook be a static member of the class
Simon Que
2016/02/17 20:49:33
Done.
|
| + |
| + private: |
| + // List of observers to notify when there's a leak report. |
| + base::ObserverList<Observer> observers_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(LeakDetector); |
| +}; |
| + |
| +} // namespace metrics |
| + |
| +#endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ |