mkql_mem_info.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #pragma once
  2. #include <util/stream/output.h>
  3. #include <util/string/builder.h>
  4. #include <util/system/src_location.h>
  5. #include <unordered_map>
  6. #ifndef NDEBUG
  7. namespace NKikimr {
  8. namespace NMiniKQL {
  9. using TMkqlLocation = TSourceLocation;
  10. }
  11. }
  12. #define __MKQL_LOCATION__ __LOCATION__
  13. # define MKQL_MEM_TAKE3(MemInfo, Mem, Size) \
  14. ::NKikimr::NMiniKQL::Take(MemInfo, (Mem), (Size), __MKQL_LOCATION__)
  15. # define MKQL_MEM_TAKE4(MemInfo, Mem, Size, Location) \
  16. ::NKikimr::NMiniKQL::Take(MemInfo, (Mem), (Size), Location)
  17. # define MKQL_MEM_RETURN(MemInfo, Mem, Size) \
  18. ::NKikimr::NMiniKQL::Return(MemInfo, (Mem), (Size))
  19. # define MKQL_MEM_RETURN_PTR(MemInfo, Mem) \
  20. ::NKikimr::NMiniKQL::Return(MemInfo, (Mem))
  21. #else
  22. namespace NKikimr {
  23. namespace NMiniKQL {
  24. using TMkqlLocation = int;
  25. }
  26. }
  27. #define __MKQL_LOCATION__ 0
  28. # define MKQL_MEM_TAKE3(MemInfo, Mem, Size) \
  29. Y_UNUSED(MemInfo); Y_UNUSED(Mem); Y_UNUSED(Size);
  30. # define MKQL_MEM_TAKE4(MemInfo, Mem, Size, Location) \
  31. Y_UNUSED(MemInfo); Y_UNUSED(Mem); Y_UNUSED(Size); Y_UNUSED(Location);
  32. # define MKQL_MEM_RETURN(MemInfo, Mem, Size) \
  33. Y_UNUSED(MemInfo); Y_UNUSED(Mem); Y_UNUSED(Size);
  34. # define MKQL_MEM_RETURN_PTR(MemInfo, Mem) \
  35. Y_UNUSED(MemInfo); Y_UNUSED(Mem);
  36. #endif
  37. #define GET_MKQL_MEM_TAKE(_1, _2, _3, _4, IMPL, ...) IMPL
  38. #define MKQL_MEM_TAKE(...) Y_PASS_VA_ARGS(GET_MKQL_MEM_TAKE(__VA_ARGS__, MKQL_MEM_TAKE4, MKQL_MEM_TAKE3)(__VA_ARGS__))
  39. namespace NKikimr {
  40. namespace NMiniKQL {
  41. class TMemoryUsageInfo : public TThrRefBase
  42. {
  43. struct TAllocationInfo {
  44. ui64 Size;
  45. TMkqlLocation Location;
  46. bool IsDeleted;
  47. };
  48. public:
  49. explicit TMemoryUsageInfo(const TStringBuf& title);
  50. ~TMemoryUsageInfo();
  51. void AllowMissing();
  52. void CheckOnExit(bool check);
  53. #ifndef NDEBUG
  54. void Take(const void* mem, ui64 size, TMkqlLocation location);
  55. #endif
  56. #ifndef NDEBUG
  57. void Return(const void* mem, ui64 size);
  58. #endif
  59. #ifndef NDEBUG
  60. void Return(const void* mem);
  61. #endif
  62. i64 GetUsage() const;
  63. ui64 GetAllocated() const;
  64. ui64 GetFreed() const;
  65. ui64 GetPeak() const;
  66. void PrintTo(IOutputStream& out) const;
  67. void VerifyDebug() const;
  68. private:
  69. const TString Title_;
  70. ui64 Allocated_;
  71. ui64 Freed_;
  72. ui64 Peak_;
  73. bool AllowMissing_;
  74. bool CheckOnExit_;
  75. #ifndef NDEBUG
  76. std::unordered_map<const void*, TAllocationInfo> AllocationsMap_;
  77. #endif
  78. };
  79. #ifndef NDEBUG
  80. inline void Take(TMemoryUsageInfo& memInfo, const void* mem, ui64 size, TMkqlLocation location)
  81. {
  82. memInfo.Take(mem, size, std::move(location));
  83. }
  84. inline void Take(TMemoryUsageInfo* memInfo, const void* mem, ui64 size, TMkqlLocation location)
  85. {
  86. memInfo->Take(mem, size, std::move(location));
  87. }
  88. inline void Return(TMemoryUsageInfo& memInfo, const void* mem, ui64 size)
  89. {
  90. memInfo.Return(mem, size);
  91. }
  92. inline void Return(TMemoryUsageInfo* memInfo, const void* mem, ui64 size)
  93. {
  94. memInfo->Return(mem, size);
  95. }
  96. inline void Return(TMemoryUsageInfo& memInfo, const void* mem)
  97. {
  98. memInfo.Return(mem);
  99. }
  100. inline void Return(TMemoryUsageInfo* memInfo, const void* mem)
  101. {
  102. memInfo->Return(mem);
  103. }
  104. #endif
  105. } // namespace NMiniKQL
  106. } // namespace NKikimr
  107. template <>
  108. inline void Out<NKikimr::NMiniKQL::TMemoryUsageInfo>(
  109. IOutputStream& out,
  110. const NKikimr::NMiniKQL::TMemoryUsageInfo& memInfo)
  111. {
  112. memInfo.PrintTo(out);
  113. }