From d2e2d9bc3b5291face2498fe2c8c7be21c94f2b5 Mon Sep 17 00:00:00 2001 From: Hayodea Hekol Date: Sun, 9 Nov 2025 01:16:17 -0400 Subject: [PATCH] StagingBuffer: Prefer mlock to io_uring_register_buffers --- .../livoxGen1/ioUringAssemblyEngine.cpp | 18 +++++++++++++++++- stimBuffApis/livoxGen1/stagingBuffer.cpp | 7 +++++++ stimBuffApis/livoxGen1/stagingBuffer.h | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/stimBuffApis/livoxGen1/ioUringAssemblyEngine.cpp b/stimBuffApis/livoxGen1/ioUringAssemblyEngine.cpp index 2a8381b..4329b35 100644 --- a/stimBuffApis/livoxGen1/ioUringAssemblyEngine.cpp +++ b/stimBuffApis/livoxGen1/ioUringAssemblyEngine.cpp @@ -26,6 +26,8 @@ #include "pcloudStimulusBuffer.h" #include "livoxGen1.h" +// #define REGISTER_IOURING_BUFFERS + namespace smo { namespace stim_buff { @@ -107,7 +109,9 @@ bool IoUringAssemblyEngine::setup() } // Declare iovec early to avoid goto crossing initialization +#ifdef REGISTER_IOURING_BUFFERS struct iovec iov; +#endif int ret; /** EXPLANATION: @@ -122,16 +126,24 @@ bool IoUringAssemblyEngine::setup() if (ret < 0) { goto cleanup; } +#ifdef REGISTER_IOURING_BUFFERS // Register staging buffer with io_uring for DMA-apt I/O iov = parent.assemblyBuffer.getIoUringRegisterIoVec(); ret = io_uring_register_buffers(&ring, &iov, 1); if (ret < 0) { goto cleanup_ring; } +#endif // Create eventfd for CQE notifications (used with boost's unified loop) eventfdFd = eventfd(0, EFD_NONBLOCK); if (eventfdFd < 0) - { goto cleanup_buffers; } + { +#ifdef REGISTER_IOURING_BUFFERS + goto cleanup_buffers; +#else + goto cleanup_ring; +#endif + } // Register eventfd with io_uring ret = io_uring_register_eventfd(&ring, eventfdFd); @@ -144,8 +156,10 @@ bool IoUringAssemblyEngine::setup() cleanup_eventfd: close(eventfdFd); eventfdFd = -1; +#ifdef REGISTER_IOURING_BUFFERS cleanup_buffers: io_uring_unregister_buffers(&ring); +#endif cleanup_ring: io_uring_queue_exit(&ring); cleanup: @@ -166,7 +180,9 @@ void IoUringAssemblyEngine::finalize() if (isSetup) { +#ifdef REGISTER_IOURING_BUFFERS io_uring_unregister_buffers(&ring); +#endif io_uring_queue_exit(&ring); isSetup = false; } diff --git a/stimBuffApis/livoxGen1/stagingBuffer.cpp b/stimBuffApis/livoxGen1/stagingBuffer.cpp index c9841e6..c74724e 100644 --- a/stimBuffApis/livoxGen1/stagingBuffer.cpp +++ b/stimBuffApis/livoxGen1/stagingBuffer.cpp @@ -197,6 +197,13 @@ assemblingFlag(false) static_cast(mmapped), MmapDeleter(bufferNBytes)); currentNBytes.store(0); + // Lock the buffer in memory to prevent swapping + if (mlock(buffer.get(), bufferNBytes) != 0) + { + throw std::runtime_error(std::string(__func__) + + ": StagingBuffer: mlock() failed"); + } + // Calculate offset and validate invariants (helper function in .cpp) firstSlotOffsetNBytes = StagingBuffer::calculateFirstSlotOffsetAndValidate( buffer.get(), bufferNBytes, nDgramsPerFrame, diff --git a/stimBuffApis/livoxGen1/stagingBuffer.h b/stimBuffApis/livoxGen1/stagingBuffer.h index 4ed963f..543467d 100644 --- a/stimBuffApis/livoxGen1/stagingBuffer.h +++ b/stimBuffApis/livoxGen1/stagingBuffer.h @@ -149,6 +149,7 @@ private: { if (ptr != nullptr && size > 0) { + munlock(ptr, size); munmap(ptr, size); } }