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

Side by Side 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: LeakDetectorImpl stores addrs as uintptr_t; Implement move semantics for RankedList 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_
6 #define COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_
7
8 #include <gperftools/custom_allocator.h>
9 #include <stdint.h>
10
11 #include <unordered_set>
12 #include <vector>
13
14 #include "components/metrics/leak_detector/call_stack_table.h"
15 #include "components/metrics/leak_detector/leak_analyzer.h"
16
17 namespace leak_detector {
18
19 //----------------------------------------------------------------------
20 // Class that contains the actual leak detection mechanism.
21 //----------------------------------------------------------------------
22 class LeakDetectorImpl {
23 public:
24 LeakDetectorImpl(uintptr_t mapping_addr,
25 size_t mapping_size,
26 int size_suspicion_threshold,
27 int call_stack_suspicion_threshold,
28 bool verbose);
29 ~LeakDetectorImpl();
30
31 // Indicates whether the given allocation size has an associated call stack
32 // table, and thus requires a stack unwind.
33 bool ShouldGetStackTraceForSize(size_t size) const;
34
35 // Record allocs and frees.
36 void RecordAlloc(const void* ptr,
37 size_t size,
38 int stack_depth,
39 const void* const call_stack[]);
40 void RecordFree(const void* ptr);
41
42 // Run check for possible leaks based on the current profiling data.
43 void TestForLeaks();
44
45 // Dump current profiling statistics to log.
46 void DumpStats() const;
47
48 private:
49 // A record of allocations for a particular size.
50 struct AllocSizeEntry {
51 // Number of allocations and frees for this size.
52 uint32_t num_allocs;
53 uint32_t num_frees;
54
55 // A stack table, if this size is being profiled for stack as well.
56 CallStackTable* stack_table;
57 };
58
59 // Info for a single allocation.
60 struct AllocInfo {
61 AllocInfo() : call_stack(nullptr) {}
62
63 // Number of bytes in this allocation.
64 size_t size;
65
66 // Points to a unique call stack.
67 const CallStack* call_stack;
68 };
69
70 // Allocator class for allocation entry map. Maps allocated addresses to
71 // AllocInfo objects.
72 using AllocationEntryAllocator =
73 STL_Allocator<std::pair<const void*, AllocInfo>, CustomAllocator>;
74
75 // Allocator class for unique call stacks.
76 using TableEntryAllocator = STL_Allocator<const CallStack*, CustomAllocator>;
77
78 // Hash class for addresses.
79 struct AddressHash {
80 size_t operator() (uintptr_t addr) const;
81 };
82
83 // Comparator class for call stack objects.
84 struct CallStackCompare {
85 bool operator() (const CallStack* c1, const CallStack* c2) const {
86 return c1->depth == c2->depth &&
87 std::equal(c1->stack, c1->stack + c1->depth, c2->stack);
88 }
89 };
90
91 // Returns a CallStack object for a given call stack. Each unique call stack
92 // has its own CallStack object. If the given call stack has already been
93 // created by a previous call to this function, return a pointer to that same
94 // call stack object.
95 CallStack* GetCallStack(int depth, const void* const stack[]);
96
97 // Returns the offset of |ptr| within the current binary. If it is not in the
98 // current binary, just return |ptr| as an integer.
99 uintptr_t GetOffset(const void *ptr) const;
100
101 // Owns all unique call stack objects, which are allocated on the heap. Any
102 // other class or function that references a call stack must get it from here,
103 // but may not take ownership of the call stack object.
104 std::unordered_set<CallStack*,
105 CallStack::ComputeHash,
106 CallStackCompare,
107 TableEntryAllocator> call_stacks_;
108
109 // Allocation stats.
110 uint64_t num_allocs_;
111 uint64_t num_frees_;
112 uint64_t alloc_size_;
113 uint64_t free_size_;
114
115 uint32_t num_allocs_with_call_stack_;
116 uint32_t num_stack_tables_;
117
118 // Stores all individual recorded allocations.
119 std::unordered_map<uintptr_t,
120 AllocInfo,
121 AddressHash,
122 std::equal_to<uintptr_t>,
123 AllocationEntryAllocator> address_map_;
124
125 // Used to analyze potential leak patterns in the allocation sizes.
126 LeakAnalyzer size_leak_analyzer_;
127
128 // Allocation stats for each size.
129 std::vector<AllocSizeEntry,
130 STL_Allocator<AllocSizeEntry, CustomAllocator>> size_entries_;
131
132 // Address mapping info of the current binary.
133 uintptr_t mapping_addr_;
134 size_t mapping_size_;
135
136 // Number of consecutive times an allocation size must trigger suspicion to be
137 // considered a leak suspect.
138 int size_suspicion_threshold_;
139
140 // Number of consecutive times a call stack must trigger suspicion to be
141 // considered a leak suspect.
142 int call_stack_suspicion_threshold_;
143
144 // Enable verbose dumping of much more leak analysis data.
145 bool verbose_;
146
147 DISALLOW_COPY_AND_ASSIGN(LeakDetectorImpl);
148 };
149
150 } // namespace leak_detector
151
152 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698