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

Side by Side Diff: third_party/tcmalloc/chromium/src/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: Remove from gn build; keep it simple; can add it in later Created 5 years, 5 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 (c) 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 // ---
6 // Author: Simon Que
7
8 #ifndef _LEAK_DETECTOR_IMPL_H_
9 #define _LEAK_DETECTOR_IMPL_H_
10
11 #include <unordered_set>
12
13 #include "addressmap-inl.h"
14 #include "base/basictypes.h"
15 #include "base/custom_allocator.h"
16 #include "heap-profile-stats.h"
17 #include "leak_analyzer.h"
18
19 namespace leak_detector {
20
21 class CallStackTable;
22
23 //----------------------------------------------------------------------
24 // Class that contains the actual leak detection mechanism.
25 //----------------------------------------------------------------------
26 class LeakDetectorImpl {
27 public:
28 // Used for tracking allocation stats.
29 using Stats = HeapProfileStats;
30
31 LeakDetectorImpl(uint64 mapping_addr, uint64 mapping_size);
32 ~LeakDetectorImpl();
33
34 // Indicates whether the given allocation size has an associated call stack
35 // table, and thus requires a stack unwind.
36 bool ShouldGetStackTraceForSize(size_t size) const;
37
38 // Record allocs and frees.
39 void RecordAlloc(const void* ptr, size_t size,
40 int stack_depth, const void* const call_stack[]);
41 void RecordFree(const void* ptr);
42
43 const Stats& stats() const {
44 return stats_;
45 }
46
47 // Run check for possible leaks based on the current profiling data.
48 void TestForLeaks();
49
50 // Dump current profiling statistics to log.
51 void DumpStats() const;
52
53 private:
54 // Used for tracking unique call stacks.
55 using CallStack = HeapProfileBucket;
56
57 // A record of allocations for a particular size.
58 struct AllocSizeEntry {
59 // Number of allocations and frees for this size.
60 uint32 num_allocs;
61 uint32 num_frees;
62
63 // A stack table, if this size is being profiled for stack as well.
64 CallStackTable* stack_table;
65 };
66
67 // Info stored in the address map
68 struct AllocInfo {
69 AllocInfo() : call_stack(NULL) {}
70
71 // Number of bytes in this allocation.
72 size_t bytes;
73
74 // Points to a unique call stack.
75 CallStack* call_stack;
76 };
77
78 // Used for recording size and call stack info for each allocation.
79 using AllocationMap = AddressMap<AllocInfo>;
80
81 // Hash table for tracking unique call stacks.
82 // TODO: Using std::unordered_set makes |CallStack::next| redundant. Consider
83 // creating a new struct or removing |next|.
84 using TableEntryAllocator = STL_Allocator<const CallStack*, CustomAllocator>;
85
86 struct CallStackCompare {
87 bool operator() (const CallStack* c1, const CallStack* c2) const {
88 return c1->depth == c2->depth &&
89 std::equal(c1->stack, c1->stack + c1->depth, c2->stack);
90 }
91 };
92
93 struct CallStackHash {
94 size_t operator() (const CallStack* call_stack) const {
95 return CallStackToHash(call_stack);
96 }
97 };
98
99 // Number of entries in the alloc size table. As sizes are aligned to 32-bits
100 // the max supported allocation size is (kNumSizeEntries * 4 - 1). Any larger
101 // sizes are ignored. This value is chosen high enough that such large sizes
102 // are rare if not nonexistent.
103 static const int kNumSizeEntries = 2048;
104
105 // Converts an allocation size to/from the array index used for |entries_|.
106 static int SizeToIndex(size_t size);
107 static size_t IndexToSize(int index);
108
109 // Accessor for the entry table.
110 inline AllocSizeEntry* GetEntryForSize(size_t size) {
111 return &entries_[SizeToIndex(size)];
112 }
113 inline const AllocSizeEntry& GetConstEntryForSize(size_t size) const {
114 return entries_[SizeToIndex(size)];
115 }
116
117 // Returns a CallStack object for a given call stack. Each unique call stack
118 // has its own CallStack object. If the given call stack has already been
119 // created by a previous call to this function, return a pointer to that same
120 // call stack object.
121 CallStack* GetCallStack(int depth, const void* const stack[]);
122
123 // Returns the offset of |ptr| within the current binary. If it is not in the
124 // current binary, just return |ptr| as an integer.
125 uint64 GetOffset(const void *ptr) const;
126
127 // Hash function for stack tables.
128 static size_t CallStackToHash(const CallStack* call_stack);
129
130 std::unordered_set<CallStack*,
131 CallStackHash,
132 CallStackCompare,
133 TableEntryAllocator> call_stacks_;
134
135 // For tracking allocation stats.
136 Stats stats_;
137 Stats call_stack_stats_;
138 int num_stack_tables_;
139
140 // Stores all individual recorded allocations.
141 AllocationMap address_map_;
142
143 // Used to analyze potential leak patterns in the allocation sizes.
144 LeakAnalyzer<uint32> size_leak_analyzer_;
145
146 // Allocation stats for each size.
147 AllocSizeEntry entries_[kNumSizeEntries];
148
149 // Address mapping info of the current binary.
150 uint64 mapping_addr_;
151 uint64 mapping_size_;
152
153 DISALLOW_COPY_AND_ASSIGN(LeakDetectorImpl);
154 };
155
156 } // namespace leak_detector
157
158 #endif // _LEAK_DETECTOR_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698