2016-11-07 12:51:36 +01:00
|
|
|
// Copyright (c) 2015 The Bitcoin Core developers
|
2015-05-04 00:20:46 +02:00
|
|
|
// 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
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <indirectmap.h>
|
2016-06-03 01:11:02 +02:00
|
|
|
|
2015-05-04 00:20:46 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <map>
|
2018-04-02 20:31:40 +02:00
|
|
|
#include <memory>
|
2015-05-04 00:20:46 +02:00
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
2017-04-25 01:01:44 +02:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
2015-05-04 00:20:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
namespace memusage
|
|
|
|
{
|
|
|
|
|
|
|
|
/** Compute the total memory used by allocating alloc bytes. */
|
|
|
|
static size_t MallocUsage(size_t alloc);
|
|
|
|
|
2015-07-09 19:56:31 +02:00
|
|
|
/** 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; }
|
|
|
|
|
2015-05-04 00:20:46 +02:00
|
|
|
/** 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
|
2015-07-17 19:46:18 +02:00
|
|
|
* iterate themselves, or use more efficient caching + updating on modification.
|
2015-05-04 00:20:46 +02:00
|
|
|
*/
|
2015-07-09 19:56:31 +02:00
|
|
|
|
2015-05-04 00:20:46 +02:00
|
|
|
static inline size_t MallocUsage(size_t alloc)
|
|
|
|
{
|
|
|
|
// Measured on libc6 2.19 on Linux.
|
2015-10-29 07:11:24 +01:00
|
|
|
if (alloc == 0) {
|
|
|
|
return 0;
|
|
|
|
} else if (sizeof(void*) == 8) {
|
2015-05-04 00:20:46 +02:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2016-06-08 14:01:05 +02:00
|
|
|
struct stl_shared_counter
|
|
|
|
{
|
|
|
|
/* Various platforms use different sized counters here.
|
|
|
|
* Conservatively assume that they won't be larger than size_t. */
|
|
|
|
void* class_type;
|
|
|
|
size_t use_count;
|
|
|
|
size_t weak_count;
|
|
|
|
};
|
|
|
|
|
2015-05-04 00:20:46 +02:00
|
|
|
template<typename X>
|
|
|
|
static inline size_t DynamicUsage(const std::vector<X>& v)
|
|
|
|
{
|
|
|
|
return MallocUsage(v.capacity() * sizeof(X));
|
|
|
|
}
|
|
|
|
|
2015-10-29 07:11:24 +01:00
|
|
|
template<unsigned int N, typename X, typename S, typename D>
|
|
|
|
static inline size_t DynamicUsage(const prevector<N, X, S, D>& v)
|
|
|
|
{
|
|
|
|
return MallocUsage(v.allocated_memory());
|
|
|
|
}
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
template<typename X, typename Y>
|
|
|
|
static inline size_t DynamicUsage(const std::set<X, Y>& s)
|
2015-05-04 00:20:46 +02:00
|
|
|
{
|
|
|
|
return MallocUsage(sizeof(stl_tree_node<X>)) * s.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename X, typename Y>
|
2015-07-15 20:47:45 +02:00
|
|
|
static inline size_t IncrementalDynamicUsage(const std::set<X, Y>& s)
|
|
|
|
{
|
|
|
|
return MallocUsage(sizeof(stl_tree_node<X>));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename X, typename Y, typename Z>
|
|
|
|
static inline size_t DynamicUsage(const std::map<X, Y, Z>& m)
|
2015-05-04 00:20:46 +02:00
|
|
|
{
|
|
|
|
return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
|
|
|
|
}
|
|
|
|
|
2015-07-15 20:47:45 +02:00
|
|
|
template<typename X, typename Y, typename Z>
|
|
|
|
static inline size_t IncrementalDynamicUsage(const std::map<X, Y, Z>& m)
|
|
|
|
{
|
|
|
|
return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >));
|
|
|
|
}
|
|
|
|
|
2016-06-03 01:11:02 +02:00
|
|
|
// indirectmap has underlying map with pointer as key
|
|
|
|
|
|
|
|
template<typename X, typename Y>
|
|
|
|
static inline size_t DynamicUsage(const indirectmap<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 IncrementalDynamicUsage(const indirectmap<X, Y>& m)
|
|
|
|
{
|
|
|
|
return MallocUsage(sizeof(stl_tree_node<std::pair<const X*, Y> >));
|
|
|
|
}
|
|
|
|
|
2016-06-08 14:01:05 +02:00
|
|
|
template<typename X>
|
|
|
|
static inline size_t DynamicUsage(const std::unique_ptr<X>& p)
|
|
|
|
{
|
|
|
|
return p ? MallocUsage(sizeof(X)) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename X>
|
|
|
|
static inline size_t DynamicUsage(const std::shared_ptr<X>& p)
|
|
|
|
{
|
|
|
|
// A shared_ptr can either use a single continuous memory block for both
|
|
|
|
// the counter and the storage (when using std::make_shared), or separate.
|
|
|
|
// We can't observe the difference, however, so assume the worst.
|
|
|
|
return p ? MallocUsage(sizeof(X)) + MallocUsage(sizeof(stl_shared_counter)) : 0;
|
|
|
|
}
|
|
|
|
|
2015-05-04 00:20:46 +02:00
|
|
|
template<typename X>
|
2017-04-25 01:01:44 +02:00
|
|
|
struct unordered_node : private X
|
2015-05-04 00:20:46 +02:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
void* ptr;
|
|
|
|
};
|
|
|
|
|
2017-04-25 01:01:44 +02:00
|
|
|
template<typename X, typename Y>
|
|
|
|
static inline size_t DynamicUsage(const std::unordered_set<X, Y>& s)
|
|
|
|
{
|
|
|
|
return MallocUsage(sizeof(unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename X, typename Y, typename Z>
|
|
|
|
static inline size_t DynamicUsage(const std::unordered_map<X, Y, Z>& m)
|
|
|
|
{
|
|
|
|
return MallocUsage(sizeof(unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());
|
2015-05-04 00:20:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-27 17:44:13 +01:00
|
|
|
#endif // BITCOIN_MEMUSAGE_H
|