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

Unified Diff: base/debug/scoped_thread_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: Change Init implementation to return a bool status. ::Init is now also tested when the shim is disa… Created 4 years, 3 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_thread_heap_usage.h
diff --git a/base/debug/scoped_thread_heap_usage.h b/base/debug/scoped_thread_heap_usage.h
new file mode 100644
index 0000000000000000000000000000000000000000..4872c1ca92434b9162d109236209da94e02a370e
--- /dev/null
+++ b/base/debug/scoped_thread_heap_usage.h
@@ -0,0 +1,98 @@
+// 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_THREAD_HEAP_USAGE_H_
+#define BASE_DEBUG_SCOPED_THREAD_HEAP_USAGE_H_
+
+#include <stdint.h>
+
+#include "base/allocator/features.h"
+#include "base/threading/thread_checker.h"
+
+namespace base {
+namespace allocator {
+struct AllocatorDispatch;
+} // namespace allocator
+
+namespace debug {
+
+// By keeping a tally on heap operations, it's possible to track:
+// - the number of alloc/free operations, where a realloc is zero or one
+// of each, depending on the input parameters (see man realloc).
+// - 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. Scopes must be cleanly nested, and each scope must be
+// destroyed on the thread where it's created.
+//
+// Note that this depends on the capabilities of the underlying heap shim. If
+// that shim can not yield a size estimate for an allocation, it's impossible to
+// keep track of overhead, freed bytes and the allocation high water mark.
+class BASE_EXPORT ScopedThreadHeapUsage {
+ public:
+ struct ThreadAllocatorUsage {
+ // The cumulative number of allocation operations.
+ uint64_t alloc_ops;
+
+ // The cumulative number of allocated bytes. Where available, this is
+ // 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;
+ };
+
+ ScopedThreadHeapUsage();
+ ~ScopedThreadHeapUsage();
+
+ const ThreadAllocatorUsage& usage_at_creation() const {
+ return usage_at_creation_;
+ }
+
+ // Returns this thread's current allocator usage.
+ static ThreadAllocatorUsage Now();
+
+ // Initializes the underlying heap shim. Must be called precisely once,
+ // before an instance of this class is instantiated. Returns true on success,
+ // or false on failure. Failure means heap usage statistics will be
+ // unavailable, but instances of this class can still be instantiated.
+ static bool Initialize();
+
+ protected:
+ // Exposed for testing only - note that it's safe to re-Initialize() after
+ // tearing down in tests.
+ static void TearDownForTesting();
+
+ // Exposed to allow testing the shim without inserting it in the allocator
+ // shim chain.
+ static void EnsureTLSInitializedForTesting();
+ static base::allocator::AllocatorDispatch* GetDispatchForTesting();
+
+ private:
+ static void EnsureTLSInitialized();
+
+ ThreadChecker thread_checker_;
+ // The allocator usage captured at creation of this instance.
+ ThreadAllocatorUsage usage_at_creation_;
+};
+
+} // namespace debug
+} // namespace base
+
+#endif // BASE_DEBUG_SCOPED_THREAD_HEAP_USAGE_H_

Powered by Google App Engine
This is Rietveld 408576698