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 #ifndef COMPONENTS_METRICS_LEAK_DETECTOR_CALL_STACK_TABLE_H_ |
| 6 #define COMPONENTS_METRICS_LEAK_DETECTOR_CALL_STACK_TABLE_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <functional> // For std::equal_to. |
| 11 |
| 12 #include "base/containers/hash_tables.h" |
| 13 #include "base/macros.h" |
| 14 #include "components/metrics/leak_detector/custom_allocator.h" |
| 15 #include "components/metrics/leak_detector/leak_analyzer.h" |
| 16 #include "components/metrics/leak_detector/stl_allocator.h" |
| 17 |
| 18 namespace metrics { |
| 19 namespace leak_detector { |
| 20 |
| 21 struct CallStack; |
| 22 |
| 23 // Contains a hash table where the key is the call stack and the value is the |
| 24 // number of allocations from that call stack. |
| 25 class CallStackTable { |
| 26 public: |
| 27 struct StoredHash { |
| 28 size_t operator() (const CallStack* call_stack) const; |
| 29 }; |
| 30 |
| 31 explicit CallStackTable(int call_stack_suspicion_threshold); |
| 32 ~CallStackTable(); |
| 33 |
| 34 // Add/Remove an allocation for the given call stack. |
| 35 // Note that this class does NOT own the CallStack objects. Instead, it |
| 36 // identifies different CallStacks by their hashes. |
| 37 void Add(const CallStack* call_stack); |
| 38 void Remove(const CallStack* call_stack); |
| 39 |
| 40 // Check for leak patterns in the allocation data. |
| 41 void TestForLeaks(); |
| 42 |
| 43 const LeakAnalyzer& leak_analyzer() const { |
| 44 return leak_analyzer_; |
| 45 } |
| 46 |
| 47 size_t size() const { |
| 48 return entry_map_.size(); |
| 49 } |
| 50 bool empty() const { |
| 51 return entry_map_.empty(); |
| 52 } |
| 53 |
| 54 uint32_t num_allocs() const { |
| 55 return num_allocs_; |
| 56 } |
| 57 uint32_t num_frees() const { |
| 58 return num_frees_; |
| 59 } |
| 60 |
| 61 private: |
| 62 // Hash table entry used to track allocation stats for a given call stack. |
| 63 struct Entry { |
| 64 // Net number of allocs (allocs minus frees). |
| 65 uint32_t net_num_allocs; |
| 66 }; |
| 67 |
| 68 // Total number of allocs and frees in this table. |
| 69 uint32_t num_allocs_; |
| 70 uint32_t num_frees_; |
| 71 |
| 72 // Hash table containing entries. |
| 73 using TableEntryAllocator = |
| 74 STLAllocator<std::pair<const CallStack*, Entry>, CustomAllocator>; |
| 75 base::hash_map<const CallStack*, |
| 76 Entry, |
| 77 StoredHash, |
| 78 std::equal_to<const CallStack*>, |
| 79 TableEntryAllocator> entry_map_; |
| 80 |
| 81 // For detecting leak patterns in incoming allocations. |
| 82 LeakAnalyzer leak_analyzer_; |
| 83 |
| 84 DISALLOW_COPY_AND_ASSIGN(CallStackTable); |
| 85 }; |
| 86 |
| 87 } // namespace leak_detector |
| 88 } // namespace metrics |
| 89 |
| 90 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_CALL_STACK_TABLE_H_ |
OLD | NEW |