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 "call_stack_table.h" |
| 9 |
| 10 #include <cstring> |
| 11 |
| 12 #include "base/commandlineflags.h" |
| 13 #include "heap-profile-stats.h" |
| 14 |
| 15 // A call stack must be suspected this many times to be reported as a leak |
| 16 // suspect. |
| 17 DECLARE_int32(call_stack_suspicion_threshold); |
| 18 |
| 19 namespace leak_detector { |
| 20 |
| 21 namespace { |
| 22 |
| 23 using CallStack = CallStackTable::CallStack; |
| 24 |
| 25 // Get the top |kRankedListSize| entries. |
| 26 const int kRankedListSize = 16; |
| 27 |
| 28 // Initial number of hash table buckets. |
| 29 const int kInitialHashTableSize = 1999; |
| 30 |
| 31 // Used for printing call stacks in LeakAnalyzer. |
| 32 class StringPrint : public LeakAnalyzer<const CallStack*>::StringPrint { |
| 33 public: |
| 34 // Gets the string representation of a value. |
| 35 virtual const char* ValueToString(const CallStack* const& call_stack, |
| 36 bool spacing_on) { |
| 37 snprintf(buffer_, sizeof(buffer_), spacing_on ? "%16p" : "%p", call_stack); |
| 38 return buffer_; |
| 39 } |
| 40 |
| 41 // Gets the word that describes the value type. |
| 42 virtual const char* ValueTypeName(bool is_plural) { |
| 43 return is_plural ? "call stacks" : "call stack"; |
| 44 } |
| 45 } string_print; |
| 46 |
| 47 } // namespace |
| 48 |
| 49 CallStackTable::CallStackTable() |
| 50 : num_allocs_(0), |
| 51 num_frees_(0), |
| 52 entry_map_(kInitialHashTableSize), |
| 53 leak_analyzer_(kRankedListSize, FLAGS_call_stack_suspicion_threshold, |
| 54 &string_print) { |
| 55 } |
| 56 |
| 57 CallStackTable::~CallStackTable() {} |
| 58 |
| 59 void CallStackTable::Add(const CallStack* call_stack) { |
| 60 auto iter = entry_map_.find(call_stack); |
| 61 Entry* entry = NULL; |
| 62 if (iter == entry_map_.end()) { |
| 63 entry = &entry_map_[call_stack]; |
| 64 } else { |
| 65 entry = &iter->second; |
| 66 } |
| 67 |
| 68 ++entry->num_allocs; |
| 69 ++num_allocs_; |
| 70 } |
| 71 |
| 72 void CallStackTable::Remove(const CallStack* call_stack) { |
| 73 auto iter = entry_map_.find(call_stack); |
| 74 if (iter == entry_map_.end()) |
| 75 return; |
| 76 Entry* entry = &iter->second; |
| 77 ++entry->num_frees; |
| 78 ++num_frees_; |
| 79 |
| 80 // If there are no net allocs, delete the entry. |
| 81 if (entry->num_allocs == entry->num_frees) |
| 82 entry_map_.erase(iter); |
| 83 } |
| 84 |
| 85 int CallStackTable::Dump(char* buffer, const int buffer_size) const { |
| 86 int size_left = buffer_size; |
| 87 |
| 88 if (entry_map_.empty()) |
| 89 return size_left; |
| 90 |
| 91 int attempted_size = |
| 92 snprintf(buffer, size_left, |
| 93 "Total number of allocations: %u\n" |
| 94 "Total number of frees: %u\n" |
| 95 "Net number of allocations: %u\n" |
| 96 "Total number of distinct stack traces: %lu\n", |
| 97 num_allocs_, num_frees_, num_allocs_ - num_frees_, |
| 98 entry_map_.size()); |
| 99 size_left -= attempted_size; |
| 100 buffer += attempted_size; |
| 101 |
| 102 if (size_left > 0) { |
| 103 int attempted_size = leak_analyzer_.Dump(buffer, size_left); |
| 104 size_left -= attempted_size; |
| 105 buffer += attempted_size; |
| 106 } |
| 107 |
| 108 if (size_left > 0) |
| 109 return buffer_size - size_left; |
| 110 |
| 111 return buffer_size; |
| 112 } |
| 113 |
| 114 void CallStackTable::TestForLeaks() { |
| 115 // Add all entries to the ranked list. |
| 116 RankedList<const CallStack*> ranked_list(kRankedListSize); |
| 117 |
| 118 for (const auto& entry_pair : entry_map_) { |
| 119 const Entry& entry = entry_pair.second; |
| 120 if (entry.num_allocs != entry.num_frees) |
| 121 ranked_list.Add(entry_pair.first, entry.num_allocs - entry.num_frees); |
| 122 } |
| 123 leak_analyzer_.AddSample(ranked_list); |
| 124 } |
| 125 |
| 126 } // namespace leak_detector |
OLD | NEW |