From d1173c68d2db65a0b6c6e992d3dc4a6a506c92dd Mon Sep 17 00:00:00 2001 From: Vlad Kuznetsov Date: Thu, 26 Sep 2024 16:26:03 +0000 Subject: [PATCH] handle all errors --- ydb/library/pdisk_io/buffers.cpp | 26 +++++++++++++++----------- ydb/library/pdisk_io/buffers.h | 4 ++-- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/ydb/library/pdisk_io/buffers.cpp b/ydb/library/pdisk_io/buffers.cpp index d4efb769e4fa..416fe52c279e 100644 --- a/ydb/library/pdisk_io/buffers.cpp +++ b/ydb/library/pdisk_io/buffers.cpp @@ -142,16 +142,23 @@ TBufferPoolCommon::TBufferPoolCommon(ui32 bufferSize, ui32 bufferCount, TBufferP : TBufferPool(bufferSize, bufferCount, params) { TBufferPool::UseHugePages = false; - Size = AlignUp((size_t)bufferSize, Alignment) * bufferCount + Alignment - 1; + Size = AlignUp((size_t)bufferSize * bufferCount, Alignment); + auto pageSize = sysconf(_SC_PAGE_SIZE); + Y_VERIFY(pageSize >= (i64)Alignment); ui8 *alignedData; if (HugePages) { void *ptr = mmap(NULL, Size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0); - Cerr << "ptr# " << ptr << " Alignment# " << Alignment << " Size# " << Size << Endl; - Y_VERIFY_S(ptr != MAP_FAILED, strerror(errno)); - RawBufferHuge = ptr; - alignedData = (ui8*)AlignUp(RawBufferHuge, Alignment); + Cerr << "Create buffer pool, mmap_ptr# " << ptr << " Alignment# " << Alignment << " Size# " << Size << Endl; + if (ptr == MAP_FAILED) { + Cerr << "hugepages mmap failed with# " << strerror(errno) << Endl; + RawBuffer.Reset(new ui8[Size]); + alignedData = (ui8*)AlignUp(RawBuffer.Get(), Alignment); + } else { + RawBufferHuge = ptr; + alignedData = (ui8*)AlignUp(RawBufferHuge, Alignment); + } } else { RawBuffer.Reset(new ui8[Size]); alignedData = (ui8*)AlignUp(RawBuffer.Get(), Alignment); @@ -161,12 +168,9 @@ TBufferPoolCommon::TBufferPoolCommon(ui32 bufferSize, ui32 bufferCount, TBufferP } TBufferPoolCommon::~TBufferPoolCommon() { - Cerr << "~TBufferPoolCommon" << Endl; - - if (HugePages) { - void *ptr = RawBufferHuge; - munmap(ptr, Size); - RawBufferHuge = nullptr; + if (HugePages && RawBufferHuge) { + int ret = munmap(RawBufferHuge, Size); + Y_VERIFY_S(ret == 0, "munmap failed, ptr# " << (void*)RawBufferHuge << " strerror# " << strerror(errno)); } } diff --git a/ydb/library/pdisk_io/buffers.h b/ydb/library/pdisk_io/buffers.h index eb9a349d8cd0..b90b78cdbd2b 100644 --- a/ydb/library/pdisk_io/buffers.h +++ b/ydb/library/pdisk_io/buffers.h @@ -155,8 +155,8 @@ class TBufferPool { // class TBufferPoolCommon : public TBufferPool { TArrayHolder RawBuffer; - void *RawBufferHuge; - size_t Size; + void *RawBufferHuge = nullptr; + size_t Size = 0; public: TBufferPoolCommon(ui32 bufferSize, ui32 bufferCount, TBufferPool::TPDiskParams params); virtual ~TBufferPoolCommon();