Chromium Code Reviews| Index: components/metrics/leak_detector/call_stack_table.cc |
| diff --git a/components/metrics/leak_detector/call_stack_table.cc b/components/metrics/leak_detector/call_stack_table.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..513dd59181594eadd62acdcccd2f9d26b9edd4dd |
| --- /dev/null |
| +++ b/components/metrics/leak_detector/call_stack_table.cc |
| @@ -0,0 +1,83 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/metrics/leak_detector/call_stack_table.h" |
| + |
| +#include "components/metrics/leak_detector/call_stack_manager.h" |
| + |
| +namespace metrics { |
| +namespace leak_detector { |
| + |
| +namespace { |
| + |
| +using ValueType = LeakDetectorValueType; |
| + |
| +// Get the top |kRankedListSize| entries. |
|
jar (doing other things)
2015/11/14 02:58:36
I don't understand this comment. Perhaps the comm
Simon Que
2015/11/17 00:28:48
Done.
|
| +const int kRankedListSize = 16; |
| + |
| +// Initial number of hash table buckets. |
|
jar (doing other things)
2015/11/14 02:58:36
nit: your choice... but the name of the variable s
Simon Que
2015/11/17 00:28:48
Done.
|
| +const int kInitialHashTableSize = 1999; |
| + |
| +} // namespace |
| + |
| +size_t CallStackTable::StoredHash::operator() ( |
| + const CallStack* call_stack) const { |
| + // The call stack object should already have a hash computed when it was |
| + // created. |
| + // |
| + // This is NOT the actual hash computation function for a new call stack. |
| + return call_stack->hash; |
| +} |
| + |
| +CallStackTable::CallStackTable(int call_stack_suspicion_threshold) |
| + : num_allocs_(0), |
| + num_frees_(0), |
| + entry_map_(kInitialHashTableSize), |
| + leak_analyzer_(kRankedListSize, call_stack_suspicion_threshold) { |
| +} |
| + |
| +CallStackTable::~CallStackTable() {} |
| + |
| +void CallStackTable::Add(const CallStack* call_stack) { |
| + auto iter = entry_map_.find(call_stack); |
| + Entry* entry = nullptr; |
| + if (iter == entry_map_.end()) { |
| + entry = &entry_map_[call_stack]; |
|
jar (doing other things)
2015/11/14 02:58:36
Would it have been enough to just do this operator
Simon Que
2015/11/17 00:28:48
Done.
|
| + } else { |
| + entry = &iter->second; |
| + } |
| + |
| + ++entry->net_num_allocs; |
| + ++num_allocs_; |
| +} |
| + |
| +void CallStackTable::Remove(const CallStack* call_stack) { |
| + auto iter = entry_map_.find(call_stack); |
| + if (iter == entry_map_.end()) |
| + return; |
| + Entry* entry = &iter->second; |
| + --entry->net_num_allocs; |
| + ++num_frees_; |
| + |
| + // Delete zero-alloc entries to free up space. |
| + if (entry->net_num_allocs == 0) |
| + entry_map_.erase(iter); |
| +} |
| + |
| +void CallStackTable::TestForLeaks() { |
| + // Add all entries to the ranked list. |
| + RankedList ranked_list(kRankedListSize); |
| + |
| + for (const auto& entry_pair : entry_map_) { |
| + const Entry& entry = entry_pair.second; |
| + if (entry.net_num_allocs > 0) { |
|
jar (doing other things)
2015/11/14 02:58:36
Given line 65, which seems to discard any entries
Simon Que
2015/11/17 00:28:48
Done.
|
| + LeakDetectorValueType call_stack_value(entry_pair.first); |
| + ranked_list.Add(call_stack_value, entry.net_num_allocs); |
| + } |
| + } |
| + leak_analyzer_.AddSample(ranked_list.Pass()); |
| +} |
| + |
| +} // namespace leak_detector |
| +} // namespace metrics |