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 #include "components/metrics/leak_detector/leak_analyzer.h" | |
6 | |
7 #include <algorithm> | |
8 #include <set> | |
9 #include <utility> | |
10 | |
11 namespace leak_detector { | |
12 | |
13 namespace { | |
14 | |
15 using RankedEntry = RankedList::Entry; | |
16 | |
17 // Increase suspicion scores by this much each time an entry is suspected as | |
18 // being a leak. | |
19 const int kSuspicionScoreIncrease = 1; | |
20 | |
21 } // namespace | |
22 | |
23 void LeakAnalyzer::AddSample(RankedList&& ranked_list) { | |
24 // Save the ranked entries from the previous call. | |
25 prev_ranked_entries_ = std::move(ranked_entries_); | |
Simon Que
2015/08/23 23:30:53
Now with std::move semantics.
| |
26 | |
27 // Save the current entries. | |
28 ranked_entries_ = std::move(ranked_list); | |
29 | |
30 RankedList ranked_deltas(ranking_size_); | |
31 for (const RankedEntry& entry : ranked_entries_) { | |
32 // Determine what count was recorded for this value last time. | |
33 uint32_t prev_count = 0; | |
34 if (GetPreviousCountForValue(entry.value, &prev_count)) | |
35 ranked_deltas.Add(entry.value, entry.count - prev_count); | |
36 } | |
37 | |
38 AnalyzeDeltas(ranked_deltas); | |
39 } | |
40 | |
41 size_t LeakAnalyzer::Dump(const size_t buffer_size, char* buffer) const { | |
42 size_t size_remaining = buffer_size; | |
43 int attempted_size = 0; | |
44 | |
45 // Add a null terminator in case the rest of the code (which is conditional) | |
46 // doesn't print anything. | |
47 if (size_remaining) | |
48 buffer[0] = '\0'; | |
49 | |
50 // Buffer used for calling LeakDetectorValueType::ToString(). | |
51 char to_string_buffer[256]; | |
52 | |
53 if (ranked_entries_.size() > 0) { | |
54 // Dump the top entries. | |
55 if (size_remaining > 1) { | |
56 attempted_size = | |
57 snprintf(buffer, size_remaining, "***** Top %zu %ss *****\n", | |
58 ranked_entries_.size(), | |
59 ranked_entries_.begin()->value.GetTypeName()); | |
60 size_remaining -= attempted_size; | |
61 buffer += attempted_size; | |
62 } | |
63 | |
64 for (const RankedEntry& entry : ranked_entries_) { | |
65 if (size_remaining <= 1) | |
66 break; | |
67 if (entry.count == 0) | |
68 break; | |
69 | |
70 // Determine what count was recorded for this value last time. | |
71 char prev_entry_buffer[256]; | |
72 prev_entry_buffer[0] = '\0'; | |
73 | |
74 uint32_t prev_count = 0; | |
75 if (GetPreviousCountForValue(entry.value, &prev_count)) { | |
76 snprintf(prev_entry_buffer, sizeof(prev_entry_buffer), | |
77 "(%10d)", entry.count - prev_count); | |
78 } | |
79 | |
80 attempted_size = | |
81 snprintf( | |
82 buffer, size_remaining, "%10s: %10u %s\n", | |
83 entry.value.ToString(sizeof(to_string_buffer), to_string_buffer), | |
84 entry.count, prev_entry_buffer); | |
85 size_remaining -= attempted_size; | |
86 buffer += attempted_size; | |
87 } | |
88 } | |
89 | |
90 if (!suspected_leaks_.empty()) { | |
91 // Report the suspected sizes. | |
92 if (size_remaining > 1) { | |
93 const ValueType& first_leak_value = suspected_leaks_[0]; | |
94 attempted_size = snprintf(buffer, size_remaining, "Suspected %ss: ", | |
95 first_leak_value.GetTypeName()); | |
96 size_remaining -= attempted_size; | |
97 buffer += attempted_size; | |
98 } | |
99 if (size_remaining > 1) { | |
100 // Change this to a comma + space after the first item is printed, so that | |
101 // subsequent items will be separated by a comma. | |
102 const char* optional_comma = ""; | |
103 for (const ValueType& leak_value : suspected_leaks_) { | |
104 attempted_size = | |
105 snprintf(buffer, size_remaining, "%s%s", | |
106 optional_comma, | |
107 leak_value.ToString( | |
108 sizeof(to_string_buffer), to_string_buffer)); | |
109 size_remaining -= attempted_size; | |
110 buffer += attempted_size; | |
111 optional_comma = ", "; | |
112 } | |
113 } | |
114 if (size_remaining > 1) { | |
115 attempted_size = snprintf(buffer, size_remaining, "\n"); | |
116 size_remaining -= attempted_size; | |
117 buffer += attempted_size; | |
118 } | |
119 } | |
120 | |
121 // Return the number of bytes written, excluding the null terminator. | |
122 return buffer_size - size_remaining; | |
123 } | |
124 | |
125 void LeakAnalyzer::AnalyzeDeltas(const RankedList& ranked_deltas) { | |
126 bool found_drop = false; | |
127 RankedList::const_iterator drop_position = ranked_deltas.end(); | |
128 | |
129 if (ranked_deltas.size() > 1) { | |
130 RankedList::const_iterator entry_iter = ranked_deltas.begin(); | |
131 RankedList::const_iterator next_entry_iter = ranked_deltas.begin(); | |
132 ++next_entry_iter; | |
133 | |
134 // If the first entry is 0, that means all deltas are 0 or negative. Do | |
135 // not treat this as a suspicion of leaks; just quit. | |
136 if (entry_iter->count > 0) { | |
137 while (next_entry_iter != ranked_deltas.end()) { | |
138 const RankedEntry& entry = *entry_iter; | |
139 const RankedEntry& next_entry = *next_entry_iter; | |
140 | |
141 // Find the first major drop in values (i.e. by 50% or more). | |
142 if (entry.count > next_entry.count * 2) { | |
143 found_drop = true; | |
144 drop_position = next_entry_iter; | |
145 break; | |
146 } | |
147 ++entry_iter; | |
148 ++next_entry_iter; | |
149 } | |
150 } | |
151 } | |
152 | |
153 // All leak values before the drop are suspected during this analysis. | |
154 std::set<ValueType, | |
155 std::less<ValueType>, | |
156 Allocator<ValueType>> current_suspects; | |
157 if (found_drop) { | |
158 for (RankedList::const_iterator ranked_list_iter = ranked_deltas.begin(); | |
159 ranked_list_iter != drop_position; | |
160 ++ranked_list_iter) { | |
161 current_suspects.insert(ranked_list_iter->value); | |
162 } | |
163 } | |
164 | |
165 // Reset the score to 0 for all previously suspected leak values that did | |
166 // not get suspected this time. | |
167 auto iter = suspected_histogram_.begin(); | |
168 while (iter != suspected_histogram_.end()) { | |
169 const ValueType& value = iter->first; | |
170 // Erase entries whose suspicion score reaches 0. | |
171 auto erase_iter = iter++; | |
172 if (current_suspects.find(value) == current_suspects.end()) | |
173 suspected_histogram_.erase(erase_iter); | |
174 } | |
175 | |
176 // For currently suspected values, increase the leak score. | |
177 for (const ValueType& value : current_suspects) { | |
178 auto histogram_iter = suspected_histogram_.find(value); | |
179 if (histogram_iter != suspected_histogram_.end()) { | |
180 histogram_iter->second += kSuspicionScoreIncrease; | |
181 } else if (suspected_histogram_.size() < ranking_size_) { | |
182 // Create a new entry if it didn't already exist. | |
183 suspected_histogram_[value] = kSuspicionScoreIncrease; | |
184 } | |
185 } | |
186 | |
187 // Now check the leak suspicion scores. Make sure to erase the suspected | |
188 // leaks from the previous call. | |
189 suspected_leaks_.clear(); | |
190 for (const auto& entry : suspected_histogram_) { | |
191 if (suspected_leaks_.size() > ranking_size_) | |
192 break; | |
193 | |
194 // Only report suspected values that have accumulated a suspicion score. | |
195 // This is achieved by maintaining suspicion for several cycles, with few | |
196 // skips. | |
197 if (entry.second >= score_threshold_) | |
198 suspected_leaks_.emplace_back(entry.first); | |
199 } | |
200 } | |
201 | |
202 bool LeakAnalyzer::GetPreviousCountForValue(const ValueType& value, | |
203 uint32_t* count) const { | |
204 // Determine what count was recorded for this value last time. | |
205 for (const RankedEntry& entry : prev_ranked_entries_) { | |
206 if (entry.value == value) { | |
207 *count = entry.count; | |
208 return true; | |
209 } | |
210 } | |
211 return false; | |
212 } | |
213 | |
214 } // namespace leak_detector | |
OLD | NEW |