From 5098c47b2430ded299c21620527ebd0544fe51e2 Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Thu, 9 Jul 2015 13:56:31 -0400 Subject: Implement accurate memory accounting for mempool --- src/memusage.h | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) (limited to 'src/memusage.h') diff --git a/src/memusage.h b/src/memusage.h index 9f7de9e2e1..7a831e6d33 100644 --- a/src/memusage.h +++ b/src/memusage.h @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -20,12 +21,28 @@ 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 static inline size_t DynamicUsage(X * const &v) { return 0; } +template static inline size_t DynamicUsage(const X * const &v) { return 0; } +template static inline size_t DynamicUsage(std::pair &p) { return 0; } + /** Compute the memory used for dynamically allocated but owned data structures. * For generic data types, this is *not* recursive. DynamicUsage(vector >) * will compute the memory used for the vector'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 - * do the recursion themselves, or use more efficient caching + updating on modification. + * use RecursiveDynamicUsage, iterate themselves, or use more efficient caching + + * updating on modification. */ template static size_t DynamicUsage(const std::vector& v); template static size_t DynamicUsage(const std::set& s); @@ -34,6 +51,12 @@ template static size_t DynamicUsage(const boost::unorder template static size_t DynamicUsage(const boost::unordered_map& s); template static size_t DynamicUsage(const X& x); +template static size_t RecursiveDynamicUsage(const std::vector& v); +template static size_t RecursiveDynamicUsage(const std::set& v); +template static size_t RecursiveDynamicUsage(const std::map& v); +template static size_t RecursiveDynamicUsage(const std::pair& v); +template static size_t RecursiveDynamicUsage(const X& v); + static inline size_t MallocUsage(size_t alloc) { // Measured on libc6 2.19 on Linux. @@ -65,18 +88,54 @@ static inline size_t DynamicUsage(const std::vector& v) return MallocUsage(v.capacity() * sizeof(X)); } +template +static inline size_t RecursiveDynamicUsage(const std::vector& v) +{ + size_t usage = DynamicUsage(v); + BOOST_FOREACH(const X& x, v) { + usage += RecursiveDynamicUsage(x); + } + return usage; +} + template static inline size_t DynamicUsage(const std::set& s) { return MallocUsage(sizeof(stl_tree_node)) * s.size(); } +template +static inline size_t RecursiveDynamicUsage(const std::set& v) +{ + size_t usage = DynamicUsage(v); + BOOST_FOREACH(const X& x, v) { + usage += RecursiveDynamicUsage(x); + } + return usage; +} + template static inline size_t DynamicUsage(const std::map& m) { return MallocUsage(sizeof(stl_tree_node >)) * m.size(); } +template +static inline size_t RecursiveDynamicUsage(const std::map& v) +{ + size_t usage = DynamicUsage(v); + for (typename std::map::const_iterator it = v.begin(); it != v.end(); it++) { + usage += RecursiveDynamicUsage(*it); + } + return usage; +} + +template +static inline size_t RecursiveDynamicUsage(const std::pair& v) +{ + return RecursiveDynamicUsage(v.first) + RecursiveDynamicUsage(v.second); +} + // Boost data structures template @@ -106,6 +165,12 @@ static inline size_t DynamicUsage(const X& x) return x.DynamicMemoryUsage(); } +template +static inline size_t RecursiveDynamicUsage(const X& x) +{ + return DynamicUsage(x); +} + } #endif -- cgit v1.2.3