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

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: Add LeakDetector class; Remove tcmalloc dependencies; Add export classes to gperftools; Remove farm… 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 <stdint.h>
9
10 #include <unordered_set>
11
12 #include "components/metrics/leak_detector/call_stack_table.h"
13 #include "components/metrics/leak_detector/leak_analyzer.h"
14 #include <gperftools/custom_allocator.h>
15
16 namespace leak_detector {
17
18 //----------------------------------------------------------------------
19 // Class that contains the actual leak detection mechanism.
20 //----------------------------------------------------------------------
21 class LeakDetectorImpl {
22 public:
23 LeakDetectorImpl(uint64_t mapping_addr,
24 uint64_t mapping_size,
25 int size_suspicion_threshold,
26 int call_stack_suspicion_threshold,
27 bool verbose);
28 ~LeakDetectorImpl();
29
30 // Indicates whether the given allocation size has an associated call stack
31 // table, and thus requires a stack unwind.
32 bool ShouldGetStackTraceForSize(size_t size) const;
33
34 // Record allocs and frees.
35 void RecordAlloc(const void* ptr, size_t size,
36 int stack_depth, const void* const call_stack[]);
37 void RecordFree(const void* ptr);
38
39 // Run check for possible leaks based on the current profiling data.
40 void TestForLeaks();
41
42 // Dump current profiling statistics to log.
43 void DumpStats() const;
44
45 private:
46 // A record of allocations for a particular size.
47 struct AllocSizeEntry {
48 // Number of allocations and frees for this size.
49 uint32_t num_allocs;
50 uint32_t num_frees;
51
52 // A stack table, if this size is being profiled for stack as well.
53 CallStackTable* stack_table;
54 };
55
56 // Info for a single allocation.
57 struct AllocInfo {
58 AllocInfo() : call_stack(nullptr) {}
59
60 // Number of bytes in this allocation.
61 size_t bytes;
62
63 // Points to a unique call stack.
64 const CallStack* call_stack;
65 };
66
67 // Allocator class for allocation entry map. Maps allocated addresses to
68 // AllocInfo objects.
69 using AllocationEntryAllocator =
70 STL_Allocator<std::pair<const void*, AllocInfo>, CustomAllocator>;
71
72 // Allocator class for unique call stacks.
73 using TableEntryAllocator = STL_Allocator<const CallStack*, CustomAllocator>;
74
75 // Hash class for addresses.
76 struct AddressHash {
77 size_t operator() (const void* ptr) const;
78 };
79
80 // Comparator class for call stack objects.
81 struct CallStackCompare {
82 bool operator() (const CallStack* c1, const CallStack* c2) const {
83 return c1->depth == c2->depth &&
84 std::equal(c1->stack, c1->stack + c1->depth, c2->stack);
85 }
86 };
87
88 // Hash class for call stack objects.
89 struct CallStackHash {
90 size_t operator() (const CallStack* call_stack) const;
91 };
92
93 // Number of entries in the alloc size table. As sizes are aligned to 32-bits
94 // the max supported allocation size is (kNumSizeEntries * 4 - 1). Any larger
95 // sizes are ignored. This value is chosen high enough that such large sizes
96 // are rare if not nonexistent.
97 static const int kNumSizeEntries = 2048;
98
99 // Converts an allocation size to/from the array index used for |entries_|.
100 static int SizeToIndex(size_t size);
101 static size_t IndexToSize(int index);
102
103 // Accessor for the entry table.
104 inline AllocSizeEntry* GetEntryForSize(size_t size) {
105 return &entries_[SizeToIndex(size)];
106 }
107 inline const AllocSizeEntry& GetConstEntryForSize(size_t size) const {
108 return entries_[SizeToIndex(size)];
109 }
110
111 // Returns a CallStack object for a given call stack. Each unique call stack
112 // has its own CallStack object. If the given call stack has already been
113 // created by a previous call to this function, return a pointer to that same
114 // call stack object.
115 CallStack* GetCallStack(int depth, const void* const stack[]);
116
117 // Returns the offset of |ptr| within the current binary. If it is not in the
118 // current binary, just return |ptr| as an integer.
119 uint64_t GetOffset(const void *ptr) const;
120
121 // Hash function for stack tables.
122 static size_t CallStackToHash(const CallStack* call_stack);
123
124 // Owns all unique call stack objects, which are allocated on the heap. Any
125 // other class or function that references a call stack must get it from here,
126 // but may not take ownership of the call stack object.
127 std::unordered_set<CallStack*,
128 CallStackHash,
129 CallStackCompare,
130 TableEntryAllocator> call_stacks_;
131
132 // Allocation stats.
133 uint64_t num_allocs_;
134 uint64_t num_frees_;
135 uint64_t alloc_size_;
136 uint64_t free_size_;
137
138 uint32_t num_allocs_with_call_stack_;
139 uint32_t num_stack_tables_;
140
141 // Stores all individual recorded allocations.
142 std::unordered_map<const void*,
143 AllocInfo,
144 AddressHash,
145 std::equal_to<const void*>,
146 AllocationEntryAllocator> address_map_;
147
148 // Used to analyze potential leak patterns in the allocation sizes.
149 LeakAnalyzer size_leak_analyzer_;
150
151 // Allocation stats for each size.
152 AllocSizeEntry entries_[kNumSizeEntries];
153
154 // Address mapping info of the current binary.
155 uint64_t mapping_addr_;
156 uint64_t mapping_size_;
157
158 // Number of consecutive times an allocation size must trigger suspicion to be
159 // considered a leak suspect.
160 int size_suspicion_threshold_;
161
162 // Number of consecutive times a call stack must trigger suspicion to be
163 // considered a leak suspect.
164 int call_stack_suspicion_threshold_;
165
166 // Enable verbose dumping of much more leak analysis data.
167 bool verbose_;
168
169 DISALLOW_COPY_AND_ASSIGN(LeakDetectorImpl);
170 };
171
172 } // namespace leak_detector
173
174 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698