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, |
| 70 std::less<ValueType>, |
| 71 Allocator<ValueType>> current_suspects; |
| 72 if (found_drop) { |
| 73 for (RankedList::const_iterator ranked_list_iter = ranked_deltas.begin(); |
| 74 ranked_list_iter != drop_position; |
| 75 ++ranked_list_iter) { |
| 76 current_suspects.insert(ranked_list_iter->value); |
| 77 } |
| 78 } |
| 79 |
| 80 // Reset the score to 0 for all previously suspected leak values that did |
| 81 // not get suspected this time. |
| 82 auto iter = suspected_histogram_.begin(); |
| 83 while (iter != suspected_histogram_.end()) { |
| 84 const ValueType& value = iter->first; |
| 85 // Erase entries whose suspicion score reaches 0. |
| 86 auto erase_iter = iter++; |
| 87 if (current_suspects.find(value) == current_suspects.end()) |
| 88 suspected_histogram_.erase(erase_iter); |
| 89 } |
| 90 |
| 91 // For currently suspected values, increase the leak score. |
| 92 for (const ValueType& value : current_suspects) { |
| 93 auto histogram_iter = suspected_histogram_.find(value); |
| 94 if (histogram_iter != suspected_histogram_.end()) { |
| 95 histogram_iter->second += kSuspicionScoreIncrease; |
| 96 } else if (suspected_histogram_.size() < ranking_size_) { |
| 97 // Create a new entry if it didn't already exist. |
| 98 suspected_histogram_[value] = kSuspicionScoreIncrease; |
| 99 } |
| 100 } |
| 101 |
| 102 // Now check the leak suspicion scores. Make sure to erase the suspected |
| 103 // leaks from the previous call. |
| 104 suspected_leaks_.clear(); |
| 105 for (const auto& entry : suspected_histogram_) { |
| 106 if (suspected_leaks_.size() > ranking_size_) |
| 107 break; |
| 108 |
| 109 // Only report suspected values that have accumulated a suspicion score. |
| 110 // This is achieved by maintaining suspicion for several cycles, with few |
| 111 // skips. |
| 112 if (entry.second >= score_threshold_) |
| 113 suspected_leaks_.emplace_back(entry.first); |
| 114 } |
| 115 } |
| 116 |
| 117 bool LeakAnalyzer::GetPreviousCountForValue(const ValueType& value, |
| 118 uint32_t* count) const { |
| 119 // Determine what count was recorded for this value last time. |
| 120 for (const RankedEntry& entry : prev_ranked_entries_) { |
| 121 if (entry.value == value) { |
| 122 *count = entry.count; |
| 123 return true; |
| 124 } |
| 125 } |
| 126 return false; |
| 127 } |
| 128 |
| 129 } // namespace leak_detector |
| 130 } // namespace metrics |
OLD | NEW |