Skip to content

Commit

Permalink
enable liburing in runtime for kernel version >=6
Browse files Browse the repository at this point in the history
The binary could have been prebuilt in a linux machine with kernel
version >= 6, but then such image could be run on an older kernel. We
need to check the kernel version in runtime and only enable the feature
when appropriate.
  • Loading branch information
jmillan committed Nov 15, 2023
1 parent 61e7320 commit 0ef4ea6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
13 changes: 11 additions & 2 deletions worker/include/DepLibUring.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,20 @@ class DepLibUring
bool PrepareWrite(
int sockfd, const void* data1, size_t len1, const void* data2, size_t len2, onSendCallback* cb);
void Submit();
void Enable()
{
this->enabled = true;
}
void SetActive()
{
this->active = true;
if (this->enabled)
{
this->active = true;
}
}
bool IsActive() const
{
return this->active;
return this->enabled && this->active;
}
io_uring* GetRing()
{
Expand All @@ -72,6 +79,8 @@ class DepLibUring
uv_poll_t* uvHandle{ nullptr };
// Whether we are currently sending RTP over io_uring.
bool active{ false };
// Whether io_uring is enabled in runtime.
bool enabled{ false };
// Pre-allocated UserData entries.
UserData userDataBuffer[QueueDepth]{};
// Indexes of available UserData entries.
Expand Down
26 changes: 26 additions & 0 deletions worker/src/DepLibUring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "Logger.hpp"
#include "MediaSoupErrors.hpp"
#include <sys/eventfd.h>
#include <sys/utsname.h>

/* Static variables. */

Expand Down Expand Up @@ -65,6 +66,8 @@ inline static void onFdEvent(uv_poll_t* handle, int status, int events)
}
}

/* Static class methods */

void DepLibUring::ClassInit()
{
const auto mayor = io_uring_major_version();
Expand All @@ -73,6 +76,29 @@ void DepLibUring::ClassInit()
MS_DEBUG_TAG(info, "liburing version: \"%i.%i\"", mayor, minor);

DepLibUring::liburing = new DepLibUring();

struct utsname buffer{};
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);

// Enable liburing for kernel versions greather than or equal to 6.
if (kernelMayorLong >= 6)
{
DepLibUring::liburing->Enable();
}
else
{
MS_DEBUG_TAG(info, "kernel version not compatible with liburing");
}
}

void DepLibUring::ClassDestroy()
Expand Down

0 comments on commit 0ef4ea6

Please sign in to comment.