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

Side by Side Diff: components/metrics/leak_detector/leak_detector_impl.h

Issue 1472763003: Reland of: components/metrics: Add runtime memory leak detector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased Created 5 years 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 <vector>
11
12 #include "base/containers/hash_tables.h"
13 #include "base/macros.h"
14 #include "components/metrics/leak_detector/call_stack_manager.h"
15 #include "components/metrics/leak_detector/custom_allocator.h"
16 #include "components/metrics/leak_detector/leak_analyzer.h"
17
18 namespace metrics {
19 namespace leak_detector {
20
21 class CallStackTable;
22
23 // Class that contains the actual leak detection mechanism.
24 // Not thread-safe.
25 class LeakDetectorImpl {
26 public:
27 // Vector type that's safe to use within the memory leak detector. Uses
28 // CustomAllocator to avoid recursive malloc hook invocation when analyzing
29 // allocs and frees.
30 template <typename T>
31 using InternalVector = std::vector<T, STLAllocator<T, CustomAllocator>>;
32
33 // Leak report generated by LeakDetectorImpl.
34 class LeakReport {
35 public:
36 LeakReport();
37 ~LeakReport();
38
39 size_t alloc_size_bytes() const { return alloc_size_bytes_; }
40
41 const InternalVector<uintptr_t>& call_stack() const { return call_stack_; }
42
43 // Used to compare the contents of two leak reports.
44 bool operator<(const LeakReport& other) const;
45
46 private:
47 // LeakDetectorImpl needs access to class members when creating a new leak
48 // report.
49 friend class LeakDetectorImpl;
50
51 // Number of bytes allocated by the leak site during each allocation.
52 size_t alloc_size_bytes_;
53
54 // Unlike the CallStack struct, which consists of addresses, this call stack
55 // will contain offsets in the executable binary.
56 InternalVector<uintptr_t> call_stack_;
57
58 // TODO(sque): Add leak detector parameters.
59 };
60
61 LeakDetectorImpl(uintptr_t mapping_addr,
62 size_t mapping_size,
63 int size_suspicion_threshold,
64 int call_stack_suspicion_threshold);
65 ~LeakDetectorImpl();
66
67 // Indicates whether the given allocation size has an associated call stack
68 // table, and thus requires a stack unwind.
69 bool ShouldGetStackTraceForSize(size_t size) const;
70
71 // Record allocs and frees.
72 void RecordAlloc(const void* ptr,
73 size_t size,
74 int stack_depth,
75 const void* const call_stack[]);
76 void RecordFree(const void* ptr);
77
78 // Run check for possible leaks based on the current profiling data.
79 void TestForLeaks(InternalVector<LeakReport>* reports);
80
81 private:
82 // A record of allocations for a particular size.
83 struct AllocSizeEntry {
84 // Number of allocations and frees for this size.
85 uint32_t num_allocs;
86 uint32_t num_frees;
87
88 // A stack table, if this size is being profiled for stack as well.
89 CallStackTable* stack_table;
90 };
91
92 // Info for a single allocation.
93 struct AllocInfo {
94 AllocInfo() : call_stack(nullptr) {}
95
96 // Number of bytes in this allocation.
97 size_t size;
98
99 // Points to a unique call stack.
100 const CallStack* call_stack;
101 };
102
103 // Allocator class for allocation entry map. Maps allocated addresses to
104 // AllocInfo objects.
105 using AllocationEntryAllocator =
106 STLAllocator<std::pair<const void*, AllocInfo>, CustomAllocator>;
107
108 // Hash class for addresses.
109 struct AddressHash {
110 size_t operator()(uintptr_t addr) const;
111 };
112
113 // Returns the offset of |ptr| within the current binary. If it is not in the
114 // current binary, just return |ptr| as an integer.
115 uintptr_t GetOffset(const void* ptr) const;
116
117 // Owns all unique call stack objects, which are allocated on the heap. Any
118 // other class or function that references a call stack must get it from here,
119 // but may not take ownership of the call stack object.
120 CallStackManager call_stack_manager_;
121
122 // Allocation stats.
123 uint64_t num_allocs_;
124 uint64_t num_frees_;
125 uint64_t alloc_size_;
126 uint64_t free_size_;
127
128 uint32_t num_allocs_with_call_stack_;
129 uint32_t num_stack_tables_;
130
131 // Stores all individual recorded allocations.
132 base::hash_map<uintptr_t,
133 AllocInfo,
134 AddressHash,
135 std::equal_to<uintptr_t>,
136 AllocationEntryAllocator> address_map_;
137
138 // Used to analyze potential leak patterns in the allocation sizes.
139 LeakAnalyzer size_leak_analyzer_;
140
141 // Allocation stats for each size.
142 InternalVector<AllocSizeEntry> size_entries_;
143
144 // Address mapping info of the current binary.
145 uintptr_t mapping_addr_;
146 size_t mapping_size_;
147
148 // Number of consecutive times an allocation size must trigger suspicion to be
149 // considered a leak suspect.
150 int size_suspicion_threshold_;
151
152 // Number of consecutive times a call stack must trigger suspicion to be
153 // considered a leak suspect.
154 int call_stack_suspicion_threshold_;
155
156 DISALLOW_COPY_AND_ASSIGN(LeakDetectorImpl);
157 };
158
159 } // namespace leak_detector
160 } // namespace metrics
161
162 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_
OLDNEW
« no previous file with comments | « components/metrics/leak_detector/leak_analyzer_unittest.cc ('k') | components/metrics/leak_detector/leak_detector_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698