From 46c433aabd33454271cd5e2890f1171acd6d7fa3 Mon Sep 17 00:00:00 2001 From: Mikhail Naganov Date: Wed, 6 Mar 2024 17:57:28 +0000 Subject: [PATCH] audio: Set correct priority for the SPATIALIZER stream thread To align with the framework, the thread serving SPATIALIZER stream I/O must use SCHED_FIFO and realtime priority. The latter can be set via `audio.spatializer.priority` property. Bug: 321233946 Test: adb shell ps -Tl -p (cherry picked from https://googleplex-android-review.googlesource.com/q/commit:81a14293f4f26bf3af740cc1682c1111d7e1e29d) Merged-In: I80107c9aa7d86cbfc2f79c2ad05959fc9f7913f7 Change-Id: I80107c9aa7d86cbfc2f79c2ad05959fc9f7913f7 --- audio/aidl/default/Stream.cpp | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/audio/aidl/default/Stream.cpp b/audio/aidl/default/Stream.cpp index 807348fad7..697ff0d0f0 100644 --- a/audio/aidl/default/Stream.cpp +++ b/audio/aidl/default/Stream.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -652,16 +653,34 @@ ndk::ScopedAStatus StreamCommonImpl::initInstance( isBitPositionFlagSet(flags.template get(), AudioInputFlags::FAST)) || (flags.getTag() == AudioIoFlags::Tag::output && - isBitPositionFlagSet(flags.template get(), - AudioOutputFlags::FAST))) { + (isBitPositionFlagSet(flags.template get(), + AudioOutputFlags::FAST) || + isBitPositionFlagSet(flags.template get(), + AudioOutputFlags::SPATIALIZER)))) { // FAST workers should be run with a SCHED_FIFO scheduler, however the host process // might be lacking the capability to request it, thus a failure to set is not an error. pid_t workerTid = mWorker->getTid(); if (workerTid > 0) { - struct sched_param param; - param.sched_priority = 3; // Must match SchedulingPolicyService.PRIORITY_MAX (Java). + constexpr int32_t kRTPriorityMin = 1; // SchedulingPolicyService.PRIORITY_MIN (Java). + constexpr int32_t kRTPriorityMax = 3; // SchedulingPolicyService.PRIORITY_MAX (Java). + int priorityBoost = kRTPriorityMax; + if (flags.getTag() == AudioIoFlags::Tag::output && + isBitPositionFlagSet(flags.template get(), + AudioOutputFlags::SPATIALIZER)) { + const int32_t sptPrio = + property_get_int32("audio.spatializer.priority", kRTPriorityMin); + if (sptPrio >= kRTPriorityMin && sptPrio <= kRTPriorityMax) { + priorityBoost = sptPrio; + } else { + LOG(WARNING) << __func__ << ": invalid spatializer priority: " << sptPrio; + return ndk::ScopedAStatus::ok(); + } + } + struct sched_param param = { + .sched_priority = priorityBoost, + }; if (sched_setscheduler(workerTid, SCHED_FIFO | SCHED_RESET_ON_FORK, ¶m) != 0) { - PLOG(WARNING) << __func__ << ": failed to set FIFO scheduler for a fast thread"; + PLOG(WARNING) << __func__ << ": failed to set FIFO scheduler and priority"; } } else { LOG(WARNING) << __func__ << ": invalid worker tid: " << workerTid;