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/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 // By keeping a tally on heap operations, it's possible to track: | |
22 // - the number of alloc/free operations, where a realloc is zero or one | |
23 // of each, depending on the input parameters (see man realloc). | |
24 // - the number of bytes allocated/freed. | |
25 // - the number of estimated bytes of heap overhead used. | |
26 // - the high-watermark amount of bytes allocated in the scope. | |
27 // This in turn allows measuring the memory usage and memory usage churn over | |
28 // a scope. Scopes must be cleanly nested, and each scope must be | |
29 // destroyed on the thread where it's created. | |
30 // | |
31 // Note that this depends on the capabilities of the underlying heap shim. If | |
32 // that shim can not yield a size estimate for an allocation, it's not possible | |
33 // to keep track of overhead, freed bytes and the allocation high water mark. | |
34 class BASE_EXPORT ScopedThreadHeapUsage { | |
35 public: | |
36 struct ThreadAllocatorUsage { | |
37 // The cumulative number of allocation operations. | |
38 uint64_t alloc_ops; | |
39 | |
40 // The cumulative number of allocated bytes. Where available, this is | |
41 // inclusive heap padding and estimated or actual heap overhead. | |
42 uint64_t alloc_bytes; | |
43 | |
44 // Where available, cumulative number of heap padding heap | |
45 // and overhead bytes. | |
46 uint64_t alloc_overhead_bytes; | |
47 | |
48 // The cumulative number of free operations. | |
49 uint64_t free_ops; | |
50 | |
51 // The cumulative number of bytes freed. | |
52 // Only recorded if the underlying heap shim can return the size of an | |
53 // allocation. | |
54 uint64_t free_bytes; | |
55 | |
56 // The maximal value of alloc_bytes - free_bytes seen for this thread. | |
57 // Only recorded if the underlying heap shim supports returning the size of | |
58 // an allocation. | |
59 uint64_t max_allocated_bytes; | |
60 }; | |
61 | |
62 ScopedThreadHeapUsage(); | |
63 ~ScopedThreadHeapUsage(); | |
64 | |
65 const ThreadAllocatorUsage& usage_at_creation() const { | |
66 return usage_at_creation_; | |
67 } | |
68 | |
69 // Returns this thread's allocator usage from the creation of the innermost | |
70 // enclosing ScopedThreadHeapUsage instance, if any. Note that this is | |
71 // inclusive allocator usage in all inner scopes. | |
72 static ThreadAllocatorUsage CurrentUsage(); | |
73 | |
74 // Initializes the TLS machinery this class uses. Must be called before | |
75 // creating instances of this class. | |
76 static void Initialize(); | |
77 | |
78 // Enables the heap intercept. May only be called once, and only if the heap | |
79 // shim is available, e.g. if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) is | |
80 // true. | |
81 static void EnableHeapTracking(); | |
82 | |
83 protected: | |
84 // Exposed for testing only - note that it's safe to re-EnableHeapTracking() | |
85 // after calling this function in tests. | |
86 static void DisableHeapTrackingForTesting(); | |
87 | |
88 // Exposed to allow testing the shim without inserting it in the allocator | |
89 // shim chain. | |
90 static base::allocator::AllocatorDispatch* GetDispatchForTesting(); | |
91 | |
92 private: | |
93 static void EnsureTLSInitialized(); | |
94 | |
95 ThreadChecker thread_checker_; | |
96 // The allocator usage captured at creation of this instance. | |
97 ThreadAllocatorUsage usage_at_creation_; | |
98 }; | |
99 | |
100 } // namespace debug | |
101 } // namespace base | |
102 | |
103 #endif // BASE_DEBUG_SCOPED_THREAD_HEAP_USAGE_H_ | |
OLD | NEW |