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

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

Powered by Google App Engine
This is Rietveld 408576698