OLD | NEW |
---|---|
(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 // Interface for printing strings for various value types. | |
24 class StringPrint { | |
25 public: | |
jar (doing other things)
2015/08/15 03:29:01
nit: declare a virtual destructor.
Simon Que
2015/08/15 23:28:31
Done.
| |
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 }; | |
jar (doing other things)
2015/08/15 03:29:01
Are you buying anything by using this class? Do y
Simon Que
2015/08/15 23:28:31
There are two derived classes. Although I could mo
Simon Que
2015/08/18 22:34:04
Done.
| |
40 | |
41 template <typename Type> | |
42 using Allocator = STL_Allocator<Type, CustomAllocator>; | |
43 | |
44 LeakAnalyzer(uint32_t ranking_size, | |
45 uint32_t 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 count for the given value from the previous analysis in | |
78 // |count|. Returns true if the given value was present in the previous | |
79 // analysis, or false if not. | |
80 bool GetPreviousCountForValue(const ValueType& value, uint32_t* count) const; | |
81 | |
82 // Look for the top |ranking_size_| entries when analyzing leaks. | |
83 const uint32_t ranking_size_; | |
84 | |
85 // Each time an entry is suspected as a possible leak, increase its suspicion | |
86 // score by this much. | |
87 const uint32_t score_increase_; | |
88 | |
89 // Report suspected leaks when the suspicion score reaches this value. | |
90 const uint32_t score_threshold_; | |
91 | |
92 // A mapping of allocation values to suspicion score. All allocations in this | |
93 // container are suspected leaks. The score can increase or decrease over | |
94 // time. Once the score reaches |score_threshold_|, the entry is reported as | |
95 // a suspected leak in |suspected_leaks_|. | |
96 std::map<ValueType, | |
97 uint32_t, | |
98 std::less<ValueType>, | |
99 Allocator<std::pair<ValueType, uint32_t>>> suspected_histogram_; | |
100 | |
101 // Array of allocated values that passed the suspicion threshold and are being | |
102 // reported. | |
103 std::vector<ValueType, Allocator<ValueType>> suspected_leaks_; | |
104 | |
105 // The most recent allocation entries, since the last call to AddSample(). | |
106 RankedList ranked_entries_; | |
107 // The previous allocation entries, from before the last call to AddSample(). | |
108 RankedList prev_ranked_entries_; | |
109 | |
110 // For logging. | |
111 StringPrint* string_print_; | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(LeakAnalyzer); | |
114 }; | |
115 | |
116 } // namespace leak_detector | |
117 | |
118 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_ANALYZER_H_ | |
OLD | NEW |