Chromium Code Reviews| 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_THREAD_HEAP_USAGE_TRACKER_H_ | |
| 6 #define BASE_DEBUG_THREAD_HEAP_USAGE_TRACKER_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include "base/allocator/features.h" | |
| 11 #include "base/base_export.h" | |
| 12 #include "base/threading/thread_checker.h" | |
| 13 | |
| 14 namespace base { | |
| 15 namespace allocator { | |
| 16 struct AllocatorDispatch; | |
| 17 } // namespace allocator | |
| 18 | |
| 19 namespace debug { | |
| 20 | |
| 21 // Used to store the heap allocator usage in a scope. | |
| 22 struct ThreadHeapUsage { | |
| 23 // The cumulative number of allocation operations. | |
| 24 uint64_t alloc_ops; | |
| 25 | |
| 26 // The cumulative number of allocated bytes. Where available, this is | |
| 27 // inclusive heap padding and estimated or actual heap overhead. | |
| 28 uint64_t alloc_bytes; | |
| 29 | |
| 30 // Where available, cumulative number of heap padding heap | |
|
chrisha
2016/10/17 19:17:48
heap padding heap?
Sigurður Ásgeirsson
2016/10/17 19:35:23
Done.
| |
| 31 // and overhead bytes. | |
| 32 uint64_t alloc_overhead_bytes; | |
| 33 | |
| 34 // The cumulative number of free operations. | |
| 35 uint64_t free_ops; | |
| 36 | |
| 37 // The cumulative number of bytes freed. | |
| 38 // Only recorded if the underlying heap shim can return the size of an | |
| 39 // allocation. | |
| 40 uint64_t free_bytes; | |
| 41 | |
| 42 // The maximal value of alloc_bytes - free_bytes seen for this thread. | |
| 43 // Only recorded if the underlying heap shim supports returning the size of | |
| 44 // an allocation. | |
| 45 uint64_t max_allocated_bytes; | |
| 46 }; | |
| 47 | |
| 48 // By keeping a tally on heap operations, it's possible to track: | |
| 49 // - the number of alloc/free operations, where a realloc is zero or one | |
| 50 // of each, depending on the input parameters (see man realloc). | |
| 51 // - the number of bytes allocated/freed. | |
| 52 // - the number of estimated bytes of heap overhead used. | |
| 53 // - the high-watermark amount of bytes allocated in the scope. | |
| 54 // This in turn allows measuring the memory usage and memory usage churn over | |
| 55 // a scope. Scopes must be cleanly nested, and each scope must be | |
| 56 // destroyed on the thread where it's created. | |
| 57 // | |
| 58 // Note that this depends on the capabilities of the underlying heap shim. If | |
| 59 // that shim can not yield a size estimate for an allocation, it's not possible | |
| 60 // to keep track of overhead, freed bytes and the allocation high water mark. | |
| 61 class BASE_EXPORT ThreadHeapUsageTracker { | |
| 62 public: | |
| 63 ThreadHeapUsageTracker(); | |
| 64 ~ThreadHeapUsageTracker(); | |
| 65 | |
| 66 // Start tracking heap usage on this thread. | |
| 67 // Note |IsHeapTrackingEnabled()| must be true. | |
| 68 void Start(); | |
| 69 | |
| 70 // Stop tracking heap usage on this thread and store the usage tallied. | |
| 71 // If |usage_is_exclusive| is true, the usage tallied won't be added to the | |
| 72 // outer scope's usage. If |usage_is_exclusive" is false, the usage tallied | |
|
chrisha
2016/10/17 19:17:48
|usage_is_exclusive"
Sigurður Ásgeirsson
2016/10/17 19:35:23
Done.
| |
| 73 // in this scope will also tally to any outer scope. | |
| 74 void Stop(bool usage_is_exclusive); | |
| 75 | |
| 76 // After Stop() returns the usage tallied from Start() to Stop(). | |
| 77 const ThreadHeapUsage& usage() const { return usage_; } | |
| 78 | |
| 79 // Returns this thread's allocator usage from the start of the innermost | |
| 80 // enclosing ThreadHeapUsageTracker instance, if any. | |
| 81 static ThreadHeapUsage CurrentUsage(); | |
| 82 | |
| 83 // Enables the heap intercept. May only be called once, and only if the heap | |
| 84 // shim is available, e.g. if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) is | |
| 85 // true. | |
| 86 static void EnableHeapTracking(); | |
| 87 | |
| 88 // Returns true iff heap tracking is enabled. | |
| 89 static bool IsHeapTrackingEnabled(); | |
| 90 | |
| 91 protected: | |
| 92 // Exposed for testing only - note that it's safe to re-EnableHeapTracking() | |
| 93 // after calling this function in tests. | |
| 94 static void DisableHeapTrackingForTesting(); | |
| 95 | |
| 96 // Exposed for testing only. | |
| 97 static void EnsureTLSInitializedForTesting(); | |
| 98 | |
| 99 // Exposed to allow testing the shim without inserting it in the allocator | |
| 100 // shim chain. | |
| 101 static base::allocator::AllocatorDispatch* GetDispatchForTesting(); | |
| 102 | |
| 103 private: | |
| 104 static void EnsureTLSInitialized(); | |
| 105 | |
| 106 ThreadChecker thread_checker_; | |
| 107 | |
| 108 // The allocator usage at Start(), or the difference from Start() to Stop(). | |
| 109 ThreadHeapUsage usage_; | |
| 110 | |
| 111 // This thread's allocator usage, non-null from Start() to Stop(). | |
| 112 ThreadHeapUsage* thread_usage_; | |
| 113 }; | |
| 114 | |
| 115 } // namespace debug | |
| 116 } // namespace base | |
| 117 | |
| 118 #endif // BASE_DEBUG_THREAD_HEAP_USAGE_TRACKER_H_ | |
| OLD | NEW |