madvise.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "madvise.h"
  2. #include "align.h"
  3. #include "info.h"
  4. #include <util/generic/yexception.h>
  5. #if defined(_win_)
  6. #include <util/system/winint.h>
  7. #else
  8. #include <sys/mman.h>
  9. #endif
  10. #ifndef MADV_DONTDUMP /* This flag is defined in sys/mman.h since Linux 3.4, but currently old libc header is in use \
  11. for capability with Ubuntu 12.04, so we need to define it here manually */
  12. #define MADV_DONTDUMP 16 /* Explicity exclude from the core dump, overrides the coredump filter bits */
  13. #endif
  14. #ifndef MADV_DODUMP /* This flag is defined in sys/mman.h since Linux 3.4, but currently old libc header is in use \
  15. for capability with Ubuntu 12.04, so we need to define it here manually */
  16. #define MADV_DODUMP 17 /* Undo the effect of an earlier MADV_DONTDUMP */
  17. #endif
  18. namespace {
  19. void Madvise(int flag, const void* cbegin, size_t size) {
  20. static const size_t pageSize = NSystemInfo::GetPageSize();
  21. void* begin = AlignDown(const_cast<void*>(cbegin), pageSize);
  22. size = AlignUp(size, pageSize);
  23. #if defined(_win_)
  24. if (!VirtualFree((LPVOID)begin, size, flag)) {
  25. TString err(LastSystemErrorText());
  26. ythrow yexception() << "VirtualFree(" << begin << ", " << size << ", " << flag << ")"
  27. << " returned error: " << err;
  28. }
  29. #else
  30. if (-1 == madvise(begin, size, flag)) {
  31. TString err(LastSystemErrorText());
  32. ythrow yexception() << "madvise(" << begin << ", " << size << ", " << flag << ")"
  33. << " returned error: " << err;
  34. }
  35. #endif
  36. }
  37. } // namespace
  38. void MadviseSequentialAccess(const void* begin, size_t size) {
  39. #if !defined(_win_)
  40. Madvise(MADV_SEQUENTIAL, begin, size);
  41. #endif
  42. }
  43. void MadviseSequentialAccess(TArrayRef<const char> data) {
  44. MadviseSequentialAccess(data.data(), data.size());
  45. }
  46. void MadviseSequentialAccess(TArrayRef<const ui8> data) {
  47. MadviseSequentialAccess(data.data(), data.size());
  48. }
  49. void MadviseRandomAccess(const void* begin, size_t size) {
  50. #if !defined(_win_)
  51. Madvise(MADV_RANDOM, begin, size);
  52. #endif
  53. }
  54. void MadviseRandomAccess(TArrayRef<const char> data) {
  55. MadviseRandomAccess(data.data(), data.size());
  56. }
  57. void MadviseRandomAccess(TArrayRef<const ui8> data) {
  58. MadviseRandomAccess(data.data(), data.size());
  59. }
  60. void MadviseEvict(const void* begin, size_t size) {
  61. #if defined(_win_)
  62. Madvise(MEM_DECOMMIT, begin, size);
  63. #elif defined(_linux_) || defined(_cygwin_)
  64. Madvise(MADV_DONTNEED, begin, size);
  65. #else // freebsd, osx
  66. Madvise(MADV_FREE, begin, size);
  67. #endif
  68. }
  69. void MadviseEvict(TArrayRef<const char> data) {
  70. MadviseEvict(data.data(), data.size());
  71. }
  72. void MadviseEvict(TArrayRef<const ui8> data) {
  73. MadviseEvict(data.data(), data.size());
  74. }
  75. void MadviseExcludeFromCoreDump(const void* begin, size_t size) {
  76. #if defined(_darwin_)
  77. // Don't try to call function with flag which doesn't work
  78. // https://st.yandex-team.ru/PASSP-31755#6050bbafc68f501f2c22caab
  79. Y_UNUSED(begin);
  80. Y_UNUSED(size);
  81. #elif !defined(_win_)
  82. Madvise(MADV_DONTDUMP, begin, size);
  83. #endif
  84. }
  85. void MadviseExcludeFromCoreDump(TArrayRef<const char> data) {
  86. MadviseExcludeFromCoreDump(data.data(), data.size());
  87. }
  88. void MadviseExcludeFromCoreDump(TArrayRef<const ui8> data) {
  89. MadviseExcludeFromCoreDump(data.data(), data.size());
  90. }
  91. void MadviseIncludeIntoCoreDump(const void* begin, size_t size) {
  92. #if defined(_darwin_)
  93. Y_UNUSED(begin);
  94. Y_UNUSED(size);
  95. #elif !defined(_win_)
  96. Madvise(MADV_DODUMP, begin, size);
  97. #endif
  98. }
  99. void MadviseIncludeIntoCoreDump(TArrayRef<const char> data) {
  100. MadviseIncludeIntoCoreDump(data.data(), data.size());
  101. }
  102. void MadviseIncludeIntoCoreDump(TArrayRef<const ui8> data) {
  103. MadviseIncludeIntoCoreDump(data.data(), data.size());
  104. }