Index: components/metrics/leak_detector/call_stack_table.h |
diff --git a/components/metrics/leak_detector/call_stack_table.h b/components/metrics/leak_detector/call_stack_table.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ab0972a1a265d0b16aa597a246863b0b9debfdc8 |
--- /dev/null |
+++ b/components/metrics/leak_detector/call_stack_table.h |
@@ -0,0 +1,87 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef COMPONENTS_METRICS_LEAK_DETECTOR_CALL_STACK_TABLE_H_ |
+#define COMPONENTS_METRICS_LEAK_DETECTOR_CALL_STACK_TABLE_H_ |
+ |
+#include <stdint.h> |
+ |
+#include <functional> |
+#include <unordered_map> |
+ |
+#include "components/metrics/leak_detector/leak_analyzer.h" |
+#include "components/metrics/leak_detector/stl_allocator.h" |
+#include <gperftools/custom_allocator.h> |
+ |
+namespace leak_detector { |
+ |
+// Struct to represent a call stack. |
+struct CallStack { |
+ static const int kMaxStackDepth = 32; // Max call stack depth. |
+ |
+ uint32_t depth; // Depth of current call stack. |
+ const void** stack; // Call stack as an array of addrs. |
+ |
+ uint64_t hash; // Hash of call stack. |
+}; |
+ |
+// Contains a hash table where the key is the call stack and the value is the |
+// number of allocations from that call stack. |
+class CallStackTable { |
+ public: |
+ struct Hash { |
+ long operator() (const CallStack* call_stack) const; |
jar (doing other things)
2015/08/15 03:29:01
nit: Style: Structs should only be used to hold pa
Simon Que
2015/08/15 23:28:30
Then it would need a "public:" access specifier. I
jar (doing other things)
2015/08/21 02:48:42
I'm not used to seeing this... but a readability r
|
+ }; |
+ |
+ CallStackTable(int call_stack_suspicion_threshold); |
jar (doing other things)
2015/08/15 03:29:01
nit: explicit
Simon Que
2015/08/15 23:28:30
Done.
Simon Que
2015/08/18 22:34:04
Also done in ranked_list.h
|
+ ~CallStackTable(); |
+ |
+ // Add/Remove an allocation for the given call stack. |
+ void Add(const CallStack* call_stack); |
+ void Remove(const CallStack* call_stack); |
+ |
+ // Dump contents to log buffer |buffer| of size |size|. |
+ int Dump(char* buffer, int size) const; |
jar (doing other things)
2015/08/15 03:29:01
nit: define meaning of return value.
Simon Que
2015/08/15 23:28:30
Done.
|
+ |
+ // Check for leak patterns in the allocation data. |
+ void TestForLeaks(); |
+ |
+ const LeakAnalyzer& leak_analyzer() const { |
+ return leak_analyzer_; |
+ } |
+ |
+ private: |
+ // Hash table entry used to track number of allocs and frees for a call stack. |
+ struct Entry { |
+ // Number of allocs/frees for the call stack. |
+ uint32_t num_allocs; |
+ uint32_t num_frees; |
+ }; |
+ |
+ // The total number of recorded allocations when this object was created. This |
+ // acts as a timestamp of sorts. |
+ uint64_t total_num_allocs_at_creation_; |
+ |
+ // Total number of allocs and frees in this table. |
+ uint32_t num_allocs_; |
+ uint32_t num_frees_; |
+ |
+ // Hash table containing entries. |
+ using TableEntryAllocator = |
+ STL_Allocator<std::pair<const CallStack*, Entry>, CustomAllocator>; |
+ std::unordered_map<const CallStack*, |
+ Entry, |
+ Hash, |
+ std::equal_to<const CallStack*>, |
+ TableEntryAllocator> entry_map_; |
+ |
+ // For detecting leak patterns in incoming allocations. |
+ LeakAnalyzer leak_analyzer_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(CallStackTable); |
+}; |
+ |
+} // namespace leak_detector |
+ |
+#endif // CALL_STACK_TABLE_H_ |