Index: base/debug/scoped_heap_usage.h |
diff --git a/base/debug/scoped_heap_usage.h b/base/debug/scoped_heap_usage.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0530333e29f47c3ca2920871a1f2472f5c1ec845 |
--- /dev/null |
+++ b/base/debug/scoped_heap_usage.h |
@@ -0,0 +1,66 @@ |
+// Copyright 2016 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 BASE_DEBUG_SCOPED_HEAP_USAGE_H_ |
+#define BASE_DEBUG_SCOPED_HEAP_USAGE_H_ |
+ |
+#include <stdint.h> |
+ |
+namespace base { |
+namespace debug { |
+ |
+// By keeping a tally on heap operations, it's possible to track: |
+// - the number of alloc/free operations (realloc is one of each?) |
chrisha
2016/07/21 14:23:31
Makes sense to count one of each, unless the reall
Sigurður Ásgeirsson
2016/08/19 18:21:50
Dunno - I guess there are degenerate cases either
|
+// - the number of bytes allocated/freed. |
+// - the number of estimated (lower bound) bytes of heap overhead used. |
chrisha
2016/07/21 14:23:31
Will this always be a lower bound, or strictly an
Sigurður Ásgeirsson
2016/08/19 18:21:49
Dunno - gotta see how that works out. I've dropped
|
+// - the high-watermark amount of bytes allocated in the scope. |
+// This in turn allows measuring the memory usage and memory usage churn over |
+// a scope. |
+class ScopedHeapUsage { |
+ public: |
+ struct AllocatorUsage { |
+ // The cumulative number of allocation operations. |
+ uint64_t alloc_ops; |
+ // The cumulative number of allocated bytes. |
+ uint64_t alloc_bytes; |
+ // The cumulative number of estimated heap overhead bytes. |
+ uint64_t alloc_overhead_bytes; |
+ |
+ // The cumulative number of free operations. |
+ uint64_t free_ops; |
+ // The cumulative number of bytes freed. |
+ // Only recorded if the underlying heap shim can return the size of an |
+ // allocation. |
+ uint64_t free_bytes; |
+ |
+ // The maximal value of alloc_bytes - free_bytes seen for this thread. |
chrisha
2016/07/21 14:23:31
Very clear comments, great!
Sigurður Ásgeirsson
2016/08/19 18:21:49
Acknowledged.
|
+ uint64_t max_allocated_bytes; |
+ }; |
+ |
+ ScopedHeapUsage(); |
+ ~ScopedHeapUsage(); |
+ |
+ const AllocatorUsage& usage_at_creation() const { return usage_at_creation_; } |
+ |
+ // TODO(siggi): Maybe add a GetUsageSinceCreation() for convenience? |
+ |
+ // Returns this thread's current allocator usage. |
+ static AllocatorUsage Now(); |
+ |
+ // Initializes the underlying heap shim. Must be called zero or one times. |
+ static void Initialize(); |
+ static void TearDownForTesting(); |
chrisha
2016/07/21 14:23:31
nit: protect the TearDownForTesting?
A comment ab
Sigurður Ásgeirsson
2016/08/19 18:21:49
Done.
|
+ |
+ private: |
+ // The allocator usage for this thread, if any. |
+ AllocatorUsage* thread_usage_; |
+ |
+ // The allocator usage captured at creation of this instance. |
+ AllocatorUsage usage_at_creation_; |
+}; |
+ |
+} // namespace debug |
+} // namespace base |
+ |
+#endif // BASE_DEBUG_SCOPED_HEAP_USAGE_H_ |