Commit 6d7c36e9 authored by Théo Lebrun's avatar Théo Lebrun
Browse files

module-rt: implement module-rt without rtkit

parent 7e85cd5e
......@@ -22,6 +22,7 @@ module_sources = [
'module-protocol-simple.c',
'module-pulse-tunnel.c',
'module-rt.c',
'module-rt-no-rtkit.c',
'module-raop-discover.c',
'module-session-manager.c',
'module-zeroconf-discover.c',
......@@ -131,8 +132,7 @@ pipewire_module_profiler = shared_library('pipewire-module-profiler',
dependencies : [spa_dep, mathlib, dl_lib, pipewire_dep],
)
build_module_rt = dbus_dep.found()
if build_module_rt
if dbus_dep.found()
pipewire_module_rt = shared_library('pipewire-module-rt', [ 'module-rt.c' ],
include_directories : [configinc],
install : true,
......@@ -149,8 +149,15 @@ pipewire_module_rtkit = shared_library('pipewire-module-rtkit', [ 'module-rt.c'
install_rpath: modules_install_dir,
dependencies : [dbus_dep, mathlib, dl_lib, pipewire_dep],
)
else
pipewire_module_rt = shared_library('pipewire-module-rt', [ 'module-rt-no-rtkit.c' ],
include_directories : [configinc],
install : true,
install_dir : modules_install_dir,
install_rpath: modules_install_dir,
dependencies : [dbus_dep, mathlib, dl_lib, pipewire_dep],
)
endif
summary({'rt': build_module_rt}, bool_yn: true, section: 'Optional Modules')
build_module_portal = dbus_dep.found()
if build_module_portal
......
/* PipeWire
*
* Copyright © 2018 Wim Taymans
* Copyright © 2022 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
......@@ -38,22 +38,12 @@
#include "config.h"
#include <spa/support/dbus.h>
#include <spa/utils/result.h>
#include <spa/utils/string.h>
#include <pipewire/impl.h>
#include <pipewire/thread.h>
/** \page page_module_rt PipeWire Module: RT
*
* The `rt` module uses the operating system's scheduler to enable realtime
* scheduling for certain threads to assist with low latency audio processing.
* This requires `RLIMIT_RTPRIO` to be set to a value that's equal to this
* module's `rt.prio` parameter or higher. Most distros will come with some
* package that configures this for certain groups or users. If this is not set
* up and DBus is available, then this module will fall back to using RTKit.
*/
#define NAME "rt"
......@@ -82,22 +72,11 @@ PW_LOG_TOPIC_STATIC(mod_topic, "mod." NAME);
static const struct spa_dict_item module_props[] = {
{ PW_KEY_MODULE_AUTHOR, "Wim Taymans <wim.taymans@gmail.com>" },
{ PW_KEY_MODULE_DESCRIPTION, "Use realtime thread scheduling, falling back to RTKit" },
{ PW_KEY_MODULE_DESCRIPTION, "Use realtime thread scheduling" },
{ PW_KEY_MODULE_USAGE, MODULE_USAGE },
{ PW_KEY_MODULE_VERSION, PACKAGE_VERSION },
};
struct pw_rtkit_bus;
struct thread {
struct impl *impl;
struct spa_list link;
pthread_t thread;
pid_t pid;
void *(*start)(void*);
void *arg;
};
struct impl {
struct pw_context *context;
......@@ -109,101 +88,16 @@ struct impl {
rlim_t rt_time_hard;
struct spa_hook module_listener;
bool use_rtkit;
struct pw_rtkit_bus *system_bus;
/* These are only for the RTKit implementation to fill in the `thread`
* struct. Since there's barely any overhead here we'll do this
* regardless of which backend is used. */
pthread_mutex_t lock;
pthread_cond_t cond;
struct spa_list threads_list;
};
/***
Copyright 2009 Lennart Poettering
Copyright 2010 David Henningsson <diwic@ubuntu.com>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
***/
#include <dbus/dbus.h>
#include "config.h"
#include <sys/syscall.h>
#define RTKIT_SERVICE_NAME "org.freedesktop.RealtimeKit1"
#define RTKIT_OBJECT_PATH "/org/freedesktop/RealtimeKit1"
#ifndef RLIMIT_RTTIME
#define RLIMIT_RTTIME 15
#endif
/** \cond */
struct pw_rtkit_bus {
DBusConnection *bus;
};
/** \endcond */
struct pw_rtkit_bus *pw_rtkit_bus_get_system(void)
{
struct pw_rtkit_bus *bus;
DBusError error;
if (getenv("DISABLE_RTKIT")) {
errno = ENOTSUP;
return NULL;
}
dbus_error_init(&error);
bus = calloc(1, sizeof(struct pw_rtkit_bus));
if (bus == NULL)
return NULL;
bus->bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
if (bus->bus == NULL)
goto error;
dbus_connection_set_exit_on_disconnect(bus->bus, false);
return bus;
error:
free(bus);
pw_log_error("Failed to connect to system bus: %s", error.message);
dbus_error_free(&error);
errno = ECONNREFUSED;
return NULL;
}
void pw_rtkit_bus_free(struct pw_rtkit_bus *system_bus)
{
dbus_connection_close(system_bus->bus);
dbus_connection_unref(system_bus->bus);
free(system_bus);
}
static pid_t _gettid(void)
{
#if defined(HAVE_GETTID)
......@@ -219,246 +113,6 @@ static pid_t _gettid(void)
#endif
}
static int translate_error(const char *name)
{
pw_log_warn("RTKit error: %s", name);
if (spa_streq(name, DBUS_ERROR_NO_MEMORY))
return -ENOMEM;
if (spa_streq(name, DBUS_ERROR_SERVICE_UNKNOWN) ||
spa_streq(name, DBUS_ERROR_NAME_HAS_NO_OWNER))
return -ENOENT;
if (spa_streq(name, DBUS_ERROR_ACCESS_DENIED) ||
spa_streq(name, DBUS_ERROR_AUTH_FAILED))
return -EACCES;
return -EIO;
}
static long long rtkit_get_int_property(struct pw_rtkit_bus *connection, const char *propname,
long long *propval)
{
DBusMessage *m = NULL, *r = NULL;
DBusMessageIter iter, subiter;
dbus_int64_t i64;
dbus_int32_t i32;
DBusError error;
int current_type;
long long ret;
const char *interfacestr = "org.freedesktop.RealtimeKit1";
dbus_error_init(&error);
if (!(m = dbus_message_new_method_call(RTKIT_SERVICE_NAME,
RTKIT_OBJECT_PATH,
"org.freedesktop.DBus.Properties", "Get"))) {
ret = -ENOMEM;
goto finish;
}
if (!dbus_message_append_args(m,
DBUS_TYPE_STRING, &interfacestr,
DBUS_TYPE_STRING, &propname, DBUS_TYPE_INVALID)) {
ret = -ENOMEM;
goto finish;
}
if (!(r = dbus_connection_send_with_reply_and_block(connection->bus, m, -1, &error))) {
ret = translate_error(error.name);
goto finish;
}
if (dbus_set_error_from_message(&error, r)) {
ret = translate_error(error.name);
goto finish;
}
ret = -EBADMSG;
dbus_message_iter_init(r, &iter);
while ((current_type = dbus_message_iter_get_arg_type(&iter)) != DBUS_TYPE_INVALID) {
if (current_type == DBUS_TYPE_VARIANT) {
dbus_message_iter_recurse(&iter, &subiter);
while ((current_type =
dbus_message_iter_get_arg_type(&subiter)) != DBUS_TYPE_INVALID) {
if (current_type == DBUS_TYPE_INT32) {
dbus_message_iter_get_basic(&subiter, &i32);
*propval = i32;
ret = 0;
}
if (current_type == DBUS_TYPE_INT64) {
dbus_message_iter_get_basic(&subiter, &i64);
*propval = i64;
ret = 0;
}
dbus_message_iter_next(&subiter);
}
}
dbus_message_iter_next(&iter);
}
finish:
if (m)
dbus_message_unref(m);
if (r)
dbus_message_unref(r);
dbus_error_free(&error);
return ret;
}
int pw_rtkit_get_max_realtime_priority(struct pw_rtkit_bus *connection)
{
long long retval;
int err;
err = rtkit_get_int_property(connection, "MaxRealtimePriority", &retval);
return err < 0 ? err : retval;
}
int pw_rtkit_get_min_nice_level(struct pw_rtkit_bus *connection, int *min_nice_level)
{
long long retval;
int err;
err = rtkit_get_int_property(connection, "MinNiceLevel", &retval);
if (err >= 0)
*min_nice_level = retval;
return err;
}
long long pw_rtkit_get_rttime_usec_max(struct pw_rtkit_bus *connection)
{
long long retval;
int err;
err = rtkit_get_int_property(connection, "RTTimeUSecMax", &retval);
return err < 0 ? err : retval;
}
int pw_rtkit_make_realtime(struct pw_rtkit_bus *connection, pid_t thread, int priority)
{
DBusMessage *m = NULL, *r = NULL;
dbus_uint64_t u64;
dbus_uint32_t u32;
DBusError error;
int ret;
dbus_error_init(&error);
if (thread == 0)
thread = _gettid();
if (!(m = dbus_message_new_method_call(RTKIT_SERVICE_NAME,
RTKIT_OBJECT_PATH,
"org.freedesktop.RealtimeKit1",
"MakeThreadRealtime"))) {
ret = -ENOMEM;
goto finish;
}
u64 = (dbus_uint64_t) thread;
u32 = (dbus_uint32_t) priority;
if (!dbus_message_append_args(m,
DBUS_TYPE_UINT64, &u64,
DBUS_TYPE_UINT32, &u32, DBUS_TYPE_INVALID)) {
ret = -ENOMEM;
goto finish;
}
if (!(r = dbus_connection_send_with_reply_and_block(connection->bus, m, -1, &error))) {
ret = translate_error(error.name);
goto finish;
}
if (dbus_set_error_from_message(&error, r)) {
ret = translate_error(error.name);
goto finish;
}
ret = 0;
finish:
if (m)
dbus_message_unref(m);
if (r)
dbus_message_unref(r);
dbus_error_free(&error);
return ret;
}
int pw_rtkit_make_high_priority(struct pw_rtkit_bus *connection, pid_t thread, int nice_level)
{
DBusMessage *m = NULL, *r = NULL;
dbus_uint64_t u64;
dbus_int32_t s32;
DBusError error;
int ret;
dbus_error_init(&error);
if (thread == 0)
thread = _gettid();
if (!(m = dbus_message_new_method_call(RTKIT_SERVICE_NAME,
RTKIT_OBJECT_PATH,
"org.freedesktop.RealtimeKit1",
"MakeThreadHighPriority"))) {
ret = -ENOMEM;
goto finish;
}
u64 = (dbus_uint64_t) thread;
s32 = (dbus_int32_t) nice_level;
if (!dbus_message_append_args(m,
DBUS_TYPE_UINT64, &u64,
DBUS_TYPE_INT32, &s32, DBUS_TYPE_INVALID)) {
ret = -ENOMEM;
goto finish;
}
if (!(r = dbus_connection_send_with_reply_and_block(connection->bus, m, -1, &error))) {
ret = translate_error(error.name);
goto finish;
}
if (dbus_set_error_from_message(&error, r)) {
ret = translate_error(error.name);
goto finish;
}
ret = 0;
finish:
if (m)
dbus_message_unref(m);
if (r)
dbus_message_unref(r);
dbus_error_free(&error);
return ret;
}
static void module_destroy(void *data)
{
struct impl *impl = data;
......@@ -466,8 +120,6 @@ static void module_destroy(void *data)
pw_thread_utils_set(NULL);
spa_hook_remove(&impl->module_listener);
if (impl->system_bus)
pw_rtkit_bus_free(impl->system_bus);
free(impl);
}
......@@ -521,23 +173,14 @@ static int set_nice(struct impl *impl, int nice_level)
int res = 0;
pid_t tid;
if (impl->use_rtkit) {
if ((res = pw_rtkit_make_high_priority(impl->system_bus, 0, nice_level)) < 0) {
pw_log_warn("could not set nice-level to %d: %s",
nice_level, spa_strerror(res));
} else {
pw_log_info("main thread nice level set to %d", nice_level);
}
tid = _gettid();
if (setpriority(PRIO_PROCESS, tid, nice_level) == 0) {
pw_log_info("main thread nice level set to %d",
nice_level);
} else {
tid = _gettid();
if (setpriority(PRIO_PROCESS, tid, nice_level) == 0) {
pw_log_info("main thread nice level set to %d",
nice_level);
} else {
res = -errno;
pw_log_warn("could not set nice-level to %d: %s",
nice_level, spa_strerror(res));
}
res = -errno;
pw_log_warn("could not set nice-level to %d: %s",
nice_level, spa_strerror(res));
}
return res;
......@@ -546,192 +189,78 @@ static int set_nice(struct impl *impl, int nice_level)
static int set_rlimit(struct impl *impl)
{
struct rlimit rl;
long long rttime;
int res = 0;
spa_zero(rl);
rl.rlim_cur = impl->rt_time_soft;
rl.rlim_max = impl->rt_time_hard;
if (impl->use_rtkit) {
rttime = pw_rtkit_get_rttime_usec_max(impl->system_bus);
if (rttime >= 0) {
if ((rlim_t)rttime < rl.rlim_cur) {
pw_log_debug("clamping rt.time.soft from %ld to %lld because of RTKit",
rl.rlim_cur, rttime);
}
rl.rlim_cur = SPA_MIN(rl.rlim_cur, (rlim_t)rttime);
rl.rlim_max = SPA_MIN(rl.rlim_max, (rlim_t)rttime);
}
}
if (setrlimit(RLIMIT_RTTIME, &rl) < 0)
if (setrlimit(RLIMIT_RTTIME, &rl) < 0) {
res = -errno;
if (res < 0)
pw_log_debug("setrlimit() failed: %s", spa_strerror(res));
else
} else {
pw_log_debug("rt.time.soft:%"PRIi64" rt.time.hard:%"PRIi64,
(int64_t)rl.rlim_cur, (int64_t)rl.rlim_max);
return res;
}
static struct thread *find_thread_by_pt(struct impl *impl, pthread_t pt)
{
struct thread *t;
spa_list_for_each(t, &impl->threads_list, link) {
if (pthread_equal(t->thread, pt))
return t;
}
return NULL;
}
static void *custom_start(void *data)
{
struct thread *this = data;
struct impl *impl = this->impl;
pthread_mutex_lock(&impl->lock);
this->pid = _gettid();
pthread_cond_broadcast(&impl->cond);
pthread_mutex_unlock(&impl->lock);
return this->start(this->arg);
return res;
}
static struct spa_thread *impl_create(void *data, const struct spa_dict *props,
void *(*start_routine)(void*), void *arg)
{
struct impl *impl = data;
struct thread *this;
int err;
pthread_t thread;
this = calloc(1, sizeof(*this));
this->impl = impl;
this->start = start_routine;
this->arg = arg;
/* This thread list is only used for the RTKit implementation */
pthread_mutex_lock(&impl->lock);
err = pthread_create(&this->thread, NULL, custom_start, this);
if (err != 0)
goto exit;
pthread_cond_wait(&impl->cond, &impl->lock);
spa_list_append(&impl->threads_list, &this->link);
exit:
pthread_mutex_unlock(&impl->lock);
err = pthread_create(&thread, NULL, start_routine, arg);
if (err != 0) {
errno = err;
free(this);
return NULL;
}
return (struct spa_thread*)this->thread;
return (struct spa_thread*)thread;
}
static int impl_join(void *data, struct spa_thread *thread, void **retval)
{
struct impl *impl = data;
pthread_t pt = (pthread_t)thread;
struct thread *thr;
pthread_mutex_lock(&impl->lock);
if ((thr = find_thread_by_pt(impl, pt)) != NULL) {
spa_list_remove(&thr->link);
free(thr);
}
pthread_mutex_unlock(&impl->lock);
return pthread_join(pt, retval);
return pthread_join((pthread_t)thread, retval);
}
static int impl_get_rt_range(void *data, const struct spa_dict *props,
int *min, int *max)
{
struct impl *impl = data;
if (impl->use_rtkit) {
if (min)
*min = 1;
if (max)
*max = pw_rtkit_get_max_realtime_priority(impl->system_bus);
} else {
if (min)
*min = sched_get_priority_min(REALTIME_POLICY);
if (max)
*max = sched_get_priority_max(REALTIME_POLICY);
}
if (min)
*min = sched_get_priority_min(REALTIME_POLICY);
if (max)
*max = sched_get_priority_max(REALTIME_POLICY);
return 0;
}
static pid_t impl_gettid(struct impl *impl, pthread_t pt)
{
struct thread *thr;
pid_t pid;
pthread_mutex_lock(&impl->lock);
if ((thr = find_thread_by_pt(impl, pt)) != NULL)
pid = thr->pid;
else