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