| 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 | |
| 57 LeakReport(); | 41 LeakReport(); |
| 58 ~LeakReport(); | 42 ~LeakReport(); |
| 59 | 43 |
| 60 size_t alloc_size_bytes() const { return alloc_size_bytes_; } | 44 size_t alloc_size_bytes() const { return alloc_size_bytes_; } |
| 61 | 45 |
| 62 const InternalVector<uintptr_t>& call_stack() const { return call_stack_; } | 46 const InternalVector<uintptr_t>& call_stack() const { return call_stack_; } |
| 63 | 47 |
| 64 const InternalVector<AllocationBreakdown>& alloc_breakdown_history() const { | 48 const InternalVector<InternalVector<uint32_t>>& size_breakdown_history() |
| 65 return alloc_breakdown_history_; | 49 const { |
| 50 return size_breakdown_history_; |
| 66 } | 51 } |
| 67 | 52 |
| 68 // Used to compare the contents of two leak reports. | 53 // Used to compare the contents of two leak reports. |
| 69 bool operator<(const LeakReport& other) const; | 54 bool operator<(const LeakReport& other) const; |
| 70 | 55 |
| 71 private: | 56 private: |
| 72 // LeakDetectorImpl needs access to class members when creating a new leak | 57 // LeakDetectorImpl needs access to class members when creating a new leak |
| 73 // report. | 58 // report. |
| 74 friend class LeakDetectorImpl; | 59 friend class LeakDetectorImpl; |
| 75 | 60 |
| 76 // Number of bytes allocated by the leak site during each allocation. | 61 // Number of bytes allocated by the leak site during each allocation. |
| 77 size_t alloc_size_bytes_; | 62 size_t alloc_size_bytes_; |
| 78 | 63 |
| 79 // Unlike the CallStack struct, which consists of addresses, this call stack | 64 // Unlike the CallStack struct, which consists of addresses, this call stack |
| 80 // will contain offsets in the executable binary. | 65 // will contain offsets in the executable binary. |
| 81 InternalVector<uintptr_t> call_stack_; | 66 InternalVector<uintptr_t> call_stack_; |
| 82 | 67 |
| 83 // Records of allocation bookkeeping over time. The first element is the | 68 // A snapshot of LeakDetectorImpl::size_breakdown_history_ when this report |
| 84 // oldest entry and the last element is the newest. | 69 // was generated. See comment description of that variable. |
| 85 InternalVector<AllocationBreakdown> alloc_breakdown_history_; | 70 InternalVector<InternalVector<uint32_t>> size_breakdown_history_; |
| 86 }; | 71 }; |
| 87 | 72 |
| 88 LeakDetectorImpl(uintptr_t mapping_addr, | 73 LeakDetectorImpl(uintptr_t mapping_addr, |
| 89 size_t mapping_size, | 74 size_t mapping_size, |
| 90 int size_suspicion_threshold, | 75 int size_suspicion_threshold, |
| 91 int call_stack_suspicion_threshold); | 76 int call_stack_suspicion_threshold); |
| 92 ~LeakDetectorImpl(); | 77 ~LeakDetectorImpl(); |
| 93 | 78 |
| 94 // Indicates whether the given allocation size has an associated call stack | 79 // Indicates whether the given allocation size has an associated call stack |
| 95 // table, and thus requires a stack unwind. | 80 // table, and thus requires a stack unwind. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 108 private: | 93 private: |
| 109 // A record of allocations for a particular size. | 94 // A record of allocations for a particular size. |
| 110 struct AllocSizeEntry { | 95 struct AllocSizeEntry { |
| 111 // Number of allocations and frees for this size. | 96 // Number of allocations and frees for this size. |
| 112 uint32_t num_allocs; | 97 uint32_t num_allocs; |
| 113 uint32_t num_frees; | 98 uint32_t num_frees; |
| 114 | 99 |
| 115 // A stack table, if this size is being profiled for stack as well. | 100 // A stack table, if this size is being profiled for stack as well. |
| 116 CallStackTable* stack_table; | 101 CallStackTable* stack_table; |
| 117 | 102 |
| 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 | |
| 128 // Returns net number of allocs. | 103 // Returns net number of allocs. |
| 129 uint32_t GetNetAllocs() const { return num_allocs - num_frees; } | 104 uint32_t GetNetAllocs() const { return num_allocs - num_frees; } |
| 130 }; | 105 }; |
| 131 | 106 |
| 132 // Info for a single allocation. | 107 // Info for a single allocation. |
| 133 struct AllocInfo { | 108 struct AllocInfo { |
| 134 AllocInfo() : call_stack(nullptr) {} | 109 AllocInfo() : call_stack(nullptr) {} |
| 135 | 110 |
| 136 // Number of bytes in this allocation. | 111 // Number of bytes in this allocation. |
| 137 size_t size; | 112 size_t size; |
| 138 | 113 |
| 139 // Points to a unique call stack. | 114 // Points to a unique call stack. |
| 140 const CallStack* call_stack; | 115 const CallStack* call_stack; |
| 141 }; | 116 }; |
| 142 | 117 |
| 143 // Allocator class for allocation entry map. Maps allocated addresses to | 118 // Allocator class for allocation entry map. Maps allocated addresses to |
| 144 // AllocInfo objects. | 119 // AllocInfo objects. |
| 145 using AllocationEntryAllocator = | 120 using AllocationEntryAllocator = |
| 146 STLAllocator<std::pair<const uintptr_t, AllocInfo>, CustomAllocator>; | 121 STLAllocator<std::pair<const uintptr_t, AllocInfo>, CustomAllocator>; |
| 147 | 122 |
| 148 // Hash class for addresses. | 123 // Hash class for addresses. |
| 149 struct AddressHash { | 124 struct AddressHash { |
| 150 size_t operator()(uintptr_t addr) const; | 125 size_t operator()(uintptr_t addr) const; |
| 151 }; | 126 }; |
| 152 | 127 |
| 153 // Returns the offset of |ptr| within the current binary. If it is not in the | 128 // Returns the offset of |ptr| within the current binary. If it is not in the |
| 154 // current binary, just return |ptr| as an integer. | 129 // current binary, just return |ptr| as an integer. |
| 155 uintptr_t GetOffset(const void* ptr) const; | 130 uintptr_t GetOffset(const void* ptr) const; |
| 156 | 131 |
| 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 | |
| 173 // Owns all unique call stack objects, which are allocated on the heap. Any | 132 // Owns all unique call stack objects, which are allocated on the heap. Any |
| 174 // other class or function that references a call stack must get it from here, | 133 // other class or function that references a call stack must get it from here, |
| 175 // but may not take ownership of the call stack object. | 134 // but may not take ownership of the call stack object. |
| 176 CallStackManager call_stack_manager_; | 135 CallStackManager call_stack_manager_; |
| 177 | 136 |
| 178 // Allocation stats. | 137 // Allocation stats. |
| 179 uint64_t num_allocs_; | 138 uint64_t num_allocs_; |
| 180 uint64_t num_frees_; | 139 uint64_t num_frees_; |
| 181 uint64_t alloc_size_; | 140 uint64_t alloc_size_; |
| 182 uint64_t free_size_; | 141 uint64_t free_size_; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 // considered a leak suspect. | 175 // considered a leak suspect. |
| 217 int call_stack_suspicion_threshold_; | 176 int call_stack_suspicion_threshold_; |
| 218 | 177 |
| 219 DISALLOW_COPY_AND_ASSIGN(LeakDetectorImpl); | 178 DISALLOW_COPY_AND_ASSIGN(LeakDetectorImpl); |
| 220 }; | 179 }; |
| 221 | 180 |
| 222 } // namespace leak_detector | 181 } // namespace leak_detector |
| 223 } // namespace metrics | 182 } // namespace metrics |
| 224 | 183 |
| 225 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_ | 184 #endif // COMPONENTS_METRICS_LEAK_DETECTOR_LEAK_DETECTOR_IMPL_H_ |
| OLD | NEW |