2023-02-08 18:12:00 +01:00
|
|
|
/* PipeWire */
|
|
|
|
|
/* SPDX-FileCopyrightText: Copyright © 2022 Wim Taymans */
|
|
|
|
|
/* SPDX-License-Identifier: MIT */
|
2022-03-10 11:19:12 +01:00
|
|
|
/***
|
|
|
|
|
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.
|
|
|
|
|
***/
|
2018-02-08 10:02:17 +01:00
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <sys/stat.h>
|
2022-06-19 14:03:56 -04:00
|
|
|
#if defined(__FreeBSD__) || defined(__MidnightBSD__)
|
2019-11-10 15:41:01 +04:00
|
|
|
#include <sys/thr.h>
|
|
|
|
|
#endif
|
2023-09-24 16:17:12 +02:00
|
|
|
#if defined(__GNU__)
|
2023-10-08 11:38:59 +02:00
|
|
|
#include <hurd.h>
|
2023-09-24 16:17:12 +02:00
|
|
|
#endif
|
2018-02-08 10:02:17 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <sys/resource.h>
|
2022-03-10 11:19:12 +01:00
|
|
|
#include <sys/syscall.h>
|
2018-02-08 10:02:17 +01:00
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
2019-10-25 15:01:02 +02:00
|
|
|
#include <spa/utils/result.h>
|
2021-05-18 11:36:13 +10:00
|
|
|
#include <spa/utils/string.h>
|
2018-02-08 10:02:17 +01:00
|
|
|
|
2019-12-11 10:57:10 +01:00
|
|
|
#include <pipewire/impl.h>
|
2021-07-05 10:26:56 +02:00
|
|
|
#include <pipewire/thread.h>
|
2018-02-08 10:02:17 +01:00
|
|
|
|
2022-03-10 11:19:12 +01:00
|
|
|
#ifdef HAVE_DBUS
|
|
|
|
|
#include <spa/support/dbus.h>
|
2023-12-30 01:26:01 +01:00
|
|
|
#include <spa-private/dbus-helpers.h>
|
2022-03-10 11:19:12 +01:00
|
|
|
#include <dbus/dbus.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-11-18 15:11:03 +02:00
|
|
|
/** \page page_module_rt RT
|
2022-01-15 20:36:48 +01:00
|
|
|
*
|
2022-04-22 17:08:45 +02:00
|
|
|
* The `rt` modules can give real-time priorities to processing threads.
|
|
|
|
|
*
|
|
|
|
|
* It uses the operating system's scheduler to enable realtime scheduling
|
|
|
|
|
* for certain threads to assist with low latency audio processing.
|
2022-01-15 20:36:48 +01:00
|
|
|
* 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
|
2023-07-04 11:11:48 +02:00
|
|
|
* up and DBus is available, then this module will fall back to using the Portal
|
|
|
|
|
* Realtime DBus API or RTKit.
|
2022-04-22 17:08:45 +02:00
|
|
|
*
|
2023-11-20 00:23:03 +02:00
|
|
|
* ## Module Name
|
|
|
|
|
*
|
|
|
|
|
* `libpipewire-module-rt`
|
|
|
|
|
*
|
2022-04-22 17:08:45 +02:00
|
|
|
* ## Module Options
|
|
|
|
|
*
|
|
|
|
|
* - `nice.level`: The nice value set for the application thread. It improves
|
|
|
|
|
* performance of the communication with the pipewire daemon.
|
|
|
|
|
* - `rt.prio`: The realtime priority of the data thread. Higher values are
|
|
|
|
|
* higher priority.
|
|
|
|
|
* - `rt.time.soft`, `rt.time.hard`: The amount of CPU time an RT thread can
|
|
|
|
|
* consume without doing any blocking calls before the kernel kills
|
|
|
|
|
* the thread. This is a safety measure to avoid lockups of the complete
|
|
|
|
|
* system when some thread consumes 100%.
|
2023-07-04 11:11:48 +02:00
|
|
|
* - `rlimits.enabled`: enable the use of rtlimits, default true.
|
|
|
|
|
* - `rtportal.enabled`: enable the use of realtime portal, default true
|
|
|
|
|
* - `rtkit.enabled`: enable the use of rtkit, default true
|
2023-11-05 20:11:19 +10:00
|
|
|
* - `uclamp.min`: the minimum utilisation value the scheduler should consider
|
|
|
|
|
* - `uclamp.max`: the maximum utilisation value the scheduler should consider
|
2022-04-22 17:08:45 +02:00
|
|
|
|
|
|
|
|
* The nice level is by default set to an invalid value so that clients don't
|
|
|
|
|
* automatically have the nice level raised.
|
|
|
|
|
*
|
|
|
|
|
* The PipeWire server processes are explicitly configured with a valid nice level.
|
|
|
|
|
*
|
|
|
|
|
* ## Example configuration
|
|
|
|
|
*
|
|
|
|
|
*\code{.unparsed}
|
|
|
|
|
* context.modules = [
|
|
|
|
|
* { name = libpipewire-module-rt
|
|
|
|
|
* args = {
|
|
|
|
|
* #nice.level = 20
|
|
|
|
|
* #rt.prio = 88
|
|
|
|
|
* #rt.time.soft = -1
|
|
|
|
|
* #rt.time.hard = -1
|
2023-07-04 11:11:48 +02:00
|
|
|
* #rlimits.enabled = true
|
|
|
|
|
* #rtportal.enabled = true
|
|
|
|
|
* #rtkit.enabled = true
|
2023-11-05 20:11:19 +10:00
|
|
|
* #uclamp.min = 0
|
|
|
|
|
* #uclamp.max = 1024
|
2022-04-22 17:08:45 +02:00
|
|
|
* }
|
|
|
|
|
* flags = [ ifexists nofail ]
|
|
|
|
|
* }
|
|
|
|
|
* ]
|
|
|
|
|
*\endcode
|
2021-06-24 14:06:30 +10:00
|
|
|
*/
|
|
|
|
|
|
2022-01-15 20:54:15 +01:00
|
|
|
#define NAME "rt"
|
2021-09-16 15:35:10 +10:00
|
|
|
|
|
|
|
|
PW_LOG_TOPIC_STATIC(mod_topic, "mod." NAME);
|
|
|
|
|
#define PW_LOG_TOPIC_DEFAULT mod_topic
|
|
|
|
|
|
2022-01-15 20:36:48 +01:00
|
|
|
#define REALTIME_POLICY SCHED_FIFO
|
|
|
|
|
#ifdef SCHED_RESET_ON_FORK
|
|
|
|
|
#define PW_SCHED_RESET_ON_FORK SCHED_RESET_ON_FORK
|
|
|
|
|
#else
|
2022-01-18 13:32:25 +01:00
|
|
|
/* FreeBSD compat */
|
2022-01-15 20:36:48 +01:00
|
|
|
#define PW_SCHED_RESET_ON_FORK 0
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-05-07 19:37:32 +02:00
|
|
|
#define MIN_NICE_LEVEL -20
|
|
|
|
|
#define MAX_NICE_LEVEL 19
|
|
|
|
|
#define IS_VALID_NICE_LEVEL(l) ((l)>=MIN_NICE_LEVEL && (l)<=MAX_NICE_LEVEL)
|
2022-01-18 13:32:25 +01:00
|
|
|
|
2023-07-04 10:35:57 +02:00
|
|
|
#define DEFAULT_NICE_LEVEL 20 /* invalid value by default, see above */
|
2023-01-16 12:37:03 +01:00
|
|
|
#define DEFAULT_RT_PRIO_MIN 11
|
2021-03-03 19:56:22 +01:00
|
|
|
#define DEFAULT_RT_PRIO 88
|
2022-01-17 14:01:06 +01:00
|
|
|
#define DEFAULT_RT_TIME_SOFT -1
|
|
|
|
|
#define DEFAULT_RT_TIME_HARD -1
|
2020-02-10 10:43:31 +01:00
|
|
|
|
2023-11-05 20:11:19 +10:00
|
|
|
#define DEFAULT_UCLAMP_MIN 0
|
|
|
|
|
#define DEFAULT_UCLAMP_MAX 1024
|
|
|
|
|
|
2023-03-22 16:35:55 +01:00
|
|
|
#define MODULE_USAGE "( nice.level=<priority: default "SPA_STRINGIFY(DEFAULT_NICE_LEVEL)"(don't change)> ) " \
|
|
|
|
|
"( rt.prio=<priority: default "SPA_STRINGIFY(DEFAULT_RT_PRIO)"> ) " \
|
2023-07-04 11:11:48 +02:00
|
|
|
"( rt.time.soft=<in usec: default "SPA_STRINGIFY(DEFAULT_RT_TIME_SOFT)"> ) " \
|
|
|
|
|
"( rt.time.hard=<in usec: default "SPA_STRINGIFY(DEFAULT_RT_TIME_HARD)"> ) " \
|
|
|
|
|
"( rlimits.enabled=<default true> ) " \
|
|
|
|
|
"( rtportal.enabled=<default true> ) " \
|
2023-11-05 20:11:19 +10:00
|
|
|
"( rtkit.enabled=<default true> ) " \
|
|
|
|
|
"( uclamp.min=<default "SPA_STRINGIFY(DEFAULT_UCLAMP_MIN)"> ) " \
|
|
|
|
|
"( uclamp.max=<default "SPA_STRINGIFY(DEFAULT_UCLAMP_MAX)"> )"
|
2020-02-10 10:43:31 +01:00
|
|
|
|
2018-06-01 11:23:02 +02:00
|
|
|
static const struct spa_dict_item module_props[] = {
|
2019-05-24 15:47:48 +02:00
|
|
|
{ PW_KEY_MODULE_AUTHOR, "Wim Taymans <wim.taymans@gmail.com>" },
|
2022-01-15 20:36:48 +01:00
|
|
|
{ PW_KEY_MODULE_DESCRIPTION, "Use realtime thread scheduling, falling back to RTKit" },
|
2020-02-10 10:43:31 +01:00
|
|
|
{ PW_KEY_MODULE_USAGE, MODULE_USAGE },
|
2019-05-24 15:47:48 +02:00
|
|
|
{ PW_KEY_MODULE_VERSION, PACKAGE_VERSION },
|
2018-06-01 11:23:02 +02:00
|
|
|
};
|
|
|
|
|
|
2022-03-10 11:19:12 +01:00
|
|
|
#ifdef HAVE_DBUS
|
|
|
|
|
#define RTKIT_SERVICE_NAME "org.freedesktop.RealtimeKit1"
|
|
|
|
|
#define RTKIT_OBJECT_PATH "/org/freedesktop/RealtimeKit1"
|
2022-07-19 00:02:37 -04:00
|
|
|
#define RTKIT_INTERFACE "org.freedesktop.RealtimeKit1"
|
|
|
|
|
|
|
|
|
|
#define XDG_PORTAL_SERVICE_NAME "org.freedesktop.portal.Desktop"
|
|
|
|
|
#define XDG_PORTAL_OBJECT_PATH "/org/freedesktop/portal/desktop"
|
|
|
|
|
#define XDG_PORTAL_INTERFACE "org.freedesktop.portal.Realtime"
|
2022-03-10 11:19:12 +01:00
|
|
|
|
|
|
|
|
/** \cond */
|
|
|
|
|
struct pw_rtkit_bus {
|
|
|
|
|
DBusConnection *bus;
|
|
|
|
|
};
|
|
|
|
|
/** \endcond */
|
2021-01-05 12:33:41 +01:00
|
|
|
|
2021-07-05 10:26:56 +02:00
|
|
|
struct thread {
|
|
|
|
|
struct impl *impl;
|
|
|
|
|
struct spa_list link;
|
|
|
|
|
pthread_t thread;
|
|
|
|
|
pid_t pid;
|
|
|
|
|
void *(*start)(void*);
|
|
|
|
|
void *arg;
|
|
|
|
|
};
|
2022-03-10 11:19:12 +01:00
|
|
|
#endif /* HAVE_DBUS */
|
2021-07-05 10:26:56 +02:00
|
|
|
|
2018-02-08 10:02:17 +01:00
|
|
|
struct impl {
|
2019-12-10 18:19:56 +01:00
|
|
|
struct pw_context *context;
|
2018-02-08 10:02:17 +01:00
|
|
|
|
2021-07-06 15:48:20 +02:00
|
|
|
struct spa_thread_utils thread_utils;
|
2021-07-05 10:26:56 +02:00
|
|
|
|
2023-07-20 12:16:13 +02:00
|
|
|
pid_t main_pid;
|
2023-07-20 12:03:29 +02:00
|
|
|
struct rlimit rl;
|
2021-01-05 12:33:41 +01:00
|
|
|
int nice_level;
|
2020-02-10 10:43:31 +01:00
|
|
|
int rt_prio;
|
|
|
|
|
rlim_t rt_time_soft;
|
|
|
|
|
rlim_t rt_time_hard;
|
2018-02-08 10:02:17 +01:00
|
|
|
|
2023-11-05 20:11:19 +10:00
|
|
|
int uclamp_min;
|
|
|
|
|
int uclamp_max;
|
|
|
|
|
|
2018-02-08 10:02:17 +01:00
|
|
|
struct spa_hook module_listener;
|
2022-01-15 20:36:48 +01:00
|
|
|
|
2023-07-04 11:11:48 +02:00
|
|
|
unsigned rlimits_enabled:1;
|
|
|
|
|
unsigned rtportal_enabled:1;
|
|
|
|
|
unsigned rtkit_enabled:1;
|
|
|
|
|
|
2022-03-10 11:19:12 +01:00
|
|
|
#ifdef HAVE_DBUS
|
2022-01-15 20:36:48 +01:00
|
|
|
bool use_rtkit;
|
2022-07-19 00:02:37 -04:00
|
|
|
/* For D-Bus. These are const static. */
|
|
|
|
|
const char* service_name;
|
|
|
|
|
const char* object_path;
|
|
|
|
|
const char* interface;
|
|
|
|
|
struct pw_rtkit_bus *rtkit_bus;
|
2023-07-20 12:03:29 +02:00
|
|
|
struct pw_thread_loop *thread_loop;
|
|
|
|
|
int max_rtprio;
|
|
|
|
|
int min_nice_level;
|
|
|
|
|
rlim_t rttime_max;
|
2022-01-15 20:36:48 +01:00
|
|
|
|
2022-01-18 13:32:25 +01:00
|
|
|
/* 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. */
|
2022-01-15 20:36:48 +01:00
|
|
|
pthread_mutex_t lock;
|
|
|
|
|
pthread_cond_t cond;
|
|
|
|
|
struct spa_list threads_list;
|
2022-03-10 11:19:12 +01:00
|
|
|
#endif
|
2018-02-08 10:02:17 +01:00
|
|
|
};
|
|
|
|
|
|
2018-11-22 19:16:24 -02:00
|
|
|
#ifndef RLIMIT_RTTIME
|
|
|
|
|
#define RLIMIT_RTTIME 15
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-11-22 14:35:24 +01:00
|
|
|
static pthread_mutex_t rlimit_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
2022-03-10 11:19:12 +01:00
|
|
|
static pid_t _gettid(void)
|
|
|
|
|
{
|
|
|
|
|
#if defined(HAVE_GETTID)
|
|
|
|
|
return (pid_t) gettid();
|
|
|
|
|
#elif defined(__linux__)
|
|
|
|
|
return syscall(SYS_gettid);
|
2022-06-19 14:03:56 -04:00
|
|
|
#elif defined(__FreeBSD__) || defined(__MidnightBSD__)
|
2022-03-10 11:19:12 +01:00
|
|
|
long pid;
|
|
|
|
|
thr_self(&pid);
|
|
|
|
|
return (pid_t)pid;
|
2023-09-24 16:17:12 +02:00
|
|
|
#elif defined(__GNU__)
|
2023-10-08 11:38:59 +02:00
|
|
|
mach_port_t thread = hurd_thread_self();
|
2023-09-24 16:17:12 +02:00
|
|
|
return (pid_t)thread;
|
2022-03-10 11:19:12 +01:00
|
|
|
#else
|
|
|
|
|
#error "No gettid impl"
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2016-11-08 18:00:52 +01:00
|
|
|
|
2022-03-10 11:19:12 +01:00
|
|
|
#ifdef HAVE_DBUS
|
2023-07-03 04:34:59 +02:00
|
|
|
static struct pw_rtkit_bus *pw_rtkit_bus_get(DBusBusType bus_type)
|
2016-11-08 18:00:52 +01:00
|
|
|
{
|
2017-05-26 08:05:01 +02:00
|
|
|
struct pw_rtkit_bus *bus;
|
|
|
|
|
DBusError error;
|
2016-11-24 18:17:29 +01:00
|
|
|
|
2020-11-04 09:42:33 +01:00
|
|
|
if (getenv("DISABLE_RTKIT")) {
|
|
|
|
|
errno = ENOTSUP;
|
2019-01-09 11:17:46 +01:00
|
|
|
return NULL;
|
2020-11-04 09:42:33 +01:00
|
|
|
}
|
2019-01-09 11:17:46 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
dbus_error_init(&error);
|
2016-11-24 18:17:29 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
bus = calloc(1, sizeof(struct pw_rtkit_bus));
|
|
|
|
|
if (bus == NULL)
|
|
|
|
|
return NULL;
|
2016-12-22 16:50:01 +01:00
|
|
|
|
2022-07-19 00:02:37 -04:00
|
|
|
bus->bus = dbus_bus_get_private(bus_type, &error);
|
2017-05-26 08:05:01 +02:00
|
|
|
if (bus->bus == NULL)
|
|
|
|
|
goto error;
|
2016-11-08 18:00:52 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
dbus_connection_set_exit_on_disconnect(bus->bus, false);
|
2016-11-08 18:00:52 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
return bus;
|
2016-11-24 18:17:29 +01:00
|
|
|
|
2019-06-20 11:04:34 +02:00
|
|
|
error:
|
2017-09-04 14:39:45 +05:30
|
|
|
free(bus);
|
2022-07-19 00:02:37 -04:00
|
|
|
pw_log_error("Failed to connect to %s bus: %s",
|
|
|
|
|
bus_type == DBUS_BUS_SYSTEM ? "system" : "session", error.message);
|
2017-05-26 08:05:01 +02:00
|
|
|
dbus_error_free(&error);
|
2020-11-04 09:42:33 +01:00
|
|
|
errno = ECONNREFUSED;
|
2017-05-26 08:05:01 +02:00
|
|
|
return NULL;
|
2016-11-08 18:00:52 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-03 04:34:59 +02:00
|
|
|
static struct pw_rtkit_bus *pw_rtkit_bus_get_system(void)
|
2022-07-19 00:02:37 -04:00
|
|
|
{
|
|
|
|
|
return pw_rtkit_bus_get(DBUS_BUS_SYSTEM);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 04:34:59 +02:00
|
|
|
static struct pw_rtkit_bus *pw_rtkit_bus_get_session(void)
|
2022-07-19 00:02:37 -04:00
|
|
|
{
|
|
|
|
|
return pw_rtkit_bus_get(DBUS_BUS_SESSION);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 04:34:59 +02:00
|
|
|
static bool pw_rtkit_check_xdg_portal(struct pw_rtkit_bus *system_bus)
|
2022-07-19 00:02:37 -04:00
|
|
|
{
|
2023-01-08 02:06:35 +00:00
|
|
|
if (!dbus_bus_name_has_owner(system_bus->bus, XDG_PORTAL_SERVICE_NAME, NULL)) {
|
2023-05-15 09:31:33 +02:00
|
|
|
pw_log_info("Can't find %s. Is xdg-desktop-portal running?", XDG_PORTAL_SERVICE_NAME);
|
2023-01-08 02:06:35 +00:00
|
|
|
return false;
|
2022-07-19 00:02:37 -04:00
|
|
|
}
|
|
|
|
|
|
2023-01-08 02:06:35 +00:00
|
|
|
return true;
|
2022-07-19 00:02:37 -04:00
|
|
|
}
|
|
|
|
|
|
2023-07-03 04:34:59 +02:00
|
|
|
static void pw_rtkit_bus_free(struct pw_rtkit_bus *system_bus)
|
2016-11-08 18:00:52 +01:00
|
|
|
{
|
2017-05-26 08:05:01 +02:00
|
|
|
dbus_connection_close(system_bus->bus);
|
|
|
|
|
dbus_connection_unref(system_bus->bus);
|
|
|
|
|
free(system_bus);
|
2016-11-24 18:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
static int translate_error(const char *name)
|
|
|
|
|
{
|
2021-01-19 15:02:37 +01:00
|
|
|
pw_log_warn("RTKit error: %s", name);
|
|
|
|
|
|
2021-05-18 11:36:13 +10:00
|
|
|
if (spa_streq(name, DBUS_ERROR_NO_MEMORY))
|
2017-05-26 08:05:01 +02:00
|
|
|
return -ENOMEM;
|
2021-05-18 11:36:13 +10:00
|
|
|
if (spa_streq(name, DBUS_ERROR_SERVICE_UNKNOWN) ||
|
|
|
|
|
spa_streq(name, DBUS_ERROR_NAME_HAS_NO_OWNER))
|
2017-05-26 08:05:01 +02:00
|
|
|
return -ENOENT;
|
2021-05-18 11:36:13 +10:00
|
|
|
if (spa_streq(name, DBUS_ERROR_ACCESS_DENIED) ||
|
|
|
|
|
spa_streq(name, DBUS_ERROR_AUTH_FAILED))
|
2017-05-26 08:05:01 +02:00
|
|
|
return -EACCES;
|
2023-05-07 13:52:17 +02:00
|
|
|
if (spa_streq(name, DBUS_ERROR_IO_ERROR))
|
|
|
|
|
return -EIO;
|
|
|
|
|
if (spa_streq(name, DBUS_ERROR_NOT_SUPPORTED))
|
|
|
|
|
return -ENOTSUP;
|
|
|
|
|
if (spa_streq(name, DBUS_ERROR_INVALID_ARGS))
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
if (spa_streq(name, DBUS_ERROR_TIMED_OUT))
|
|
|
|
|
return -ETIMEDOUT;
|
2017-05-26 08:05:01 +02:00
|
|
|
return -EIO;
|
2016-11-24 18:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
2022-07-19 00:02:37 -04:00
|
|
|
static long long rtkit_get_int_property(struct impl *impl, const char *propname,
|
2017-05-26 08:05:01 +02:00
|
|
|
long long *propval)
|
|
|
|
|
{
|
2023-12-30 01:26:01 +01:00
|
|
|
spa_autoptr(DBusMessage) m = NULL, r = NULL;
|
2017-05-26 08:05:01 +02:00
|
|
|
DBusMessageIter iter, subiter;
|
|
|
|
|
dbus_int64_t i64;
|
|
|
|
|
dbus_int32_t i32;
|
2022-07-19 00:02:37 -04:00
|
|
|
struct pw_rtkit_bus *connection = impl->rtkit_bus;
|
2017-05-26 08:05:01 +02:00
|
|
|
|
2022-07-19 00:02:37 -04:00
|
|
|
if (!(m = dbus_message_new_method_call(impl->service_name,
|
|
|
|
|
impl->object_path,
|
2017-05-26 08:05:01 +02:00
|
|
|
"org.freedesktop.DBus.Properties", "Get"))) {
|
2023-12-30 01:26:01 +01:00
|
|
|
return -ENOMEM;
|
2017-05-26 08:05:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!dbus_message_append_args(m,
|
2022-07-19 00:02:37 -04:00
|
|
|
DBUS_TYPE_STRING, &impl->interface,
|
2017-05-26 08:05:01 +02:00
|
|
|
DBUS_TYPE_STRING, &propname, DBUS_TYPE_INVALID)) {
|
2023-12-30 01:26:01 +01:00
|
|
|
return -ENOMEM;
|
2017-05-26 08:05:01 +02:00
|
|
|
}
|
|
|
|
|
|
2023-12-30 01:26:01 +01:00
|
|
|
spa_auto(DBusError) error = DBUS_ERROR_INIT;
|
2017-05-26 08:05:01 +02:00
|
|
|
|
2023-12-30 01:26:01 +01:00
|
|
|
if (!(r = dbus_connection_send_with_reply_and_block(connection->bus, m, -1, &error)))
|
|
|
|
|
return translate_error(error.name);
|
|
|
|
|
|
|
|
|
|
if (dbus_set_error_from_message(&error, r))
|
|
|
|
|
return translate_error(error.name);
|
2017-05-26 08:05:01 +02:00
|
|
|
|
2024-02-04 16:38:54 +01:00
|
|
|
if (!dbus_message_has_signature(r, "v"))
|
|
|
|
|
return -EBADMSG;
|
|
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
dbus_message_iter_init(r, &iter);
|
2024-02-04 16:38:54 +01:00
|
|
|
|
|
|
|
|
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
|
|
|
|
|
return -EBADMSG;
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_recurse(&iter, &subiter);
|
|
|
|
|
|
|
|
|
|
switch (dbus_message_iter_get_arg_type(&subiter)) {
|
|
|
|
|
case DBUS_TYPE_INT32:
|
|
|
|
|
dbus_message_iter_get_basic(&subiter, &i32);
|
|
|
|
|
*propval = i32;
|
|
|
|
|
break;
|
|
|
|
|
case DBUS_TYPE_INT64:
|
|
|
|
|
dbus_message_iter_get_basic(&subiter, &i64);
|
|
|
|
|
*propval = i64;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return -EBADMSG;
|
2017-05-26 08:05:01 +02:00
|
|
|
}
|
|
|
|
|
|
2024-02-04 16:38:54 +01:00
|
|
|
return 0;
|
2016-11-24 18:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-03 04:34:59 +02:00
|
|
|
static int pw_rtkit_make_realtime(struct impl *impl, pid_t thread, int priority)
|
2016-10-28 16:56:33 +02:00
|
|
|
{
|
2023-12-30 01:26:01 +01:00
|
|
|
spa_autoptr(DBusMessage) m = NULL;
|
2022-07-19 00:02:37 -04:00
|
|
|
dbus_uint64_t pid;
|
2017-05-26 08:05:01 +02:00
|
|
|
dbus_uint64_t u64;
|
2024-02-04 16:05:03 +01:00
|
|
|
dbus_uint32_t u32;
|
2022-07-19 00:02:37 -04:00
|
|
|
struct pw_rtkit_bus *connection = impl->rtkit_bus;
|
2016-11-24 18:17:29 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
if (thread == 0)
|
|
|
|
|
thread = _gettid();
|
2016-11-24 18:17:29 +01:00
|
|
|
|
2022-07-19 00:02:37 -04:00
|
|
|
if (!(m = dbus_message_new_method_call(impl->service_name,
|
|
|
|
|
impl->object_path, impl->interface,
|
|
|
|
|
"MakeThreadRealtimeWithPID"))) {
|
2023-12-30 01:26:01 +01:00
|
|
|
return -ENOMEM;
|
2017-05-26 08:05:01 +02:00
|
|
|
}
|
2016-11-24 18:17:29 +01:00
|
|
|
|
2022-07-19 00:02:37 -04:00
|
|
|
pid = (dbus_uint64_t) getpid();
|
2017-05-26 08:05:01 +02:00
|
|
|
u64 = (dbus_uint64_t) thread;
|
|
|
|
|
u32 = (dbus_uint32_t) priority;
|
2016-11-24 18:17:29 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
if (!dbus_message_append_args(m,
|
2022-07-19 00:02:37 -04:00
|
|
|
DBUS_TYPE_UINT64, &pid,
|
2017-05-26 08:05:01 +02:00
|
|
|
DBUS_TYPE_UINT64, &u64,
|
|
|
|
|
DBUS_TYPE_UINT32, &u32, DBUS_TYPE_INVALID)) {
|
2023-12-30 01:26:01 +01:00
|
|
|
return -ENOMEM;
|
2017-05-26 08:05:01 +02:00
|
|
|
}
|
2016-11-24 18:17:29 +01:00
|
|
|
|
2024-02-04 16:05:03 +01:00
|
|
|
if (!dbus_connection_send(connection->bus, m, NULL))
|
2023-12-30 01:26:01 +01:00
|
|
|
return -EIO;
|
2016-11-24 18:17:29 +01:00
|
|
|
|
2023-12-30 01:26:01 +01:00
|
|
|
return 0;
|
2016-10-28 16:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-03 04:34:59 +02:00
|
|
|
static int pw_rtkit_make_high_priority(struct impl *impl, pid_t thread, int nice_level)
|
2016-10-28 16:56:33 +02:00
|
|
|
{
|
2023-12-30 01:26:01 +01:00
|
|
|
spa_autoptr(DBusMessage) m = NULL;
|
2022-07-19 00:02:37 -04:00
|
|
|
dbus_uint64_t pid;
|
2017-05-26 08:05:01 +02:00
|
|
|
dbus_uint64_t u64;
|
|
|
|
|
dbus_int32_t s32;
|
2022-07-19 00:02:37 -04:00
|
|
|
struct pw_rtkit_bus *connection = impl->rtkit_bus;
|
2016-11-24 18:17:29 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
if (thread == 0)
|
|
|
|
|
thread = _gettid();
|
2016-11-24 18:17:29 +01:00
|
|
|
|
2022-07-19 00:02:37 -04:00
|
|
|
if (!(m = dbus_message_new_method_call(impl->service_name,
|
|
|
|
|
impl->object_path, impl->interface,
|
|
|
|
|
"MakeThreadHighPriorityWithPID"))) {
|
2023-12-30 01:26:01 +01:00
|
|
|
return -ENOMEM;
|
2017-05-26 08:05:01 +02:00
|
|
|
}
|
2016-11-24 18:17:29 +01:00
|
|
|
|
2022-07-19 00:02:37 -04:00
|
|
|
pid = (dbus_uint64_t) getpid();
|
2017-05-26 08:05:01 +02:00
|
|
|
u64 = (dbus_uint64_t) thread;
|
|
|
|
|
s32 = (dbus_int32_t) nice_level;
|
2016-11-24 18:17:29 +01:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
if (!dbus_message_append_args(m,
|
2022-07-19 00:02:37 -04:00
|
|
|
DBUS_TYPE_UINT64, &pid,
|
2017-05-26 08:05:01 +02:00
|
|
|
DBUS_TYPE_UINT64, &u64,
|
|
|
|
|
DBUS_TYPE_INT32, &s32, DBUS_TYPE_INVALID)) {
|
2023-12-30 01:26:01 +01:00
|
|
|
return -ENOMEM;
|
2017-05-26 08:05:01 +02:00
|
|
|
}
|
2016-11-24 18:17:29 +01:00
|
|
|
|
2024-02-04 16:05:03 +01:00
|
|
|
if (!dbus_connection_send(connection->bus, m, NULL))
|
2023-12-30 01:26:01 +01:00
|
|
|
return -EIO;
|
2016-11-24 18:17:29 +01:00
|
|
|
|
2023-12-30 01:26:01 +01:00
|
|
|
return 0;
|
2016-10-28 16:56:33 +02:00
|
|
|
}
|
2022-03-10 11:19:12 +01:00
|
|
|
#endif /* HAVE_DBUS */
|
2016-10-28 16:56:33 +02:00
|
|
|
|
2018-02-08 10:02:17 +01:00
|
|
|
static void module_destroy(void *data)
|
2017-05-26 08:05:01 +02:00
|
|
|
{
|
2018-02-08 10:02:17 +01:00
|
|
|
struct impl *impl = data;
|
2022-03-30 20:31:42 +02:00
|
|
|
pw_context_set_object(impl->context, SPA_TYPE_INTERFACE_ThreadUtils, NULL);
|
2018-02-08 10:02:17 +01:00
|
|
|
spa_hook_remove(&impl->module_listener);
|
|
|
|
|
|
2022-03-10 11:19:12 +01:00
|
|
|
#ifdef HAVE_DBUS
|
2023-07-20 12:03:29 +02:00
|
|
|
if (impl->thread_loop)
|
|
|
|
|
pw_thread_loop_destroy(impl->thread_loop);
|
2023-07-20 16:17:14 +02:00
|
|
|
if (impl->rtkit_bus)
|
|
|
|
|
pw_rtkit_bus_free(impl->rtkit_bus);
|
2022-03-10 11:19:12 +01:00
|
|
|
#endif
|
|
|
|
|
|
2018-02-08 10:02:17 +01:00
|
|
|
free(impl);
|
2016-10-28 16:56:33 +02:00
|
|
|
}
|
|
|
|
|
|
2019-12-11 11:57:56 +01:00
|
|
|
static const struct pw_impl_module_events module_events = {
|
|
|
|
|
PW_VERSION_IMPL_MODULE_EVENTS,
|
2018-02-08 10:02:17 +01:00
|
|
|
.destroy = module_destroy,
|
|
|
|
|
};
|
|
|
|
|
|
2023-01-16 12:37:03 +01:00
|
|
|
static int get_rt_priority_range(int *out_min, int *out_max)
|
|
|
|
|
{
|
|
|
|
|
int min, max;
|
|
|
|
|
|
|
|
|
|
if ((min = sched_get_priority_min(REALTIME_POLICY)) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
if ((max = sched_get_priority_max(REALTIME_POLICY)) < 0)
|
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
|
|
if (out_min)
|
|
|
|
|
*out_min = min;
|
|
|
|
|
if (out_max)
|
|
|
|
|
*out_max = max;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2022-01-15 20:36:48 +01:00
|
|
|
/**
|
|
|
|
|
* Check if the current user has permissions to use realtime scheduling at the
|
|
|
|
|
* specified priority.
|
|
|
|
|
*/
|
2023-01-16 12:37:03 +01:00
|
|
|
static bool check_realtime_privileges(struct impl *impl)
|
2022-01-15 20:36:48 +01:00
|
|
|
{
|
2023-01-16 12:37:03 +01:00
|
|
|
rlim_t priority = impl->rt_prio;
|
|
|
|
|
int err, old_policy, new_policy, min, max;
|
2022-01-19 16:10:02 +01:00
|
|
|
struct sched_param old_sched_params;
|
|
|
|
|
struct sched_param new_sched_params;
|
2023-11-15 17:11:19 +01:00
|
|
|
struct rlimit old_rlim;
|
|
|
|
|
struct rlimit no_rlim = { -1, -1 };
|
2023-01-16 12:37:03 +01:00
|
|
|
int try = 0;
|
2023-11-22 14:35:24 +01:00
|
|
|
bool ret = false;
|
2023-01-16 12:37:03 +01:00
|
|
|
|
2023-07-04 11:11:48 +02:00
|
|
|
if (!impl->rlimits_enabled)
|
2023-11-22 14:35:24 +01:00
|
|
|
return ret;
|
2023-07-04 11:11:48 +02:00
|
|
|
|
2023-11-22 14:35:24 +01:00
|
|
|
while (!ret && try++ < 2) {
|
2023-01-16 12:37:03 +01:00
|
|
|
/* We could check `RLIMIT_RTPRIO`, but the BSDs generally don't have
|
|
|
|
|
* that available, and there are also other ways to use realtime
|
|
|
|
|
* scheduling without that rlimit being set such as `CAP_SYS_NICE` or
|
|
|
|
|
* running as root. Instead of checking a bunch of preconditions, we
|
|
|
|
|
* just try if setting realtime scheduling works or not. */
|
|
|
|
|
if ((err = pthread_getschedparam(pthread_self(), &old_policy, &old_sched_params)) != 0) {
|
|
|
|
|
pw_log_warn("Failed to check RLIMIT_RTPRIO: %s", strerror(err));
|
2023-11-22 14:35:24 +01:00
|
|
|
break;
|
2023-01-16 12:37:03 +01:00
|
|
|
}
|
|
|
|
|
if ((err = get_rt_priority_range(&min, &max)) < 0) {
|
|
|
|
|
pw_log_warn("Failed to get priority range: %s", strerror(err));
|
2023-11-22 14:35:24 +01:00
|
|
|
break;
|
2023-01-16 12:37:03 +01:00
|
|
|
}
|
|
|
|
|
if (try == 2) {
|
2023-03-15 17:58:08 +03:00
|
|
|
#ifdef RLIMIT_RTPRIO
|
2023-01-16 12:37:03 +01:00
|
|
|
struct rlimit rlim;
|
|
|
|
|
/* second try, try to clamp to RLIMIT_RTPRIO */
|
|
|
|
|
if (getrlimit(RLIMIT_RTPRIO, &rlim) == 0 && max > (int)rlim.rlim_max) {
|
|
|
|
|
pw_log_info("Clamp rtprio %d to %d", (int)priority, (int)rlim.rlim_max);
|
|
|
|
|
max = (int)rlim.rlim_max;
|
|
|
|
|
}
|
|
|
|
|
else
|
2023-03-15 17:58:08 +03:00
|
|
|
#endif
|
2023-01-16 12:37:03 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (max < DEFAULT_RT_PRIO_MIN) {
|
|
|
|
|
pw_log_info("Priority max (%d) must be at least %d", max, DEFAULT_RT_PRIO_MIN);
|
2023-11-22 14:35:24 +01:00
|
|
|
break;
|
2023-01-16 12:37:03 +01:00
|
|
|
}
|
2022-01-19 16:10:02 +01:00
|
|
|
|
2023-01-16 12:37:03 +01:00
|
|
|
/* If the current scheduling policy has `SCHED_RESET_ON_FORK` set, then
|
|
|
|
|
* this also needs to be set here or `pthread_setschedparam()` will return
|
|
|
|
|
* an error code. Similarly, if it is not set, then we don't want to set
|
|
|
|
|
* it here as it would irreversible change the current thread's
|
|
|
|
|
* scheduling policy. */
|
|
|
|
|
spa_zero(new_sched_params);
|
|
|
|
|
new_sched_params.sched_priority = SPA_CLAMP((int)priority, min, max);
|
|
|
|
|
new_policy = REALTIME_POLICY;
|
|
|
|
|
if ((old_policy & PW_SCHED_RESET_ON_FORK) != 0)
|
|
|
|
|
new_policy |= PW_SCHED_RESET_ON_FORK;
|
|
|
|
|
|
2023-11-22 14:35:24 +01:00
|
|
|
/* Disable RLIMIT_RTTIME in a thread safe way and hope that the application
|
|
|
|
|
* doesn't also set RLIMIT_RTTIME while trying new_policy. */
|
|
|
|
|
pthread_mutex_lock(&rlimit_lock);
|
|
|
|
|
if (getrlimit(RLIMIT_RTTIME, &old_rlim) < 0)
|
|
|
|
|
pw_log_info("getrlimit() failed: %m");
|
|
|
|
|
if (setrlimit(RLIMIT_RTTIME, &no_rlim) < 0)
|
|
|
|
|
pw_log_info("setrlimit() failed: %m");
|
2023-11-22 16:17:08 +01:00
|
|
|
if ((err = pthread_setschedparam(pthread_self(), new_policy, &new_sched_params)) == 0) {
|
2023-01-16 12:37:03 +01:00
|
|
|
impl->rt_prio = new_sched_params.sched_priority;
|
|
|
|
|
pthread_setschedparam(pthread_self(), old_policy, &old_sched_params);
|
2023-11-22 14:35:24 +01:00
|
|
|
ret = true;
|
|
|
|
|
} else
|
|
|
|
|
pw_log_info("failed to set realtime policy: %s", strerror(err));
|
|
|
|
|
if (setrlimit(RLIMIT_RTTIME, &old_rlim) < 0)
|
|
|
|
|
pw_log_info("setrlimit() failed: %m");
|
|
|
|
|
pthread_mutex_unlock(&rlimit_lock);
|
2022-01-15 20:36:48 +01:00
|
|
|
}
|
2023-11-22 14:35:24 +01:00
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
|
pw_log_debug("can set rt prio to %d", (int)priority);
|
|
|
|
|
else
|
|
|
|
|
pw_log_info("can't set rt prio to %d (try increasing rlimits)", (int)priority);
|
|
|
|
|
return ret;
|
2022-01-15 20:36:48 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-20 12:16:13 +02:00
|
|
|
static int sched_set_nice(pid_t pid, int nice_level)
|
2022-03-10 11:19:12 +01:00
|
|
|
{
|
2023-07-20 12:16:13 +02:00
|
|
|
if (setpriority(PRIO_PROCESS, pid, nice_level) == 0)
|
2022-03-10 11:19:12 +01:00
|
|
|
return 0;
|
|
|
|
|
else
|
|
|
|
|
return -errno;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 15:15:56 +02:00
|
|
|
static int set_nice(struct impl *impl, int nice_level, bool warn)
|
2021-07-05 10:26:56 +02:00
|
|
|
{
|
2022-01-15 20:36:48 +01:00
|
|
|
int res = 0;
|
|
|
|
|
|
2022-03-10 11:19:12 +01:00
|
|
|
#ifdef HAVE_DBUS
|
2023-05-07 19:37:32 +02:00
|
|
|
if (impl->use_rtkit) {
|
2023-07-20 12:03:29 +02:00
|
|
|
if (nice_level < impl->min_nice_level) {
|
2023-05-07 19:37:32 +02:00
|
|
|
pw_log_info("clamped nice level %d to %d",
|
2023-07-20 12:03:29 +02:00
|
|
|
nice_level, impl->min_nice_level);
|
|
|
|
|
nice_level = impl->min_nice_level;
|
2023-05-07 19:37:32 +02:00
|
|
|
}
|
2023-07-20 12:16:13 +02:00
|
|
|
res = pw_rtkit_make_high_priority(impl, impl->main_pid, nice_level);
|
2023-05-07 19:37:32 +02:00
|
|
|
}
|
2023-07-04 11:11:48 +02:00
|
|
|
else
|
2023-07-20 12:03:29 +02:00
|
|
|
#endif
|
2023-07-04 11:11:48 +02:00
|
|
|
if (impl->rlimits_enabled)
|
2023-07-20 12:16:13 +02:00
|
|
|
res = sched_set_nice(impl->main_pid, nice_level);
|
2023-07-04 11:11:48 +02:00
|
|
|
else
|
|
|
|
|
res = -ENOTSUP;
|
2022-03-10 11:19:12 +01:00
|
|
|
|
|
|
|
|
if (res < 0) {
|
2022-08-30 15:15:56 +02:00
|
|
|
if (warn)
|
|
|
|
|
pw_log_warn("could not set nice-level to %d: %s",
|
|
|
|
|
nice_level, spa_strerror(res));
|
2023-07-19 13:41:18 +02:00
|
|
|
} else if (res > 0) {
|
|
|
|
|
pw_log_info("main thread setting nice level to %d: %s",
|
|
|
|
|
nice_level, spa_strerror(-res));
|
2021-07-05 10:26:56 +02:00
|
|
|
} else {
|
2022-03-10 11:19:12 +01:00
|
|
|
pw_log_info("main thread nice level set to %d",
|
|
|
|
|
nice_level);
|
2021-07-05 10:26:56 +02:00
|
|
|
}
|
2022-01-15 20:36:48 +01:00
|
|
|
return res;
|
2021-07-05 10:26:56 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-15 17:11:19 +01:00
|
|
|
static int set_rlimit(struct rlimit *rlim)
|
2017-05-26 08:05:01 +02:00
|
|
|
{
|
2021-07-05 10:26:56 +02:00
|
|
|
int res = 0;
|
2019-01-09 11:17:46 +01:00
|
|
|
|
2023-11-22 14:35:24 +01:00
|
|
|
pthread_mutex_lock(&rlimit_lock);
|
2023-11-15 17:11:19 +01:00
|
|
|
if (setrlimit(RLIMIT_RTTIME, rlim) < 0)
|
2021-07-05 10:26:56 +02:00
|
|
|
res = -errno;
|
2023-11-22 14:35:24 +01:00
|
|
|
pthread_mutex_unlock(&rlimit_lock);
|
2018-02-08 10:02:17 +01:00
|
|
|
|
2021-07-05 10:26:56 +02:00
|
|
|
if (res < 0)
|
2023-11-22 14:35:24 +01:00
|
|
|
pw_log_info("setrlimit() failed: %s", spa_strerror(res));
|
2021-07-05 10:26:56 +02:00
|
|
|
else
|
|
|
|
|
pw_log_debug("rt.time.soft:%"PRIi64" rt.time.hard:%"PRIi64,
|
2023-11-15 17:11:19 +01:00
|
|
|
(int64_t)rlim->rlim_cur, (int64_t)rlim->rlim_max);
|
2021-01-05 12:33:41 +01:00
|
|
|
|
2021-07-05 10:26:56 +02:00
|
|
|
return res;
|
2016-11-24 18:17:29 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-16 12:37:03 +01:00
|
|
|
static int acquire_rt_sched(struct spa_thread *thread, int priority)
|
2022-03-10 11:19:12 +01:00
|
|
|
{
|
2023-01-16 12:37:03 +01:00
|
|
|
int err, min, max;
|
2022-03-10 11:19:12 +01:00
|
|
|
struct sched_param sp;
|
|
|
|
|
pthread_t pt = (pthread_t)thread;
|
|
|
|
|
|
2023-01-16 12:37:03 +01:00
|
|
|
if ((err = get_rt_priority_range(&min, &max)) < 0)
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
if (priority < min || priority > max) {
|
|
|
|
|
pw_log_info("clamping priority %d to range %d - %d for policy %d",
|
|
|
|
|
priority, min, max, REALTIME_POLICY);
|
|
|
|
|
priority = SPA_CLAMP(priority, min, max);
|
2022-03-10 11:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spa_zero(sp);
|
|
|
|
|
sp.sched_priority = priority;
|
|
|
|
|
if ((err = pthread_setschedparam(pt, REALTIME_POLICY | PW_SCHED_RESET_ON_FORK, &sp)) != 0) {
|
|
|
|
|
pw_log_warn("could not make thread %p realtime: %s", thread, strerror(err));
|
|
|
|
|
return -err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pw_log_info("acquired realtime priority %d for thread %p", priority, thread);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 15:09:32 +02:00
|
|
|
static int impl_drop_rt_generic(void *object, struct spa_thread *thread)
|
2022-03-10 11:19:12 +01:00
|
|
|
{
|
|
|
|
|
struct sched_param sp;
|
|
|
|
|
pthread_t pt = (pthread_t)thread;
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
spa_zero(sp);
|
|
|
|
|
if ((err = pthread_setschedparam(pt, SCHED_OTHER | PW_SCHED_RESET_ON_FORK, &sp)) != 0) {
|
|
|
|
|
pw_log_debug("thread %p: SCHED_OTHER|SCHED_RESET_ON_FORK failed: %s",
|
|
|
|
|
thread, strerror(err));
|
|
|
|
|
return -err;
|
|
|
|
|
}
|
|
|
|
|
pw_log_info("thread %p dropped realtime priority", thread);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_DBUS
|
2021-07-05 10:26:56 +02:00
|
|
|
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);
|
2021-07-20 12:01:21 -03:00
|
|
|
this->pid = _gettid();
|
2021-07-05 10:26:56 +02:00
|
|
|
pthread_cond_broadcast(&impl->cond);
|
|
|
|
|
pthread_mutex_unlock(&impl->lock);
|
|
|
|
|
|
|
|
|
|
return this->start(this->arg);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-16 22:38:38 +02:00
|
|
|
static struct spa_thread *impl_create(void *object, const struct spa_dict *props,
|
2021-07-05 10:26:56 +02:00
|
|
|
void *(*start_routine)(void*), void *arg)
|
|
|
|
|
{
|
2022-04-16 22:38:38 +02:00
|
|
|
struct impl *impl = object;
|
2021-07-05 10:26:56 +02:00
|
|
|
struct thread *this;
|
2022-04-18 13:02:27 +02:00
|
|
|
struct spa_thread *thread;
|
2021-07-05 10:26:56 +02:00
|
|
|
|
|
|
|
|
this = calloc(1, sizeof(*this));
|
|
|
|
|
this->impl = impl;
|
|
|
|
|
this->start = start_routine;
|
|
|
|
|
this->arg = arg;
|
|
|
|
|
|
2022-01-18 13:32:25 +01:00
|
|
|
/* This thread list is only used for the RTKit implementation */
|
2021-07-05 10:26:56 +02:00
|
|
|
pthread_mutex_lock(&impl->lock);
|
2022-04-18 13:02:27 +02:00
|
|
|
thread = pw_thread_utils_create(props, custom_start, this);
|
|
|
|
|
if (thread == NULL)
|
2021-07-05 10:26:56 +02:00
|
|
|
goto exit;
|
|
|
|
|
|
2022-04-18 13:02:27 +02:00
|
|
|
this->thread = (pthread_t)thread;
|
2021-07-05 10:26:56 +02:00
|
|
|
pthread_cond_wait(&impl->cond, &impl->lock);
|
|
|
|
|
|
|
|
|
|
spa_list_append(&impl->threads_list, &this->link);
|
|
|
|
|
exit:
|
|
|
|
|
pthread_mutex_unlock(&impl->lock);
|
|
|
|
|
|
2022-04-18 13:02:27 +02:00
|
|
|
if (thread == NULL) {
|
2021-07-05 10:26:56 +02:00
|
|
|
free(this);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2022-04-18 13:02:27 +02:00
|
|
|
return thread;
|
2021-07-05 10:26:56 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-27 15:09:32 +02:00
|
|
|
static int impl_join(void *object, struct spa_thread *thread, void **retval)
|
2021-07-05 10:26:56 +02:00
|
|
|
{
|
2021-09-27 15:09:32 +02:00
|
|
|
struct impl *impl = object;
|
2021-07-05 10:26:56 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 12:37:03 +01:00
|
|
|
|
|
|
|
|
static int get_rtkit_priority_range(struct impl *impl, int *min, int *max)
|
|
|
|
|
{
|
|
|
|
|
if (min)
|
|
|
|
|
*min = 1;
|
|
|
|
|
if (max) {
|
2023-07-20 12:03:29 +02:00
|
|
|
*max = impl->max_rtprio;
|
2023-01-16 12:37:03 +01:00
|
|
|
if (*max < 1)
|
|
|
|
|
*max = 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 15:09:32 +02:00
|
|
|
static int impl_get_rt_range(void *object, const struct spa_dict *props,
|
2021-07-05 11:35:27 +02:00
|
|
|
int *min, int *max)
|
|
|
|
|
{
|
2021-09-27 15:09:32 +02:00
|
|
|
struct impl *impl = object;
|
2023-01-16 12:37:03 +01:00
|
|
|
int res;
|
|
|
|
|
if (impl->use_rtkit)
|
|
|
|
|
res = get_rtkit_priority_range(impl, min, max);
|
|
|
|
|
else
|
|
|
|
|
res = get_rt_priority_range(min, max);
|
|
|
|
|
return res;
|
2021-07-05 11:35:27 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-20 12:03:29 +02:00
|
|
|
struct rt_params {
|
|
|
|
|
pid_t pid;
|
|
|
|
|
int priority;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int do_make_realtime(struct spa_loop *loop, bool async, uint32_t seq,
|
|
|
|
|
const void *data, size_t size, void *user_data)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = user_data;
|
|
|
|
|
const struct rt_params *params = data;
|
|
|
|
|
int err, min, max, priority = params->priority;
|
|
|
|
|
pid_t pid = params->pid;
|
|
|
|
|
|
2023-07-20 12:16:13 +02:00
|
|
|
pw_log_debug("rtkit realtime");
|
2023-07-20 12:03:29 +02:00
|
|
|
|
|
|
|
|
if ((err = get_rtkit_priority_range(impl, &min, &max)) < 0)
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
if (priority < min || priority > max) {
|
|
|
|
|
pw_log_info("clamping requested priority %d for thread %d "
|
|
|
|
|
"between %d and %d", priority, pid, min, max);
|
|
|
|
|
priority = SPA_CLAMP(priority, min, max);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((err = pw_rtkit_make_realtime(impl, pid, priority)) < 0) {
|
|
|
|
|
pw_log_warn("could not make thread %d realtime using RTKit: %s", pid, spa_strerror(err));
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pw_log_info("acquired realtime priority %d for thread %d using RTKit", priority, pid);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-27 15:09:32 +02:00
|
|
|
static int impl_acquire_rt(void *object, struct spa_thread *thread, int priority)
|
2021-07-05 10:26:56 +02:00
|
|
|
{
|
2021-09-27 15:09:32 +02:00
|
|
|
struct impl *impl = object;
|
2021-07-05 10:26:56 +02:00
|
|
|
struct sched_param sp;
|
|
|
|
|
pthread_t pt = (pthread_t)thread;
|
2023-07-20 15:39:08 +02:00
|
|
|
int res;
|
2021-07-05 10:26:56 +02:00
|
|
|
|
2022-01-18 13:32:25 +01:00
|
|
|
/* See the docstring on `spa_thread_utils_methods::acquire_rt` */
|
2022-01-16 16:49:03 +01:00
|
|
|
if (priority == -1) {
|
|
|
|
|
priority = impl->rt_prio;
|
|
|
|
|
}
|
2022-01-15 20:36:48 +01:00
|
|
|
if (impl->use_rtkit) {
|
2023-07-20 12:03:29 +02:00
|
|
|
struct rt_params params;
|
2023-07-20 15:39:08 +02:00
|
|
|
struct thread *thr;
|
2021-07-05 10:26:56 +02:00
|
|
|
|
2022-01-15 20:36:48 +01:00
|
|
|
spa_zero(sp);
|
|
|
|
|
if (pthread_setschedparam(pt, SCHED_OTHER | PW_SCHED_RESET_ON_FORK, &sp) == 0) {
|
|
|
|
|
pw_log_debug("SCHED_OTHER|SCHED_RESET_ON_FORK worked.");
|
|
|
|
|
}
|
2023-07-20 15:39:08 +02:00
|
|
|
|
2023-07-20 12:03:29 +02:00
|
|
|
params.priority = priority;
|
2021-07-05 10:26:56 +02:00
|
|
|
|
2023-07-20 15:39:08 +02:00
|
|
|
pthread_mutex_lock(&impl->lock);
|
|
|
|
|
if ((thr = find_thread_by_pt(impl, pt)) != NULL)
|
|
|
|
|
params.pid = thr->pid;
|
|
|
|
|
else
|
|
|
|
|
params.pid = _gettid();
|
|
|
|
|
|
|
|
|
|
res = pw_loop_invoke(pw_thread_loop_get_loop(impl->thread_loop),
|
2023-07-20 12:03:29 +02:00
|
|
|
do_make_realtime, 0, ¶ms, sizeof(params), false, impl);
|
2023-07-20 15:39:08 +02:00
|
|
|
pthread_mutex_unlock(&impl->lock);
|
|
|
|
|
|
|
|
|
|
return res;
|
2021-07-05 10:26:56 +02:00
|
|
|
} else {
|
2023-01-16 12:37:03 +01:00
|
|
|
return acquire_rt_sched(thread, priority);
|
2022-03-10 11:19:12 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-01-15 20:36:48 +01:00
|
|
|
|
2022-03-10 11:19:12 +01:00
|
|
|
static const struct spa_thread_utils_methods impl_thread_utils = {
|
|
|
|
|
SPA_VERSION_THREAD_UTILS_METHODS,
|
|
|
|
|
.create = impl_create,
|
|
|
|
|
.join = impl_join,
|
|
|
|
|
.get_rt_range = impl_get_rt_range,
|
|
|
|
|
.acquire_rt = impl_acquire_rt,
|
|
|
|
|
.drop_rt = impl_drop_rt_generic,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#else /* HAVE_DBUS */
|
|
|
|
|
|
2022-04-16 22:38:38 +02:00
|
|
|
static struct spa_thread *impl_create(void *object, const struct spa_dict *props,
|
2022-03-10 11:19:12 +01:00
|
|
|
void *(*start_routine)(void*), void *arg)
|
|
|
|
|
{
|
2022-04-18 13:02:27 +02:00
|
|
|
return pw_thread_utils_create(props, start_routine, arg);
|
2022-03-10 11:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-16 22:38:38 +02:00
|
|
|
static int impl_join(void *object, struct spa_thread *thread, void **retval)
|
2022-03-10 11:19:12 +01:00
|
|
|
{
|
2022-04-18 13:02:27 +02:00
|
|
|
return pw_thread_utils_join(thread, retval);
|
2022-03-10 11:19:12 +01:00
|
|
|
}
|
2022-01-15 20:36:48 +01:00
|
|
|
|
2022-04-16 22:38:38 +02:00
|
|
|
static int impl_get_rt_range(void *object, const struct spa_dict *props,
|
2022-03-10 11:19:12 +01:00
|
|
|
int *min, int *max)
|
|
|
|
|
{
|
2023-01-16 12:37:03 +01:00
|
|
|
return get_rt_priority_range(min, max);
|
2021-07-05 10:26:56 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-16 22:38:38 +02:00
|
|
|
static int impl_acquire_rt(void *object, struct spa_thread *thread, int priority)
|
2021-07-05 10:26:56 +02:00
|
|
|
{
|
2022-04-16 22:38:38 +02:00
|
|
|
struct impl *impl = object;
|
2021-07-05 10:26:56 +02:00
|
|
|
|
2022-03-10 11:19:12 +01:00
|
|
|
/* See the docstring on `spa_thread_utils_methods::acquire_rt` */
|
2023-01-16 12:37:03 +01:00
|
|
|
if (priority == -1)
|
2022-03-10 11:19:12 +01:00
|
|
|
priority = impl->rt_prio;
|
|
|
|
|
|
2023-01-16 12:37:03 +01:00
|
|
|
return acquire_rt_sched(thread, priority);
|
2021-07-05 10:26:56 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-06 15:48:20 +02:00
|
|
|
static const struct spa_thread_utils_methods impl_thread_utils = {
|
|
|
|
|
SPA_VERSION_THREAD_UTILS_METHODS,
|
2021-07-05 10:26:56 +02:00
|
|
|
.create = impl_create,
|
|
|
|
|
.join = impl_join,
|
2021-07-05 11:35:27 +02:00
|
|
|
.get_rt_range = impl_get_rt_range,
|
2021-07-05 10:26:56 +02:00
|
|
|
.acquire_rt = impl_acquire_rt,
|
2022-03-10 11:19:12 +01:00
|
|
|
.drop_rt = impl_drop_rt_generic,
|
2021-07-05 10:26:56 +02:00
|
|
|
};
|
2022-03-10 11:19:12 +01:00
|
|
|
#endif /* HAVE_DBUS */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_DBUS
|
2022-08-30 15:15:56 +02:00
|
|
|
static int check_rtkit(struct impl *impl, struct pw_context *context, bool *can_use_rtkit)
|
2022-03-10 11:19:12 +01:00
|
|
|
{
|
|
|
|
|
const struct pw_properties *context_props;
|
|
|
|
|
const char *str;
|
2021-07-05 10:26:56 +02:00
|
|
|
|
2022-08-30 15:15:56 +02:00
|
|
|
*can_use_rtkit = true;
|
2022-03-10 11:19:12 +01:00
|
|
|
|
|
|
|
|
if ((context_props = pw_context_get_properties(context)) != NULL &&
|
|
|
|
|
(str = pw_properties_get(context_props, "support.dbus")) != NULL &&
|
|
|
|
|
!pw_properties_parse_bool(str))
|
2022-08-30 15:15:56 +02:00
|
|
|
*can_use_rtkit = false;
|
2022-03-10 11:19:12 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-07-20 12:03:29 +02:00
|
|
|
|
2023-07-22 01:20:58 +00:00
|
|
|
static int rtkit_get_bus(struct impl *impl)
|
2023-07-20 12:03:29 +02:00
|
|
|
{
|
|
|
|
|
int res;
|
|
|
|
|
|
2023-07-22 01:20:58 +00:00
|
|
|
pw_log_debug("enter rtkit get bus");
|
2023-07-20 12:03:29 +02:00
|
|
|
|
|
|
|
|
/* Checking xdg-desktop-portal. It works fine in all situations. */
|
|
|
|
|
if (impl->rtportal_enabled)
|
|
|
|
|
impl->rtkit_bus = pw_rtkit_bus_get_session();
|
|
|
|
|
else
|
|
|
|
|
pw_log_info("Portal Realtime disabled");
|
|
|
|
|
|
|
|
|
|
if (impl->rtkit_bus != NULL) {
|
|
|
|
|
if (pw_rtkit_check_xdg_portal(impl->rtkit_bus)) {
|
|
|
|
|
impl->service_name = XDG_PORTAL_SERVICE_NAME;
|
|
|
|
|
impl->object_path = XDG_PORTAL_OBJECT_PATH;
|
|
|
|
|
impl->interface = XDG_PORTAL_INTERFACE;
|
|
|
|
|
} else {
|
|
|
|
|
pw_log_info("found session bus but no portal, trying RTKit fallback");
|
|
|
|
|
pw_rtkit_bus_free(impl->rtkit_bus);
|
|
|
|
|
impl->rtkit_bus = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* Failed to get xdg-desktop-portal, try to use rtkit. */
|
|
|
|
|
if (impl->rtkit_bus == NULL) {
|
|
|
|
|
if (impl->rtkit_enabled)
|
|
|
|
|
impl->rtkit_bus = pw_rtkit_bus_get_system();
|
|
|
|
|
else
|
|
|
|
|
pw_log_info("RTkit disabled");
|
|
|
|
|
|
|
|
|
|
if (impl->rtkit_bus != NULL) {
|
|
|
|
|
impl->service_name = RTKIT_SERVICE_NAME;
|
|
|
|
|
impl->object_path = RTKIT_OBJECT_PATH;
|
|
|
|
|
impl->interface = RTKIT_INTERFACE;
|
|
|
|
|
} else {
|
|
|
|
|
res = -errno;
|
|
|
|
|
pw_log_warn("Realtime scheduling disabled: unsufficient realtime privileges, "
|
|
|
|
|
"Portal not found on session bus, and no system bus for RTKit: %m");
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-22 01:20:58 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int do_rtkit_setup(struct spa_loop *loop, bool async, uint32_t seq,
|
|
|
|
|
const void *data, size_t size, void *user_data)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl = user_data;
|
|
|
|
|
long long retval;
|
|
|
|
|
|
|
|
|
|
pw_log_debug("enter rtkit setup");
|
|
|
|
|
|
2023-07-20 12:03:29 +02:00
|
|
|
/* get some properties */
|
2023-07-20 15:57:31 +02:00
|
|
|
if (rtkit_get_int_property(impl, "MaxRealtimePriority", &retval) < 0) {
|
|
|
|
|
retval = 1;
|
|
|
|
|
pw_log_warn("RTKit does not give us MaxRealtimePriority, using %lld", retval);
|
|
|
|
|
}
|
|
|
|
|
impl->max_rtprio = retval;
|
|
|
|
|
if (rtkit_get_int_property(impl, "MinNiceLevel", &retval) < 0) {
|
|
|
|
|
retval = 0;
|
|
|
|
|
pw_log_warn("RTKit does not give us MinNiceLevel, using %lld", retval);
|
|
|
|
|
}
|
|
|
|
|
impl->min_nice_level = retval;
|
|
|
|
|
if (rtkit_get_int_property(impl, "RTTimeUSecMax", &retval) < 0) {
|
|
|
|
|
retval = impl->rl.rlim_cur;
|
|
|
|
|
pw_log_warn("RTKit does not give us RTTimeUSecMax, using %lld", retval);
|
|
|
|
|
}
|
|
|
|
|
impl->rttime_max = retval;
|
2023-07-20 12:03:29 +02:00
|
|
|
|
|
|
|
|
/* Retry set_nice with rtkit */
|
|
|
|
|
if (IS_VALID_NICE_LEVEL(impl->nice_level))
|
|
|
|
|
set_nice(impl, impl->nice_level, true);
|
|
|
|
|
|
|
|
|
|
/* Set rlimit with rtkit limits */
|
|
|
|
|
if (impl->rttime_max < impl->rl.rlim_cur) {
|
|
|
|
|
pw_log_debug("clamping rt.time.soft from %llu to %lld because of RTKit",
|
|
|
|
|
(long long)impl->rl.rlim_cur, (long long)impl->rttime_max);
|
|
|
|
|
}
|
|
|
|
|
impl->rl.rlim_cur = SPA_MIN(impl->rl.rlim_cur, impl->rttime_max);
|
|
|
|
|
impl->rl.rlim_max = SPA_MIN(impl->rl.rlim_max, impl->rttime_max);
|
|
|
|
|
|
2023-11-15 17:11:19 +01:00
|
|
|
set_rlimit(&impl->rl);
|
2023-07-20 12:03:29 +02:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2022-03-10 11:19:12 +01:00
|
|
|
#endif /* HAVE_DBUS */
|
2021-07-05 10:26:56 +02:00
|
|
|
|
2023-11-05 20:11:19 +10:00
|
|
|
int set_uclamp(int uclamp_min, int uclamp_max, pid_t pid) {
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
int ret;
|
|
|
|
|
struct sched_attr {
|
|
|
|
|
uint32_t size;
|
|
|
|
|
uint32_t sched_policy;
|
|
|
|
|
uint64_t sched_flags;
|
|
|
|
|
int32_t sched_nice;
|
|
|
|
|
uint32_t sched_priority;
|
|
|
|
|
uint64_t sched_runtime;
|
|
|
|
|
uint64_t sched_deadline;
|
|
|
|
|
uint64_t sched_period;
|
|
|
|
|
uint32_t sched_util_min;
|
|
|
|
|
uint32_t sched_util_max;
|
|
|
|
|
} attr;
|
|
|
|
|
|
|
|
|
|
ret = syscall(SYS_sched_getattr, pid, &attr, sizeof(struct sched_attr), 0);
|
|
|
|
|
if (ret) {
|
|
|
|
|
pw_log_warn("Could not retrieve scheduler attributes: %d", -errno);
|
|
|
|
|
return -errno;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* SCHED_FLAG_KEEP_POLICY |
|
|
|
|
|
* SCHED_FLAG_KEEP_PARAMS |
|
|
|
|
|
* SCHED_FLAG_UTIL_CLAMP_MIN |
|
|
|
|
|
* SCHED_FLAG_UTIL_CLAMP_MAX */
|
|
|
|
|
attr.sched_flags = 0x8 | 0x10 | 0x20 | 0x40;
|
|
|
|
|
attr.sched_util_min = uclamp_min;
|
|
|
|
|
attr.sched_util_max = uclamp_max;
|
|
|
|
|
|
|
|
|
|
ret = syscall(SYS_sched_setattr, pid, &attr, 0);
|
|
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
|
pw_log_warn("Could not set scheduler attributes: %d", -errno);
|
|
|
|
|
return -errno;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
#else
|
|
|
|
|
pw_log_warn("Setting UCLAMP values is only supported on Linux");
|
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
#endif /* __linux__ */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-16 22:11:42 +02:00
|
|
|
SPA_EXPORT
|
2019-12-11 11:57:56 +01:00
|
|
|
int pipewire__module_init(struct pw_impl_module *module, const char *args)
|
2017-05-26 08:05:01 +02:00
|
|
|
{
|
2019-12-11 11:57:56 +01:00
|
|
|
struct pw_context *context = pw_impl_module_get_context(module);
|
2018-02-08 10:02:17 +01:00
|
|
|
struct impl *impl;
|
2022-01-15 20:36:48 +01:00
|
|
|
struct pw_properties *props;
|
|
|
|
|
int res = 0;
|
2018-02-08 10:02:17 +01:00
|
|
|
|
2021-09-16 15:35:10 +10:00
|
|
|
PW_LOG_TOPIC_INIT(mod_topic);
|
|
|
|
|
|
2018-02-08 10:02:17 +01:00
|
|
|
impl = calloc(1, sizeof(struct impl));
|
|
|
|
|
if (impl == NULL)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
pw_log_debug("module %p: new", impl);
|
|
|
|
|
|
2022-01-15 20:36:48 +01:00
|
|
|
props = args ? pw_properties_new_string(args) : pw_properties_new(NULL, NULL);
|
|
|
|
|
if (!props) {
|
2020-02-10 10:43:31 +01:00
|
|
|
res = -errno;
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-15 20:36:48 +01:00
|
|
|
impl->context = context;
|
|
|
|
|
impl->nice_level = pw_properties_get_int32(props, "nice.level", DEFAULT_NICE_LEVEL);
|
|
|
|
|
impl->rt_prio = pw_properties_get_int32(props, "rt.prio", DEFAULT_RT_PRIO);
|
|
|
|
|
impl->rt_time_soft = pw_properties_get_int32(props, "rt.time.soft", DEFAULT_RT_TIME_SOFT);
|
|
|
|
|
impl->rt_time_hard = pw_properties_get_int32(props, "rt.time.hard", DEFAULT_RT_TIME_HARD);
|
2023-07-04 11:11:48 +02:00
|
|
|
impl->rlimits_enabled = pw_properties_get_bool(props, "rlimits.enabled", true);
|
|
|
|
|
impl->rtportal_enabled = pw_properties_get_bool(props, "rtportal.enabled", true);
|
|
|
|
|
impl->rtkit_enabled = pw_properties_get_bool(props, "rtkit.enabled", true);
|
2023-11-05 20:11:19 +10:00
|
|
|
impl->uclamp_min = pw_properties_get_int32(props, "uclamp.min", DEFAULT_UCLAMP_MIN);
|
|
|
|
|
impl->uclamp_max = pw_properties_get_int32(props, "uclamp.max", DEFAULT_UCLAMP_MAX);
|
2022-01-15 20:36:48 +01:00
|
|
|
|
2023-07-20 12:03:29 +02:00
|
|
|
impl->rl.rlim_cur = impl->rt_time_soft;
|
|
|
|
|
impl->rl.rlim_max = impl->rt_time_hard;
|
2023-07-20 12:16:13 +02:00
|
|
|
impl->main_pid = _gettid();
|
2023-07-20 12:03:29 +02:00
|
|
|
|
2022-08-30 15:15:56 +02:00
|
|
|
bool can_use_rtkit = false, use_rtkit = false;
|
2022-08-14 16:44:46 +01:00
|
|
|
|
2023-05-07 19:37:32 +02:00
|
|
|
if (!IS_VALID_NICE_LEVEL(impl->nice_level)) {
|
|
|
|
|
pw_log_info("invalid nice level %d (not between %d and %d). "
|
|
|
|
|
"nice level will not be adjusted",
|
|
|
|
|
impl->nice_level, MIN_NICE_LEVEL, MAX_NICE_LEVEL);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-10 11:19:12 +01:00
|
|
|
#ifdef HAVE_DBUS
|
|
|
|
|
spa_list_init(&impl->threads_list);
|
|
|
|
|
pthread_mutex_init(&impl->lock, NULL);
|
|
|
|
|
pthread_cond_init(&impl->cond, NULL);
|
2021-01-05 12:33:41 +01:00
|
|
|
|
2022-08-30 15:15:56 +02:00
|
|
|
if ((res = check_rtkit(impl, context, &can_use_rtkit)) < 0)
|
2022-03-10 11:19:12 +01:00
|
|
|
goto error;
|
2023-07-20 12:03:29 +02:00
|
|
|
|
2022-08-14 16:44:46 +01:00
|
|
|
#endif
|
|
|
|
|
/* If the user has permissions to use regular realtime scheduling, as well as
|
|
|
|
|
* the nice level we want, then we'll use that instead of RTKit */
|
2023-01-16 12:37:03 +01:00
|
|
|
if (!check_realtime_privileges(impl)) {
|
2022-08-30 15:15:56 +02:00
|
|
|
if (!can_use_rtkit) {
|
2022-08-14 16:44:46 +01:00
|
|
|
res = -ENOTSUP;
|
2023-05-15 09:31:33 +02:00
|
|
|
pw_log_warn("regular realtime scheduling not available"
|
|
|
|
|
" (Portal/RTKit fallback disabled)");
|
2022-08-14 16:44:46 +01:00
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
use_rtkit = true;
|
|
|
|
|
}
|
2021-01-05 12:33:41 +01:00
|
|
|
|
2022-08-14 16:44:46 +01:00
|
|
|
if (IS_VALID_NICE_LEVEL(impl->nice_level)) {
|
2022-08-30 15:15:56 +02:00
|
|
|
if (set_nice(impl, impl->nice_level, !can_use_rtkit) < 0)
|
|
|
|
|
use_rtkit = can_use_rtkit;
|
2022-08-14 16:44:46 +01:00
|
|
|
}
|
2023-07-20 12:03:29 +02:00
|
|
|
if (!use_rtkit)
|
2023-11-15 17:11:19 +01:00
|
|
|
set_rlimit(&impl->rl);
|
2022-08-14 16:44:46 +01:00
|
|
|
|
2023-11-05 20:11:19 +10:00
|
|
|
if (impl->uclamp_max > 1024) {
|
|
|
|
|
pw_log_warn("uclamp.max out of bounds. Got %d, clamping to 1024.", impl->uclamp_max);
|
|
|
|
|
impl->uclamp_max = 1024;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (impl->uclamp_min || impl->uclamp_max < 1024)
|
|
|
|
|
set_uclamp(impl->uclamp_min, impl->uclamp_max, impl->main_pid);
|
|
|
|
|
|
2022-08-14 16:44:46 +01:00
|
|
|
#ifdef HAVE_DBUS
|
|
|
|
|
impl->use_rtkit = use_rtkit;
|
2022-01-15 20:36:48 +01:00
|
|
|
if (impl->use_rtkit) {
|
2023-07-23 18:16:00 +02:00
|
|
|
struct spa_dict_item items[] = {
|
|
|
|
|
{ "thread-loop.start-signal", "true" }
|
|
|
|
|
};
|
2023-07-22 01:20:58 +00:00
|
|
|
if ((res = rtkit_get_bus(impl)) < 0)
|
|
|
|
|
goto error;
|
|
|
|
|
|
2023-07-23 18:16:00 +02:00
|
|
|
impl->thread_loop = pw_thread_loop_new("module-rt",
|
|
|
|
|
&SPA_DICT_INIT_ARRAY(items));
|
2023-07-20 12:03:29 +02:00
|
|
|
if (impl->thread_loop == NULL) {
|
|
|
|
|
res = -errno;
|
|
|
|
|
goto error;
|
2022-01-15 20:36:48 +01:00
|
|
|
}
|
2023-07-20 12:03:29 +02:00
|
|
|
pw_thread_loop_lock(impl->thread_loop);
|
|
|
|
|
pw_thread_loop_start(impl->thread_loop);
|
2023-07-20 15:30:01 +02:00
|
|
|
pw_thread_loop_wait(impl->thread_loop);
|
2023-07-20 12:03:29 +02:00
|
|
|
pw_thread_loop_unlock(impl->thread_loop);
|
|
|
|
|
|
|
|
|
|
pw_loop_invoke(pw_thread_loop_get_loop(impl->thread_loop),
|
|
|
|
|
do_rtkit_setup, 0, NULL, 0, false, impl);
|
|
|
|
|
|
|
|
|
|
pw_log_debug("initialized using RTKit");
|
|
|
|
|
} else {
|
|
|
|
|
pw_log_debug("initialized using regular realtime scheduling");
|
2022-01-15 20:36:48 +01:00
|
|
|
}
|
2023-07-20 12:03:29 +02:00
|
|
|
#else
|
|
|
|
|
pw_log_debug("initialized using regular realtime scheduling");
|
2022-08-14 16:44:46 +01:00
|
|
|
#endif
|
2021-07-05 10:26:56 +02:00
|
|
|
|
|
|
|
|
impl->thread_utils.iface = SPA_INTERFACE_INIT(
|
2021-07-06 15:48:20 +02:00
|
|
|
SPA_TYPE_INTERFACE_ThreadUtils,
|
|
|
|
|
SPA_VERSION_THREAD_UTILS,
|
2021-07-05 10:26:56 +02:00
|
|
|
&impl_thread_utils, impl);
|
2022-03-30 20:31:42 +02:00
|
|
|
|
|
|
|
|
pw_context_set_object(context, SPA_TYPE_INTERFACE_ThreadUtils,
|
|
|
|
|
&impl->thread_utils);
|
2018-02-08 10:02:17 +01:00
|
|
|
|
2019-12-11 11:57:56 +01:00
|
|
|
pw_impl_module_add_listener(module, &impl->module_listener, &module_events, impl);
|
2018-02-08 10:02:17 +01:00
|
|
|
|
2019-12-11 11:57:56 +01:00
|
|
|
pw_impl_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props));
|
2022-01-15 20:36:48 +01:00
|
|
|
pw_impl_module_update_properties(module, &props->dict);
|
2018-06-01 11:23:02 +02:00
|
|
|
|
2022-01-15 20:36:48 +01:00
|
|
|
goto done;
|
2018-04-20 16:27:19 +02:00
|
|
|
|
2019-06-20 11:04:34 +02:00
|
|
|
error:
|
2022-03-10 11:19:12 +01:00
|
|
|
#ifdef HAVE_DBUS
|
2023-07-20 12:03:29 +02:00
|
|
|
if (impl->thread_loop)
|
|
|
|
|
pw_thread_loop_destroy(impl->thread_loop);
|
2023-07-20 16:17:14 +02:00
|
|
|
if (impl->rtkit_bus)
|
|
|
|
|
pw_rtkit_bus_free(impl->rtkit_bus);
|
2022-03-10 11:19:12 +01:00
|
|
|
#endif
|
2018-04-20 16:27:19 +02:00
|
|
|
free(impl);
|
2022-01-15 20:36:48 +01:00
|
|
|
done:
|
|
|
|
|
pw_properties_free(props);
|
|
|
|
|
|
2018-04-20 16:27:19 +02:00
|
|
|
return res;
|
2016-10-28 16:56:33 +02:00
|
|
|
}
|