OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 // --- |
| 6 // Author: Simon Que |
| 7 |
| 8 #include "leak_detector_impl.h" |
| 9 |
| 10 #include <cstddef> |
| 11 #include <cstring> |
| 12 |
| 13 #include <algorithm> |
| 14 |
| 15 #include "base/basictypes.h" |
| 16 #include "base/commandlineflags.h" |
| 17 #include "base/logging.h" |
| 18 #include "call_stack_table.h" |
| 19 #include "farmhash.h" |
| 20 #include "ranked_list.h" |
| 21 |
| 22 // A size must be suspected this many times to be reported as a leak suspect. |
| 23 DECLARE_int32(size_suspicion_threshold); |
| 24 |
| 25 // If set, dumps all leak analysis data, not just suspected leak reports. |
| 26 DECLARE_bool(dump_leak_analysis); |
| 27 |
| 28 namespace leak_detector { |
| 29 |
| 30 namespace { |
| 31 |
| 32 // Look for leaks in the the top N entries in each tier, where N is this value. |
| 33 const int kRankedListSize = 16; |
| 34 |
| 35 // Used for printing size values in LeakAnalyzer. |
| 36 class SizeStringPrint : public LeakAnalyzer<uint32>::StringPrint { |
| 37 public: |
| 38 // Gets the string representation of a value. |
| 39 virtual const char* ValueToString(const uint32& value, bool spacing_on) { |
| 40 snprintf(buffer_, sizeof(buffer_), spacing_on ? "%10u" : "%u", value); |
| 41 return buffer_; |
| 42 } |
| 43 |
| 44 // Gets the word that describes the value type. |
| 45 virtual const char* ValueTypeName(bool is_plural) { |
| 46 return is_plural ? "sizes" : "size"; |
| 47 } |
| 48 } size_string_print; |
| 49 |
| 50 // Prints the input string buffer using RAW_LOG, pre-fixing each line with the |
| 51 // process id. |
| 52 void PrintWithPidOnEachLine(char* buf) { |
| 53 char *ptr = strchr(buf, '\n'); |
| 54 do { |
| 55 // Break up the string. |
| 56 if (ptr) |
| 57 *ptr = '\0'; |
| 58 // Print out the former part. |
| 59 RAW_LOG(0, "%d: %s", getpid(), buf); |
| 60 // Re-point |buf| to the latter part. |
| 61 if (ptr) |
| 62 buf = ptr + 1; |
| 63 } while (ptr = strchr(buf, '\n')); |
| 64 } |
| 65 |
| 66 // For passing to AddressMap. |
| 67 void* Alloc(size_t size) { |
| 68 return CustomAllocator::Allocate(size); |
| 69 } |
| 70 void DeAlloc(void* ptr) { |
| 71 CustomAllocator::Free(ptr, 0); |
| 72 } |
| 73 |
| 74 } // namespace |
| 75 |
| 76 LeakDetectorImpl::LeakDetectorImpl(uint64 mapping_addr, uint64 mapping_size) |
| 77 : num_stack_tables_(0), |
| 78 address_map_(Alloc, DeAlloc), |
| 79 size_leak_analyzer_(kRankedListSize, FLAGS_size_suspicion_threshold, |
| 80 &size_string_print), |
| 81 mapping_addr_(mapping_addr), |
| 82 mapping_size_(mapping_size) { |
| 83 // Initialize. |
| 84 memset(entries_, 0, sizeof(entries_)); |
| 85 memset(&stats_, 0, sizeof(stats_)); |
| 86 memset(&call_stack_stats_, 0, sizeof(call_stack_stats_)); |
| 87 } |
| 88 |
| 89 LeakDetectorImpl::~LeakDetectorImpl() { |
| 90 for (CallStack* call_stack : call_stacks_) |
| 91 DeAlloc(call_stack); |
| 92 |
| 93 // Free any call stack tables. |
| 94 for (int i = 0; i < kNumSizeEntries; ++i) { |
| 95 CallStackTable* table = entries_[i].stack_table; |
| 96 if (!table) |
| 97 continue; |
| 98 table->~CallStackTable(); |
| 99 DeAlloc(table); |
| 100 } |
| 101 } |
| 102 |
| 103 bool LeakDetectorImpl::ShouldGetStackTraceForSize(size_t size) const { |
| 104 return entries_[SizeToIndex(size)].stack_table != NULL; |
| 105 } |
| 106 |
| 107 void LeakDetectorImpl::RecordAlloc( |
| 108 const void* ptr, size_t size, |
| 109 int stack_depth, const void* const stack[]) { |
| 110 AllocInfo alloc_info; |
| 111 alloc_info.bytes = size; |
| 112 |
| 113 stats_.alloc_size += alloc_info.bytes; |
| 114 stats_.allocs++; |
| 115 |
| 116 AllocSizeEntry* entry = GetEntryForSize(size); |
| 117 ++entry->num_allocs; |
| 118 |
| 119 if (stack_depth > 0) { |
| 120 CallStack* call_stack = GetCallStack(stack_depth, stack); |
| 121 call_stack->allocs++; |
| 122 call_stack->alloc_size += size; |
| 123 alloc_info.call_stack = call_stack; |
| 124 |
| 125 call_stack_stats_.alloc_size += alloc_info.bytes; |
| 126 call_stack_stats_.allocs++; |
| 127 |
| 128 if (entry->stack_table) |
| 129 entry->stack_table->Add(call_stack); |
| 130 } |
| 131 |
| 132 address_map_.Insert(ptr, alloc_info); |
| 133 } |
| 134 |
| 135 void LeakDetectorImpl::RecordFree(const void* ptr) { |
| 136 // Look up entry. |
| 137 AllocInfo alloc_info; |
| 138 if (!address_map_.FindAndRemove(ptr, &alloc_info)) |
| 139 return; |
| 140 |
| 141 AllocSizeEntry* entry = GetEntryForSize(alloc_info.bytes); |
| 142 ++entry->num_frees; |
| 143 |
| 144 CallStack* call_stack = alloc_info.call_stack; |
| 145 if (call_stack) { |
| 146 call_stack->frees++; |
| 147 call_stack->free_size += alloc_info.bytes; |
| 148 |
| 149 call_stack_stats_.frees++; |
| 150 call_stack_stats_.free_size += alloc_info.bytes; |
| 151 |
| 152 if (entry->stack_table) |
| 153 entry->stack_table->Remove(call_stack); |
| 154 } |
| 155 stats_.frees++; |
| 156 stats_.free_size += alloc_info.bytes; |
| 157 } |
| 158 |
| 159 void LeakDetectorImpl::TestForLeaks() { |
| 160 // Add net alloc counts for each size to a ranked list. |
| 161 RankedList<uint32> size_ranked_list(kRankedListSize); |
| 162 for (int i = 0; i < kNumSizeEntries; ++i) { |
| 163 const AllocSizeEntry& entry = entries_[i]; |
| 164 size_ranked_list.Add(IndexToSize(i), entry.num_allocs - entry.num_frees); |
| 165 } |
| 166 size_leak_analyzer_.AddSample(size_ranked_list); |
| 167 |
| 168 // Dump out the top entries. |
| 169 if (FLAGS_dump_leak_analysis) { |
| 170 char buf[0x4000]; |
| 171 buf[0] = '\0'; |
| 172 size_leak_analyzer_.Dump(buf, sizeof(buf)); |
| 173 PrintWithPidOnEachLine(buf); |
| 174 } |
| 175 |
| 176 // Get suspected leaks by size. |
| 177 for (uint32 size : size_leak_analyzer_.suspected_leaks()) { |
| 178 AllocSizeEntry* entry = GetEntryForSize(size); |
| 179 if (entry->stack_table) |
| 180 continue; |
| 181 RAW_VLOG(0, "%d: Adding stack table for size %u", getpid(), size); |
| 182 entry->stack_table = new(Alloc(sizeof(CallStackTable))) CallStackTable; |
| 183 ++num_stack_tables_; |
| 184 } |
| 185 |
| 186 // Check for leaks in each CallStackTable. It makes sense to this before |
| 187 // checking the size allocations, because that could potentially create new |
| 188 // CallStackTable. However, the overhead to check a new CallStackTable is |
| 189 // small since this function is run very rarely. So handle the leak checks of |
| 190 // Tier 2 here. |
| 191 for (int i = 0; i < arraysize(entries_); ++i) { |
| 192 const AllocSizeEntry& entry = entries_[i]; |
| 193 CallStackTable* stack_table = entry.stack_table; |
| 194 if (!stack_table) |
| 195 continue; |
| 196 |
| 197 if (FLAGS_dump_leak_analysis) { |
| 198 // Dump table info. |
| 199 char buf[0x4000]; |
| 200 RAW_VLOG(0, "%d: Stack table for size %d", getpid(), IndexToSize(i)); |
| 201 buf[0] = '\0'; |
| 202 stack_table->Dump(buf, sizeof(buf)); |
| 203 PrintWithPidOnEachLine(buf); |
| 204 } |
| 205 |
| 206 // Get suspected leaks by call stack. |
| 207 stack_table->TestForLeaks(); |
| 208 const LeakAnalyzer<const CallStack*>& leak_analyzer = |
| 209 stack_table->leak_analyzer(); |
| 210 for (const CallStack* call_stack : leak_analyzer.suspected_leaks()) { |
| 211 RAW_VLOG(0, "%d: Suspected call stack for size %u: %p", |
| 212 getpid(), IndexToSize(i), call_stack); |
| 213 for (int k = 0; k < call_stack->depth; ++k) |
| 214 RAW_VLOG(0, "%d: %p", getpid(), GetOffset(call_stack->stack[k])); |
| 215 } |
| 216 } |
| 217 } |
| 218 |
| 219 void LeakDetectorImpl::DumpStats() const { |
| 220 RAW_VLOG(0, "%d: Alloc size: %lu\n", getpid(), stats_.alloc_size); |
| 221 RAW_VLOG(0, "%d: Free size: %lu\n", getpid(), stats_.free_size); |
| 222 RAW_VLOG(0, "%d: Net alloc size: %lu\n", getpid(), |
| 223 stats_.alloc_size - stats_.free_size); |
| 224 RAW_VLOG(0, "%d: Number of stack tables: %d\n", getpid(), num_stack_tables_); |
| 225 if (stats_.alloc_size) { |
| 226 RAW_VLOG(0, "%d: %% of calls with stack trace: %.2f%%\n", getpid(), |
| 227 static_cast<double>(call_stack_stats_.alloc_size * 100) / |
| 228 stats_.alloc_size); |
| 229 } |
| 230 RAW_VLOG(0, "%d: Number of call stack buckets: %d\n", getpid(), |
| 231 call_stacks_.bucket_count()); |
| 232 } |
| 233 |
| 234 // static |
| 235 inline int LeakDetectorImpl::SizeToIndex(size_t size) { |
| 236 int result = static_cast<int>(size /= sizeof(uint32)); |
| 237 if (result < kNumSizeEntries) |
| 238 return result; |
| 239 return 0; |
| 240 } |
| 241 |
| 242 // static |
| 243 inline size_t LeakDetectorImpl::IndexToSize(int index) { |
| 244 return sizeof(uint32) * index; |
| 245 } |
| 246 |
| 247 LeakDetectorImpl::CallStack* LeakDetectorImpl::GetCallStack( |
| 248 int depth, const void* const stack[]) { |
| 249 // Temporarily create a call stack object for lookup in |call_stacks_|. |
| 250 CallStack temp; |
| 251 temp.depth = depth; |
| 252 temp.stack = const_cast<const void**>(stack); |
| 253 |
| 254 auto iter = call_stacks_.find(&temp); |
| 255 if (iter != call_stacks_.end()) |
| 256 return *iter; |
| 257 |
| 258 // Since |call_stacks_| stores CallStack pointers rather than actual objects, |
| 259 // create new call objects manually here. |
| 260 CallStack* new_call_stack = new(Alloc(sizeof(CallStack))) CallStack; |
| 261 memset(new_call_stack, 0, sizeof(*new_call_stack)); |
| 262 new_call_stack->depth = depth; |
| 263 new_call_stack->hash = call_stacks_.hash_function()(&temp); |
| 264 new_call_stack->stack = |
| 265 reinterpret_cast<const void**>(Alloc(sizeof(*stack) * depth)); |
| 266 std::copy(stack, stack + depth, new_call_stack->stack); |
| 267 |
| 268 call_stacks_.insert(new_call_stack); |
| 269 return new_call_stack; |
| 270 } |
| 271 |
| 272 uint64 LeakDetectorImpl::GetOffset(const void *ptr) const { |
| 273 uint64 ptr_value = reinterpret_cast<uint64>(ptr); |
| 274 if (ptr_value >= mapping_addr_ && ptr_value < mapping_addr_ + mapping_size_) |
| 275 return ptr_value - mapping_addr_; |
| 276 return ptr_value; |
| 277 } |
| 278 |
| 279 // static |
| 280 // Use FarmHash to hash a call stack. |
| 281 uint64 LeakDetectorImpl::CallStackToHash(const CallStack* call_stack) { |
| 282 return util::Hash(reinterpret_cast<const char*>(call_stack->stack), |
| 283 sizeof(*(call_stack->stack)) * call_stack->depth); |
| 284 } |
| 285 |
| 286 } // namespace leak_detector |
OLD | NEW |