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