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