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