Skip to content

Commit

Permalink
DepLibUring improvements
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
ibc committed Aug 9, 2024
1 parent 28a1483 commit 54c80c7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
11 changes: 9 additions & 2 deletions worker/include/DepLibUring.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<FBS::LibUring::Dump> FillBuffer(flatbuffers::FlatBufferBuilder& builder);
static void StartPollingCQEs();
static void StopPollingCQEs();
Expand All @@ -51,8 +52,10 @@ class DepLibUring
class LibUring;

thread_local static LibUring* liburing;
static bool runtimeSupported{ false };

public:
private:
// Private singleton.
class LibUring
{
public:
Expand Down Expand Up @@ -98,6 +101,10 @@ class DepLibUring
}

private:
void SetInactive()
{
this->active = false;
}
UserData* GetUserData();
bool IsDataInSendBuffers(const uint8_t* data) const
{
Expand Down
56 changes: 32 additions & 24 deletions worker/src/DepLibUring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,36 +114,16 @@ 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();
const auto minor = io_uring_minor_version();

MS_DEBUG_TAG(info, "liburing version: \"%i.%i\"", mayor, minor);

// This must be called first.
DepLibUring::CheckRuntimeSupport();

if (DepLibUring::IsRuntimeSupported())
{
DepLibUring::liburing = new LibUring();
Expand All @@ -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<FBS::LibUring::Dump> DepLibUring::FillBuffer(flatbuffers::FlatBufferBuilder& builder)
{
MS_TRACE();
Expand Down Expand Up @@ -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));

Expand Down

0 comments on commit 54c80c7

Please sign in to comment.