StagingBuffer: Prefer mlock to io_uring_register_buffers

This commit is contained in:
2025-11-09 01:16:17 -04:00
parent 010ba9c7bd
commit d2e2d9bc3b
3 changed files with 25 additions and 1 deletions
@@ -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;
}
+7
View File
@@ -197,6 +197,13 @@ assemblingFlag(false)
static_cast<uint8_t*>(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,
+1
View File
@@ -149,6 +149,7 @@ private:
{
if (ptr != nullptr && size > 0)
{
munlock(ptr, size);
munmap(ptr, size);
}
}