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