Skip to content

Commit 79da900

Browse files
afq984Chromeos LUCI
authored and
Chromeos LUCI
committed
audio-worker: Set priority when called from cras_processor
BUG=b:233174528 TEST=cras_tests capture /dev/null --effects=0x300 TEST=ps -o rtprio,comm -T -u cras Change-Id: I8d74f16fb787cbe544374c53eb8a03f84945f9a9 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/5665517 Tested-by: [email protected] <[email protected]> Reviewed-by: Chih-Yang Hsia <[email protected]> Commit-Queue: Li-Yu Yu <[email protected]>
1 parent ac974a5 commit 79da900

File tree

6 files changed

+51
-28
lines changed

6 files changed

+51
-28
lines changed

audio_processor/src/bin/offline-pipeline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ fn run(command: Command) {
169169
let (profile_sender, profile_receiver) = channel();
170170
let mut pipeline = PipelineBuilder::new(check_shape.get_output_format())
171171
.with_profile_sender(profile_sender)
172-
.with_worker_factory(AudioWorkerSubprocessFactory)
172+
.with_worker_factory(AudioWorkerSubprocessFactory::default())
173173
.build(Processor::Pipeline {
174174
processors: pipeline_decl,
175175
})

audio_processor/src/processors/peer/managed.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use command_fds::CommandFdExt;
1010
use command_fds::FdMapping;
1111

1212
use super::create_socketpair;
13+
use super::worker::AUDIO_WORKER_SET_THREAD_PRIORITY;
1314
use super::BlockingSeqPacketProcessor;
1415
use super::Worker;
1516
use crate::config::Processor;
@@ -90,18 +91,31 @@ impl Drop for ThreadedWorkerHandle {
9091
}
9192
}
9293

93-
pub struct AudioWorkerSubprocessFactory;
94+
#[derive(Default)]
95+
pub struct AudioWorkerSubprocessFactory {
96+
set_thread_priority: bool,
97+
}
98+
99+
impl AudioWorkerSubprocessFactory {
100+
pub fn with_set_thread_priority(mut self) -> Self {
101+
self.set_thread_priority = true;
102+
self
103+
}
104+
}
94105

95106
impl WorkerFactory for AudioWorkerSubprocessFactory {
96107
fn create(&self, worker_fd: OwnedFd) -> anyhow::Result<Box<dyn WorkerHandle>> {
97-
let child = std::process::Command::new("audio-worker")
108+
let mut command = std::process::Command::new("audio-worker");
109+
command
98110
.fd_mappings(vec![FdMapping {
99111
parent_fd: worker_fd,
100112
child_fd: 3,
101113
}])
102-
.context("fd_mappings")?
103-
.spawn()
104-
.context("spawn audio-worker")?;
114+
.context("fd_mappings")?;
115+
if self.set_thread_priority {
116+
command.env(AUDIO_WORKER_SET_THREAD_PRIORITY, "1");
117+
}
118+
let child = command.spawn().context("spawn audio-worker")?;
105119
Ok(Box::new(AudioWorkerSubprocessHandle { child }))
106120
}
107121
}

audio_processor/src/processors/peer/worker.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ use crate::processors::peer::messages::recv;
1919
use crate::processors::peer::messages::Init;
2020
use crate::processors::peer::messages::RequestOp;
2121
use crate::processors::peer::messages::INIT_MAX_SIZE;
22+
use crate::util::set_thread_priority;
2223
use crate::AudioProcessor;
2324
use crate::MultiBuffer;
2425
use crate::MultiSlice;
2526
use crate::ProcessorVec;
2627

28+
pub(super) const AUDIO_WORKER_SET_THREAD_PRIORITY: &'static str =
29+
"AUDIO_WORKER_SET_THREAD_PRIORITY";
30+
2731
pub struct Worker<'a> {
2832
fd: BorrowedFd<'a>,
2933
pipeline: ProcessorVec,
@@ -37,6 +41,10 @@ enum Response<'a> {
3741

