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

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

Issue 986503002: components/metrics: Add runtime memory leak detector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use uintptr_t/size_t for mapping info; Fix formatting of RecordAlloc Created 5 years, 4 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 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_CALL_STACK_TABLE_H_
6 #define COMPONENTS_METRICS_LEAK_DETECTOR_CALL_STACK_TABLE_H_
7
8 #include <stdint.h>
9
10 #include <functional>
11 #include <unordered_map>
12
13 #include "components/metrics/leak_detector/leak_analyzer.h"
14 #include "components/metrics/leak_detector/stl_allocator.h"
15 #include <gperftools/custom_allocator.h>
wtc 2015/08/21 20:02:12 This header file name should probably be put in ""
Simon Que 2015/08/22 22:41:51 Done. The presubmit will fail if they are put in "
16
17 namespace leak_detector {
18
19 // Struct to represent a call stack.
20 struct CallStack {
21 static const int kMaxStackDepth = 32; // Max call stack depth.
22
23 uint32_t depth; // Depth of current call stack.
24 const void** stack; // Call stack as an array of addrs.
25
26 size_t hash; // Hash of call stack.
27 };
28
29 // Contains a hash table where the key is the call stack and the value is the
30 // number of allocations from that call stack.
31 class CallStackTable {
32 public:
33 struct Hash {
34 size_t operator() (const CallStack* call_stack) const {
35 // The call stack object should already have a hash computed when it was
36 // created.
37 return call_stack->hash;
38 }
39 };
40
41 explicit CallStackTable(int call_stack_suspicion_threshold);
42 ~CallStackTable();
43
44 // Add/Remove an allocation for the given call stack.
45 void Add(const CallStack* call_stack);
46 void Remove(const CallStack* call_stack);
47
48 // Dump contents to log buffer |buffer| of size |size|. Returns the number of
49 // bytes remaining in the buffer after writing to it. The number of bytes
50 // remaining includes the zero terminator that was just written, so this will
51 // always return at least 1, unless |size| == 0.
52 int Dump(char* buffer, int size) const;
53
54 // Check for leak patterns in the allocation data.
55 void TestForLeaks();
56
57 const LeakAnalyzer& leak_analyzer() const {
58 return leak_analyzer_;
59 }
60
61 bool empty() const {
62 return entry_map_.empty();
63 }
64
65 private:
66 // Hash table entry used to track number of allocs and frees for a call stack.
67 struct Entry {
68 // Number of allocs/frees for the call stack.
69 uint32_t num_allocs;
70 uint32_t num_frees;
71 };
72
73 // The total number of recorded allocations when this object was created. This
74 // acts as a timestamp of sorts.
75 uint64_t total_num_allocs_at_creation_;
76
77 // Total number of allocs and frees in this table.
78 uint32_t num_allocs_;
79 uint32_t num_frees_;
80
81 // Hash table containing entries.
82 using TableEntryAllocator =
83 STL_Allocator<std::pair<const CallStack*, Entry>, CustomAllocator>;
84 std::unordered_map<const CallStack*,
85 Entry,
86 Hash,
87 std::equal_to<const CallStack*>,
88 TableEntryAllocator> entry_map_;
89
90 // For detecting leak patterns in incoming allocations.
91 LeakAnalyzer leak_analyzer_;
92
93 DISALLOW_COPY_AND_ASSIGN(CallStackTable);
94 };
95
96 } // namespace leak_detector
97
98 #endif // CALL_STACK_TABLE_H_
wtc 2015/08/21 20:02:12 Nit: this comment should say COMPONENTS_METRICS_LE
Simon Que 2015/08/22 22:41:51 Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698