dbg_info.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "dbg_info.h"
  2. #include <library/cpp/malloc/api/malloc.h>
  3. namespace NAllocDbg {
  4. ////////////////////////////////////////////////////////////////////////////////
  5. using TGetAllocationCounter = i64(int counter);
  6. using TSetThreadAllocTag = int(int tag);
  7. using TGetPerTagAllocInfo = void(
  8. bool flushPerThreadCounters,
  9. TPerTagAllocInfo* info,
  10. int& maxTag,
  11. int& numSizes);
  12. using TSetProfileCurrentThread = bool(bool newVal);
  13. using TSetProfileAllThreads = bool(bool newVal);
  14. using TSetAllocationSamplingEnabled = bool(bool newVal);
  15. using TSetAllocationSampleRate = size_t(size_t newVal);
  16. using TSetAllocationSampleMaxSize = size_t(size_t newVal);
  17. using TSetAllocationCallback = TAllocationCallback*(TAllocationCallback* newVal);
  18. using TSetDeallocationCallback = TDeallocationCallback*(TDeallocationCallback* newVal);
  19. struct TAllocFn {
  20. TGetAllocationCounter* GetAllocationCounterFast = nullptr;
  21. TGetAllocationCounter* GetAllocationCounterFull = nullptr;
  22. TSetThreadAllocTag* SetThreadAllocTag = nullptr;
  23. TGetPerTagAllocInfo* GetPerTagAllocInfo = nullptr;
  24. TSetProfileCurrentThread* SetProfileCurrentThread = nullptr;
  25. TSetProfileAllThreads* SetProfileAllThreads = nullptr;
  26. TSetAllocationSamplingEnabled* SetAllocationSamplingEnabled = nullptr;
  27. TSetAllocationSampleRate* SetAllocationSampleRate = nullptr;
  28. TSetAllocationSampleMaxSize* SetAllocationSampleMaxSize = nullptr;
  29. TSetAllocationCallback* SetAllocationCallback = nullptr;
  30. TSetDeallocationCallback* SetDeallocationCallback = nullptr;
  31. TAllocFn() {
  32. auto mallocInfo = NMalloc::MallocInfo();
  33. GetAllocationCounterFast = (TGetAllocationCounter*)mallocInfo.GetParam("GetLFAllocCounterFast");
  34. GetAllocationCounterFull = (TGetAllocationCounter*)mallocInfo.GetParam("GetLFAllocCounterFull");
  35. SetThreadAllocTag = (TSetThreadAllocTag*)mallocInfo.GetParam("SetThreadAllocTag");
  36. GetPerTagAllocInfo = (TGetPerTagAllocInfo*)mallocInfo.GetParam("GetPerTagAllocInfo");
  37. SetProfileCurrentThread = (TSetProfileCurrentThread*)mallocInfo.GetParam("SetProfileCurrentThread");
  38. SetProfileAllThreads = (TSetProfileAllThreads*)mallocInfo.GetParam("SetProfileAllThreads");
  39. SetAllocationSamplingEnabled = (TSetAllocationSamplingEnabled*)mallocInfo.GetParam("SetAllocationSamplingEnabled");
  40. SetAllocationSampleRate = (TSetAllocationSampleRate*)mallocInfo.GetParam("SetAllocationSampleRate");
  41. SetAllocationSampleMaxSize = (TSetAllocationSampleMaxSize*)mallocInfo.GetParam("SetAllocationSampleMaxSize");
  42. SetAllocationCallback = (TSetAllocationCallback*)mallocInfo.GetParam("SetAllocationCallback");
  43. SetDeallocationCallback = (TSetDeallocationCallback*)mallocInfo.GetParam("SetDeallocationCallback");
  44. }
  45. };
  46. ////////////////////////////////////////////////////////////////////////////////
  47. static TAllocFn AllocFn;
  48. i64 GetAllocationCounterFast(ELFAllocCounter counter) {
  49. return AllocFn.GetAllocationCounterFast ? AllocFn.GetAllocationCounterFast(counter) : 0;
  50. }
  51. i64 GetAllocationCounterFull(ELFAllocCounter counter) {
  52. return AllocFn.GetAllocationCounterFull ? AllocFn.GetAllocationCounterFull(counter) : 0;
  53. }
  54. int SetThreadAllocTag(int tag) {
  55. return AllocFn.SetThreadAllocTag ? AllocFn.SetThreadAllocTag(tag) : 0;
  56. }
  57. TArrayPtr<TPerTagAllocInfo> GetPerTagAllocInfo(
  58. bool flushPerThreadCounters,
  59. int& maxTag,
  60. int& numSizes) {
  61. if (AllocFn.GetPerTagAllocInfo) {
  62. AllocFn.GetPerTagAllocInfo(flushPerThreadCounters, nullptr, maxTag, numSizes);
  63. TArrayPtr<TPerTagAllocInfo> info = new TPerTagAllocInfo[maxTag * numSizes];
  64. AllocFn.GetPerTagAllocInfo(flushPerThreadCounters, info.Get(), maxTag, numSizes);
  65. return info;
  66. }
  67. maxTag = 0;
  68. numSizes = 0;
  69. return nullptr;
  70. }
  71. bool SetProfileCurrentThread(bool newVal) {
  72. return AllocFn.SetProfileCurrentThread ? AllocFn.SetProfileCurrentThread(newVal) : false;
  73. }
  74. bool SetProfileAllThreads(bool newVal) {
  75. return AllocFn.SetProfileAllThreads ? AllocFn.SetProfileAllThreads(newVal) : false;
  76. }
  77. bool SetAllocationSamplingEnabled(bool newVal) {
  78. return AllocFn.SetAllocationSamplingEnabled ? AllocFn.SetAllocationSamplingEnabled(newVal) : false;
  79. }
  80. size_t SetAllocationSampleRate(size_t newVal) {
  81. return AllocFn.SetAllocationSampleRate ? AllocFn.SetAllocationSampleRate(newVal) : 0;
  82. }
  83. size_t SetAllocationSampleMaxSize(size_t newVal) {
  84. return AllocFn.SetAllocationSampleMaxSize ? AllocFn.SetAllocationSampleMaxSize(newVal) : 0;
  85. }
  86. TAllocationCallback* SetAllocationCallback(TAllocationCallback* newVal) {
  87. return AllocFn.SetAllocationCallback ? AllocFn.SetAllocationCallback(newVal) : nullptr;
  88. }
  89. TDeallocationCallback* SetDeallocationCallback(TDeallocationCallback* newVal) {
  90. return AllocFn.SetDeallocationCallback ? AllocFn.SetDeallocationCallback(newVal) : nullptr;
  91. }
  92. }