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..c71dfa340123354b7e69e97d35a384e429856999 |
| --- /dev/null |
| +++ b/components/metrics/leak_detector/leak_detector.h |
| @@ -0,0 +1,101 @@ |
| +// 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 |
|
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.
|
| +// the leak detector logic, and the leak reporting pipeline. |
| +// |
| +// 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. |
|
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.
|
| + |
| +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 { |
|
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
|
| + 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_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); |
| + |
| + ~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_; |
|
Alexei Svitkine (slow)
2016/02/10 20:16:58
Consider using base/observer_list.h
Simon Que
2016/02/11 01:45:38
Done.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(LeakDetector); |
| +}; |
| + |
| +} // namespace metrics |
| + |
| +#endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_H_ |