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_LEAK_DETECTOR_IMPL_H_ |
| 6 #define COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_ |
| 7 |
| 8 #include <gperftools/custom_allocator.h> |
| 9 #include <stdint.h> |
| 10 |
| 11 #include <unordered_map> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/macros.h" |
| 15 #include "components/metrics/leak_detector/call_stack_manager.h" |
| 16 #include "components/metrics/leak_detector/leak_analyzer.h" |
| 17 |
| 18 namespace leak_detector { |
| 19 |
| 20 // Vector type that's safe to use within the memory leak detector. Uses |
| 21 // CustomAllocator to avoid recursive malloc hook invocation. |
| 22 template <typename T> |
| 23 using InternalVector = std::vector<T, STL_Allocator<T, CustomAllocator>>; |
| 24 |
| 25 struct CallStackTable; |
| 26 |
| 27 struct InternalLeakReport { |
| 28 size_t alloc_size_bytes; |
| 29 |
| 30 // Unlike the CallStack struct, which consists of addresses, this call stack |
| 31 // will contain offsets in the executable binary. |
| 32 InternalVector<uintptr_t> call_stack; |
| 33 |
| 34 // TODO(sque): Add leak detector parameters. |
| 35 |
| 36 bool operator< (const InternalLeakReport& other) const; |
| 37 }; |
| 38 |
| 39 // Class that contains the actual leak detection mechanism. |
| 40 class LeakDetectorImpl { |
| 41 public: |
| 42 LeakDetectorImpl(uintptr_t mapping_addr, |
| 43 size_t mapping_size, |
| 44 int size_suspicion_threshold, |
| 45 int call_stack_suspicion_threshold, |
| 46 bool verbose); |
| 47 ~LeakDetectorImpl(); |
| 48 |
| 49 // Indicates whether the given allocation size has an associated call stack |
| 50 // table, and thus requires a stack unwind. |
| 51 bool ShouldGetStackTraceForSize(size_t size) const; |
| 52 |
| 53 // Record allocs and frees. |
| 54 void RecordAlloc(const void* ptr, |
| 55 size_t size, |
| 56 int stack_depth, |
| 57 const void* const call_stack[]); |
| 58 void RecordFree(const void* ptr); |
| 59 |
| 60 // Run check for possible leaks based on the current profiling data. |
| 61 void TestForLeaks(bool do_logging, |
| 62 InternalVector<InternalLeakReport>* reports); |
| 63 |
| 64 private: |
| 65 // A record of allocations for a particular size. |
| 66 struct AllocSizeEntry { |
| 67 // Number of allocations and frees for this size. |
| 68 uint32_t num_allocs; |
| 69 uint32_t num_frees; |
| 70 |
| 71 // A stack table, if this size is being profiled for stack as well. |
| 72 CallStackTable* stack_table; |
| 73 }; |
| 74 |
| 75 // Info for a single allocation. |
| 76 struct AllocInfo { |
| 77 AllocInfo() : call_stack(nullptr) {} |
| 78 |
| 79 // Number of bytes in this allocation. |
| 80 size_t size; |
| 81 |
| 82 // Points to a unique call stack. |
| 83 const CallStack* call_stack; |
| 84 }; |
| 85 |
| 86 // Allocator class for allocation entry map. Maps allocated addresses to |
| 87 // AllocInfo objects. |
| 88 using AllocationEntryAllocator = |
| 89 STL_Allocator<std::pair<const void*, AllocInfo>, CustomAllocator>; |
| 90 |
| 91 // Hash class for addresses. |
| 92 struct AddressHash { |
| 93 size_t operator() (uintptr_t addr) const; |
| 94 }; |
| 95 |
| 96 // Returns the offset of |ptr| within the current binary. If it is not in the |
| 97 // current binary, just return |ptr| as an integer. |
| 98 uintptr_t GetOffset(const void *ptr) const; |
| 99 |
| 100 // Dump current profiling statistics to log. |
| 101 void DumpStats() const; |
| 102 |
| 103 // Owns all unique call stack objects, which are allocated on the heap. Any |
| 104 // other class or function that references a call stack must get it from here, |
| 105 // but may not take ownership of the call stack object. |
| 106 CallStackManager call_stack_manager_; |
| 107 |
| 108 // Allocation stats. |
| 109 uint64_t num_allocs_; |
| 110 uint64_t num_frees_; |
| 111 uint64_t alloc_size_; |
| 112 uint64_t free_size_; |
| 113 |
| 114 uint32_t num_allocs_with_call_stack_; |
| 115 uint32_t num_stack_tables_; |
| 116 |
| 117 // Stores all individual recorded allocations. |
| 118 std::unordered_map<uintptr_t, |
| 119 AllocInfo, |
| 120 AddressHash, |
| 121 std::equal_to<uintptr_t>, |
| 122 AllocationEntryAllocator> address_map_; |
| 123 |
| 124 // Used to analyze potential leak patterns in the allocation sizes. |
| 125 LeakAnalyzer size_leak_analyzer_; |
| 126 |
| 127 // Allocation stats for each size. |
| 128 InternalVector<AllocSizeEntry> size_entries_; |
| 129 |
| 130 // Address mapping info of the current binary. |
| 131 uintptr_t mapping_addr_; |
| 132 size_t mapping_size_; |
| 133 |
| 134 // Number of consecutive times an allocation size must trigger suspicion to be |
| 135 // considered a leak suspect. |
| 136 int size_suspicion_threshold_; |
| 137 |
| 138 // Number of consecutive times a call stack must trigger suspicion to be |
| 139 // considered a leak suspect. |
| 140 int call_stack_suspicion_threshold_; |
| 141 |
| 142 // Enable verbose dumping of much more leak analysis data. |
| 143 bool verbose_; |
| 144 |
| 145 DISALLOW_COPY_AND_ASSIGN(LeakDetectorImpl); |
| 146 }; |
| 147 |
| 148 } // namespace leak_detector |
| 149 |
| 150 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_ |
OLD | NEW |