1 // Copyright (c) 2015-2018 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
10 #include <stdlib.h>
11
12 #include <map>
13 #include <memory>
14 #include <set>
15 #include <vector>
16 #include <unordered_map>
17 #include <unordered_set>
18
19
20 namespace memusage
21 {
22
23 /** Compute the total memory used by allocating alloc bytes. */
24 static size_t MallocUsage(size_t alloc);
25
26 /** Dynamic memory usage for built-in types is zero. */
DynamicUsage(const int8_t & v)27 static inline size_t DynamicUsage(const int8_t& v) { return 0; }
DynamicUsage(const uint8_t & v)28 static inline size_t DynamicUsage(const uint8_t& v) { return 0; }
DynamicUsage(const int16_t & v)29 static inline size_t DynamicUsage(const int16_t& v) { return 0; }
DynamicUsage(const uint16_t & v)30 static inline size_t DynamicUsage(const uint16_t& v) { return 0; }
DynamicUsage(const int32_t & v)31 static inline size_t DynamicUsage(const int32_t& v) { return 0; }
DynamicUsage(const uint32_t & v)32 static inline size_t DynamicUsage(const uint32_t& v) { return 0; }
DynamicUsage(const int64_t & v)33 static inline size_t DynamicUsage(const int64_t& v) { return 0; }
DynamicUsage(const uint64_t & v)34 static inline size_t DynamicUsage(const uint64_t& v) { return 0; }
DynamicUsage(const float & v)35 static inline size_t DynamicUsage(const float& v) { return 0; }
DynamicUsage(const double & v)36 static inline size_t DynamicUsage(const double& v) { return 0; }
DynamicUsage(X * const & v)37 template<typename X> static inline size_t DynamicUsage(X * const &v) { return 0; }
DynamicUsage(const X * const & v)38 template<typename X> static inline size_t DynamicUsage(const X * const &v) { return 0; }
39
40 /** Compute the memory used for dynamically allocated but owned data structures.
41 * For generic data types, this is *not* recursive. DynamicUsage(vector<vector<int> >)
42 * will compute the memory used for the vector<int>'s, but not for the ints inside.
43 * This is for efficiency reasons, as these functions are intended to be fast. If
44 * application data structures require more accurate inner accounting, they should
45 * iterate themselves, or use more efficient caching + updating on modification.
46 */
47
MallocUsage(size_t alloc)48 static inline size_t MallocUsage(size_t alloc)
49 {
50 // Measured on libc6 2.19 on Linux.
51 if (alloc == 0) {
52 return 0;
53 } else if (sizeof(void*) == 8) {
54 return ((alloc + 31) >> 4) << 4;
55 } else if (sizeof(void*) == 4) {
56 return ((alloc + 15) >> 3) << 3;
57 } else {
58 assert(0);
59 }
60 }
61
62 // STL data structures
63
64 template<typename X>
65 struct stl_tree_node
66 {
67 private:
68 int color;
69 void* parent;
70 void* left;
71 void* right;
72 X x;
73 };
74
75 struct stl_shared_counter
76 {
77 /* Various platforms use different sized counters here.
78 * Conservatively assume that they won't be larger than size_t. */
79 void* class_type;
80 size_t use_count;
81 size_t weak_count;
82 };
83
84 template<typename X>
DynamicUsage(const std::vector<X> & v)85 static inline size_t DynamicUsage(const std::vector<X>& v)
86 {
87 return MallocUsage(v.capacity() * sizeof(X));
88 }
89
90 template<unsigned int N, typename X, typename S, typename D>
DynamicUsage(const prevector<N,X,S,D> & v)91 static inline size_t DynamicUsage(const prevector<N, X, S, D>& v)
92 {
93 return MallocUsage(v.allocated_memory());
94 }
95
96 template<typename X, typename Y>
DynamicUsage(const std::set<X,Y> & s)97 static inline size_t DynamicUsage(const std::set<X, Y>& s)
98 {
99 return MallocUsage(sizeof(stl_tree_node<X>)) * s.size();
100 }
101
102 template<typename X, typename Y>
IncrementalDynamicUsage(const std::set<X,Y> & s)103 static inline size_t IncrementalDynamicUsage(const std::set<X, Y>& s)
104 {
105 return MallocUsage(sizeof(stl_tree_node<X>));
106 }
107
108 template<typename X, typename Y, typename Z>
DynamicUsage(const std::map<X,Y,Z> & m)109 static inline size_t DynamicUsage(const std::map<X, Y, Z>& m)
110 {
111 return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
112 }
113
114 template<typename X, typename Y, typename Z>
IncrementalDynamicUsage(const std::map<X,Y,Z> & m)115 static inline size_t IncrementalDynamicUsage(const std::map<X, Y, Z>& m)
116 {
117 return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >));
118 }
119
120 // indirectmap has underlying map with pointer as key
121
122 template<typename X, typename Y>
DynamicUsage(const indirectmap<X,Y> & m)123 static inline size_t DynamicUsage(const indirectmap<X, Y>& m)
124 {
125 return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >)) * m.size();
126 }
127
128 template<typename X, typename Y>
IncrementalDynamicUsage(const indirectmap<X,Y> & m)129 static inline size_t IncrementalDynamicUsage(const indirectmap<X, Y>& m)
130 {
131 return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >));
132 }
133
134 template<typename X>
DynamicUsage(const std::unique_ptr<X> & p)135 static inline size_t DynamicUsage(const std::unique_ptr<X>& p)
136 {
137 return p ? MallocUsage(sizeof(X)) : 0;
138 }
139
140 template<typename X>
DynamicUsage(const std::shared_ptr<X> & p)141 static inline size_t DynamicUsage(const std::shared_ptr<X>& p)
142 {
143 // A shared_ptr can either use a single continuous memory block for both
144 // the counter and the storage (when using std::make_shared), or separate.
145 // We can't observe the difference, however, so assume the worst.
146 return p ? MallocUsage(sizeof(X)) + MallocUsage(sizeof(stl_shared_counter)) : 0;
147 }
148
149 template<typename X>
150 struct unordered_node : private X
151 {
152 private:
153 void* ptr;
154 };
155
156 template<typename X, typename Y>
DynamicUsage(const std::unordered_set<X,Y> & s)157 static inline size_t DynamicUsage(const std::unordered_set<X, Y>& s)
158 {
159 return MallocUsage(sizeof(unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());
160 }
161
162 template<typename X, typename Y, typename Z>
DynamicUsage(const std::unordered_map<X,Y,Z> & m)163 static inline size_t DynamicUsage(const std::unordered_map<X, Y, Z>& m)
164 {
165 return MallocUsage(sizeof(unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());
166 }
167
168 }
169
170 #endif // BITCOIN_MEMUSAGE_H
171