Branch data Line data Source code
1 : : // Copyright (c) 2015-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #ifndef BITCOIN_MEMUSAGE_H
6 : : #define BITCOIN_MEMUSAGE_H
7 : :
8 : : #include <indirectmap.h>
9 : : #include <prevector.h>
10 : : #include <support/allocators/pool.h>
11 : :
12 : : #include <cstdlib>
13 : : #include <list>
14 : : #include <map>
15 : : #include <memory>
16 : : #include <set>
17 : : #include <string>
18 : : #include <unordered_map>
19 : : #include <unordered_set>
20 : : #include <vector>
21 : :
22 : :
23 : : namespace memusage {
24 : :
25 : : /** Compute the total memory used by allocating alloc bytes. */
26 : : static size_t MallocUsage(size_t alloc);
27 : :
28 : : /** Dynamic memory usage for built-in types is zero. */
29 : : static inline size_t DynamicUsage(const int8_t& v) { return 0; }
30 : : static inline size_t DynamicUsage(const uint8_t& v) { return 0; }
31 : : static inline size_t DynamicUsage(const int16_t& v) { return 0; }
32 : : static inline size_t DynamicUsage(const uint16_t& v) { return 0; }
33 : : static inline size_t DynamicUsage(const int32_t& v) { return 0; }
34 : : static inline size_t DynamicUsage(const uint32_t& v) { return 0; }
35 : : static inline size_t DynamicUsage(const int64_t& v) { return 0; }
36 : : static inline size_t DynamicUsage(const uint64_t& v) { return 0; }
37 : : static inline size_t DynamicUsage(const float& v) { return 0; }
38 : : static inline size_t DynamicUsage(const double& v) { return 0; }
39 : : template<typename X> static inline size_t DynamicUsage(X * const &v) { return 0; }
40 : : template<typename X> static inline size_t DynamicUsage(const X * const &v) { return 0; }
41 : :
42 : : /** Compute the memory used for dynamically allocated but owned data structures.
43 : : * For generic data types, this is *not* recursive. DynamicUsage(vector<vector<int> >)
44 : : * will compute the memory used for the vector<int>'s, but not for the ints inside.
45 : : * This is for efficiency reasons, as these functions are intended to be fast. If
46 : : * application data structures require more accurate inner accounting, they should
47 : : * iterate themselves, or use more efficient caching + updating on modification.
48 : : */
49 : :
50 : 2012176 : static inline size_t MallocUsage(size_t alloc)
51 : : {
52 : : // Measured on libc6 2.19 on Linux.
53 [ + - # # : 977235 : if (alloc == 0) {
# # ]
[ + - + - ]
[ + - + -
+ - ]
54 : : return 0;
55 : 1708923 : } else if constexpr (sizeof(void*) == 8) {
56 : 1658018 : return ((alloc + 31) >> 4) << 4;
57 : : } else if constexpr (sizeof(void*) == 4) {
58 : : return ((alloc + 15) >> 3) << 3;
59 : : } else {
60 : : static_assert(sizeof(void*) == 4 || sizeof(void*) == 8);
61 : : }
62 : : }
63 : :
64 : : // STL data structures
65 : :
66 : : template<typename X>
67 : : struct stl_tree_node
68 : : {
69 : : int color;
70 : : void* parent;
71 : : void* left;
72 : : void* right;
73 : : X x;
74 : : };
75 : :
76 : : struct stl_shared_counter
77 : : {
78 : : /* Various platforms use different sized counters here.
79 : : * Conservatively assume that they won't be larger than size_t. */
80 : : void* class_type;
81 : : size_t use_count;
82 : : size_t weak_count;
83 : : };
84 : :
85 : : template<typename T, typename Allocator>
86 [ - + - + : 676777 : static inline size_t DynamicUsage(const std::vector<T, Allocator>& v)
- + - + ]
[ - + - +
- + ][ - -
- + # # ]
[ - + ]
87 : : {
88 [ + + - + : 993443 : return MallocUsage(v.capacity() * sizeof(T));
+ + + + +
+ ][ + + -
- - - - -
- - - - ]
[ - - + +
# # # # #
# # # ]
[ + + ]
89 : : }
90 : :
91 : 5466 : static inline size_t DynamicUsage(const std::string& s)
92 : : {
93 : 5466 : const char* s_ptr = reinterpret_cast<const char*>(&s);
94 : : // Don't count the dynamic memory used for string, if it resides in the
95 : : // "small string" optimization area (which stores data inside the object itself, up to some
96 : : // size; 15 bytes in modern libstdc++).
97 [ + + - + : 5466 : if (!std::less{}(s.data(), s_ptr) && !std::greater{}(s.data() + s.size(), s_ptr + sizeof(s))) {
+ + ]
98 : : return 0;
99 : : }
100 [ - + + - ]: 5304 : return MallocUsage(s.capacity());
101 : : }
102 : :
103 : : template<unsigned int N, typename X, typename S, typename D>
104 [ - + + + : 2726883 : static inline size_t DynamicUsage(const prevector<N, X, S, D>& v)
- + ][ + +
+ + # # ]
[ # # ][ + +
+ + - + +
+ + + + +
+ + + + +
+ + + + +
+ + - - ]
105 : : {
106 [ - - + - : 3544635 : return MallocUsage(v.allocated_memory());
- - - - ]
[ + - + -
- + # # ]
[ # # # # ]
[ + - + +
- + + - -
- - - + +
+ - + + -
+ + - - +
+ - + - +
+ - + + -
+ - - + +
- + - + -
+ - - - -
- ]
107 : : }
108 : :
109 : : template<typename X, typename Y>
110 : 175062 : static inline size_t DynamicUsage(const std::set<X, Y>& s)
111 : : {
112 : 175062 : return MallocUsage(sizeof(stl_tree_node<X>)) * s.size();
113 : : }
114 : :
115 : : template<typename X, typename Y>
116 : : static inline size_t IncrementalDynamicUsage(const std::set<X, Y>& s)
117 : : {
118 : : return MallocUsage(sizeof(stl_tree_node<X>));
119 : : }
120 : :
121 : : template<typename X, typename Y, typename Z>
122 : 87531 : static inline size_t DynamicUsage(const std::map<X, Y, Z>& m)
123 : : {
124 [ - + ]: 87531 : return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
125 : : }
126 : :
127 : : template<typename X, typename Y, typename Z>
128 : : static inline size_t IncrementalDynamicUsage(const std::map<X, Y, Z>& m)
129 : : {
130 : : return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >));
131 : : }
132 : :
133 : : // indirectmap has underlying map with pointer as key
134 : :
135 : : template<typename X, typename Y>
136 : 87531 : static inline size_t DynamicUsage(const indirectmap<X, Y>& m)
137 : : {
138 [ - + ]: 87531 : return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >)) * m.size();
139 : : }
140 : :
141 : : template<typename X, typename Y>
142 : : static inline size_t IncrementalDynamicUsage(const indirectmap<X, Y>& m)
143 : : {
144 : : return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >));
145 : : }
146 : :
147 : : template<typename X>
148 : : static inline size_t DynamicUsage(const std::unique_ptr<X>& p)
149 : : {
150 : : return p ? MallocUsage(sizeof(X)) : 0;
151 : : }
152 : :
153 : : template<typename X>
154 : : static inline size_t DynamicUsage(const std::shared_ptr<X>& p)
155 : : {
156 : : // A shared_ptr can either use a single continuous memory block for both
157 : : // the counter and the storage (when using std::make_shared), or separate.
158 : : // We can't observe the difference, however, so assume the worst.
159 : 220633 : return p ? MallocUsage(sizeof(X)) + MallocUsage(sizeof(stl_shared_counter)) : 0;
160 : : }
161 : :
162 : : template<typename X>
163 : : struct list_node
164 : : {
165 : : void* ptr_next;
166 : : void* ptr_prev;
167 : : X x;
168 : : };
169 : :
170 : : template<typename X>
171 : 491 : static inline size_t DynamicUsage(const std::list<X>& l)
172 : : {
173 : 491 : return MallocUsage(sizeof(list_node<X>)) * l.size();
174 : : }
175 : :
176 : : template<typename X>
177 : : struct unordered_node : private X
178 : : {
179 : : void* ptr;
180 : : };
181 : :
182 : : template<typename X, typename Y>
183 : : static inline size_t DynamicUsage(const std::unordered_set<X, Y>& s)
184 : : {
185 : : return MallocUsage(sizeof(unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());
186 : : }
187 : :
188 : : template<typename X, typename Y, typename Z>
189 : 495 : static inline size_t DynamicUsage(const std::unordered_map<X, Y, Z>& m)
190 : : {
191 [ + - + - : 498 : return MallocUsage(sizeof(unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());
+ - # # ]
[ + - + -
+ - + - ]
[ + - ]
192 : : }
193 : :
194 : : template <class Key, class T, class Hash, class Pred, std::size_t MAX_BLOCK_SIZE_BYTES, std::size_t ALIGN_BYTES>
195 [ + - ]: 188032 : static inline size_t DynamicUsage(const std::unordered_map<Key,
196 : : T,
197 : : Hash,
198 : : Pred,
199 : : PoolAllocator<std::pair<const Key, T>,
200 : : MAX_BLOCK_SIZE_BYTES,
201 : : ALIGN_BYTES>>& m)
202 : : {
203 : 188032 : auto* pool_resource = m.get_allocator().resource();
204 : :
205 : : // The allocated chunks are stored in a std::list. Size per node should
206 : : // therefore be 3 pointers: next, previous, and a pointer to the chunk.
207 [ + - ]: 188032 : size_t estimated_list_node_size = MallocUsage(sizeof(void*) * 3);
208 [ + - ]: 188032 : size_t usage_resource = estimated_list_node_size * pool_resource->NumAllocatedChunks();
209 [ + - + - ]: 376064 : size_t usage_chunks = MallocUsage(pool_resource->ChunkSizeBytes()) * pool_resource->NumAllocatedChunks();
210 [ + - ]: 188032 : return usage_resource + usage_chunks + MallocUsage(sizeof(void*) * m.bucket_count());
211 : : }
212 : :
213 : : } // namespace memusage
214 : :
215 : : #endif // BITCOIN_MEMUSAGE_H
|