You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

memusage.h 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (c) 2015 The Bitcoin developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #ifndef BITCOIN_MEMUSAGE_H
  5. #define BITCOIN_MEMUSAGE_H
  6. #include <stdlib.h>
  7. #include <map>
  8. #include <set>
  9. #include <vector>
  10. #include <boost/foreach.hpp>
  11. #include <boost/unordered_set.hpp>
  12. #include <boost/unordered_map.hpp>
  13. namespace memusage
  14. {
  15. /** Compute the total memory used by allocating alloc bytes. */
  16. static size_t MallocUsage(size_t alloc);
  17. /** Dynamic memory usage for built-in types is zero. */
  18. static inline size_t DynamicUsage(const int8_t& v) { return 0; }
  19. static inline size_t DynamicUsage(const uint8_t& v) { return 0; }
  20. static inline size_t DynamicUsage(const int16_t& v) { return 0; }
  21. static inline size_t DynamicUsage(const uint16_t& v) { return 0; }
  22. static inline size_t DynamicUsage(const int32_t& v) { return 0; }
  23. static inline size_t DynamicUsage(const uint32_t& v) { return 0; }
  24. static inline size_t DynamicUsage(const int64_t& v) { return 0; }
  25. static inline size_t DynamicUsage(const uint64_t& v) { return 0; }
  26. static inline size_t DynamicUsage(const float& v) { return 0; }
  27. static inline size_t DynamicUsage(const double& v) { return 0; }
  28. template<typename X> static inline size_t DynamicUsage(X * const &v) { return 0; }
  29. template<typename X> static inline size_t DynamicUsage(const X * const &v) { return 0; }
  30. /** Compute the memory used for dynamically allocated but owned data structures.
  31. * For generic data types, this is *not* recursive. DynamicUsage(vector<vector<int> >)
  32. * will compute the memory used for the vector<int>'s, but not for the ints inside.
  33. * This is for efficiency reasons, as these functions are intended to be fast. If
  34. * application data structures require more accurate inner accounting, they should
  35. * iterate themselves, or use more efficient caching + updating on modification.
  36. */
  37. static inline size_t MallocUsage(size_t alloc)
  38. {
  39. // Measured on libc6 2.19 on Linux.
  40. if (sizeof(void*) == 8) {
  41. return ((alloc + 31) >> 4) << 4;
  42. } else if (sizeof(void*) == 4) {
  43. return ((alloc + 15) >> 3) << 3;
  44. } else {
  45. assert(0);
  46. }
  47. }
  48. // STL data structures
  49. template<typename X>
  50. struct stl_tree_node
  51. {
  52. private:
  53. int color;
  54. void* parent;
  55. void* left;
  56. void* right;
  57. X x;
  58. };
  59. template<typename X>
  60. static inline size_t DynamicUsage(const std::vector<X>& v)
  61. {
  62. return MallocUsage(v.capacity() * sizeof(X));
  63. }
  64. template<typename X, typename Y>
  65. static inline size_t DynamicUsage(const std::set<X, Y>& s)
  66. {
  67. return MallocUsage(sizeof(stl_tree_node<X>)) * s.size();
  68. }
  69. template<typename X, typename Y>
  70. static inline size_t IncrementalDynamicUsage(const std::set<X, Y>& s)
  71. {
  72. return MallocUsage(sizeof(stl_tree_node<X>));
  73. }
  74. template<typename X, typename Y, typename Z>
  75. static inline size_t DynamicUsage(const std::map<X, Y, Z>& m)
  76. {
  77. return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >)) * m.size();
  78. }
  79. template<typename X, typename Y, typename Z>
  80. static inline size_t IncrementalDynamicUsage(const std::map<X, Y, Z>& m)
  81. {
  82. return MallocUsage(sizeof(stl_tree_node<std::pair<const X, Y> >));
  83. }
  84. // Boost data structures
  85. template<typename X>
  86. struct boost_unordered_node : private X
  87. {
  88. private:
  89. void* ptr;
  90. };
  91. template<typename X, typename Y>
  92. static inline size_t DynamicUsage(const boost::unordered_set<X, Y>& s)
  93. {
  94. return MallocUsage(sizeof(boost_unordered_node<X>)) * s.size() + MallocUsage(sizeof(void*) * s.bucket_count());
  95. }
  96. template<typename X, typename Y, typename Z>
  97. static inline size_t DynamicUsage(const boost::unordered_map<X, Y, Z>& m)
  98. {
  99. return MallocUsage(sizeof(boost_unordered_node<std::pair<const X, Y> >)) * m.size() + MallocUsage(sizeof(void*) * m.bucket_count());
  100. }
  101. }
  102. #endif