Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

liburing #1218

Merged
merged 53 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
79c0b3d
liburing
jmillan Nov 8, 2023
b819171
Update CHANGELOG.md
jmillan Nov 14, 2023
60acaab
add missing MS_LIBURING_ENABLED flag
jmillan Nov 14, 2023
e011fd4
Update CHANGELOG.md
jmillan Nov 14, 2023
1000439
feedback
jmillan Nov 14, 2023
16acc51
feedback
jmillan Nov 14, 2023
f17b15c
feedback, cont
jmillan Nov 14, 2023
e6ac1dc
feedback, cont
jmillan Nov 14, 2023
610797a
Add comments
jmillan Nov 14, 2023
b146937
use std::addressoff() rather than &
jmillan Nov 14, 2023
edbf854
fix typo
jmillan Nov 14, 2023
d53fc7a
add comments
jmillan Nov 14, 2023
8870b70
fix liburing Rust linkage
jmillan Nov 15, 2023
f6e25f8
close the uv_poll_t handle
jmillan Nov 15, 2023
fa2707e
properly clean uv_poll_t handle
jmillan Nov 15, 2023
61e7320
use 'err' rather than 'error' for consistency
jmillan Nov 15, 2023
0ef4ea6
enable liburing in runtime for kernel version >=6
jmillan Nov 15, 2023
a6c3151
comment typo
jmillan Nov 15, 2023
e6d85ce
disable clang-format for variable
jmillan Nov 15, 2023
e8f59f4
new class LibUring
jmillan Nov 15, 2023
808c2c1
Do not create a LibUring instance if not supported by kernel
jmillan Nov 15, 2023
55979f9
New line
jmillan Nov 15, 2023
c9d0f25
meson: remove extra tabs
jmillan Nov 15, 2023
4f01a2f
Merge remote-tracking branch 'origin/v3' into liburing
jmillan Nov 15, 2023
c69486d
handle feedback
jmillan Nov 17, 2023
ab9ce28
IsSupported() is boolean
jmillan Nov 17, 2023
2c261bf
handle feedback
jmillan Nov 20, 2023
6f7bedc
feedback: Rename MS_LIBURING_ENABLED for MS_LIBURING_SUPPORTED
jmillan Nov 20, 2023
dd322f0
Merge branch 'v3' into liburing
ibc Nov 20, 2023
097287e
Merge branch 'v3' into liburing
ibc Nov 20, 2023
6468706
use MS_LIBURING_SUPPORTED macro everywhere needed
jmillan Nov 20, 2023
05e5039
Perf: add two optiomisation flags
jmillan Nov 20, 2023
69058f6
remove flag
jmillan Nov 20, 2023
19bf1a4
format
jmillan Nov 20, 2023
feb0dc3
add debug logs
jmillan Nov 20, 2023
b353b23
fail if we cannot get the local file descriptor
jmillan Nov 20, 2023
b99c4ca
fix comment
jmillan Nov 20, 2023
f06d95f
feedback: use MS_LIBURING_SUPPORTED around
jmillan Nov 20, 2023
402bc5d
add comments
jmillan Nov 20, 2023
61f28d9
use liburing to send Nacked RTP
jmillan Nov 21, 2023
fb9c872
use liburing to send RTCP compound packets
jmillan Nov 21, 2023
05c58d8
rename DepLibUring::IsSupported by IsRuntimeSupported
jmillan Nov 21, 2023
198551f
Liburing::Dump()
jmillan Nov 21, 2023
7e0682c
Merge branch 'v3' into liburing
ibc Nov 21, 2023
0c4b797
consistency in numerical initializations
jmillan Nov 21, 2023
d4c8e4e
fix Dump()
jmillan Nov 21, 2023
4adf1b2
FBS: LibUring::Dump
jmillan Nov 22, 2023
f6286c9
Add missing liburing.fbs
jmillan Nov 22, 2023
403172e
format
jmillan Nov 22, 2023
e6e198e
Node: lint
jmillan Nov 22, 2023
781f8f4
Node: adapt tests for 'worker.dump()'
jmillan Nov 22, 2023
af995a8
Update CHANGELOG.md
jmillan Nov 23, 2023
cfc953a
Update CHANGELOG.md
jmillan Nov 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog


### NEXT

