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

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: Add OWNERS file 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_ANALYZER_H_
6 #define COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_ANALYZER_H_
7
8 #include <map>
9 #include <vector>
10
11 #include "base/macros.h"
12 #include "components/metrics/leak_detector/custom_allocator.h"
13 #include "components/metrics/leak_detector/leak_detector_value_type.h"
14 #include "components/metrics/leak_detector/ranked_list.h"
15 #include "components/metrics/leak_detector/stl_allocator.h"
16
17 // This class looks for possible leak patterns in allocation data over time.
18
19 namespace metrics {
20 namespace leak_detector {
21
22 class LeakAnalyzer {
23 public:
24 using ValueType = LeakDetectorValueType;
25
26 template <typename Type>
27 using Allocator = STLAllocator<Type, CustomAllocator>;
28
29 LeakAnalyzer(uint32_t ranking_size, uint32_t num_suspicions_threshold)
30 : ranking_size_(ranking_size),
31 score_threshold_(num_suspicions_threshold),
32 ranked_entries_(ranking_size),
33 prev_ranked_entries_(ranking_size) {
34 suspected_leaks_.reserve(ranking_size);
35 }
36
37 ~LeakAnalyzer() {}
38
39 // Take in a RankedList of allocations, sorted by count. Removes the contents
40 // of |ranked_list|.
41 void AddSample(RankedList ranked_list);
42
43 // Used to report suspected leaks. Reported leaks are sorted by ValueType.
44 const std::vector<ValueType, Allocator<ValueType>>& suspected_leaks() const {
45 return suspected_leaks_;
46 }
47
48 private:
49 // Analyze a list of allocation count deltas from the previous iteration. If
50 // anything looks like a possible leak, update the suspicion scores.
51 void AnalyzeDeltas(const RankedList& ranked_deltas);
52
53 // Returns the count for the given value from the previous analysis in
54 // |count|. Returns true if the given value was present in the previous
55 // analysis, or false if not.
56 bool GetPreviousCountForValue(const ValueType& value, uint32_t* count) const;
57
58 // Look for the top |ranking_size_| entries when analyzing leaks.
59 const uint32_t ranking_size_;
60
61 // Report suspected leaks when the suspicion score reaches this value.
62 const uint32_t score_threshold_;
63
64 // A mapping of allocation values to suspicion score. All allocations in this
65 // container are suspected leaks. The score can increase or decrease over
66 // time. Once the score reaches |score_threshold_|, the entry is reported as
67 // a suspected leak in |suspected_leaks_|.
68 std::map<ValueType,
69 uint32_t,
70 std::less<ValueType>,
71 Allocator<std::pair<ValueType, uint32_t>>> suspected_histogram_;
72
73 // Array of allocated values that passed the suspicion threshold and are being
74 // reported.
75 std::vector<ValueType, Allocator<ValueType>> suspected_leaks_;
76
77 // The most recent allocation entries, since the last call to AddSample().
78 RankedList ranked_entries_;
79 // The previous allocation entries, from before the last call to AddSample().
80 RankedList prev_ranked_entries_;
81
82 DISALLOW_COPY_AND_ASSIGN(LeakAnalyzer);
83 };
84
85 } // namespace leak_detector
86 } // namespace metrics
87
88 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_ANALYZER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698