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