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

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: LeakDetectorImpl stores addrs as uintptr_t; Implement move semantics for RankedList 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 <gperftools/custom_allocator.h>
9
10 #include <map>
11 #include <vector>
12
13 #include "components/metrics/leak_detector/leak_detector_value_type.h"
14 #include "components/metrics/leak_detector/ranked_list.h"
15
16 // This class looks for possible leak patterns in allocation data over time.
17
18 namespace leak_detector {
19
20 class LeakAnalyzer {
21 public:
22 using ValueType = LeakDetectorValueType;
23
24 template <typename Type>
25 using Allocator = STL_Allocator<Type, CustomAllocator>;
26
27 LeakAnalyzer(uint32_t ranking_size, uint32_t num_suspicions_threshold)
28 : ranking_size_(ranking_size),
29 score_threshold_(num_suspicions_threshold),
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 RankedList of allocations, sorted by count. Removes the contents
38 // of |ranked_list|.
39 void AddSample(RankedList&& ranked_list);
40
41 // Used to report suspected leaks. Reported leaks are sorted by ValueType.
42 const std::vector<ValueType, Allocator<ValueType>>& suspected_leaks() const {
43 return suspected_leaks_;
44 }
45
46 // Log the leak detector's top sizes and suspected sizes. Writes output to log
47 // buffer |buffer| of size |size|. Returns the number of bytes remaining in
48 // the buffer after writing to it. The number of bytes remaining includes the
49 // zero terminator that was just written, so this will always return at least
50 // 1, unless |size| == 0.
51 size_t Dump(const size_t buffer_size, char* buffer) const;
52
53 private:
54 // Analyze a list of allocation count deltas from the previous iteration. If
55 // anything looks like a possible leak, update the suspicion scores.
56 void AnalyzeDeltas(const RankedList& ranked_deltas);
57
58 // Returns the count for the given value from the previous analysis in
59 // |count|. Returns true if the given value was present in the previous
60 // analysis, or false if not.
61 bool GetPreviousCountForValue(const ValueType& value, uint32_t* count) const;
62
63 // Look for the top |ranking_size_| entries when analyzing leaks.
64 const uint32_t ranking_size_;
65
66 // Report suspected leaks when the suspicion score reaches this value.
67 const uint32_t score_threshold_;
68
69 // A mapping of allocation values to suspicion score. All allocations in this
70 // container are suspected leaks. The score can increase or decrease over
71 // time. Once the score reaches |score_threshold_|, the entry is reported as
72 // a suspected leak in |suspected_leaks_|.
73 std::map<ValueType,
74 uint32_t,
75 std::less<ValueType>,
76 Allocator<std::pair<ValueType, uint32_t>>> suspected_histogram_;
77
78 // Array of allocated values that passed the suspicion threshold and are being
79 // reported.
80 std::vector<ValueType, Allocator<ValueType>> suspected_leaks_;
81
82 // The most recent allocation entries, since the last call to AddSample().
83 RankedList ranked_entries_;
84 // The previous allocation entries, from before the last call to AddSample().
85 RankedList prev_ranked_entries_;
86
87 DISALLOW_COPY_AND_ASSIGN(LeakAnalyzer);
88 };
89
90 } // namespace leak_detector
91
92 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_ANALYZER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698