malloc-info.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <library/cpp/malloc/api/malloc.h>
  2. using namespace NMalloc;
  3. #if defined(_MSC_VER)
  4. TMallocInfo NMalloc::MallocInfo() {
  5. TMallocInfo r;
  6. r.Name = "jemalloc";
  7. return r;
  8. }
  9. #else
  10. #include <strings.h>
  11. #include <stdlib.h>
  12. #include <inttypes.h>
  13. #include <contrib/libs/jemalloc/include/jemalloc/jemalloc.h>
  14. namespace {
  15. bool JESetParam(const char* param, const char* value) {
  16. if (param) {
  17. if (strcmp(param, "j:reset_epoch") == 0) {
  18. uint64_t epoch = 1;
  19. size_t sz = sizeof(epoch);
  20. mallctl("epoch", &epoch, &sz, &epoch, sz);
  21. return true;
  22. }
  23. if (strcmp(param, "j:prof") == 0) {
  24. if (strcmp(value, "start") == 0) {
  25. bool is_active = true;
  26. const int ret = mallctl("prof.active", nullptr, nullptr, &is_active, sizeof(is_active));
  27. return ret == 0;
  28. }
  29. if (strcmp(value, "stop") == 0) {
  30. bool is_active = false;
  31. const int ret = mallctl("prof.active", nullptr, nullptr, &is_active, sizeof(is_active));
  32. return ret == 0;
  33. }
  34. if (strcmp(value, "dump") == 0) {
  35. const int ret = mallctl("prof.dump", nullptr, nullptr, nullptr, 0);
  36. return ret == 0;
  37. }
  38. }
  39. if (strcmp(param, "j:bg_threads") == 0) {
  40. if (strcmp(value, "start") == 0) {
  41. bool is_active = true;
  42. const int ret = mallctl("background_thread", nullptr, nullptr, &is_active, sizeof(is_active));
  43. return ret == 0;
  44. }
  45. if (strcmp(value, "stop") == 0) {
  46. bool is_active = false;
  47. // NOTE: joins bg thread
  48. const int ret = mallctl("background_thread", nullptr, nullptr, &is_active, sizeof(is_active));
  49. return ret == 0;
  50. }
  51. if (strncmp(value, "max=", 4) == 0) {
  52. int num_value = atoi(value + 4);
  53. if (num_value <= 0) {
  54. return false;
  55. }
  56. size_t max_threads = num_value;
  57. const int ret = mallctl("max_background_threads", nullptr, nullptr, &max_threads, sizeof(max_threads));
  58. return ret == 0;
  59. }
  60. }
  61. return false;
  62. }
  63. return false;
  64. }
  65. const char* JEGetParam(const char* param) {
  66. if (param) {
  67. if (strcmp(param, "allocated") == 0) {
  68. JESetParam("j:reset_epoch", nullptr);
  69. size_t allocated = 0;
  70. size_t sz = sizeof(allocated);
  71. mallctl("stats.allocated", &allocated, &sz, nullptr, 0);
  72. static_assert(sizeof(size_t) == sizeof(void*), "fix me");
  73. return (const char*)(void*)allocated;
  74. } else if (strcmp(param, "j:stats_print_func") == 0) {
  75. return (const char*)&malloc_stats_print;
  76. }
  77. return nullptr;
  78. }
  79. return nullptr;
  80. }
  81. }
  82. TMallocInfo NMalloc::MallocInfo() {
  83. TMallocInfo r;
  84. r.Name = "jemalloc";
  85. r.SetParam = JESetParam;
  86. r.GetParam = JEGetParam;
  87. return r;
  88. }
  89. #endif