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

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

Issue 986503002: components/metrics: Add runtime memory leak detector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix style, comments, RAW_CHECK in stl_allocator.h Created 5 years, 4 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_impl.h
diff --git a/components/metrics/leak_detector/leak_detector_impl.h b/components/metrics/leak_detector/leak_detector_impl.h
new file mode 100644
index 0000000000000000000000000000000000000000..e27f4a5f30109eb3e17f541d2211b7eba35b6780
--- /dev/null
+++ b/components/metrics/leak_detector/leak_detector_impl.h
@@ -0,0 +1,155 @@
+// Copyright 2015 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_IMPL_H_
+#define COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_
+
+#include <stdint.h>
+
+#include <unordered_set>
+#include <vector>
+
+#include "components/metrics/leak_detector/call_stack_table.h"
+#include "components/metrics/leak_detector/leak_analyzer.h"
+#include <gperftools/custom_allocator.h>
+
+namespace leak_detector {
+
+//----------------------------------------------------------------------
+// Class that contains the actual leak detection mechanism.
+//----------------------------------------------------------------------
+class LeakDetectorImpl {
+ public:
+ LeakDetectorImpl(uint64_t mapping_addr,
+ uint64_t mapping_size,
+ int size_suspicion_threshold,
+ int call_stack_suspicion_threshold,
+ bool verbose);
+ ~LeakDetectorImpl();
+
+ // Indicates whether the given allocation size has an associated call stack
+ // table, and thus requires a stack unwind.
+ bool ShouldGetStackTraceForSize(size_t size) const;
+
+ // Record allocs and frees.
+ void RecordAlloc(const void* ptr, size_t size,
+ int stack_depth, const void* const call_stack[]);
+ void RecordFree(const void* ptr);
+
+ // Run check for possible leaks based on the current profiling data.
+ void TestForLeaks();
+
+ // Dump current profiling statistics to log.
+ void DumpStats() const;
+
+ private:
+ // A record of allocations for a particular size.
+ struct AllocSizeEntry {
+ // Number of allocations and frees for this size.
+ uint32_t num_allocs;
+ uint32_t num_frees;
+
+ // A stack table, if this size is being profiled for stack as well.
+ CallStackTable* stack_table;
+ };
+
+ // Info for a single allocation.
+ struct AllocInfo {
+ AllocInfo() : call_stack(nullptr) {}
+
+ // Number of bytes in this allocation.
+ size_t size;
+
+ // Points to a unique call stack.
+ const CallStack* call_stack;
+ };
+
+ // Allocator class for allocation entry map. Maps allocated addresses to
+ // AllocInfo objects.
+ using AllocationEntryAllocator =
+ STL_Allocator<std::pair<const void*, AllocInfo>, CustomAllocator>;
+
+ // Allocator class for unique call stacks.
+ using TableEntryAllocator = STL_Allocator<const CallStack*, CustomAllocator>;
+
+ // Hash class for addresses.
+ struct AddressHash {
+ size_t operator() (const void* ptr) const;
+ };
+
+ // Comparator class for call stack objects.
+ struct CallStackCompare {
+ bool operator() (const CallStack* c1, const CallStack* c2) const {
+ return c1->depth == c2->depth &&
+ std::equal(c1->stack, c1->stack + c1->depth, c2->stack);
+ }
+ };
+
+ // Hash class for call stack objects.
+ struct CallStackHash {
+ size_t operator() (const CallStack* call_stack) const;
+ };
+
+ // Returns a CallStack object for a given call stack. Each unique call stack
+ // has its own CallStack object. If the given call stack has already been
+ // created by a previous call to this function, return a pointer to that same
+ // call stack object.
+ CallStack* GetCallStack(int depth, const void* const stack[]);
+
+ // Returns the offset of |ptr| within the current binary. If it is not in the
+ // current binary, just return |ptr| as an integer.
+ uintptr_t GetOffset(const void *ptr) const;
+
+ // Owns all unique call stack objects, which are allocated on the heap. Any
+ // other class or function that references a call stack must get it from here,
+ // but may not take ownership of the call stack object.
+ std::unordered_set<CallStack*,
+ CallStackHash,
+ CallStackCompare,
+ TableEntryAllocator> call_stacks_;
+
+ // Allocation stats.
+ uint64_t num_allocs_;
+ uint64_t num_frees_;
+ uint64_t alloc_size_;
+ uint64_t free_size_;
+
+ uint32_t num_allocs_with_call_stack_;
+ uint32_t num_stack_tables_;
+
+ // Stores all individual recorded allocations.
+ std::unordered_map<const void*,
+ AllocInfo,
+ AddressHash,
+ std::equal_to<const void*>,
+ AllocationEntryAllocator> address_map_;
+
+ // Used to analyze potential leak patterns in the allocation sizes.
+ LeakAnalyzer size_leak_analyzer_;
+
+ // Allocation stats for each size.
+ std::vector<AllocSizeEntry,
+ STL_Allocator<AllocSizeEntry, CustomAllocator>> size_entries_;
+
+ // Address mapping info of the current binary.
+ uint64_t mapping_addr_;
+ uint64_t mapping_size_;
+
+ // Number of consecutive times an allocation size must trigger suspicion to be
+ // considered a leak suspect.
+ int size_suspicion_threshold_;
+
+ // Number of consecutive times a call stack must trigger suspicion to be
+ // considered a leak suspect.
+ int call_stack_suspicion_threshold_;
+
+ // Enable verbose dumping of much more leak analysis data.
+ bool verbose_;
+
+ DISALLOW_COPY_AND_ASSIGN(LeakDetectorImpl);
+};
+
+} // namespace leak_detector
+
+#endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_

Powered by Google App Engine
This is Rietveld 408576698