Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: components/metrics/leak_detector/leak_analyzer.cc

Issue 986503002: components/metrics: Add runtime memory leak detector (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add LeakDetector class; Remove tcmalloc dependencies; Add export classes to gperftools; Remove farm… Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 namespace leak_detector {
8
9 void LeakAnalyzer::AddSample(const RankedList& ranked_list) {
10 // Save the ranked entries from the previous call.
11 prev_ranked_entries_ = ranked_entries_;
12
13 // Save the current entries.
14 ranked_entries_ = ranked_list;
15
16 RankedList ranked_deltas(ranking_size_);
17 for (size_t i = 0; i < ranked_list.size(); ++i) {
18 const RankedEntry& entry = ranked_list.entry(i);
19
20 // Determine what count was recorded for this value last time.
21 const RankedEntry* prev_entry = GetPreviousEntryWithValue(entry.value);
22 if (prev_entry)
23 ranked_deltas.Add(entry.value, entry.count - prev_entry->count);
24 }
25
26 AnalyzeDeltas(ranked_deltas);
27 }
28
29 int LeakAnalyzer::Dump(char* buffer, const int buffer_size) const {
30 int size_remaining = buffer_size;
31 int attempted_size = 0;
32
33 // Dump the top entries.
34 if (size_remaining > 0) {
35 attempted_size =
36 snprintf(buffer, size_remaining, "***** Top %lu %s *****\n",
37 ranked_entries_.size(), string_print_->ValueTypeName(true));
38 size_remaining -= attempted_size;
39 buffer += attempted_size;
40 }
41
42 for (size_t i = 0; i < ranked_entries_.size() && size_remaining > 0; ++i) {
jar (doing other things) 2015/08/14 02:34:38 I think you want size_remaining > 1 here, and in l
Simon Que 2015/08/14 04:21:26 Done.
43 const RankedEntry& entry = ranked_entries_.entry(i);
44 if (entry.count == 0)
45 continue;
46
47 // Determine what count was recorded for this value last time.
48 const RankedEntry* prev_entry = GetPreviousEntryWithValue(entry.value);
49
50 char prev_entry_buffer[256];
51 prev_entry_buffer[0] = '\0';
52 if (prev_entry)
53 sprintf(prev_entry_buffer, "(%10d)", entry.count - prev_entry->count);
jar (doing other things) 2015/08/14 02:34:38 snprintf is probably better, to avoid buffer overr
Simon Que 2015/08/14 04:21:26 Done.
54
55 int attempted_size =
56 snprintf(buffer, size_remaining, "%s: %10u %s\n",
57 string_print_->ValueToString(entry.value, true), entry.count,
58 prev_entry ? prev_entry_buffer : "");
59 size_remaining -= attempted_size;
60 buffer += attempted_size;
61 }
62
63 // Now report the suspected sizes.
64 if (size_remaining > 0) {
65 attempted_size = snprintf(buffer, size_remaining, "Suspected %s: ",
66 string_print_->ValueTypeName(true));
67 size_remaining -= attempted_size;
68 buffer += attempted_size;
69 }
70 if (size_remaining > 0) {
71 for (const ValueType& leak_value : suspected_leaks_) {
72 attempted_size =
73 snprintf(buffer, size_remaining, "%s, ",
74 string_print_->ValueToString(leak_value, false));
75 size_remaining -= attempted_size;
76 buffer += attempted_size;
77 }
78 // Erase the last comma and space.
jar (doing other things) 2015/08/14 02:34:38 How do you know you wrote a comma, and didn't run
Simon Que 2015/08/14 04:21:26 Done.
79 buffer -= 2;
80 size_remaining += 2;
81 }
82 if (size_remaining > 0) {
83 attempted_size = snprintf(buffer, size_remaining, "\n");
84 size_remaining -= attempted_size;
85 buffer += attempted_size;
86 }
87
88 // Return the number of bytes written.
89 if (size_remaining >= 0)
jar (doing other things) 2015/08/14 02:34:38 This will always be at least 1. What do you want
Simon Que 2015/08/14 04:21:26 Done.
90 return buffer_size - size_remaining;
91 return buffer_size;
92 }
93
94 void LeakAnalyzer::AnalyzeDeltas(const RankedList& ranked_deltas) {
95 // First, let the suspicion scores decay to deprecate older suspicions.
96 auto iter = suspected_histogram_.begin();
97 while (iter != suspected_histogram_.end()) {
98 // Suspicion score is the map value.
99 iter->second /= 2;
100
101 // Erase entries whose suspicion score reaches 0.
102 if (iter->second == 0) {
103 auto erase_iter = iter++;
104 suspected_histogram_.erase(erase_iter);
105 } else {
106 ++iter;
107 }
jar (doing other things) 2015/08/14 02:34:38 nit: Personal preference: I like shorter code with
Simon Que 2015/08/14 04:21:26 This is not external code. Done.
108 }
109
110 bool found_drop = false;
111 int drop_index = -1;
112 for (int i = 0; i < static_cast<int>(ranked_deltas.size()) - 1; ++i) {
113 const RankedEntry& entry = ranked_deltas.entry(i);
114 const RankedEntry& next_entry = ranked_deltas.entry(i + 1);
115
116 // If the first entry is 0, that means all deltas are 0 or negative. Do
117 // not treat this as a suspicion of leaks; just quit.
118 if (i == 0 && entry.count == 0)
119 break;
120
121 // Find the first major drop in values.
122 if (entry.count > next_entry.count * 2) {
123 found_drop = true;
124 drop_index = i + 1;
125 break;
126 }
127 }
128
129 // Take the pre-drop sizes and increase their suspicion score.
130 for (int i = 0; found_drop && i < drop_index; ++i) {
131 const ValueType& value = ranked_deltas.entry(i).value;
132
133 auto iter = suspected_histogram_.find(value);
134 if (iter != suspected_histogram_.end()) {
135 iter->second += score_increase_;
136 } else if (suspected_histogram_.size() < ranking_size_) {
137 // Create a new entry.
138 suspected_histogram_[value] = score_increase_;
139 }
140 }
141
142 // Now check the leak suspicion scores. Make sure to erase the suspected
143 // leaks from the previous call.
144 suspected_leaks_.clear();
145 for (const auto& entry : suspected_histogram_) {
146 if (suspected_leaks_.size() > ranking_size_)
147 break;
148
149 // Only report suspected values that have accumulated a suspicion score.
150 // This is achieved by maintaining suspicion for several cycles, with few
151 // skips.
152 if (entry.second >= score_threshold_)
153 suspected_leaks_.emplace_back(entry.first);
154 }
155 }
156
157 const LeakAnalyzer::RankedEntry* LeakAnalyzer::GetPreviousEntryWithValue(
158 const ValueType& value) const {
159 // Determine what count was recorded for this value last time.
160 for (size_t i = 0; i < prev_ranked_entries_.size(); ++i) {
161 if (prev_ranked_entries_.entry(i).value == value)
162 return &prev_ranked_entries_.entry(i);
jar (doing other things) 2015/08/14 02:34:38 This concerned me a bit... why do you want to retu
Simon Que 2015/08/14 04:21:26 Done.
163 }
164 return NULL;
165 }
166
167 } // namespace leak_detector
OLDNEW
« no previous file with comments | « components/metrics/leak_detector/leak_analyzer.h ('k') | components/metrics/leak_detector/leak_detector.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698