From 54c80c739981ed5b327dd4860022b866a07fdf18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1aki=20Baz=20Castillo?= Date: Fri, 9 Aug 2024 13:14:22 +0200 Subject: [PATCH] DepLibUring improvements ### Details - Be explicit. Always check if liburing is supported before calling any method in `DepLibUring`. This is cosmetic but helps when reading the whole code. --- worker/include/DepLibUring.hpp | 11 +++++-- worker/src/DepLibUring.cpp | 56 +++++++++++++++++++--------------- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/worker/include/DepLibUring.hpp b/worker/include/DepLibUring.hpp index 973acaff42..7c8f0357c3 100644 --- a/worker/include/DepLibUring.hpp +++ b/worker/include/DepLibUring.hpp @@ -33,9 +33,10 @@ class DepLibUring using SendBuffer = uint8_t[SendBufferSize]; - static bool IsRuntimeSupported(); static void ClassInit(); static void ClassDestroy(); + static void CheckRuntimeSupport(); + static bool IsRuntimeSupported(); static flatbuffers::Offset FillBuffer(flatbuffers::FlatBufferBuilder& builder); static void StartPollingCQEs(); static void StopPollingCQEs(); @@ -51,8 +52,10 @@ class DepLibUring class LibUring; thread_local static LibUring* liburing; + static bool runtimeSupported{ false }; -public: +private: + // Private singleton. class LibUring { public: @@ -98,6 +101,10 @@ class DepLibUring } private: + void SetInactive() + { + this->active = false; + } UserData* GetUserData(); bool IsDataInSendBuffers(const uint8_t* data) const { diff --git a/worker/src/DepLibUring.cpp b/worker/src/DepLibUring.cpp index 95d2b6631e..ee64880c5e 100644 --- a/worker/src/DepLibUring.cpp +++ b/worker/src/DepLibUring.cpp @@ -114,29 +114,6 @@ inline static void onFdEvent(uv_poll_t* handle, int status, int events) /* Static class methods */ -bool DepLibUring::IsRuntimeSupported() -{ - // clang-format off - struct utsname buffer{}; - // clang-format on - - auto err = uname(std::addressof(buffer)); - - if (err != 0) - { - MS_THROW_ERROR("uname() failed: %s", std::strerror(err)); - } - - MS_DEBUG_TAG(info, "kernel version: %s", buffer.version); - - auto* kernelMayorCstr = buffer.release; - auto kernelMayorLong = strtol(kernelMayorCstr, &kernelMayorCstr, 10); - - // liburing `sento` capabilities are supported for kernel versions greather - // than or equal to 6. - return kernelMayorLong >= 6; -} - void DepLibUring::ClassInit() { const auto mayor = io_uring_major_version(); @@ -144,6 +121,9 @@ void DepLibUring::ClassInit() MS_DEBUG_TAG(info, "liburing version: \"%i.%i\"", mayor, minor); + // This must be called first. + DepLibUring::CheckRuntimeSupport(); + if (DepLibUring::IsRuntimeSupported()) { DepLibUring::liburing = new LibUring(); @@ -163,6 +143,34 @@ void DepLibUring::ClassDestroy() delete DepLibUring::liburing; } +void DepLibUring::CheckRuntimeSupport() +{ + // clang-format off + struct utsname buffer{}; + // clang-format on + + auto err = uname(std::addressof(buffer)); + + if (err != 0) + { + MS_THROW_ERROR("uname() failed: %s", std::strerror(err)); + } + + MS_DEBUG_TAG(info, "kernel version: %s", buffer.version); + + auto* kernelMayorCstr = buffer.release; + auto kernelMayorLong = strtol(kernelMayorCstr, &kernelMayorCstr, 10); + + // liburing `sento` capabilities are supported for kernel versions greather + // than or equal to 6. + DepLibUring::runtimeSupported = kernelMayorLong >= 6; +} + +bool DepLibUring::IsRuntimeSupported() +{ + return DepLibUring::runtimeSupported; +} + flatbuffers::Offset DepLibUring::FillBuffer(flatbuffers::FlatBufferBuilder& builder) { MS_TRACE(); @@ -580,7 +588,7 @@ void DepLibUring::LibUring::Submit() MS_TRACE(); // Unset active flag. - this->active = false; + SetInactive(); auto err = io_uring_submit(std::addressof(this->ring));