* Enable `liburing` usage for Linux (kernel versions >= 6) ([PR #1218](https://github.com/versatica/mediasoup/pull/1218)).


### 3.13.6

* Replace make + Makefile with Python Invoke library + tasks.py (also fix installation under path with whitespaces) ([PR #1239](https://github.com/versatica/mediasoup/pull/1239)).
Expand Down
20 changes: 19 additions & 1 deletion node/src/Worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ export type WorkerDump =
channelRequestHandlers : string[];
channelNotificationHandlers : string[];
};
liburing? :
{
sqeProcessCount: number;
sqeMissCount: number;
userDataMissCount: number;
};
};

export type WorkerEvents =
Expand Down Expand Up @@ -814,7 +820,7 @@ export function parseWorkerDumpResponse(
binary: FbsWorker.DumpResponse
): WorkerDump
{
return {
const dump: WorkerDump = {
pid : binary.pid()!,
webRtcServerIds : parseVector(binary, 'webRtcServerIds'),
routerIds : parseVector(binary, 'routerIds'),
Expand All @@ -824,4 +830,16 @@ export function parseWorkerDumpResponse(
channelNotificationHandlers : parseVector(binary.channelMessageHandlers()!, 'channelNotificationHandlers')
}
};

if (binary.liburing())
{
dump.liburing =
{
sqeProcessCount : Number(binary.liburing()!.sqeProcessCount()),
sqeMissCount : Number(binary.liburing()!.sqeMissCount()),
userDataMissCount : Number(binary.liburing()!.userDataMissCount())
};
}

return dump;
}
2 changes: 1 addition & 1 deletion node/src/tests/test-Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ test('worker.createRouter() succeeds', async () =>

await expect(worker.dump())
.resolves
.toEqual(
.toMatchObject(
{
pid : worker.pid,
webRtcServerIds : [],
Expand Down
6 changes: 3 additions & 3 deletions node/src/tests/test-WebRtcServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ test('worker.createWebRtcServer() succeeds', async () =>

await expect(worker.dump())
.resolves
.toEqual(
.toMatchObject(
{
pid : worker.pid,
webRtcServerIds : [ webRtcServer.id ],
Expand Down Expand Up @@ -120,7 +120,7 @@ test('worker.createWebRtcServer() without specifying port succeeds', async () =>

await expect(worker.dump())
.resolves
.toEqual(
.toMatchObject(
{
pid : worker.pid,
webRtcServerIds : [ webRtcServer.id ],
Expand Down Expand Up @@ -565,7 +565,7 @@ test('router.createWebRtcTransport() with webRtcServer succeeds and webRtcServer

await expect(worker.dump())
.resolves
.toEqual(
.toMatchObject(
{
pid : worker.pid,
webRtcServerIds : [],
Expand Down
2 changes: 1 addition & 1 deletion node/src/tests/test-Worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ test('worker.dump() succeeds', async () =>

await expect(worker.dump())
.resolves
.toEqual(
.toMatchObject(
{
pid : worker.pid,
webRtcServerIds : [],
Expand Down
7 changes: 6 additions & 1 deletion rust/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::webrtc_server::{
use crate::webrtc_transport::{
WebRtcTransportListen, WebRtcTransportListenInfos, WebRtcTransportOptions,
};
use crate::worker::{ChannelMessageHandlers, WorkerDump, WorkerUpdateSettings};
use crate::worker::{ChannelMessageHandlers, LibUringDump, WorkerDump, WorkerUpdateSettings};
use mediasoup_sys::fbs::{
active_speaker_observer, audio_level_observer, consumer, data_consumer, data_producer,
direct_transport, message, notification, pipe_transport, plain_transport, producer, request,
Expand Down Expand Up @@ -165,6 +165,11 @@ impl Request for WorkerDumpRequest {
.map(|id| id.parse())
.collect::<Result<_, _>>()?,
},
liburing: data.liburing.map(|liburing| LibUringDump {
sqe_process_count: liburing.sqe_process_count,
sqe_miss_count: liburing.sqe_miss_count,
user_data_miss_count: liburing.user_data_miss_count,
}),
})
}
}
Expand Down
9 changes: 9 additions & 0 deletions rust/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@ pub struct ChannelMessageHandlers {
pub channel_notification_handlers: Vec<Uuid>,
}

#[derive(Debug, Clone, Deserialize, Serialize, Eq, PartialEq)]
#[doc(hidden)]
pub struct LibUringDump {
pub sqe_process_count: u64,
pub sqe_miss_count: u64,
pub user_data_miss_count: u64,
}

#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
#[doc(hidden)]
Expand All @@ -288,6 +296,7 @@ pub struct WorkerDump {
#[serde(rename = "webRtcServerIds")]
pub webrtc_server_ids: Vec<WebRtcServerId>,
pub channel_message_handlers: ChannelMessageHandlers,
pub liburing: Option<LibUringDump>,
}

/// Error that caused [`Worker::create_webrtc_server`] to fail.
Expand Down
8 changes: 8 additions & 0 deletions worker/fbs/liburing.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace FBS.LibUring;

table Dump {
sqe_process_count: uint64;
sqe_miss_count: uint64;
user_data_miss_count: uint64;
}

1 change: 1 addition & 0 deletions worker/fbs/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ flatbuffers_schemas = [
'dataConsumer.fbs',
'dataProducer.fbs',
'directTransport.fbs',
'liburing.fbs',
'log.fbs',
'message.fbs',
'notification.fbs',
Expand Down
2 changes: 2 additions & 0 deletions worker/fbs/worker.fbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include "liburing.fbs";
include "transport.fbs";

namespace FBS.Worker;
Expand All @@ -12,6 +13,7 @@ table DumpResponse {
web_rtc_server_ids: [string] (required);
router_ids: [string] (required);
channel_message_handlers: ChannelMessageHandlers (required);
liburing: FBS.LibUring.Dump;
}

table ResourceUsageResponse {
Expand Down
104 changes: 104 additions & 0 deletions worker/include/DepLibUring.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#ifndef MS_DEP_LIBURING_HPP
#define MS_DEP_LIBURING_HPP

#include "DepLibUV.hpp"
#include "FBS/liburing.h"
#include <functional>
#include <liburing.h>
#include <queue>

class DepLibUring
{
public:
using onSendCallback = const std::function<void(bool sent)>;

/* Struct for the user data field of SQE and CQE. */
struct UserData
{
uint8_t store[1500]{};
onSendCallback* cb{ nullptr };
size_t idx{ 0 };
};

/* Number of submission queue entries (SQE). */
static constexpr size_t QueueDepth{ 1024 * 4 };

static bool IsRuntimeSupported();
static void ClassInit();
static void ClassDestroy();
static flatbuffers::Offset<FBS::LibUring::Dump> FillBuffer(flatbuffers::FlatBufferBuilder& builder);
static void StartPollingCQEs();
static void StopPollingCQEs();
static bool PrepareSend(
int sockfd, const void* data, size_t len, const struct sockaddr* addr, onSendCallback* cb);
static bool PrepareWrite(
int sockfd, const void* data1, size_t len1, const void* data2, size_t len2, onSendCallback* cb);
static void Submit();
static void SetActive();
static bool IsActive();

class LibUring;

thread_local static LibUring* liburing;

public:
class LibUring
{
public:
LibUring();
~LibUring();
flatbuffers::Offset<FBS::LibUring::Dump> FillBuffer(flatbuffers::FlatBufferBuilder& builder) const;
void StartPollingCQEs();
void StopPollingCQEs();
bool PrepareSend(
int sockfd, const void* data, size_t len, const struct sockaddr* addr, onSendCallback* cb);
bool PrepareWrite(
int sockfd, const void* data1, size_t len1, const void* data2, size_t len2, onSendCallback* cb);
void Submit();
void SetActive()
{
this->active = true;
}
bool IsActive() const
{
return this->active;
}
io_uring* GetRing()
{
return std::addressof(this->ring);
}
int GetEventFd() const
{
return this->efd;
}
void ReleaseUserDataEntry(size_t idx)
{
this->availableUserDataEntries.push(idx);
}

private:
UserData* GetUserData();

private:
// io_uring instance.
io_uring ring;
// Event file descriptor to watch for completions.
int efd;
// libuv handle used to poll io_uring completions.
uv_poll_t* uvHandle{ nullptr };
// Whether we are currently sending RTP over io_uring.
bool active{ false };
// Pre-allocated UserData entries.
UserData userDataBuffer[QueueDepth]{};
// Indexes of available UserData entries.
std::queue<size_t> availableUserDataEntries;
// Submission queue entry process count.
uint64_t sqeProcessCount{ 0u };
// Submission queue entry miss count.
uint64_t sqeMissCount{ 0u };
// User data miss count.
uint64_t userDataMissCount{ 0u };
};
};

#endif
4 changes: 4 additions & 0 deletions worker/include/handles/TcpConnectionHandle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ class TcpConnectionHandle
uv_tcp_t* uvHandle{ nullptr };
// Others.
struct sockaddr_storage* localAddr{ nullptr };
#ifdef MS_LIBURING_SUPPORTED
// Local file descriptor for io_uring.
uv_os_fd_t fd{ 0u };
#endif
bool closed{ false };
size_t recvBytes{ 0u };
size_t sentBytes{ 0u };
Expand Down
4 changes: 4 additions & 0 deletions worker/include/handles/UdpSocketHandle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ class UdpSocketHandle
// Allocated by this (may be passed by argument).
uv_udp_t* uvHandle{ nullptr };
// Others.
#ifdef MS_LIBURING_SUPPORTED
// Local file descriptor for io_uring.
uv_os_fd_t fd{ 0u };
#endif
bool closed{ false };
size_t recvBytes{ 0u };
size_t sentBytes{ 0u };
Expand Down
22 changes: 22 additions & 0 deletions worker/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,28 @@ if host_machine.system() == 'windows'
]
endif

if host_machine.system() == 'linux'
kernel_version = run_command('uname', '-r').stdout().strip()

# Enable liburing for kernel versions greather than or equal to 6.
if kernel_version[0].to_int() >= 6
liburing_proj = subproject('liburing', default_options: ['default_library=static'], required: true)

dependencies += [
liburing_proj.get_variable('uring'),
]
link_whole += [
liburing_proj.get_variable('liburing')
]
common_sources += [
'src/DepLibUring.cpp',
]
cpp_args += [
'-DMS_LIBURING_SUPPORTED',
]
endif
endif

libmediasoup_worker = library(
'libmediasoup-worker',
name_prefix: '',
Expand Down