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

Side by Side Diff: third_party/tcmalloc/chromium/src/leak_analyzer.h

Issue 986503002: components/metrics: Add runtime memory leak detector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove Author label from new files; Check type in LeakDetectorValueType comparators; Add missing fi… Created 5 years, 5 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 (c) 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 _LEAK_ANALYZER_H_
6 #define _LEAK_ANALYZER_H_
7
8 #include <map>
9 #include <vector>
10
11 #include "base/custom_allocator.h"
12 #include "leak_detector_value_type.h"
13 #include "ranked_list.h"
14
15 // This class looks for possible leak patterns in allocation data over time.
16
17 namespace leak_detector {
18
19 class LeakAnalyzer {
20 public:
21 using ValueType = LeakDetectorValueType;
22
23 // Interface for printing strings for various value types.
24 class StringPrint {
25 public:
26 // Gets the string representation of a value. Returns the string stored in
27 // |buffer_|. The contents of |buffer_| will change if this is called again
28 // with different inputs.
29 virtual const char* ValueToString(const ValueType& value,
30 bool spacing_on) = 0;
31
32 // Gets the word that describes the value type.
33 virtual const char* ValueTypeName(bool is_plural) = 0;
34
35 protected:
36 // Buffer for returning the string representation of a value.
37 static const int kStringPrintBufferSize = 1024;
38 char buffer_[kStringPrintBufferSize];
39 };
40
41 template <typename Type>
42 using Allocator = STL_Allocator<Type, CustomAllocator>;
43
44 LeakAnalyzer(int ranking_size,
45 int num_suspicions_threshold,
46 StringPrint* string_print)
47 : ranking_size_(ranking_size),
48 score_increase_(1 << num_suspicions_threshold),
49 score_threshold_((1 << num_suspicions_threshold) * 2 - 1),
50 ranked_entries_(ranking_size),
51 prev_ranked_entries_(ranking_size),
52 string_print_(string_print) {
53 suspected_leaks_.reserve(ranking_size);
54 }
55
56 ~LeakAnalyzer() {}
57
58 // Take in a list of allocations, sorted by count.
59 void AddSample(const RankedList& ranked_list);
60
61 // Used to report suspected leaks.
62 const std::vector<ValueType, Allocator<ValueType>>& suspected_leaks() const {
63 return suspected_leaks_;
64 }
65
66 // Log the leak detector's top sizes and suspected sizes. |buffer| is a char
67 // buffer of size |buffer_size| that can eventually be logged.
68 int Dump(char* buffer, const int buffer_size) const;
69
70 private:
71 using RankedEntry = RankedList::Entry;
72
73 // Analyze a list of allocation count deltas from the previous iteration. If
74 // anything looks like a possible leak, update the suspicion scores.
75 void AnalyzeDeltas(const RankedList& ranked_deltas);
76
77 // Returns the RankedEntry from the previous analysis that had the given
78 // value, or NULL if it didn't exist.
79 const RankedEntry* GetPreviousEntryWithValue(const ValueType& value) const;
80
81 // Look for the top |ranking_size_| entries when analyzing leaks.
82 const int ranking_size_;
83
84 // Each time an entry is suspected as a possible leak, increase its suspicion
85 // score by this much.
86 const int score_increase_;
87
88 // Report suspected leaks when the suspicion score reaches this value.
89 const int score_threshold_;
90
91 // A mapping of allocation values to suspicion score. All allocations in this
92 // container are suspected leaks. The score can increase or decrease over
93 // time. Once the score reaches |score_threshold_|, the entry is reported as
94 // a suspected leak in |suspected_leaks_|.
95 std::map<ValueType,
96 uint32,
97 std::less<ValueType>,
98 Allocator<std::pair<ValueType, uint32>>> suspected_histogram_;
99
100 // Array of allocated values that passed the suspicion threshold and are being
101 // reported.
102 std::vector<ValueType, Allocator<ValueType>> suspected_leaks_;
103
104 // The most recent allocation entries, since the last call to AddSample().
105 RankedList ranked_entries_;
106 // The previous allocation entries, from before the last call to AddSample().
107 RankedList prev_ranked_entries_;
108
109 // For logging.
110 StringPrint* string_print_;
111
112 DISALLOW_COPY_AND_ASSIGN(LeakAnalyzer);
113 };
114
115 } // namespace leak_detector
116
117 #endif // _LEAK_ANALYZER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698