| 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 #include <algorithm> |
| 8 |
| 9 #include "components/metrics/leak_detector/call_stack_manager.h" |
| 10 |
| 11 namespace metrics { |
| 12 namespace leak_detector { |
| 13 |
| 14 namespace { |
| 15 |
| 16 using ValueType = LeakDetectorValueType; |
| 17 |
| 18 // During leak analysis, we only want to examine the top |
| 19 // |kMaxCountOfSuspciousStacks| entries. |
| 20 const int kMaxCountOfSuspciousStacks = 16; |
| 21 |
| 22 const int kInitialHashTableSize = 1999; |
| 23 |
| 24 } // namespace |
| 25 |
| 26 size_t CallStackTable::StoredHash::operator()( |
| 27 const CallStack* call_stack) const { |
| 28 // The call stack object should already have a hash computed when it was |
| 29 // created. |
| 30 // |
| 31 // This is NOT the actual hash computation function for a new call stack. |
| 32 return call_stack->hash; |
| 33 } |
| 34 |
| 35 CallStackTable::CallStackTable(int call_stack_suspicion_threshold) |
| 36 : num_allocs_(0), |
| 37 num_frees_(0), |
| 38 entry_map_(kInitialHashTableSize), |
| 39 leak_analyzer_(kMaxCountOfSuspciousStacks, |
| 40 call_stack_suspicion_threshold) {} |
| 41 |
| 42 CallStackTable::~CallStackTable() {} |
| 43 |
| 44 void CallStackTable::Add(const CallStack* call_stack) { |
| 45 Entry* entry = &entry_map_[call_stack]; |
| 46 |
| 47 ++entry->net_num_allocs; |
| 48 ++num_allocs_; |
| 49 } |
| 50 |
| 51 void CallStackTable::Remove(const CallStack* call_stack) { |
| 52 auto iter = entry_map_.find(call_stack); |
| 53 if (iter == entry_map_.end()) |
| 54 return; |
| 55 Entry* entry = &iter->second; |
| 56 --entry->net_num_allocs; |
| 57 ++num_frees_; |
| 58 |
| 59 // Delete zero-alloc entries to free up space. |
| 60 if (entry->net_num_allocs == 0) |
| 61 entry_map_.erase(iter); |
| 62 } |
| 63 |
| 64 void CallStackTable::TestForLeaks() { |
| 65 // Add all entries to the ranked list. |
| 66 RankedList ranked_list(kMaxCountOfSuspciousStacks); |
| 67 |
| 68 for (const auto& entry_pair : entry_map_) { |
| 69 const Entry& entry = entry_pair.second; |
| 70 // Assumes that |entry.net_num_allocs| is always > 0. If that changes |
| 71 // elsewhere in this class, this code should be updated to only pass values |
| 72 // > 0 to |ranked_list|. |
| 73 LeakDetectorValueType call_stack_value(entry_pair.first); |
| 74 ranked_list.Add(call_stack_value, entry.net_num_allocs); |
| 75 } |
| 76 leak_analyzer_.AddSample(std::move(ranked_list)); |
| 77 } |
| 78 |
| 79 } // namespace leak_detector |
| 80 } // namespace metrics |
| OLD | NEW |