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 "leak_detector_impl.h" |
| 6 |
| 7 #include <inttypes.h> |
| 8 #include <stddef.h> |
| 9 #include <unistd.h> // for getpid() |
| 10 |
| 11 #include <algorithm> |
| 12 #include <new> |
| 13 #include <utility> |
| 14 |
| 15 #include "base/hash.h" |
| 16 #include "components/metrics/leak_detector/call_stack_table.h" |
| 17 #include "components/metrics/leak_detector/ranked_list.h" |
| 18 |
| 19 namespace leak_detector { |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Look for leaks in the the top N entries in each tier, where N is this value. |
| 24 const int kRankedListSize = 16; |
| 25 |
| 26 // Initial hash table size for |LeakDetectorImpl::address_map_|. |
| 27 const int kAddressMapNumBuckets = 100003; |
| 28 |
| 29 // Number of entries in the alloc size table. As sizes are aligned to 32-bits |
| 30 // the max supported allocation size is (kNumSizeEntries * 4 - 1). Any larger |
| 31 // sizes are ignored. This value is chosen high enough that such large sizes |
| 32 // are rare if not nonexistent. |
| 33 const int kNumSizeEntries = 2048; |
| 34 |
| 35 using ValueType = LeakDetectorValueType; |
| 36 |
| 37 // Print the contents of |str| prefixed with the current pid. |
| 38 void PrintWithPid(const char* str) { |
| 39 char line[1024]; |
| 40 snprintf(line, sizeof(line), "%d: %s\n", getpid(), str); |
| 41 RAW_LOG(ERROR, line); |
| 42 } |
| 43 |
| 44 // Prints the input string buffer using RAW_LOG, pre-fixing each line with the |
| 45 // process id. Will modify |str| temporarily but restore it at the end. |
| 46 void PrintWithPidOnEachLine(char* str) { |
| 47 char* current_line = str; |
| 48 // Attempt to find a newline that will indicate the end of the first line |
| 49 // and the start of the second line. |
| 50 while (char *newline_ptr = strchr(current_line, '\n')) { |
| 51 // Terminate the current line so it can be printed as a separate string. |
| 52 // Restore the original string when done. |
| 53 *newline_ptr = '\0'; |
| 54 PrintWithPid(current_line); |
| 55 *newline_ptr = '\n'; |
| 56 |
| 57 // Point |current_line| to the next line. |
| 58 current_line = newline_ptr + 1; |
| 59 } |
| 60 // There may be an extra line at the end of the input string that is not |
| 61 // newline-terminated. e.g. if the input was only one line, or the last line |
| 62 // did not end with a newline. |
| 63 if (current_line[0] != '\0') |
| 64 PrintWithPid(current_line); |
| 65 } |
| 66 |
| 67 // Functions to convert an allocation size to/from the array index used for |
| 68 // |LeakDetectorImpl::size_entries_|. |
| 69 int SizeToIndex(const size_t size) { |
| 70 int result = static_cast<int>(size / sizeof(uint32_t)); |
| 71 if (result < kNumSizeEntries) |
| 72 return result; |
| 73 return 0; |
| 74 } |
| 75 |
| 76 size_t IndexToSize(int index){ |
| 77 return sizeof(uint32_t) * index; |
| 78 } |
| 79 |
| 80 } // namespace |
| 81 |
| 82 bool InternalLeakReport::operator< (const InternalLeakReport& other) const { |
| 83 if (alloc_size_bytes != other.alloc_size_bytes) |
| 84 return alloc_size_bytes < other.alloc_size_bytes; |
| 85 for (size_t i = 0; |
| 86 i < call_stack.size() && i < other.call_stack.size(); |
| 87 ++i) { |
| 88 if (call_stack[i] != other.call_stack[i]) |
| 89 return call_stack[i] < other.call_stack[i]; |
| 90 } |
| 91 return call_stack.size() < other.call_stack.size(); |
| 92 } |
| 93 |
| 94 LeakDetectorImpl::LeakDetectorImpl(uintptr_t mapping_addr, |
| 95 size_t mapping_size, |
| 96 int size_suspicion_threshold, |
| 97 int call_stack_suspicion_threshold, |
| 98 bool verbose) |
| 99 : num_stack_tables_(0), |
| 100 address_map_(kAddressMapNumBuckets), |
| 101 size_leak_analyzer_(kRankedListSize, size_suspicion_threshold), |
| 102 size_entries_(kNumSizeEntries, {0}), |
| 103 mapping_addr_(mapping_addr), |
| 104 mapping_size_(mapping_size), |
| 105 call_stack_suspicion_threshold_(call_stack_suspicion_threshold), |
| 106 verbose_(verbose) { |
| 107 } |
| 108 |
| 109 LeakDetectorImpl::~LeakDetectorImpl() { |
| 110 // Free any call stack tables. |
| 111 for (AllocSizeEntry& entry : size_entries_) { |
| 112 CallStackTable* table = entry.stack_table; |
| 113 if (!table) |
| 114 continue; |
| 115 table->~CallStackTable(); |
| 116 CustomAllocator::Free(table, sizeof(CallStackTable)); |
| 117 } |
| 118 size_entries_.clear(); |
| 119 } |
| 120 |
| 121 bool LeakDetectorImpl::ShouldGetStackTraceForSize(size_t size) const { |
| 122 return size_entries_[SizeToIndex(size)].stack_table != nullptr; |
| 123 } |
| 124 |
| 125 void LeakDetectorImpl::RecordAlloc( |
| 126 const void* ptr, size_t size, |
| 127 int stack_depth, const void* const stack[]) { |
| 128 AllocInfo alloc_info; |
| 129 alloc_info.size = size; |
| 130 |
| 131 alloc_size_ += alloc_info.size; |
| 132 ++num_allocs_; |
| 133 |
| 134 AllocSizeEntry* entry = &size_entries_[SizeToIndex(size)]; |
| 135 ++entry->num_allocs; |
| 136 |
| 137 if (entry->stack_table && stack_depth > 0) { |
| 138 alloc_info.call_stack = |
| 139 call_stack_manager_.GetCallStack(stack_depth, stack); |
| 140 entry->stack_table->Add(alloc_info.call_stack); |
| 141 |
| 142 ++num_allocs_with_call_stack_; |
| 143 } |
| 144 |
| 145 uintptr_t addr = reinterpret_cast<uintptr_t>(ptr); |
| 146 address_map_.insert(std::pair<uintptr_t, AllocInfo>(addr, alloc_info)); |
| 147 } |
| 148 |
| 149 void LeakDetectorImpl::RecordFree(const void* ptr) { |
| 150 // Look up address. |
| 151 uintptr_t addr = reinterpret_cast<uintptr_t>(ptr); |
| 152 auto iter = address_map_.find(addr); |
| 153 if (iter == address_map_.end()) |
| 154 return; |
| 155 |
| 156 const AllocInfo& alloc_info = iter->second; |
| 157 |
| 158 AllocSizeEntry* entry = &size_entries_[SizeToIndex(alloc_info.size)]; |
| 159 ++entry->num_frees; |
| 160 |
| 161 const CallStack* call_stack = alloc_info.call_stack; |
| 162 if (call_stack) { |
| 163 if (entry->stack_table) |
| 164 entry->stack_table->Remove(call_stack); |
| 165 } |
| 166 ++num_frees_; |
| 167 free_size_ += alloc_info.size; |
| 168 |
| 169 address_map_.erase(iter); |
| 170 } |
| 171 |
| 172 void LeakDetectorImpl::TestForLeaks( |
| 173 bool do_logging, |
| 174 InternalVector<InternalLeakReport>* reports) { |
| 175 if (do_logging) |
| 176 DumpStats(); |
| 177 |
| 178 // Add net alloc counts for each size to a ranked list. |
| 179 RankedList size_ranked_list(kRankedListSize); |
| 180 for (size_t i = 0; i < size_entries_.size(); ++i) { |
| 181 const AllocSizeEntry& entry = size_entries_[i]; |
| 182 ValueType size_value(IndexToSize(i)); |
| 183 size_ranked_list.Add(size_value, entry.num_allocs - entry.num_frees); |
| 184 } |
| 185 size_leak_analyzer_.AddSample(std::move(size_ranked_list)); |
| 186 |
| 187 // Dump out the top entries. |
| 188 char buf[0x4000]; |
| 189 if (do_logging && verbose_) { |
| 190 if (size_leak_analyzer_.Dump(sizeof(buf), buf) < sizeof(buf)) |
| 191 PrintWithPidOnEachLine(buf); |
| 192 } |
| 193 |
| 194 // Get suspected leaks by size. |
| 195 for (const ValueType& size_value : size_leak_analyzer_.suspected_leaks()) { |
| 196 uint32_t size = size_value.size(); |
| 197 AllocSizeEntry* entry = &size_entries_[SizeToIndex(size)]; |
| 198 if (entry->stack_table) |
| 199 continue; |
| 200 if (do_logging) { |
| 201 snprintf(buf, sizeof(buf), "Adding stack table for size %u\n", size); |
| 202 PrintWithPidOnEachLine(buf); |
| 203 } |
| 204 entry->stack_table = new(CustomAllocator::Allocate(sizeof(CallStackTable))) |
| 205 CallStackTable(call_stack_suspicion_threshold_); |
| 206 ++num_stack_tables_; |
| 207 } |
| 208 |
| 209 // Check for leaks in each CallStackTable. It makes sense to this before |
| 210 // checking the size allocations, because that could potentially create new |
| 211 // CallStackTable. However, the overhead to check a new CallStackTable is |
| 212 // small since this function is run very rarely. So handle the leak checks of |
| 213 // Tier 2 here. |
| 214 reports->clear(); |
| 215 for (size_t i = 0; i < size_entries_.size(); ++i) { |
| 216 const AllocSizeEntry& entry = size_entries_[i]; |
| 217 CallStackTable* stack_table = entry.stack_table; |
| 218 if (!stack_table || stack_table->empty()) |
| 219 continue; |
| 220 |
| 221 size_t size = IndexToSize(i); |
| 222 if (do_logging && verbose_) { |
| 223 // Dump table info. |
| 224 snprintf(buf, sizeof(buf), "Stack table for size %zu:\n", size); |
| 225 PrintWithPidOnEachLine(buf); |
| 226 |
| 227 if (stack_table->Dump(sizeof(buf), buf) < sizeof(buf)) |
| 228 PrintWithPidOnEachLine(buf); |
| 229 } |
| 230 |
| 231 // Get suspected leaks by call stack. |
| 232 stack_table->TestForLeaks(); |
| 233 const LeakAnalyzer& leak_analyzer = stack_table->leak_analyzer(); |
| 234 for (const ValueType& call_stack_value : leak_analyzer.suspected_leaks()) { |
| 235 const CallStack* call_stack = call_stack_value.call_stack(); |
| 236 |
| 237 // Return reports by storing in |*reports|. |
| 238 reports->resize(reports->size() + 1); |
| 239 InternalLeakReport* report = &reports->back(); |
| 240 report->alloc_size_bytes = size; |
| 241 report->call_stack.resize(call_stack->depth); |
| 242 for (size_t j = 0; j < call_stack->depth; ++j) { |
| 243 report->call_stack[j] = GetOffset(call_stack->stack[j]); |
| 244 } |
| 245 |
| 246 if (do_logging) { |
| 247 int offset = snprintf(buf, sizeof(buf), |
| 248 "Suspected call stack for size %zu, %p:\n", |
| 249 size, call_stack); |
| 250 for (size_t j = 0; j < call_stack->depth; ++j) { |
| 251 offset += snprintf(buf + offset, sizeof(buf) - offset, |
| 252 "\t%" PRIxPTR "\n", |
| 253 GetOffset(call_stack->stack[j])); |
| 254 } |
| 255 PrintWithPidOnEachLine(buf); |
| 256 } |
| 257 } |
| 258 } |
| 259 } |
| 260 |
| 261 size_t LeakDetectorImpl::AddressHash::operator() (uintptr_t addr) const { |
| 262 return base::Hash(reinterpret_cast<const char*>(&addr), sizeof(addr)); |
| 263 } |
| 264 |
| 265 uintptr_t LeakDetectorImpl::GetOffset(const void *ptr) const { |
| 266 uintptr_t ptr_value = reinterpret_cast<uintptr_t>(ptr); |
| 267 if (ptr_value >= mapping_addr_ && ptr_value < mapping_addr_ + mapping_size_) |
| 268 return ptr_value - mapping_addr_; |
| 269 return ptr_value; |
| 270 } |
| 271 |
| 272 void LeakDetectorImpl::DumpStats() const { |
| 273 char buf[1024]; |
| 274 snprintf(buf, sizeof(buf), |
| 275 "Alloc size: %" PRIu64"\n" |
| 276 "Free size: %" PRIu64 "\n" |
| 277 "Net alloc size: %" PRIu64 "\n" |
| 278 "Number of stack tables: %u\n" |
| 279 "Percentage of allocs with stack traces: %.2f%%\n" |
| 280 "Number of call stack buckets: %zu\n", |
| 281 alloc_size_, free_size_, alloc_size_ - free_size_, num_stack_tables_, |
| 282 num_allocs_ ? 100.0f * num_allocs_with_call_stack_ / num_allocs_ : 0, |
| 283 call_stack_manager_.size()); |
| 284 PrintWithPidOnEachLine(buf); |
| 285 } |
| 286 |
| 287 } // namespace leak_detector |
OLD | NEW |