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

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: Create CallStackManager out of LeakDetectorImpl Created 5 years, 3 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 <gperftools/custom_allocator.h>
9
10 #include <map>
11 #include <vector>
12
13 #include "base/macros.h"
14 #include "components/metrics/leak_detector/leak_detector_value_type.h"
15 #include "components/metrics/leak_detector/ranked_list.h"
16
17 // This class looks for possible leak patterns in allocation data over time.
18
19 namespace leak_detector {
20
21 class LeakAnalyzer {
22 public:
23 using ValueType = LeakDetectorValueType;
24
25 template <typename Type>
26 using Allocator = STL_Allocator<Type, CustomAllocator>;
27
28 LeakAnalyzer(uint32_t ranking_size, uint32_t num_suspicions_threshold)
29 : ranking_size_(ranking_size),
30 score_threshold_(num_suspicions_threshold),
31 ranked_entries_(ranking_size),
32 prev_ranked_entries_(ranking_size) {
33 suspected_leaks_.reserve(ranking_size);
34 }
35
36 ~LeakAnalyzer() {}
37
38 // Take in a RankedList of allocations, sorted by count. Removes the contents
39 // of |ranked_list|.
40 void AddSample(RankedList&& ranked_list);
41
42 // Used to report suspected leaks. Reported leaks are sorted by ValueType.
43 const std::vector<ValueType, Allocator<ValueType>>& suspected_leaks() const {
44 return suspected_leaks_;
45 }
46
47 // Log the leak detector's top sizes and suspected sizes. Writes output to log
48 // buffer |buffer| of size |size|. Returns the number of bytes remaining in
49 // the buffer after writing to it. The number of bytes remaining includes the
50 // zero terminator that was just written, so this will always return at least
51 // 1, unless |size| == 0.
52 size_t Dump(const size_t buffer_size, char* buffer) const;
53
54 private:
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 // Report suspected leaks when the suspicion score reaches this value.
68 const uint32_t score_threshold_;
69
70 // A mapping of allocation values to suspicion score. All allocations in this
71 // container are suspected leaks. The score can increase or decrease over
72 // time. Once the score reaches |score_threshold_|, the entry is reported as
73 // a suspected leak in |suspected_leaks_|.
74 std::map<ValueType,
75 uint32_t,
76 std::less<ValueType>,
77 Allocator<std::pair<ValueType, uint32_t>>> suspected_histogram_;
78
79 // Array of allocated values that passed the suspicion threshold and are being
80 // reported.
81 std::vector<ValueType, Allocator<ValueType>> suspected_leaks_;
82
83 // The most recent allocation entries, since the last call to AddSample().
84 RankedList ranked_entries_;
85 // The previous allocation entries, from before the last call to AddSample().
86 RankedList prev_ranked_entries_;
87
88 DISALLOW_COPY_AND_ASSIGN(LeakAnalyzer);
89 };
90
91 } // namespace leak_detector
92
93 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_ANALYZER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698