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 #include <unordered_map> |
| 12 |
| 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 // Dump contents to log buffer |buffer| of size |size|. Returns the number of |
| 41 // bytes remaining in the buffer after writing to it. The number of bytes |
| 42 // remaining includes the zero terminator that was just written, so this will |
| 43 // always return at least 1, unless |size| == 0. |
| 44 size_t Dump(const size_t buffer_size, char* buffer) const; |
| 45 |
| 46 // Check for leak patterns in the allocation data. |
| 47 void TestForLeaks(); |
| 48 |
| 49 const LeakAnalyzer& leak_analyzer() const { |
| 50 return leak_analyzer_; |
| 51 } |
| 52 |
| 53 size_t size() const { |
| 54 return entry_map_.size(); |
| 55 } |
| 56 bool empty() const { |
| 57 return entry_map_.empty(); |
| 58 } |
| 59 |
| 60 uint32_t num_allocs() const { |
| 61 return num_allocs_; |
| 62 } |
| 63 uint32_t num_frees() const { |
| 64 return num_frees_; |
| 65 } |
| 66 |
| 67 private: |
| 68 // Hash table entry used to track allocation stats for a given call stack. |
| 69 struct Entry { |
| 70 // Net number of allocs (allocs minus frees). |
| 71 uint32_t net_num_allocs; |
| 72 }; |
| 73 |
| 74 // Total number of allocs and frees in this table. |
| 75 uint32_t num_allocs_; |
| 76 uint32_t num_frees_; |
| 77 |
| 78 // Hash table containing entries. |
| 79 using TableEntryAllocator = |
| 80 STL_Allocator<std::pair<const CallStack*, Entry>, CustomAllocator>; |
| 81 std::unordered_map<const CallStack*, |
| 82 Entry, |
| 83 StoredHash, |
| 84 std::equal_to<const CallStack*>, |
| 85 TableEntryAllocator> entry_map_; |
| 86 |
| 87 // For detecting leak patterns in incoming allocations. |
| 88 LeakAnalyzer leak_analyzer_; |
| 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(CallStackTable); |
| 91 }; |
| 92 |
| 93 } // namespace leak_detector |
| 94 } // namespace metrics |
| 95 |
| 96 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_CALL_STACK_TABLE_H_ |
OLD | NEW |