Chromium Code Reviews| 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 | |
| 9 namespace leak_detector { | |
| 10 | |
| 11 void LeakAnalyzer::AddSample(const RankedList& ranked_list) { | |
| 12 // Save the ranked entries from the previous call. | |
| 13 prev_ranked_entries_ = ranked_entries_; | |
|
jar (doing other things)
2015/08/21 02:48:42
This seems to instigate a vector copy... which pre
Simon Que
2015/08/21 05:44:05
I can optimize this.
However, keep in mind that t
Simon Que
2015/08/21 21:59:19
I'm going to hold off on implementing this until I
| |
| 14 | |
| 15 // Save the current entries. | |
| 16 ranked_entries_ = ranked_list; | |
| 17 | |
| 18 RankedList ranked_deltas(ranking_size_); | |
| 19 for (size_t i = 0; i < ranked_list.size(); ++i) { | |
| 20 const RankedEntry& entry = ranked_list.entry(i); | |
| 21 | |
| 22 // Determine what count was recorded for this value last time. | |
| 23 uint32_t prev_count = 0; | |
| 24 if (GetPreviousCountForValue(entry.value, &prev_count)) | |
| 25 ranked_deltas.Add(entry.value, entry.count - prev_count); | |
|
jar (doing other things)
2015/08/21 02:48:42
Since each Add() is an O(N) operation (to do the e
Simon Que
2015/08/21 05:44:04
N=16.
| |
| 26 } | |
| 27 | |
| 28 AnalyzeDeltas(ranked_deltas); | |
| 29 } | |
| 30 | |
| 31 int LeakAnalyzer::Dump(char* buffer, const int buffer_size) const { | |
| 32 int size_remaining = buffer_size; | |
| 33 int attempted_size = 0; | |
| 34 | |
| 35 // Buffer used for calling LeakDetectorValueType::ToString(). | |
| 36 char to_string_buffer[256]; | |
| 37 to_string_buffer[0] = '\0'; | |
|
jar (doing other things)
2015/08/21 02:48:42
nit: What is the significance of this line? Shoul
Simon Que
2015/08/21 21:59:19
Deleted.
| |
| 38 | |
| 39 if (ranked_entries_.size() > 0) { | |
| 40 // Dump the top entries. | |
| 41 if (size_remaining > 1) { | |
| 42 attempted_size = | |
| 43 snprintf(buffer, size_remaining, "***** Top %zu %ss *****\n", | |
| 44 ranked_entries_.size(), | |
| 45 ranked_entries_.entry(0).value.GetTypeName()); | |
| 46 size_remaining -= attempted_size; | |
| 47 buffer += attempted_size; | |
| 48 } | |
| 49 | |
| 50 for (size_t i = 0; i < ranked_entries_.size() && size_remaining > 1; ++i) { | |
| 51 const RankedEntry& entry = ranked_entries_.entry(i); | |
| 52 if (entry.count == 0) | |
| 53 continue; | |
|
jar (doing other things)
2015/08/21 02:48:42
*IF* you assume RankedEntries are indeed in revers
Simon Que
2015/08/21 21:59:19
You're right, it makes more sense to break.
| |
| 54 | |
| 55 // Determine what count was recorded for this value last time. | |
| 56 char prev_entry_buffer[256]; | |
| 57 prev_entry_buffer[0] = '\0'; | |
| 58 | |
| 59 uint32_t prev_count = 0; | |
| 60 if (GetPreviousCountForValue(entry.value, &prev_count)) { | |
| 61 snprintf(prev_entry_buffer, sizeof(prev_entry_buffer), | |
| 62 "(%10d)", entry.count - prev_count); | |
| 63 } | |
| 64 | |
| 65 attempted_size = | |
| 66 snprintf( | |
| 67 buffer, size_remaining, "%10s: %10u %s\n", | |
| 68 entry.value.ToString(to_string_buffer, sizeof(to_string_buffer)), | |
| 69 entry.count, prev_entry_buffer); | |
| 70 size_remaining -= attempted_size; | |
| 71 buffer += attempted_size; | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 if (!suspected_leaks_.empty()) { | |
| 76 // Report the suspected sizes. | |
| 77 if (size_remaining > 1) { | |
| 78 const ValueType& first_leak_value = suspected_leaks_[0]; | |
| 79 attempted_size = snprintf(buffer, size_remaining, "Suspected %ss: ", | |
| 80 first_leak_value.GetTypeName()); | |
| 81 size_remaining -= attempted_size; | |
| 82 buffer += attempted_size; | |
| 83 } | |
| 84 if (size_remaining > 1) { | |
| 85 bool wrote_suspected_leak = false; | |
| 86 for (const ValueType& leak_value : suspected_leaks_) { | |
| 87 attempted_size = | |
| 88 snprintf(buffer, size_remaining, "%s, ", | |
| 89 leak_value.ToString( | |
| 90 to_string_buffer, sizeof(to_string_buffer))); | |
| 91 size_remaining -= attempted_size; | |
| 92 buffer += attempted_size; | |
| 93 wrote_suspected_leak = true; | |
| 94 } | |
| 95 // Erase the last comma and space if they were written, even if partially. | |
| 96 if (wrote_suspected_leak) { | |
| 97 int length_of_comma_and_space = std::min(2, attempted_size); | |
| 98 buffer -= length_of_comma_and_space; | |
| 99 size_remaining += length_of_comma_and_space; | |
|
jar (doing other things)
2015/08/21 02:48:42
Note that this "backtracking" has left you buffer
Simon Que
2015/08/21 21:59:20
Done.
| |
| 100 } | |
| 101 } | |
| 102 if (size_remaining > 1) { | |
| 103 attempted_size = snprintf(buffer, size_remaining, "\n"); | |
| 104 size_remaining -= attempted_size; | |
| 105 buffer += attempted_size; | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 // Return the number of bytes written, excluding the null terminator. | |
| 110 return buffer_size - size_remaining; | |
| 111 } | |
| 112 | |
| 113 void LeakAnalyzer::AnalyzeDeltas(const RankedList& ranked_deltas) { | |
| 114 // First, let the suspicion scores decay to deprecate older suspicions. | |
| 115 auto iter = suspected_histogram_.begin(); | |
| 116 while (iter != suspected_histogram_.end()) { | |
| 117 // Suspicion score is the map value. | |
| 118 iter->second /= 2; | |
| 119 | |
| 120 // Erase entries whose suspicion score reaches 0. | |
| 121 auto erase_iter = iter++; | |
| 122 if (iter->second == 0) { | |
| 123 suspected_histogram_.erase(erase_iter); | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 bool found_drop = false; | |
| 128 int drop_index = -1; | |
| 129 for (size_t i = 1; i < ranked_deltas.size(); ++i) { | |
| 130 const RankedEntry& entry = ranked_deltas.entry(i - 1); | |
| 131 const RankedEntry& next_entry = ranked_deltas.entry(i); | |
| 132 | |
| 133 // If the first entry is 0, that means all deltas are 0 or negative. Do | |
| 134 // not treat this as a suspicion of leaks; just quit. | |
| 135 if (i == 1 && entry.count == 0) | |
| 136 break; | |
| 137 | |
| 138 // Find the first major drop in values. | |
| 139 if (entry.count > next_entry.count * 2) { | |
| 140 found_drop = true; | |
| 141 drop_index = i; | |
| 142 break; | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 // Take the pre-drop sizes and increase their suspicion score. | |
| 147 if (found_drop) { | |
| 148 for (int i = 0; i < drop_index; ++i) { | |
| 149 const ValueType& value = ranked_deltas.entry(i).value; | |
| 150 | |
| 151 auto iter = suspected_histogram_.find(value); | |
| 152 if (iter != suspected_histogram_.end()) { | |
| 153 iter->second += score_increase_; | |
| 154 } else if (suspected_histogram_.size() < ranking_size_) { | |
| 155 // Create a new entry. | |
| 156 suspected_histogram_[value] = score_increase_; | |
| 157 } | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 // Now check the leak suspicion scores. Make sure to erase the suspected | |
| 162 // leaks from the previous call. | |
| 163 suspected_leaks_.clear(); | |
| 164 for (const auto& entry : suspected_histogram_) { | |
| 165 if (suspected_leaks_.size() > ranking_size_) | |
| 166 break; | |
| 167 | |
| 168 // Only report suspected values that have accumulated a suspicion score. | |
| 169 // This is achieved by maintaining suspicion for several cycles, with few | |
| 170 // skips. | |
| 171 if (entry.second >= score_threshold_) | |
| 172 suspected_leaks_.emplace_back(entry.first); | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 bool LeakAnalyzer::GetPreviousCountForValue(const ValueType& value, | |
| 177 uint32_t* count) const { | |
| 178 // Determine what count was recorded for this value last time. | |
| 179 for (size_t i = 0; i < prev_ranked_entries_.size(); ++i) { | |
| 180 if (prev_ranked_entries_.entry(i).value == value) { | |
| 181 *count = prev_ranked_entries_.entry(i).count; | |
| 182 return true; | |
| 183 } | |
| 184 } | |
| 185 return false; | |
| 186 } | |
| 187 | |
| 188 } // namespace leak_detector | |
| OLD | NEW |