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> |
| 11 #include <unordered_map> |
| 12 |
| 13 #include "components/metrics/leak_detector/leak_analyzer.h" |
| 14 #include "components/metrics/leak_detector/stl_allocator.h" |
| 15 #include <gperftools/custom_allocator.h> |
| 16 |
| 17 namespace leak_detector { |
| 18 |
| 19 // Struct to represent a call stack. |
| 20 struct CallStack { |
| 21 static const int kMaxStackDepth = 32; // Max call stack depth. |
| 22 |
| 23 uint32_t depth; // Depth of current call stack. |
| 24 const void** stack; // Call stack as an array of addrs. |
| 25 |
| 26 uint64_t hash; // Hash of call stack. |
| 27 }; |
| 28 |
| 29 // Contains a hash table where the key is the call stack and the value is the |
| 30 // number of allocations from that call stack. |
| 31 class CallStackTable { |
| 32 public: |
| 33 struct Hash { |
| 34 long operator() (const CallStack* call_stack) const; |
| 35 }; |
| 36 |
| 37 CallStackTable(int call_stack_suspicion_threshold); |
| 38 ~CallStackTable(); |
| 39 |
| 40 // Add/Remove an allocation for the given call stack. |
| 41 void Add(const CallStack* call_stack); |
| 42 void Remove(const CallStack* call_stack); |
| 43 |
| 44 // Dump contents to log buffer |buffer| of size |size|. |
| 45 int Dump(char* buffer, int size) const; |
| 46 |
| 47 // Check for leak patterns in the allocation data. |
| 48 void TestForLeaks(); |
| 49 |
| 50 const LeakAnalyzer& leak_analyzer() const { |
| 51 return leak_analyzer_; |
| 52 } |
| 53 |
| 54 private: |
| 55 // Hash table entry used to track number of allocs and frees for a call stack. |
| 56 struct Entry { |
| 57 // Number of allocs/frees for the call stack. |
| 58 uint32_t num_allocs; |
| 59 uint32_t num_frees; |
| 60 }; |
| 61 |
| 62 // The total number of recorded allocations when this object was created. This |
| 63 // acts as a timestamp of sorts. |
| 64 uint64_t total_num_allocs_at_creation_; |
| 65 |
| 66 // Total number of allocs and frees in this table. |
| 67 uint32_t num_allocs_; |
| 68 uint32_t num_frees_; |
| 69 |
| 70 // Hash table containing entries. |
| 71 using TableEntryAllocator = |
| 72 STL_Allocator<std::pair<const CallStack*, Entry>, CustomAllocator>; |
| 73 std::unordered_map<const CallStack*, |
| 74 Entry, |
| 75 Hash, |
| 76 std::equal_to<const CallStack*>, |
| 77 TableEntryAllocator> entry_map_; |
| 78 |
| 79 // For detecting leak patterns in incoming allocations. |
| 80 LeakAnalyzer leak_analyzer_; |
| 81 |
| 82 DISALLOW_COPY_AND_ASSIGN(CallStackTable); |
| 83 }; |
| 84 |
| 85 } // namespace leak_detector |
| 86 |
| 87 #endif // CALL_STACK_TABLE_H_ |
OLD | NEW |