3842
impl<'a> Worker<'a> {
3943
fn new(fd: BorrowedFd<'a>) -> anyhow::Result<Self> {
44+
if std::env::var(AUDIO_WORKER_SET_THREAD_PRIORITY).is_ok() {
45+
set_thread_priority().context("set_thread_priority")?;
46+
}
47+
4048
let mut buf = vec![0u8; INIT_MAX_SIZE];
4149
let (request_op, payload_len) = recv::<RequestOp>(fd.as_fd(), &mut buf).context("init")?;
4250
if request_op != RequestOp::Init {

audio_processor/src/processors/thread.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,15 @@ use std::marker::PhantomData;
66
use std::sync::mpsc::Receiver;
77
use std::sync::mpsc::Sender;
88

9-
use anyhow::ensure;
109
use anyhow::Context;
11-
use nix::sys::resource::setrlimit;
12-
use nix::sys::resource::Resource;
1310

11+
use crate::util::set_thread_priority;
1412
use crate::AudioProcessor;
1513
use crate::Format;
1614
use crate::MultiBuffer;
1715
use crate::Result;
1816
use crate::Shape;
1917

20-
// TODO(b/268271100): Call the C version when we can build C code before Rust.
21-
fn set_thread_priority() -> anyhow::Result<()> {
22-
// CRAS_SERVER_RT_THREAD_PRIORITY 12
23-
let p = 12;
24-
setrlimit(Resource::RLIMIT_RTPRIO, p, p).context("setrlimit")?;
25-
26-
// SAFETY: sched_param is properly initialized.
27-
unsafe {
28-
let sched_param = libc::sched_param {
29-
sched_priority: p as i32,
30-
};
31-
let rc = libc::pthread_setschedparam(libc::pthread_self(), libc::SCHED_RR, &sched_param);
32-
ensure!(rc == 0, "pthread_setschedparam returned {rc}");
33-
}
34-
35-
Ok(())
36-
}
37-
3818
pub struct ThreadedProcessor<T: AudioProcessor> {
3919
phantom: PhantomData<T>,
4020
join_handle: Option<std::thread::JoinHandle<()>>,

audio_processor/src/util.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
use std::path::Path;
66

7+
use anyhow::ensure;
78
use anyhow::Context;
89
use hound::WavReader;
10+
use nix::sys::resource::setrlimit;
11+
use nix::sys::resource::Resource;
912

1013
use crate::MultiBuffer;
1114
use crate::Sample;
@@ -25,3 +28,21 @@ pub fn read_wav<T: Sample + hound::Sample>(
2528

2629
Ok((spec, out))
2730
}
31+
32+
// TODO(b/268271100): Call the C version when we can build C code before Rust.
33+
pub fn set_thread_priority() -> anyhow::Result<()> {
34+
// CRAS_SERVER_RT_THREAD_PRIORITY 12
35+
let p = 12;
36+
setrlimit(Resource::RLIMIT_RTPRIO, p, p).context("setrlimit")?;
37+
38+
// SAFETY: sched_param is properly initialized.
39+
unsafe {
40+
let sched_param = libc::sched_param {
41+
sched_priority: p as i32,
42+
};
43+
let rc = libc::pthread_setschedparam(libc::pthread_self(), libc::SCHED_RR, &sched_param);
44+
ensure!(rc == 0, "pthread_setschedparam returned {rc}");
45+
}
46+
47+
Ok(())
48+
}

cras/src/server/rust/src/cras_processor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ impl CrasProcessor {
256256
let decl_debug = format!("{decl:?}");
257257
let pipeline = PipelineBuilder::new(config.format())
258258
// TODO(b/349784210): Use a hardened worker factory.
259-
.with_worker_factory(AudioWorkerSubprocessFactory)
259+
.with_worker_factory(AudioWorkerSubprocessFactory::default().with_set_thread_priority())
260260
.build(Processor::Pipeline { processors: decl })
261261
.context("failed to build pipeline")?;
262262

0 commit comments

Comments
 (0)