alloc_stats.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <library/cpp/balloc/lib/alloc_stats.h>
  2. #include <util/system/compiler.h>
  3. #include <atomic>
  4. namespace NAllocStats {
  5. struct TThreadAllocStats {
  6. i64 CurrSize = 0;
  7. i64 MaxSize = 0;
  8. };
  9. struct TGlobalAllocStats {
  10. std::atomic<ui64> LiveLock = {0};
  11. std::atomic<ui64> Mmap = {0};
  12. };
  13. #if defined(_unix_) && !defined(_darwin_)
  14. __thread bool isEnabled = false;
  15. bool IsEnabled() noexcept {
  16. return isEnabled;
  17. }
  18. void EnableAllocStats(bool enable) noexcept {
  19. isEnabled = enable;
  20. }
  21. __thread TThreadAllocStats threadAllocStats;
  22. void IncThreadAllocStats(i64 size) noexcept {
  23. threadAllocStats.CurrSize += size;
  24. if (Y_UNLIKELY(threadAllocStats.CurrSize > threadAllocStats.MaxSize)) {
  25. threadAllocStats.MaxSize = threadAllocStats.CurrSize;
  26. }
  27. }
  28. void DecThreadAllocStats(i64 size) noexcept {
  29. threadAllocStats.CurrSize -= size;
  30. }
  31. void ResetThreadAllocStats() noexcept {
  32. threadAllocStats.CurrSize = 0;
  33. threadAllocStats.MaxSize = 0;
  34. }
  35. i64 GetThreadAllocMax() noexcept {
  36. return threadAllocStats.MaxSize;
  37. }
  38. #else // _unix_ && ! _darwin_
  39. bool IsEnabled() noexcept {
  40. return false;
  41. }
  42. void EnableAllocStats(bool /*enable*/) noexcept {
  43. }
  44. void IncThreadAllocStats(i64 /*size*/) noexcept {
  45. }
  46. void DecThreadAllocStats(i64 /*size*/) noexcept {
  47. }
  48. void ResetThreadAllocStats() noexcept {
  49. }
  50. i64 GetThreadAllocMax() noexcept {
  51. return 0;
  52. }
  53. #endif // _unix_ && ! _darwin_
  54. #if defined(_x86_64_) || defined(_i386_)
  55. static constexpr size_t CACHE_LINE_SIZE = 64;
  56. #elif defined(_arm64_) || defined(_ppc64_)
  57. static constexpr size_t CACHE_LINE_SIZE = 128;
  58. #else
  59. static constexpr size_t CACHE_LINE_SIZE = 256; // default large enough
  60. #endif
  61. template <typename T>
  62. struct alignas(sizeof(T)) TCacheLineDoublePaddedAtomic {
  63. char Prefix[CACHE_LINE_SIZE - sizeof(T)];
  64. T Value;
  65. char Postfix[CACHE_LINE_SIZE - sizeof(T)];
  66. };
  67. TCacheLineDoublePaddedAtomic<TGlobalAllocStats> GlobalCounters;
  68. void IncLiveLockCounter() noexcept {
  69. GlobalCounters.Value.LiveLock.fetch_add(1, std::memory_order_seq_cst);
  70. }
  71. ui64 GetLiveLockCounter() noexcept {
  72. return GlobalCounters.Value.LiveLock.load(std::memory_order_acquire);
  73. }
  74. void IncMmapCounter(ui64 amount) noexcept {
  75. GlobalCounters.Value.Mmap.fetch_add(amount, std::memory_order_seq_cst);
  76. }
  77. ui64 GetMmapCounter() noexcept {
  78. return GlobalCounters.Value.Mmap.load(std::memory_order_acquire);
  79. }
  80. } // namespace NAllocStats