1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
// Copyright (c) 2015 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_MEMUSAGE_H
#define BITCOIN_MEMUSAGE_H
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/unordered_set.hpp>
#include <boost/unordered_map.hpp>
namespace memusage
{
/** Compute the total memory used by allocating alloc bytes. */
static size_t MallocUsage(size_t alloc);
/** Dynamic memory usage for built-in types is zero. */
static inline size_t DynamicUsage(const int8_t& v) { return 0; }
static inline size_t DynamicUsage(const uint8_t& v) { return 0; }
static inline size_t DynamicUsage(const int16_t& v) { return 0; }
static inline size_t DynamicUsage(const uint16_t& v) { return 0; }
static inline size_t DynamicUsage(const int32_t& v) { return 0; }
static inline size_t DynamicUsage(const uint32_t& v) { return 0; }
static inline size_t DynamicUsage(const int64_t& v) { return 0; }
static inline size_t DynamicUsage(const uint64_t& v) { return 0; }
static inline size_t DynamicUsage(const float& v) { return 0; }
static inline size_t DynamicUsage(const double& v) { return 0; }
template<typename X> static inline size_t DynamicUsage(X * const &v) { return 0; }
template<typename X> static inline size_t DynamicUsage(const X * const &v) { return 0; }
template<typename X, typename Y> static inline size_t DynamicUsage(std::pair<X, Y> &p) { return 0; }
/** Compute the memory used for dynamically allocated but owned data structures.
* For generic data types, this is *not* recursive. DynamicUsage(vector<vector<int> >)
* will compute the memory used for the vector<int>'s, but not for the ints inside.
* This is for efficiency reasons, as these functions are intended to be fast. If
* application data structures require more accurate inner accounting, they should
* use RecursiveDynamicUsage, iterate themselves, or use more efficient caching +
* updating on modification.
*/
template<typename X> static size_t DynamicUsage(const std::vector<X>& v);
template<typename X> static size_t DynamicUsage(const std::set<X>& s);
template<typename X, typename Y> static size_t DynamicUsage(const std::map<X, Y>& m);
template<typename X, typename Y> static size_t DynamicUsage(const boost::unordered_set<X, Y>& s);
template<typename X, typename Y, typename Z> static size_t DynamicUsage(const boost::unordered_map<X, Y, Z>& s);
template<typename X> static size_t DynamicUsage(const X& x);
template<typename X> static size_t RecursiveDynamicUsage(const std::vector<X>& v);
template<typename X> static size_t RecursiveDynamicUsage(const std::set<X>& v);
template<typename X, typename Y> static size_t RecursiveDynamicUsage(const std::map<X, Y>& v);
template<typename X, typename Y> static size_t RecursiveDynamicUsage(const std::pair<X, Y>& v);
template<typename X> static size_t RecursiveDynamicUsage(const X& v);
static inline size_t MallocUsage(size_t alloc)
{
// Measured on libc6 2.19 on Linux.
if (sizeof(void*) == 8) {
return ((alloc + 31) >> 4) << 4;
} else if (sizeof(void*) == 4) {
return ((alloc + 15) >> 3) << 3;
} else {
assert(0);
}
}
// STL data structures
template<typename X>
struct stl_tree_node
{
private:
int color;
void* parent;
void* left;
void* right;
X x;
};
template<typename X>
static inline size_t DynamicUsage(const std::vector<X>& v)
{
return MallocUsage(v.capacity() * sizeof(X));
}
template<typename X>
static inline size_t RecursiveDynamicUsage(const std::vector<X>& v)
{
size_t usage = DynamicUsage(v);
BOOST_FOREACH(const X& x, v) {
usage += RecursiveDynamicUsage(x);
}
return usage;
}
template<typename X>
static inline size_t DynamicUsage(const std::set<X>& s)
{
return MallocUsage(sizeof(stl_tree_node<X>)) * s.size();
}
template<typename X>
static inline size_t RecursiveDynamicUsage(const std::set<X>& v)
{
size_t usage = DynamicUsage(v);
BOOST_FOREACH(const X& x, v) {
usage += RecursiveDynamicUsage(x);
}
return usage;
}
template<typename X, typename Y>
static inline size_t DynamicUsage(const std::map<X, Y>& m)
{
return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
}
template<typename X, typename Y>
static inline size_t RecursiveDynamicUsage(const std::map<X, Y>& v)
{
size_t usage = DynamicUsage(v);
for (typename std::map<X, Y>::const_iterator it = v.begin(); it != v.end(); it++) {
usage += RecursiveDynamicUsage(*it);
}
return usage;
}
template<typename X, typename Y>
static inline size_t RecursiveDynamicUsage(const std::pair<X, Y>& v)
{
return RecursiveDynamicUsage(v.first) + RecursiveDynamicUsage(v.second);
}
// Boost data structures
template<typename X>
struct boost_unordered_node : private X
{
private:
void* ptr;
};
template<typename X, typename Y>
static inline size_t DynamicUsage(const boost::unordered_set<X, Y>& s)
{
return MallocUsage(sizeof(boost_unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());
}
template<typename X, typename Y, typename Z>
static inline size_t DynamicUsage(const boost::unordered_map<X, Y, Z>& m)
{
return MallocUsage(sizeof(boost_unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());
}
// Dispatch to class method as fallback
template<typename X>
static inline size_t DynamicUsage(const X& x)
{
return x.DynamicMemoryUsage();
}
template<typename X>
static inline size_t RecursiveDynamicUsage(const X& x)
{
return DynamicUsage(x);
}
}
#endif
|