From dd4a1a6d73b2fd5fa293361cc4a1d43d1be63b5b Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Sat, 28 Sep 2024 03:59:37 +0200 Subject: [PATCH] Set thread priority relative to backend This also removed direct calls to backend real_time_priority for good measure. --- libs/ardour/ardour/audio_backend.h | 2 +- libs/ardour/ardour/audioengine.h | 3 --- libs/ardour/audioengine.cc | 38 +++++---------------------- libs/pbd/pthread_utils.cc | 41 +++++++++++++++++------------- 4 files changed, 30 insertions(+), 54 deletions(-) diff --git a/libs/ardour/ardour/audio_backend.h b/libs/ardour/ardour/audio_backend.h index e775bec59f..5986b895e4 100644 --- a/libs/ardour/ardour/audio_backend.h +++ b/libs/ardour/ardour/audio_backend.h @@ -164,7 +164,7 @@ public: /** Return true if the backed is JACK */ virtual bool is_jack () const { return false; } - virtual int client_real_time_priority () { return PBD_RT_PRI_PROC; } + virtual int client_real_time_priority () { return 0; } /* Discovering devices and parameters */ diff --git a/libs/ardour/ardour/audioengine.h b/libs/ardour/ardour/audioengine.h index 531ae315a2..cc1b0db6bc 100644 --- a/libs/ardour/ardour/audioengine.h +++ b/libs/ardour/ardour/audioengine.h @@ -115,9 +115,6 @@ class LIBARDOUR_API AudioEngine : public PortManager, public SessionHandlePtr void request_device_list_update(); void launch_device_control_app(); - int client_real_time_priority (); - bool is_realtime() const; - // for the user which hold state_lock to check if reset operation is pending bool is_reset_requested() const { return _hw_reset_request_count.load(); } diff --git a/libs/ardour/audioengine.cc b/libs/ardour/audioengine.cc index d46ea648eb..019dd3b7cf 100644 --- a/libs/ardour/audioengine.cc +++ b/libs/ardour/audioengine.cc @@ -1067,6 +1067,12 @@ AudioEngine::start (bool for_latency) return -1; } + if (_backend->is_realtime ()) { + pbd_set_engine_rt_priority (_backend->client_real_time_priority ()); + } else { + pbd_set_engine_rt_priority (0); + } + _running = true; if (_session) { @@ -1190,38 +1196,6 @@ AudioEngine::get_dsp_load() const return _backend->dsp_load (); } -bool -AudioEngine::is_realtime() const -{ - if (!_backend) { - return false; - } - - return _backend->is_realtime(); -} - -int -AudioEngine::client_real_time_priority () -{ - if (!_backend) { - assert (0); - return PBD_RT_PRI_PROC; - } - if (!_backend->is_realtime ()) { - /* this is only an issue with the Dummy backend. - * - with JACK, we require rt permissions. - * - with ALSA/PulseAudio this can only happen if rt permissions - * are n/a. Other attempts to get rt will fail likewise. - * - * perhaps: - * TODO: use is_realtime () ? PBD_SCHED_FIFO : PBD_SCHED_OTHER - */ - return PBD_RT_PRI_PROC; // XXX - } - - return _backend->client_real_time_priority(); -} - void AudioEngine::transport_start () { diff --git a/libs/pbd/pthread_utils.cc b/libs/pbd/pthread_utils.cc index 9655665988..f798da4d8a 100644 --- a/libs/pbd/pthread_utils.cc +++ b/libs/pbd/pthread_utils.cc @@ -53,6 +53,7 @@ typedef std::map ThreadMap; static ThreadMap all_threads; static pthread_mutex_t thread_map_lock = PTHREAD_MUTEX_INITIALIZER; static Glib::Threads::Private thread_name (free); +static int base_priority_relative_to_max = -20; namespace PBD { @@ -272,6 +273,19 @@ pbd_pthread_create ( return rv; } +void +pbd_set_engine_rt_priority (int p) +{ + /* this is mainly for JACK's benefit */ + const int p_max = sched_get_priority_max (SCHED_FIFO); + const int p_min = sched_get_priority_min (SCHED_FIFO); + if (p <= 0 || p <= p_min + 10 || p > p_max) { + base_priority_relative_to_max = -20; + } else { + base_priority_relative_to_max = p - p_max; + } +} + int pbd_pthread_priority (PBDThreadClass which) { @@ -292,11 +306,11 @@ pbd_pthread_priority (PBDThreadClass which) return -13; } #else - int base = -20; + int base = base_priority_relative_to_max; const char* p = getenv ("ARDOUR_SCHED_PRI"); if (p && *p) { base = atoi (p); - if (base > -5 && base < 5) { + if (base > -5 || base < -85) { base = -20; } } @@ -309,6 +323,8 @@ pbd_pthread_priority (PBDThreadClass which) default: case THREAD_PROC: return base - 2; + case THREAD_CTRL: + return base - 3; case THREAD_IO: return base - 10; } @@ -322,23 +338,12 @@ pbd_absolute_rt_priority (int policy, int priority) const int p_min = sched_get_priority_min (policy); // Linux: 1 const int p_max = sched_get_priority_max (policy); // Linux: 99 - if (priority == 0) { - assert (0); - priority = (p_min + p_max) / 2; - } else if (priority > 0) { - /* value relative to minium */ - priority += p_min - 1; - } else { - /* value relative maximum */ - priority += p_max + 1; - } + /* priority is relative to the max */ + assert (priority < 0); + priority += p_max + 1; - if (priority > p_max) { - priority = p_max; - } - if (priority < p_min) { - priority = p_min; - } + priority = std::min (p_max, priority); + priority = std::max (p_min, priority); return priority; }