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 <set> | |
8 | |
9 namespace metrics { | |
10 namespace leak_detector { | |
11 | |
12 namespace { | |
13 | |
14 using RankedEntry = RankedList::Entry; | |
15 | |
16 // Increase suspicion scores by this much each time an entry is suspected as | |
17 // being a leak. | |
18 const int kSuspicionScoreIncrease = 1; | |
19 | |
20 } // namespace | |
21 | |
22 LeakAnalyzer::LeakAnalyzer(uint32_t ranking_size, | |
23 uint32_t num_suspicions_threshold) | |
24 : ranking_size_(ranking_size), | |
25 score_threshold_(num_suspicions_threshold), | |
26 ranked_entries_(ranking_size), | |
27 prev_ranked_entries_(ranking_size) { | |
28 suspected_leaks_.reserve(ranking_size); | |
29 } | |
30 | |
31 LeakAnalyzer::~LeakAnalyzer() {} | |
32 | |
33 void LeakAnalyzer::AddSample(RankedList ranked_list) { | |
34 // Save the ranked entries from the previous call. | |
35 prev_ranked_entries_ = ranked_entries_.Pass(); | |
36 | |
37 // Save the current entries. | |
38 ranked_entries_ = ranked_list.Pass(); | |
39 | |
40 RankedList ranked_deltas(ranking_size_); | |
41 for (const RankedEntry& entry : ranked_entries_) { | |
42 // Determine what count was recorded for this value last time. | |
43 uint32_t prev_count = 0; | |
44 if (GetPreviousCountForValue(entry.value, &prev_count)) | |
45 ranked_deltas.Add(entry.value, entry.count - prev_count); | |
46 } | |
47 | |
48 AnalyzeDeltas(ranked_deltas); | |
49 } | |
50 | |
51 void LeakAnalyzer::AnalyzeDeltas(const RankedList& ranked_deltas) { | |
52 bool found_drop = false; | |
53 RankedList::const_iterator drop_position = ranked_deltas.end(); | |
54 | |
55 if (ranked_deltas.size() > 1) { | |
56 RankedList::const_iterator entry_iter = ranked_deltas.begin(); | |
57 RankedList::const_iterator next_entry_iter = ranked_deltas.begin(); | |
58 ++next_entry_iter; | |
59 | |
60 // If the first entry is 0, that means all deltas are 0 or negative. Do | |
61 // not treat this as a suspicion of leaks; just quit. | |
62 if (entry_iter->count > 0) { | |
63 while (next_entry_iter != ranked_deltas.end()) { | |
64 const RankedEntry& entry = *entry_iter; | |
65 const RankedEntry& next_entry = *next_entry_iter; | |
66 | |
67 // Find the first major drop in values (i.e. by 50% or more). | |
68 if (entry.count > next_entry.count * 2) { | |
69 found_drop = true; | |
70 drop_position = next_entry_iter; | |
71 break; | |
72 } | |
73 ++entry_iter; | |
74 ++next_entry_iter; | |
75 } | |
76 } | |
77 } | |
78 | |
79 // All leak values before the drop are suspected during this analysis. | |
80 std::set<ValueType, std::less<ValueType>, Allocator<ValueType>> | |
81 current_suspects; | |
82 if (found_drop) { | |
83 for (RankedList::const_iterator ranked_list_iter = ranked_deltas.begin(); | |
84 ranked_list_iter != drop_position; ++ranked_list_iter) { | |
85 current_suspects.insert(ranked_list_iter->value); | |
86 } | |
87 } | |
88 | |
89 // Reset the score to 0 for all previously suspected leak values that did | |
90 // not get suspected this time. | |
91 auto iter = suspected_histogram_.begin(); | |
92 while (iter != suspected_histogram_.end()) { | |
93 const ValueType& value = iter->first; | |
94 // Erase entries whose suspicion score reaches 0. | |
95 auto erase_iter = iter++; | |
96 if (current_suspects.find(value) == current_suspects.end()) | |
97 suspected_histogram_.erase(erase_iter); | |
98 } | |
99 | |
100 // For currently suspected values, increase the leak score. | |
101 for (const ValueType& value : current_suspects) { | |
102 auto histogram_iter = suspected_histogram_.find(value); | |
103 if (histogram_iter != suspected_histogram_.end()) { | |
104 histogram_iter->second += kSuspicionScoreIncrease; | |
105 } else if (suspected_histogram_.size() < ranking_size_) { | |
106 // Create a new entry if it didn't already exist. | |
107 suspected_histogram_[value] = kSuspicionScoreIncrease; | |
108 } | |
109 } | |
110 | |
111 // Now check the leak suspicion scores. Make sure to erase the suspected | |
112 // leaks from the previous call. | |
113 suspected_leaks_.clear(); | |
114 for (const auto& entry : suspected_histogram_) { | |
115 if (suspected_leaks_.size() > ranking_size_) | |
116 break; | |
117 | |
118 // Only report suspected values that have accumulated a suspicion score. | |
119 // This is achieved by maintaining suspicion for several cycles, with few | |
120 // skips. | |
121 if (entry.second >= score_threshold_) | |
122 suspected_leaks_.emplace_back(entry.first); | |
123 } | |
124 } | |
125 | |
126 bool LeakAnalyzer::GetPreviousCountForValue(const ValueType& value, | |
127 uint32_t* count) const { | |
128 // Determine what count was recorded for this value last time. | |
129 for (const RankedEntry& entry : prev_ranked_entries_) { | |
130 if (entry.value == value) { | |
131 *count = entry.count; | |
132 return true; | |
133 } | |
134 } | |
135 return false; | |
136 } | |
137 | |
138 } // namespace leak_detector | |
139 } // namespace metrics | |
OLD | NEW |