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

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: Use uintptr_t/size_t for mapping info; Fix formatting of RecordAlloc 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 #include <vector>
12
13 #include "components/metrics/leak_detector/call_stack_table.h"
14 #include "components/metrics/leak_detector/leak_analyzer.h"
15 #include <gperftools/custom_allocator.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() (const void* ptr) 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 // Hash class for call stack objects.
92 struct CallStackHash {
93 size_t operator() (const CallStack* call_stack) const;
94 };
95
96 // Returns a CallStack object for a given call stack. Each unique call stack
97 // has its own CallStack object. If the given call stack has already been
98 // created by a previous call to this function, return a pointer to that same
99 // call stack object.
100 CallStack* GetCallStack(int depth, const void* const stack[]);
101
102 // Returns the offset of |ptr| within the current binary. If it is not in the
103 // current binary, just return |ptr| as an integer.
104 uintptr_t GetOffset(const void *ptr) const;
105
106 // Owns all unique call stack objects, which are allocated on the heap. Any
107 // other class or function that references a call stack must get it from here,
108 // but may not take ownership of the call stack object.
109 std::unordered_set<CallStack*,
110 CallStackHash,
111 CallStackCompare,
112 TableEntryAllocator> call_stacks_;
113
114 // Allocation stats.
115 uint64_t num_allocs_;
116 uint64_t num_frees_;
117 uint64_t alloc_size_;
118 uint64_t free_size_;
119
120 uint32_t num_allocs_with_call_stack_;
121 uint32_t num_stack_tables_;
122
123 // Stores all individual recorded allocations.
124 std::unordered_map<const void*,
125 AllocInfo,
126 AddressHash,
127 std::equal_to<const void*>,
128 AllocationEntryAllocator> address_map_;
129
130 // Used to analyze potential leak patterns in the allocation sizes.
131 LeakAnalyzer size_leak_analyzer_;
132
133 // Allocation stats for each size.
134 std::vector<AllocSizeEntry,
135 STL_Allocator<AllocSizeEntry, CustomAllocator>> size_entries_;
136
137 // Address mapping info of the current binary.
138 uintptr_t mapping_addr_;
139 size_t mapping_size_;
140
141 // Number of consecutive times an allocation size must trigger suspicion to be
142 // considered a leak suspect.
143 int size_suspicion_threshold_;
144
145 // Number of consecutive times a call stack must trigger suspicion to be
146 // considered a leak suspect.
147 int call_stack_suspicion_threshold_;
148
149 // Enable verbose dumping of much more leak analysis data.
150 bool verbose_;
151
152 DISALLOW_COPY_AND_ASSIGN(LeakDetectorImpl);
153 };
154
155 } // namespace leak_detector
156
157 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698