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

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: 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 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(uint64_t mapping_addr,
25 uint64_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, size_t size,
37 int stack_depth, const void* const call_stack[]);
38 void RecordFree(const void* ptr);
39
40 // Run check for possible leaks based on the current profiling data.
41 void TestForLeaks();
42
43 // Dump current profiling statistics to log.
44 void DumpStats() const;
45
46 private:
47 // A record of allocations for a particular size.
48 struct AllocSizeEntry {
49 // Number of allocations and frees for this size.
50 uint32_t num_allocs;
51 uint32_t num_frees;
52
53 // A stack table, if this size is being profiled for stack as well.
54 CallStackTable* stack_table;
55 };
56
57 // Info for a single allocation.
58 struct AllocInfo {
59 AllocInfo() : call_stack(nullptr) {}
60
61 // Number of bytes in this allocation.
62 size_t size;
63
64 // Points to a unique call stack.
65 const CallStack* call_stack;
66 };
67
68 // Allocator class for allocation entry map. Maps allocated addresses to
69 // AllocInfo objects.
70 using AllocationEntryAllocator =
71 STL_Allocator<std::pair<const void*, AllocInfo>, CustomAllocator>;
72
73 // Allocator class for unique call stacks.
74 using TableEntryAllocator = STL_Allocator<const CallStack*, CustomAllocator>;
75
76 // Hash class for addresses.
77 struct AddressHash {
78 size_t operator() (const void* ptr) const;
79 };
80
81 // Comparator class for call stack objects.
82 struct CallStackCompare {
83 bool operator() (const CallStack* c1, const CallStack* c2) const {
84 return c1->depth == c2->depth &&
85 std::equal(c1->stack, c1->stack + c1->depth, c2->stack);
86 }
87 };
88
89 // Hash class for call stack objects.
90 struct CallStackHash {
91 size_t operator() (const CallStack* call_stack) const;
92 };
93
94 // Returns a CallStack object for a given call stack. Each unique call stack
95 // has its own CallStack object. If the given call stack has already been
96 // created by a previous call to this function, return a pointer to that same
97 // call stack object.
98 CallStack* GetCallStack(int depth, const void* const stack[]);
99
100 // Returns the offset of |ptr| within the current binary. If it is not in the
101 // current binary, just return |ptr| as an integer.
102 uintptr_t GetOffset(const void *ptr) const;
103
104 // Owns all unique call stack objects, which are allocated on the heap. Any
105 // other class or function that references a call stack must get it from here,
106 // but may not take ownership of the call stack object.
107 std::unordered_set<CallStack*,
108 CallStackHash,
109 CallStackCompare,
110 TableEntryAllocator> call_stacks_;
111
112 // Allocation stats.
113 uint64_t num_allocs_;
114 uint64_t num_frees_;
115 uint64_t alloc_size_;
116 uint64_t free_size_;
117
118 uint32_t num_allocs_with_call_stack_;
119 uint32_t num_stack_tables_;
120
121 // Stores all individual recorded allocations.
122 std::unordered_map<const void*,
123 AllocInfo,
124 AddressHash,
125 std::equal_to<const void*>,
126 AllocationEntryAllocator> address_map_;
127
128 // Used to analyze potential leak patterns in the allocation sizes.
129 LeakAnalyzer size_leak_analyzer_;
130
131 // Allocation stats for each size.
132 std::vector<AllocSizeEntry,
133 STL_Allocator<AllocSizeEntry, CustomAllocator>> size_entries_;
134
135 // Address mapping info of the current binary.
136 uint64_t mapping_addr_;
137 uint64_t mapping_size_;
138
139 // Number of consecutive times an allocation size must trigger suspicion to be
140 // considered a leak suspect.
141 int size_suspicion_threshold_;
142
143 // Number of consecutive times a call stack must trigger suspicion to be
144 // considered a leak suspect.
145 int call_stack_suspicion_threshold_;
146
147 // Enable verbose dumping of much more leak analysis data.
148 bool verbose_;
149
150 DISALLOW_COPY_AND_ASSIGN(LeakDetectorImpl);
151 };
152
153 } // namespace leak_detector
154
155 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698