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

Unified Diff: base/debug/thread_heap_usage_tracker.h

Issue 2427503003: Rename ScopedThreadHeapUsage to ThreadHeapUsageTracker. (Closed)
Patch Set: Address Lei Zhang's comments, fix leak in test. Created 4 years, 2 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/thread_heap_usage_tracker.h
diff --git a/base/debug/scoped_thread_heap_usage.h b/base/debug/thread_heap_usage_tracker.h
similarity index 46%
rename from base/debug/scoped_thread_heap_usage.h
rename to base/debug/thread_heap_usage_tracker.h
index a843fc93d0c41398e8e25d68acc9480a78d9d3d3..39f45d506d8d9f0b5c920b3f03d012ae9ca0cebe 100644
--- a/base/debug/scoped_thread_heap_usage.h
+++ b/base/debug/thread_heap_usage_tracker.h
@@ -2,8 +2,8 @@
// 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_
+#ifndef BASE_DEBUG_THREAD_HEAP_USAGE_TRACKER_H_
+#define BASE_DEBUG_THREAD_HEAP_USAGE_TRACKER_H_
#include <stdint.h>
@@ -18,6 +18,32 @@ struct AllocatorDispatch;
namespace debug {
+// Used to store the heap allocator usage in a scope.
+struct ThreadHeapUsage {
+ // 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 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;
+};
+
// 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).
@@ -31,60 +57,44 @@ namespace debug {
// 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 not possible
// to keep track of overhead, freed bytes and the allocation high water mark.
-class BASE_EXPORT ScopedThreadHeapUsage {
+class BASE_EXPORT ThreadHeapUsageTracker {
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;
+ ThreadHeapUsageTracker();
+ ~ThreadHeapUsageTracker();
- // Where available, cumulative number of heap padding heap
- // and overhead bytes.
- uint64_t alloc_overhead_bytes;
+ // Start tracking heap usage on this thread.
+ // Note IsHeapTrackingEnabled() must be true.
+ void Start();
- // The cumulative number of free operations.
- uint64_t free_ops;
+ // Stop tracking heap usage on this thread and store the usage tallied.
+ // If |usage_is_exclusive| is true, the usage tallied won't be added to the
+ // outer scope's usage. If |usage_is_exclusive| is false, the usage tallied
+ // in this scope will also tally to any outer scope.
+ void Stop(bool usage_is_exclusive);
Primiano Tucci (use gerrit) 2016/10/18 15:11:13 maybe mention that this is expected to be called o
Sigurður Ásgeirsson 2016/10/18 15:48:40 Done.
- // The cumulative number of bytes freed.
- // Only recorded if the underlying heap shim can return the size of an
- // allocation.
- uint64_t free_bytes;
+ // After Stop() returns the usage tallied from Start() to Stop().
+ const ThreadHeapUsage& usage() const { return usage_; }
- // 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 allocator usage from the creation of the innermost
- // enclosing ScopedThreadHeapUsage instance, if any. Note that this is
- // inclusive allocator usage in all inner scopes.
- static ThreadAllocatorUsage CurrentUsage();
-
- // Initializes the TLS machinery this class uses. Must be called before
- // creating instances of this class.
- static void Initialize();
+ // Returns this thread's allocator usage from the start of the innermost
+ // enclosing ThreadHeapUsageTracker instance, if any.
+ static ThreadHeapUsage CurrentUsage();
Primiano Tucci (use gerrit) 2016/10/18 15:11:14 I see the difference between these two, but the tw
Sigurður Ásgeirsson 2016/10/18 15:48:40 Done.
// Enables the heap intercept. May only be called once, and only if the heap
// shim is available, e.g. if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) is
// true.
static void EnableHeapTracking();
+ // Returns true iff heap tracking is enabled.
+ static bool IsHeapTrackingEnabled();
+
protected:
// Exposed for testing only - note that it's safe to re-EnableHeapTracking()
// after calling this function in tests.
static void DisableHeapTrackingForTesting();
+ // Exposed for testing only.
+ static void EnsureTLSInitializedForTesting();
+
// Exposed to allow testing the shim without inserting it in the allocator
// shim chain.
static base::allocator::AllocatorDispatch* GetDispatchForTesting();
@@ -93,11 +103,15 @@ class BASE_EXPORT ScopedThreadHeapUsage {
static void EnsureTLSInitialized();
ThreadChecker thread_checker_;
- // The allocator usage captured at creation of this instance.
- ThreadAllocatorUsage usage_at_creation_;
+
+ // The allocator usage at Start(), or the difference from Start() to Stop().
+ ThreadHeapUsage usage_;
+
+ // This thread's allocator usage, non-null from Start() to Stop().
+ ThreadHeapUsage* thread_usage_;
};
} // namespace debug
} // namespace base
-#endif // BASE_DEBUG_SCOPED_THREAD_HEAP_USAGE_H_
+#endif // BASE_DEBUG_THREAD_HEAP_USAGE_TRACKER_H_

Powered by Google App Engine
This is Rietveld 408576698