OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef BASE_DEBUG_SCOPED_THREAD_HEAP_USAGE_H_ |
| 6 #define BASE_DEBUG_SCOPED_THREAD_HEAP_USAGE_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include "base/allocator/features.h" |
| 11 #include "base/threading/thread_checker.h" |
| 12 |
| 13 namespace base { |
| 14 namespace allocator { |
| 15 struct AllocatorDispatch; |
| 16 } // namespace allocator |
| 17 |
| 18 namespace debug { |
| 19 |
| 20 // By keeping a tally on heap operations, it's possible to track: |
| 21 // - the number of alloc/free operations, where a realloc is zero or one |
| 22 // of each, depending on the input parameters (see man realloc). |
| 23 // - the number of bytes allocated/freed. |
| 24 // - the number of estimated bytes of heap overhead used. |
| 25 // - the high-watermark amount of bytes allocated in the scope. |
| 26 // This in turn allows measuring the memory usage and memory usage churn over |
| 27 // a scope. Scopes must be cleanly nested, and each scope must be |
| 28 // destroyed on the thread where it's created. |
| 29 // |
| 30 // Note that this depends on the capabilities of the underlying heap shim. If |
| 31 // that shim can not yield a size estimate for an allocation, it's impossible to |
| 32 // keep track of overhead, freed bytes and the allocation high water mark. |
| 33 class BASE_EXPORT ScopedThreadHeapUsage { |
| 34 public: |
| 35 struct ThreadAllocatorUsage { |
| 36 // The cumulative number of allocation operations. |
| 37 uint64_t alloc_ops; |
| 38 |
| 39 // The cumulative number of allocated bytes. Where available, this is |
| 40 // inclusive heap padding and estimated or actual heap overhead. |
| 41 uint64_t alloc_bytes; |
| 42 |
| 43 // Where available, cumulative number of heap padding heap |
| 44 // and overhead bytes. |
| 45 uint64_t alloc_overhead_bytes; |
| 46 |
| 47 // The cumulative number of free operations. |
| 48 uint64_t free_ops; |
| 49 |
| 50 // The cumulative number of bytes freed. |
| 51 // Only recorded if the underlying heap shim can return the size of an |
| 52 // allocation. |
| 53 uint64_t free_bytes; |
| 54 |
| 55 // The maximal value of alloc_bytes - free_bytes seen for this thread. |
| 56 // Only recorded if the underlying heap shim supports returning the size of |
| 57 // an allocation. |
| 58 uint64_t max_allocated_bytes; |
| 59 }; |
| 60 |
| 61 ScopedThreadHeapUsage(); |
| 62 ~ScopedThreadHeapUsage(); |
| 63 |
| 64 const ThreadAllocatorUsage& usage_at_creation() const { |
| 65 return usage_at_creation_; |
| 66 } |
| 67 |
| 68 // Returns this thread's current allocator usage. |
| 69 static ThreadAllocatorUsage Now(); |
| 70 |
| 71 // Initializes the underlying heap shim. Must be called precisely once, |
| 72 // before an instance of this class is instantiated. Returns true on success, |
| 73 // or false on failure. Failure means heap usage statistics will be |
| 74 // unavailable, but instances of this class can still be instantiated. |
| 75 static bool Initialize(); |
| 76 |
| 77 protected: |
| 78 // Exposed for testing only - note that it's safe to re-Initialize() after |
| 79 // tearing down in tests. |
| 80 static void TearDownForTesting(); |
| 81 |
| 82 // Exposed to allow testing the shim without inserting it in the allocator |
| 83 // shim chain. |
| 84 static void EnsureTLSInitializedForTesting(); |
| 85 static base::allocator::AllocatorDispatch* GetDispatchForTesting(); |
| 86 |
| 87 private: |
| 88 static void EnsureTLSInitialized(); |
| 89 |
| 90 ThreadChecker thread_checker_; |
| 91 // The allocator usage captured at creation of this instance. |
| 92 ThreadAllocatorUsage usage_at_creation_; |
| 93 }; |
| 94 |
| 95 } // namespace debug |
| 96 } // namespace base |
| 97 |
| 98 #endif // BASE_DEBUG_SCOPED_THREAD_HEAP_USAGE_H_ |
OLD | NEW |