OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_DEBUG_SCOPED_THREAD_HEAP_USAGE_H_ | 5 #ifndef BASE_DEBUG_SCOPED_THREAD_HEAP_USAGE_H_ |
6 #define BASE_DEBUG_SCOPED_THREAD_HEAP_USAGE_H_ | 6 #define BASE_DEBUG_SCOPED_THREAD_HEAP_USAGE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/allocator/features.h" | 10 #include "base/allocator/features.h" |
11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
12 #include "base/threading/thread_checker.h" | 12 #include "base/threading/thread_checker.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 namespace allocator { | 15 namespace allocator { |
16 struct AllocatorDispatch; | 16 struct AllocatorDispatch; |
17 } // namespace allocator | 17 } // namespace allocator |
18 | 18 |
19 namespace debug { | 19 namespace debug { |
20 | 20 |
| 21 // Used to store the heap allocator usage in a scope. |
| 22 struct ThreadAllocatorUsage { |
| 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 |
| 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 |
21 // By keeping a tally on heap operations, it's possible to track: | 48 // 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 | 49 // - the number of alloc/free operations, where a realloc is zero or one |
23 // of each, depending on the input parameters (see man realloc). | 50 // of each, depending on the input parameters (see man realloc). |
24 // - the number of bytes allocated/freed. | 51 // - the number of bytes allocated/freed. |
25 // - the number of estimated bytes of heap overhead used. | 52 // - the number of estimated bytes of heap overhead used. |
26 // - the high-watermark amount of bytes allocated in the scope. | 53 // - the high-watermark amount of bytes allocated in the scope. |
27 // This in turn allows measuring the memory usage and memory usage churn over | 54 // 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 | 55 // a scope. Scopes must be cleanly nested, and each scope must be |
29 // destroyed on the thread where it's created. | 56 // destroyed on the thread where it's created. |
30 // | 57 // |
31 // Note that this depends on the capabilities of the underlying heap shim. If | 58 // 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 | 59 // 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. | 60 // to keep track of overhead, freed bytes and the allocation high water mark. |
34 class BASE_EXPORT ScopedThreadHeapUsage { | 61 class BASE_EXPORT HeapUsageTracker { |
35 public: | 62 public: |
36 struct ThreadAllocatorUsage { | 63 HeapUsageTracker(); |
37 // The cumulative number of allocation operations. | 64 ~HeapUsageTracker(); |
38 uint64_t alloc_ops; | |
39 | 65 |
40 // The cumulative number of allocated bytes. Where available, this is | 66 void Start(); |
41 // inclusive heap padding and estimated or actual heap overhead. | 67 void Stop(bool usage_is_exclusive); |
42 uint64_t alloc_bytes; | |
43 | 68 |
44 // Where available, cumulative number of heap padding heap | 69 const ThreadAllocatorUsage& usage() const { return usage_; } |
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 | 70 |
69 // Returns this thread's allocator usage from the creation of the innermost | 71 // Returns this thread's allocator usage from the creation of the innermost |
70 // enclosing ScopedThreadHeapUsage instance, if any. Note that this is | 72 // enclosing ScopedThreadHeapUsage instance, if any. Note that this is |
71 // inclusive allocator usage in all inner scopes. | 73 // inclusive allocator usage in all inner scopes. |
72 static ThreadAllocatorUsage CurrentUsage(); | 74 static ThreadAllocatorUsage CurrentUsage(); |
73 | 75 |
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 | 76 // 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 | 77 // shim is available, e.g. if BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) is |
80 // true. | 78 // true. |
81 static void EnableHeapTracking(); | 79 static void EnableHeapTracking(); |
82 | 80 |
| 81 // Returns true iff heap tracking is enabled. |
| 82 static bool IsHeapTrackingEnabled(); |
| 83 |
83 protected: | 84 protected: |
84 // Exposed for testing only - note that it's safe to re-EnableHeapTracking() | 85 // Exposed for testing only - note that it's safe to re-EnableHeapTracking() |
85 // after calling this function in tests. | 86 // after calling this function in tests. |
86 static void DisableHeapTrackingForTesting(); | 87 static void DisableHeapTrackingForTesting(); |
87 | 88 |
| 89 // Exposed for testing only. |
| 90 static void EnsureTLSInitializedForTesting(); |
| 91 |
88 // Exposed to allow testing the shim without inserting it in the allocator | 92 // Exposed to allow testing the shim without inserting it in the allocator |
89 // shim chain. | 93 // shim chain. |
90 static base::allocator::AllocatorDispatch* GetDispatchForTesting(); | 94 static base::allocator::AllocatorDispatch* GetDispatchForTesting(); |
91 | 95 |
92 private: | 96 private: |
93 static void EnsureTLSInitialized(); | 97 static void EnsureTLSInitialized(); |
94 | 98 |
95 ThreadChecker thread_checker_; | 99 ThreadChecker thread_checker_; |
96 // The allocator usage captured at creation of this instance. | 100 |
97 ThreadAllocatorUsage usage_at_creation_; | 101 // The allocator usage at Start(), or the difference from Start() to Stop(). |
| 102 ThreadAllocatorUsage usage_; |
| 103 |
| 104 // This thread's allocator usage, non-null from Start() to Stop(). |
| 105 ThreadAllocatorUsage* thread_usage_; |
98 }; | 106 }; |
99 | 107 |
100 } // namespace debug | 108 } // namespace debug |
101 } // namespace base | 109 } // namespace base |
102 | 110 |
103 #endif // BASE_DEBUG_SCOPED_THREAD_HEAP_USAGE_H_ | 111 #endif // BASE_DEBUG_SCOPED_THREAD_HEAP_USAGE_H_ |
OLD | NEW |