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

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: Fix a brain****, may even compile now. Created 4 years, 4 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..e2f2f31559ad789ab2bd48c05ed40e7d81d26189
--- /dev/null
+++ b/base/debug/scoped_heap_usage.h
@@ -0,0 +1,80 @@
+// 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>
+
+#include "base/allocator/features.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?)
+// - the number of bytes allocated/freed.
+// - the number of estimated bytes of heap overhead used.
+// - 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. Note that scopes must be cleanly nested, and each scope must be
+// destroyed on the thread where it's created.
+// TODO(siggi): Should this perhaps leave the class declaration, cut the
Primiano Tucci (use gerrit) 2016/08/24 14:11:46 Yeah I think you really just need this in the Init
Sigurður Ásgeirsson 2016/09/01 15:18:18 Done.
+// implementation and return zeros? That would make it easier to use
+// as there wouldn't be a need to ifdef every use of the class.
+#if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM)
+class ScopedHeapUsage {
Primiano Tucci (use gerrit) 2016/08/24 14:11:46 After reading all this CL I think it would help if
Primiano Tucci (use gerrit) 2016/08/24 14:11:46 you could probably use a threadchecker to check in
Sigurður Ásgeirsson 2016/09/01 15:18:18 Good idea, done.
Sigurður Ásgeirsson 2016/09/01 15:18:18 Done.
+ public:
+ struct AllocatorUsage {
Primiano Tucci (use gerrit) 2016/08/24 14:11:46 Similarly I'd put thread in the name here as well.
Sigurður Ásgeirsson 2016/09/01 15:18:18 Done.
+ // The cumulative number of allocation operations.
+ uint64_t alloc_ops;
+ // The cumulative number of allocated bytes. Where available, this is
Primiano Tucci (use gerrit) 2016/08/24 14:11:46 nit: newlines here and below between comments and
Sigurður Ásgeirsson 2016/09/01 15:18:18 Done.
+ // inclusive heap padding and estimated or actual heap overhead.
+ uint64_t alloc_bytes;
+ // Where available, cumulative number of heap padding heap
+ // and 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.
+ // Only recorded if the underlying heap shim supports returning the size of
+ // an allocation.
+ uint64_t max_allocated_bytes;
+ };
+
+ ScopedHeapUsage();
+ ~ScopedHeapUsage();
+
+ const AllocatorUsage& usage_at_creation() const { return usage_at_creation_; }
+
+ // 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();
+
+ protected:
+ // Exposed for testing only - note that it's safe to re-Initialize() after
+ // tearing down in tests.
+ static void TearDownForTesting();
+
+ 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_;
+};
+#endif // BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM)
+
+} // namespace debug
+} // namespace base
+
+#endif // BASE_DEBUG_SCOPED_HEAP_USAGE_H_

Powered by Google App Engine
This is Rietveld 408576698