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

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

Powered by Google App Engine
This is Rietveld 408576698