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

Side by Side Diff: components/metrics/leak_detector/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: Fix style, comments, RAW_CHECK in stl_allocator.h 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_LEAK_ANALYZER_H_
6 #define COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_ANALYZER_H_
7
8 #include <map>
9 #include <vector>
10
11 #include "components/metrics/leak_detector/leak_detector_value_type.h"
12 #include "components/metrics/leak_detector/ranked_list.h"
13 #include <gperftools/custom_allocator.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 template <typename Type>
24 using Allocator = STL_Allocator<Type, CustomAllocator>;
25
26 LeakAnalyzer(uint32_t ranking_size, uint32_t num_suspicions_threshold)
27 : ranking_size_(ranking_size),
28 score_increase_(1 << num_suspicions_threshold),
29 score_threshold_((1 << num_suspicions_threshold) * 2 - 1),
jar (doing other things) 2015/08/21 02:48:42 Is it worth a CHECK_LT(num_suspicions_threshold, 3
Simon Que 2015/08/21 21:59:20 I simplified the score system. Now it goes linearl
30 ranked_entries_(ranking_size),
31 prev_ranked_entries_(ranking_size) {
32 suspected_leaks_.reserve(ranking_size);
33 }
34
35 ~LeakAnalyzer() {}
36
37 // Take in a list of allocations, sorted by count.
38 void AddSample(const RankedList& ranked_list);
39
40 // Used to report suspected leaks.
41 const std::vector<ValueType, Allocator<ValueType>>& suspected_leaks() const {
42 return suspected_leaks_;
43 }
44
45 // Log the leak detector's top sizes and suspected sizes. Writes output to log
46 // buffer |buffer| of size |size|. Returns the number of bytes remaining in
47 // the buffer after writing to it. The number of bytes remaining includes the
48 // zero terminator that was just written, so this will always return at least
49 // 1, unless |size| == 0.
50 int Dump(char* buffer, const int buffer_size) const;
jar (doing other things) 2015/08/21 02:48:42 nit: mutable operands (like buffer), used to conve
Simon Que 2015/08/21 21:59:20 Done.
51
52 private:
53 using RankedEntry = RankedList::Entry;
54
55 // Analyze a list of allocation count deltas from the previous iteration. If
56 // anything looks like a possible leak, update the suspicion scores.
57 void AnalyzeDeltas(const RankedList& ranked_deltas);
58
59 // Returns the count for the given value from the previous analysis in
60 // |count|. Returns true if the given value was present in the previous
61 // analysis, or false if not.
62 bool GetPreviousCountForValue(const ValueType& value, uint32_t* count) const;
63
64 // Look for the top |ranking_size_| entries when analyzing leaks.
65 const uint32_t ranking_size_;
66
67 // Each time an entry is suspected as a possible leak, increase its suspicion
68 // score by this much.
69 const uint32_t score_increase_;
70
71 // Report suspected leaks when the suspicion score reaches this value.
72 const uint32_t score_threshold_;
73
74 // A mapping of allocation values to suspicion score. All allocations in this
75 // container are suspected leaks. The score can increase or decrease over
76 // time. Once the score reaches |score_threshold_|, the entry is reported as
77 // a suspected leak in |suspected_leaks_|.
78 std::map<ValueType,
79 uint32_t,
80 std::less<ValueType>,
81 Allocator<std::pair<ValueType, uint32_t>>> suspected_histogram_;
82
83 // Array of allocated values that passed the suspicion threshold and are being
84 // reported.
85 std::vector<ValueType, Allocator<ValueType>> suspected_leaks_;
86
87 // The most recent allocation entries, since the last call to AddSample().
88 RankedList ranked_entries_;
89 // The previous allocation entries, from before the last call to AddSample().
90 RankedList prev_ranked_entries_;
91
92 DISALLOW_COPY_AND_ASSIGN(LeakAnalyzer);
93 };
94
95 } // namespace leak_detector
96
97 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_ANALYZER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698