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 "components/metrics/leak_detector/call_stack_table.h" |
| 6 |
| 7 namespace leak_detector { |
| 8 |
| 9 namespace { |
| 10 |
| 11 using ValueType = LeakDetectorValueType; |
| 12 |
| 13 // Get the top |kRankedListSize| entries. |
| 14 const int kRankedListSize = 16; |
| 15 |
| 16 // Initial number of hash table buckets. |
| 17 const int kInitialHashTableSize = 1999; |
| 18 |
| 19 } // namespace |
| 20 |
| 21 CallStackTable::CallStackTable(int call_stack_suspicion_threshold) |
| 22 : num_allocs_(0), |
| 23 num_frees_(0), |
| 24 entry_map_(kInitialHashTableSize), |
| 25 leak_analyzer_(kRankedListSize, call_stack_suspicion_threshold) { |
| 26 } |
| 27 |
| 28 CallStackTable::~CallStackTable() {} |
| 29 |
| 30 void CallStackTable::Add(const CallStack* call_stack) { |
| 31 auto iter = entry_map_.find(call_stack); |
| 32 Entry* entry = nullptr; |
| 33 if (iter == entry_map_.end()) { |
| 34 entry = &entry_map_[call_stack]; |
| 35 } else { |
| 36 entry = &iter->second; |
| 37 } |
| 38 |
| 39 ++entry->num_allocs; |
| 40 ++num_allocs_; |
| 41 } |
| 42 |
| 43 void CallStackTable::Remove(const CallStack* call_stack) { |
| 44 auto iter = entry_map_.find(call_stack); |
| 45 if (iter == entry_map_.end()) |
| 46 return; |
| 47 Entry* entry = &iter->second; |
| 48 ++entry->num_frees; |
| 49 ++num_frees_; |
| 50 |
| 51 // If there are no net allocs, delete the entry. |
| 52 if (entry->num_allocs == entry->num_frees) |
| 53 entry_map_.erase(iter); |
| 54 } |
| 55 |
| 56 int CallStackTable::Dump(char* buffer, const int buffer_size) const { |
| 57 int size_left = buffer_size; |
| 58 |
| 59 if (entry_map_.empty()) |
| 60 return size_left; |
| 61 |
| 62 int attempted_size = |
| 63 snprintf(buffer, size_left, |
| 64 "Total number of allocations: %u\n" |
| 65 "Total number of frees: %u\n" |
| 66 "Net number of allocations: %u\n" |
| 67 "Total number of distinct stack traces: %zu\n", |
| 68 num_allocs_, num_frees_, num_allocs_ - num_frees_, |
| 69 entry_map_.size()); |
| 70 size_left -= attempted_size; |
| 71 buffer += attempted_size; |
| 72 |
| 73 if (size_left > 1) { |
| 74 int attempted_size = leak_analyzer_.Dump(buffer, size_left); |
| 75 size_left -= attempted_size; |
| 76 buffer += attempted_size; |
| 77 } |
| 78 |
| 79 return buffer_size - size_left; |
| 80 } |
| 81 |
| 82 void CallStackTable::TestForLeaks() { |
| 83 // Add all entries to the ranked list. |
| 84 RankedList ranked_list(kRankedListSize); |
| 85 |
| 86 for (const auto& entry_pair : entry_map_) { |
| 87 const Entry& entry = entry_pair.second; |
| 88 if (entry.num_allocs != entry.num_frees) { |
| 89 LeakDetectorValueType call_stack_value(entry_pair.first); |
| 90 ranked_list.Add(call_stack_value, entry.num_allocs - entry.num_frees); |
| 91 } |
| 92 } |
| 93 leak_analyzer_.AddSample(ranked_list); |
| 94 } |
| 95 |
| 96 } // namespace leak_detector |
OLD | NEW |