nalf_alloc.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #pragma once
  2. #include <util/generic/vector.h>
  3. #include <util/stream/output.h>
  4. #ifndef NALF_ALLOC_DEFAULTMODE
  5. #define NALF_ALLOC_DEFAULTMODE (TAllocHint::Chunked)
  6. #endif
  7. #ifndef NALF_ALLOC_DEFAULTALIGN
  8. #define NALF_ALLOC_DEFAULTALIGN (16)
  9. #endif
  10. #if defined(_tsan_enabled_) || defined(_msan_enabled_) || defined(_asan_enabled_) || defined(WITH_VALGRIND)
  11. #define NALF_FORCE_MALLOC_FREE 1
  12. #define NALF_DONOT_DEFINE_GLOBALS 1
  13. #endif
  14. namespace NNumaAwareLockFreeAllocator {
  15. struct TAllocHint {
  16. enum EHint {
  17. Undefined,
  18. Incremental,
  19. Chunked,
  20. System,
  21. ForceIncremental,
  22. ForceChunked,
  23. ForceSystem,
  24. Bootstrap,
  25. };
  26. // valid op hint values: incremental, chunked, system, force*
  27. // valid thread hint values: undefined, incremental, chunked, system
  28. // bootstrap is used in node initialization only
  29. };
  30. class TPerThreadAllocator;
  31. TPerThreadAllocator* GetThreadAllocator();
  32. void* Allocate(ui64 len, TAllocHint::EHint hint = NALF_ALLOC_DEFAULTMODE, ui64 align = NALF_ALLOC_DEFAULTALIGN);
  33. void Free(void* mem);
  34. void* Realloc(void* mem, ui64 len);
  35. TAllocHint::EHint SwapHint(TAllocHint::EHint hint) noexcept;
  36. std::pair<ui64, TAllocHint::EHint> MemBlockSize(void* mem);
  37. void* Allocate(TPerThreadAllocator* pta, ui64 len, TAllocHint::EHint hint = NALF_ALLOC_DEFAULTMODE, ui64 align = NALF_ALLOC_DEFAULTALIGN);
  38. void Free(TPerThreadAllocator* pta, void* mem);
  39. void* Realloc(TPerThreadAllocator* pta, void* mem, ui64 len);
  40. TAllocHint::EHint SwapHint(TPerThreadAllocator* pta, TAllocHint::EHint hint) noexcept;
  41. template <TAllocHint::EHint Hint>
  42. struct TSwapHint : TNonCopyable {
  43. const TAllocHint::EHint Old;
  44. TSwapHint()
  45. : Old(SwapHint(Hint))
  46. {
  47. }
  48. ~TSwapHint() {
  49. SwapHint(Old);
  50. }
  51. };
  52. void* SystemAllocation(ui64 size);
  53. void SystemFree(void* mem, ui64 size);
  54. void* SystemRemap(void* mem, ui64 oldsize, ui64 newsize);
  55. ui32 GetNumaNode();
  56. struct TAllocatorStats {
  57. ui64 TotalBytesReserved; // w/o system bytes!
  58. ui32 PerThreadEntries;
  59. struct TSizeStats {
  60. ui32 PageSize;
  61. ui32 ChunkSize;
  62. ui64 TotalPagesReserved;
  63. ui64 TotalPagesCached;
  64. ui64 TotalAllocations;
  65. ui64 TotalReclaimed;
  66. ui64 PagesClaimed;
  67. ui64 PagesFromCache;
  68. ui64 PagesReleased;
  69. TSizeStats();
  70. };
  71. struct TIncrementalStats {
  72. ui64 TotalPagesReserved;
  73. ui64 TotalPagesCached;
  74. ui64 TotalAllocations;
  75. ui64 TotalReclaimed;
  76. ui64 PagesClaimed;
  77. ui64 PagesFromCache;
  78. ui64 PagesReleased;
  79. TIncrementalStats();
  80. };
  81. struct TSysStats {
  82. ui64 TotalBytesReserved;
  83. ui64 TotalBytesCached;
  84. ui64 TotalAllocations;
  85. ui64 TotalReclaimed;
  86. TSysStats();
  87. };
  88. TVector<TSizeStats> BySizeStats;
  89. TIncrementalStats IncrementalStats;
  90. TSysStats SysStats;
  91. TAllocatorStats();
  92. void Out(IOutputStream& out) const;
  93. };
  94. TVector<TAllocatorStats> GetAllocatorStats(); // one entry per numa-node
  95. static const ui64 SystemPageSize = 4096;
  96. }