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

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: Add unit test for allocator hook registration 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..4b34076868e4ac60b211bb58b7dab2a11318cb3d 100644
--- a/components/metrics/leak_detector/leak_detector.h
+++ b/components/metrics/leak_detector/leak_detector.h
@@ -14,9 +14,12 @@
#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/synchronization/lock.h"
#include "base/threading/thread_checker.h"
+#include "components/metrics/leak_detector/leak_detector_impl.h"
namespace metrics {
@@ -86,8 +89,29 @@ class LeakDetector {
void RemoveObserver(Observer* observer);
private:
+ FRIEND_TEST_ALL_PREFIXES(LeakDetectorTest, AllocatorHooks);
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
+ // 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,8 +119,34 @@ 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_;
+ base::Lock lock_;
+
+ // 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().
+ uint32_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