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