| 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
|
| index a843fc93d0c41398e8e25d68acc9480a78d9d3d3..d5fd805171ae33e6d26791a64cdd165a8e97110c 100644
|
| --- a/base/debug/scoped_thread_heap_usage.h
|
| +++ b/base/debug/scoped_thread_heap_usage.h
|
| @@ -18,6 +18,33 @@ struct AllocatorDispatch;
|
|
|
| namespace debug {
|
|
|
| +// Used to store the heap allocator usage in a scope.
|
| +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;
|
| +};
|
| +
|
| // 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 +58,37 @@ 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 HeapUsageTracker {
|
| 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;
|
| + HeapUsageTracker();
|
| + ~HeapUsageTracker();
|
|
|
| - // 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;
|
| - };
|
| + void Start();
|
| + void Stop(bool usage_is_exclusive);
|
|
|
| - ScopedThreadHeapUsage();
|
| - ~ScopedThreadHeapUsage();
|
| -
|
| - const ThreadAllocatorUsage& usage_at_creation() const {
|
| - return usage_at_creation_;
|
| - }
|
| + const ThreadAllocatorUsage& usage() const { return usage_; }
|
|
|
| // 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();
|
| -
|
| // 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,8 +97,12 @@ 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().
|
| + ThreadAllocatorUsage usage_;
|
| +
|
| + // This thread's allocator usage, non-null from Start() to Stop().
|
| + ThreadAllocatorUsage* thread_usage_;
|
| };
|
|
|
| } // namespace debug
|
|
|