Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(594)

Unified Diff: base/debug/scoped_heap_usage.h

Issue 2163783003: Implement a ScopedThreadHeapUsage class to allow profiling per-thread heap usage. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@shim-default
Patch Set: Improve comments, fix cosmetic problems. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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_
« no previous file with comments | « base/BUILD.gn ('k') | base/debug/scoped_heap_usage.cc » ('j') | base/debug/scoped_heap_usage.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698