Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(249)

Unified Diff: components/metrics/leak_detector/leak_detector.h

Issue 1665553002: metrics: Connect leak detector to allocator (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove use of add_pointer; Simplify GetCallStack Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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
index e23609444490d78765744910f02334b6f7fbbc8f..d856831780cc2db8f09f3248fdbc64d0d57c3f9c 100644
--- a/components/metrics/leak_detector/leak_detector.h
+++ b/components/metrics/leak_detector/leak_detector.h
@@ -14,9 +14,11 @@
#include "base/feature_list.h"
#include "base/gtest_prod_util.h"
#include "base/macros.h"
+#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/threading/thread_checker.h"
+#include "components/metrics/leak_detector/leak_detector_impl.h"
Primiano Tucci (use gerrit) 2016/03/03 16:23:44 Are you adding this just for the scoped_ptr? I thi
Simon Que 2016/03/04 01:59:18 Done.
namespace metrics {
@@ -70,6 +72,9 @@ class LeakDetector {
// 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.
+ //
+ // The non-const, non-static member functions of this class are not thread-
+ // safe and should be either called under lock or from the same thread.
LeakDetector(float sampling_rate,
size_t max_call_stack_unwind_depth,
uint64_t analysis_interval_bytes,
@@ -88,6 +93,26 @@ class LeakDetector {
private:
FRIEND_TEST_ALL_PREFIXES(LeakDetectorTest, NotifyObservers);
+ // Allocator hook function that processes each alloc. Performs sampling and
+ // unwinds call stack if necessary. Passes the allocated memory |ptr| and
Primiano Tucci (use gerrit) 2016/03/03 16:23:44 These functions seem internal only and hence no ne
Simon Que 2016/03/04 01:59:18 These functions need to access the contents of the
+ // allocation size |size| along with call stack info to RecordAlloc().
+ static void AllocHook(const void* ptr, size_t size);
+
+ // Allocator hook function that processes each free. Performs sampling and
+ // passes the allocation address |ptr| to |impl_|.
+ static void FreeHook(const void* ptr);
+
+ // Give an pointer |ptr|, computes a hash of the pointer value and compares it
+ // against |sampling_factor_| to determine if it should be sampled. This
+ // allows the same pointer to be sampled during both alloc and free.
+ bool ShouldSample(const void* ptr) const;
+
+ // Given an allocation |ptr| with size |size|, and a call stack |call_stack|
+ // with depth of |depth| frames, passes it all to |impl_|. Then performs leak
+ // analysis if a sufficient interval of allocated bytes has passed since the
+ // previous analysis.
+ void RecordAlloc(const void* ptr, size_t size, int depth, void** call_stack);
+
// Notifies all Observers in |observers_| with the given vector of leak
// reports.
void NotifyObservers(const std::vector<LeakReport>& reports);
@@ -95,9 +120,36 @@ class LeakDetector {
// List of observers to notify when there's a leak report.
base::ObserverList<Observer> observers_;
+ // Handles leak detection logic. Must be called under lock as LeakDetectorImpl
+ // uses shared resources.
+ scoped_ptr<leak_detector::LeakDetectorImpl> impl_;
+
// For thread safety.
base::ThreadChecker thread_checker_;
+ // Mapping size and address of Chrome binary in memory.
+ uintptr_t binary_mapping_addr_;
+ size_t binary_mapping_size_;
+
+ // Total number of bytes allocated, computed before sampling.
+ size_t total_alloc_size_;
+
+ // The value of |total_alloc_size_| the last time there was a leak analysis,
+ // rounded down to the nearest multiple of |analysis_interval_bytes_|.
+ size_t last_analysis_alloc_size_;
+
+ // Perform a leak analysis each time this many bytes have been allocated since
+ // the previous analysis.
+ size_t analysis_interval_bytes_;
+
+ // When unwinding call stacks, unwind no more than this number of frames.
+ size_t max_call_stack_unwind_depth_;
+
+ // Sampling factor used by ShouldSample(). It's full range of values
+ // corresponds to the allowable range of |sampling_rate| passed in during
+ // initialization: [0.0f, 1.0f] -> [0, UINT64_MAX].
+ uint64_t sampling_factor_;
+
// For generating closures containing objects of this class.
base::WeakPtrFactory<LeakDetector> weak_factory_;

Powered by Google App Engine
This is Rietveld 408576698