| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_ | 5 #ifndef COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_ |
| 6 #define COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_ | 6 #define COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 // CustomAllocator to avoid recursive malloc hook invocation when analyzing | 31 // CustomAllocator to avoid recursive malloc hook invocation when analyzing |
| 32 // allocs and frees. | 32 // allocs and frees. |
| 33 template <typename T> | 33 template <typename T> |
| 34 using InternalList = std::list<T, STLAllocator<T, CustomAllocator>>; | 34 using InternalList = std::list<T, STLAllocator<T, CustomAllocator>>; |
| 35 template <typename T> | 35 template <typename T> |
| 36 using InternalVector = std::vector<T, STLAllocator<T, CustomAllocator>>; | 36 using InternalVector = std::vector<T, STLAllocator<T, CustomAllocator>>; |
| 37 | 37 |
| 38 // Leak report generated by LeakDetectorImpl. | 38 // Leak report generated by LeakDetectorImpl. |
| 39 class LeakReport { | 39 class LeakReport { |
| 40 public: | 40 public: |
| 41 // Stores a record of the allocation bookkeeping taken at a single moment in |
| 42 // time. |
| 43 struct AllocationBreakdown { |
| 44 AllocationBreakdown(); |
| 45 ~AllocationBreakdown(); |
| 46 |
| 47 // The contents of |LeakDetectorImpl::size_breakdown_history_| when this |
| 48 // report was generated. See comment description of that variable. |
| 49 InternalVector<uint32_t> counts_by_size; |
| 50 |
| 51 // The net number of allocations with alloc size of |
| 52 // |Leakreport::alloc_size_bytes_| allocated from |call_stack_|, at the |
| 53 // time this record was generated. |
| 54 uint32_t count_for_call_stack; |
| 55 }; |
| 56 |
| 41 LeakReport(); | 57 LeakReport(); |
| 42 ~LeakReport(); | 58 ~LeakReport(); |
| 43 | 59 |
| 44 size_t alloc_size_bytes() const { return alloc_size_bytes_; } | 60 size_t alloc_size_bytes() const { return alloc_size_bytes_; } |
| 45 | 61 |
| 46 const InternalVector<uintptr_t>& call_stack() const { return call_stack_; } | 62 const InternalVector<uintptr_t>& call_stack() const { return call_stack_; } |
| 47 | 63 |
| 48 const InternalVector<InternalVector<uint32_t>>& size_breakdown_history() | 64 const InternalVector<AllocationBreakdown>& alloc_breakdown_history() const { |
| 49 const { | 65 return alloc_breakdown_history_; |
| 50 return size_breakdown_history_; | |
| 51 } | 66 } |
| 52 | 67 |
| 53 // Used to compare the contents of two leak reports. | 68 // Used to compare the contents of two leak reports. |
| 54 bool operator<(const LeakReport& other) const; | 69 bool operator<(const LeakReport& other) const; |
| 55 | 70 |
| 56 private: | 71 private: |
| 57 // LeakDetectorImpl needs access to class members when creating a new leak | 72 // LeakDetectorImpl needs access to class members when creating a new leak |
| 58 // report. | 73 // report. |
| 59 friend class LeakDetectorImpl; | 74 friend class LeakDetectorImpl; |
| 60 | 75 |
| 61 // Number of bytes allocated by the leak site during each allocation. | 76 // Number of bytes allocated by the leak site during each allocation. |
| 62 size_t alloc_size_bytes_; | 77 size_t alloc_size_bytes_; |
| 63 | 78 |
| 64 // Unlike the CallStack struct, which consists of addresses, this call stack | 79 // Unlike the CallStack struct, which consists of addresses, this call stack |
| 65 // will contain offsets in the executable binary. | 80 // will contain offsets in the executable binary. |
| 66 InternalVector<uintptr_t> call_stack_; | 81 InternalVector<uintptr_t> call_stack_; |
| 67 | 82 |
| 68 // A snapshot of LeakDetectorImpl::size_breakdown_history_ when this report | 83 // Records of allocation bookkeeping over time. The first element is the |
| 69 // was generated. See comment description of that variable. | 84 // oldest entry and the last element is the newest. |
| 70 InternalVector<InternalVector<uint32_t>> size_breakdown_history_; | 85 InternalVector<AllocationBreakdown> alloc_breakdown_history_; |
| 71 }; | 86 }; |
| 72 | 87 |
| 73 LeakDetectorImpl(uintptr_t mapping_addr, | 88 LeakDetectorImpl(uintptr_t mapping_addr, |
| 74 size_t mapping_size, | 89 size_t mapping_size, |
| 75 int size_suspicion_threshold, | 90 int size_suspicion_threshold, |
| 76 int call_stack_suspicion_threshold); | 91 int call_stack_suspicion_threshold); |
| 77 ~LeakDetectorImpl(); | 92 ~LeakDetectorImpl(); |
| 78 | 93 |
| 79 // Indicates whether the given allocation size has an associated call stack | 94 // Indicates whether the given allocation size has an associated call stack |
| 80 // table, and thus requires a stack unwind. | 95 // table, and thus requires a stack unwind. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 93 private: | 108 private: |
| 94 // A record of allocations for a particular size. | 109 // A record of allocations for a particular size. |
| 95 struct AllocSizeEntry { | 110 struct AllocSizeEntry { |
| 96 // Number of allocations and frees for this size. | 111 // Number of allocations and frees for this size. |
| 97 uint32_t num_allocs; | 112 uint32_t num_allocs; |
| 98 uint32_t num_frees; | 113 uint32_t num_frees; |
| 99 | 114 |
| 100 // A stack table, if this size is being profiled for stack as well. | 115 // A stack table, if this size is being profiled for stack as well. |
| 101 CallStackTable* stack_table; | 116 CallStackTable* stack_table; |
| 102 | 117 |
| 118 // Historical records of allocation breakdown by call site, for a particular |
| 119 // allocation size. Each entry in the list is a RankedSet containing the top |
| 120 // call sites ordered by most number of call sites, collected during a leak |
| 121 // analysis. The oldest record is at the head and the newest record is at |
| 122 // the tail. |
| 123 InternalList<RankedSet> call_site_breakdown_history; |
| 124 |
| 125 AllocSizeEntry(); |
| 126 ~AllocSizeEntry(); |
| 127 |
| 103 // Returns net number of allocs. | 128 // Returns net number of allocs. |
| 104 uint32_t GetNetAllocs() const { return num_allocs - num_frees; } | 129 uint32_t GetNetAllocs() const { return num_allocs - num_frees; } |
| 105 }; | 130 }; |
| 106 | 131 |
| 107 // Info for a single allocation. | 132 // Info for a single allocation. |
| 108 struct AllocInfo { | 133 struct AllocInfo { |
| 109 AllocInfo() : call_stack(nullptr) {} | 134 AllocInfo() : call_stack(nullptr) {} |
| 110 | 135 |
| 111 // Number of bytes in this allocation. | 136 // Number of bytes in this allocation. |
| 112 size_t size; | 137 size_t size; |
| 113 | 138 |
| 114 // Points to a unique call stack. | 139 // Points to a unique call stack. |
| 115 const CallStack* call_stack; | 140 const CallStack* call_stack; |
| 116 }; | 141 }; |
| 117 | 142 |
| 118 // Allocator class for allocation entry map. Maps allocated addresses to | 143 // Allocator class for allocation entry map. Maps allocated addresses to |
| 119 // AllocInfo objects. | 144 // AllocInfo objects. |
| 120 using AllocationEntryAllocator = | 145 using AllocationEntryAllocator = |
| 121 STLAllocator<std::pair<const uintptr_t, AllocInfo>, CustomAllocator>; | 146 STLAllocator<std::pair<const uintptr_t, AllocInfo>, CustomAllocator>; |
| 122 | 147 |
| 123 // Hash class for addresses. | 148 // Hash class for addresses. |
| 124 struct AddressHash { | 149 struct AddressHash { |
| 125 size_t operator()(uintptr_t addr) const; | 150 size_t operator()(uintptr_t addr) const; |
| 126 }; | 151 }; |
| 127 | 152 |
| 128 // Returns the offset of |ptr| within the current binary. If it is not in the | 153 // Returns the offset of |ptr| within the current binary. If it is not in the |
| 129 // current binary, just return |ptr| as an integer. | 154 // current binary, just return |ptr| as an integer. |
| 130 uintptr_t GetOffset(const void* ptr) const; | 155 uintptr_t GetOffset(const void* ptr) const; |
| 131 | 156 |
| 157 // Record some of the current allocation bookkeeping. The net number of allocs |
| 158 // per size is recorded in |size_breakdown_history_|. The net number of allocs |
| 159 // per call site for each size is recorded in |
| 160 // |AllocSizeEntry::call_site_breakdown_history|. |
| 161 // |
| 162 // Not all the net alloc counts are recorded. And the number of historical |
| 163 // records kept is capped. If adding a new record exceeds that limit, the |
| 164 // oldest record is discarded. See the function definition for more details. |
| 165 void RecordCurrentAllocationDataInHistory(); |
| 166 |
| 167 // Store the data collected by RecordCurrentAllocationDataInHistory() in |
| 168 // |*report|. Not all net alloc counts per call site will be stored, only the |
| 169 // count for size=|size| and made from |call_site|. |
| 170 void StoreHistoricalDataInReport(size_t size, const CallStack* call_site, |
| 171 LeakReport* report); |
| 172 |
| 132 // Owns all unique call stack objects, which are allocated on the heap. Any | 173 // Owns all unique call stack objects, which are allocated on the heap. Any |
| 133 // other class or function that references a call stack must get it from here, | 174 // other class or function that references a call stack must get it from here, |
| 134 // but may not take ownership of the call stack object. | 175 // but may not take ownership of the call stack object. |
| 135 CallStackManager call_stack_manager_; | 176 CallStackManager call_stack_manager_; |
| 136 | 177 |
| 137 // Allocation stats. | 178 // Allocation stats. |
| 138 uint64_t num_allocs_; | 179 uint64_t num_allocs_; |
| 139 uint64_t num_frees_; | 180 uint64_t num_frees_; |
| 140 uint64_t alloc_size_; | 181 uint64_t alloc_size_; |
| 141 uint64_t free_size_; | 182 uint64_t free_size_; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 // considered a leak suspect. | 216 // considered a leak suspect. |
| 176 int call_stack_suspicion_threshold_; | 217 int call_stack_suspicion_threshold_; |
| 177 | 218 |
| 178 DISALLOW_COPY_AND_ASSIGN(LeakDetectorImpl); | 219 DISALLOW_COPY_AND_ASSIGN(LeakDetectorImpl); |
| 179 }; | 220 }; |
| 180 | 221 |
| 181 } // namespace leak_detector | 222 } // namespace leak_detector |
| 182 } // namespace metrics | 223 } // namespace metrics |
| 183 | 224 |
| 184 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_ | 225 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_ |
| OLD | NEW |