diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/audio_thread_priority/src | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/audio_thread_priority/src')
-rw-r--r-- | third_party/rust/audio_thread_priority/src/lib.rs | 688 | ||||
-rw-r--r-- | third_party/rust/audio_thread_priority/src/mach_sys.rs | 36 | ||||
-rw-r--r-- | third_party/rust/audio_thread_priority/src/rt_linux.rs | 311 | ||||
-rw-r--r-- | third_party/rust/audio_thread_priority/src/rt_mach.rs | 162 | ||||
-rw-r--r-- | third_party/rust/audio_thread_priority/src/rt_win.rs | 99 |
5 files changed, 1296 insertions, 0 deletions
diff --git a/third_party/rust/audio_thread_priority/src/lib.rs b/third_party/rust/audio_thread_priority/src/lib.rs new file mode 100644 index 0000000000..bd6cd3705f --- /dev/null +++ b/third_party/rust/audio_thread_priority/src/lib.rs @@ -0,0 +1,688 @@ +//! # audio_thread_priority +//! +//! Promote the current thread, or another thread (possibly in another process), to real-time +//! priority, suitable for low-latency audio processing. +//! +//! # Example +//! +//! ```rust +//! +//! use audio_thread_priority::{promote_current_thread_to_real_time, demote_current_thread_from_real_time}; +//! +//! // ... on a thread that will compute audio and has to be real-time: +//! match promote_current_thread_to_real_time(512, 44100) { +//! Ok(h) => { +//! println!("this thread is now bumped to real-time priority."); +//! +//! // Do some real-time work... +//! +//! match demote_current_thread_from_real_time(h) { +//! Ok(_) => { +//! println!("this thread is now bumped back to normal.") +//! } +//! Err(_) => { +//! println!("Could not bring the thread back to normal priority.") +//! } +//! }; +//! } +//! Err(e) => { +//! eprintln!("Error promoting thread to real-time: {}", e); +//! } +//! } +//! +//! ``` + +#![warn(missing_docs)] + +use cfg_if::cfg_if; +use std::error::Error; +use std::fmt; + +/// The OS-specific issue is available as `inner` +#[derive(Debug)] +pub struct AudioThreadPriorityError { + message: String, + inner: Option<Box<dyn Error + 'static>>, +} + +impl AudioThreadPriorityError { + cfg_if! { + if #[cfg(all(target_os = "linux", feature = "dbus"))] { + fn new_with_inner(message: &str, inner: Box<dyn Error>) -> AudioThreadPriorityError { + AudioThreadPriorityError { + message: message.into(), + inner: Some(inner), + } + } + } + } + fn new(message: &str) -> AudioThreadPriorityError { + AudioThreadPriorityError { + message: message.into(), + inner: None, + } + } +} + +impl fmt::Display for AudioThreadPriorityError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut rv = write!(f, "AudioThreadPriorityError: {}", &self.message); + if let Some(inner) = &self.inner { + rv = write!(f, " ({})", inner); + } + rv + } +} + +impl Error for AudioThreadPriorityError { + fn description(&self) -> &str { + &self.message + } + + fn source(&self) -> Option<&(dyn Error + 'static)> { + self.inner.as_ref().map(|e| e.as_ref()) + } +} + +cfg_if! { + if #[cfg(target_os = "macos")] { + mod rt_mach; +#[allow(unused, non_camel_case_types, non_snake_case, non_upper_case_globals)] + mod mach_sys; + extern crate mach; + extern crate libc; + use rt_mach::promote_current_thread_to_real_time_internal; + use rt_mach::demote_current_thread_from_real_time_internal; + use rt_mach::RtPriorityHandleInternal; + } else if #[cfg(target_os = "windows")] { + mod rt_win; + use rt_win::promote_current_thread_to_real_time_internal; + use rt_win::demote_current_thread_from_real_time_internal; + use rt_win::RtPriorityHandleInternal; + } else if #[cfg(all(target_os = "linux", feature = "dbus"))] { + mod rt_linux; + extern crate dbus; + extern crate libc; + use rt_linux::promote_current_thread_to_real_time_internal; + use rt_linux::demote_current_thread_from_real_time_internal; + use rt_linux::set_real_time_hard_limit_internal as set_real_time_hard_limit; + use rt_linux::get_current_thread_info_internal; + use rt_linux::promote_thread_to_real_time_internal; + use rt_linux::demote_thread_from_real_time_internal; + use rt_linux::RtPriorityThreadInfoInternal; + use rt_linux::RtPriorityHandleInternal; + #[no_mangle] + /// Size of a RtPriorityThreadInfo or atp_thread_info struct, for use in FFI. + pub static ATP_THREAD_INFO_SIZE: usize = std::mem::size_of::<RtPriorityThreadInfo>(); + } else { + // blanket implementations for Android, Linux Desktop without dbus and others + pub struct RtPriorityHandleInternal {} + #[derive(Clone, Copy, PartialEq)] + pub struct RtPriorityThreadInfoInternal { + _dummy: u8 + } + + cfg_if! { + if #[cfg(not(target_os = "linux"))] { + pub type RtPriorityThreadInfo = RtPriorityThreadInfoInternal; + } + } + + impl RtPriorityThreadInfo { + pub fn serialize(&self) -> [u8; 1] { + [0] + } + pub fn deserialize(_: [u8; 1]) -> Self { + RtPriorityThreadInfo{_dummy: 0} + } + } + pub fn promote_current_thread_to_real_time_internal(_: u32, audio_samplerate_hz: u32) -> Result<RtPriorityHandle, AudioThreadPriorityError> { + if audio_samplerate_hz == 0 { + return Err(AudioThreadPriorityError{message: "sample rate is zero".to_string(), inner: None}); + } + // no-op + Ok(RtPriorityHandle{}) + } + pub fn demote_current_thread_from_real_time_internal(_: RtPriorityHandle) -> Result<(), AudioThreadPriorityError> { + // no-op + Ok(()) + } + pub fn set_real_time_hard_limit( + _: u32, + _: u32, + ) -> Result<(), AudioThreadPriorityError> { + Ok(()) + } + pub fn get_current_thread_info_internal() -> Result<RtPriorityThreadInfo, AudioThreadPriorityError> { + Ok(RtPriorityThreadInfo{_dummy: 0}) + } + pub fn promote_thread_to_real_time_internal( + _: RtPriorityThreadInfo, + _: u32, + audio_samplerate_hz: u32, + ) -> Result<RtPriorityHandle, AudioThreadPriorityError> { + if audio_samplerate_hz == 0 { + return Err(AudioThreadPriorityError::new("sample rate is zero")); + } + return Ok(RtPriorityHandle{}); + } + + pub fn demote_thread_from_real_time_internal(_: RtPriorityThreadInfo) -> Result<(), AudioThreadPriorityError> { + return Ok(()); + } + #[no_mangle] + /// Size of a RtPriorityThreadInfo or atp_thread_info struct, for use in FFI. + pub static ATP_THREAD_INFO_SIZE: usize = std::mem::size_of::<RtPriorityThreadInfo>(); + } +} + +/// Opaque handle to a thread handle structure. +pub type RtPriorityHandle = RtPriorityHandleInternal; + +cfg_if! { + if #[cfg(target_os = "linux")] { +/// Opaque handle to a thread info. +/// +/// This can be serialized to raw bytes to be sent via IPC. +/// +/// This call is useful on Linux desktop only, when the process is sandboxed and +/// cannot promote itself directly. +pub type RtPriorityThreadInfo = RtPriorityThreadInfoInternal; + + +/// Get the calling thread's information, to be able to promote it to real-time from somewhere +/// else, later. +/// +/// This call is useful on Linux desktop only, when the process is sandboxed and +/// cannot promote itself directly. +/// +/// # Return value +/// +/// Ok in case of success, with an opaque structure containing relevant info for the platform, Err +/// otherwise. +pub fn get_current_thread_info() -> Result<RtPriorityThreadInfo, AudioThreadPriorityError> { + get_current_thread_info_internal() +} + +/// Return a byte buffer containing serialized information about a thread, to promote it to +/// real-time from elsewhere. +/// +/// This call is useful on Linux desktop only, when the process is sandboxed and +/// cannot promote itself directly. +pub fn thread_info_serialize( + thread_info: RtPriorityThreadInfo, +) -> [u8; std::mem::size_of::<RtPriorityThreadInfo>()] { + thread_info.serialize() +} + +/// From a byte buffer, return a `RtPriorityThreadInfo`. +/// +/// This call is useful on Linux desktop only, when the process is sandboxed and +/// cannot promote itself directly. +/// +/// # Arguments +/// +/// A byte buffer containing a serializezd `RtPriorityThreadInfo`. +pub fn thread_info_deserialize( + bytes: [u8; std::mem::size_of::<RtPriorityThreadInfo>()], +) -> RtPriorityThreadInfo { + RtPriorityThreadInfoInternal::deserialize(bytes) +} + +/// Get the calling threads' information, to promote it from another process or thread, with a C +/// API. +/// +/// This is intended to call on the thread that will end up being promoted to real time priority, +/// but that cannot do it itself (probably because of sandboxing reasons). +/// +/// After use, it MUST be freed by calling `atp_free_thread_info`. +/// +/// # Return value +/// +/// A pointer to a struct that can be serialized and deserialized, and that can be passed to +/// `atp_promote_thread_to_real_time`, even from another process. +#[no_mangle] +pub extern "C" fn atp_get_current_thread_info() -> *mut atp_thread_info { + match get_current_thread_info() { + Ok(thread_info) => Box::into_raw(Box::new(atp_thread_info(thread_info))), + _ => std::ptr::null_mut(), + } +} + +/// Frees a thread info, with a c api. +/// +/// # Arguments +/// +/// thread_info: the `atp_thread_info` structure to free. +/// +/// # Return value +/// +/// 0 in case of success, 1 otherwise (if `thread_info` is NULL). +#[no_mangle] +pub unsafe extern "C" fn atp_free_thread_info(thread_info: *mut atp_thread_info) -> i32 { + if thread_info.is_null() { + return 1; + } + Box::from_raw(thread_info); + 0 +} + +/// Return a byte buffer containing serialized information about a thread, to promote it to +/// real-time from elsewhere, with a C API. +/// +/// `bytes` MUST be `std::mem::size_of<RtPriorityThreadInfo>()` bytes long. +/// +/// This is exposed in the C API as `ATP_THREAD_INFO_SIZE`. +/// +/// This call is useful on Linux desktop only, when the process is sandboxed, cannot promote itself +/// directly, and the `atp_thread_info` struct must be passed via IPC. +#[no_mangle] +pub unsafe extern "C" fn atp_serialize_thread_info( + thread_info: *mut atp_thread_info, + bytes: *mut libc::c_void, +) { + let thread_info = &mut *thread_info; + let source = thread_info.0.serialize(); + std::ptr::copy(source.as_ptr(), bytes as *mut u8, source.len()); +} + +/// From a byte buffer, return a `RtPriorityThreadInfo`, with a C API. +/// +/// This call is useful on Linux desktop only, when the process is sandboxed and +/// cannot promote itself directly. +/// +/// # Arguments +/// +/// A byte buffer containing a serializezd `RtPriorityThreadInfo`. +#[no_mangle] +pub unsafe extern "C" fn atp_deserialize_thread_info( + in_bytes: *mut u8, +) -> *mut atp_thread_info { + let bytes = *(in_bytes as *mut [u8; std::mem::size_of::<RtPriorityThreadInfoInternal>()]); + let thread_info = RtPriorityThreadInfoInternal::deserialize(bytes); + Box::into_raw(Box::new(atp_thread_info(thread_info))) +} + +/// Promote a particular thread thread to real-time priority. +/// +/// This call is useful on Linux desktop only, when the process is sandboxed and +/// cannot promote itself directly. +/// +/// # Arguments +/// +/// * `thread_info` - informations about the thread to promote, gathered using +/// `get_current_thread_info`. +/// * `audio_buffer_frames` - the exact or an upper limit on the number of frames that have to be +/// rendered each callback, or 0 for a sensible default value. +/// * `audio_samplerate_hz` - the sample-rate for this audio stream, in Hz. +/// +/// # Return value +/// +/// This function returns a `Result<RtPriorityHandle>`, which is an opaque struct to be passed to +/// `demote_current_thread_from_real_time` to revert to the previous thread priority. +pub fn promote_thread_to_real_time( + thread_info: RtPriorityThreadInfo, + audio_buffer_frames: u32, + audio_samplerate_hz: u32, +) -> Result<RtPriorityHandle, AudioThreadPriorityError> { + if audio_samplerate_hz == 0 { + return Err(AudioThreadPriorityError::new("sample rate is zero")); + } + promote_thread_to_real_time_internal( + thread_info, + audio_buffer_frames, + audio_samplerate_hz, + ) +} + +/// Demotes a thread from real-time priority. +/// +/// # Arguments +/// +/// * `thread_info` - An opaque struct returned from a successful call to +/// `get_current_thread_info`. +/// +/// # Return value +/// +/// `Ok` in case of success, `Err` otherwise. +pub fn demote_thread_from_real_time(thread_info: RtPriorityThreadInfo) -> Result<(), AudioThreadPriorityError> { + demote_thread_from_real_time_internal(thread_info) +} + +/// Opaque info to a particular thread. +#[allow(non_camel_case_types)] +pub struct atp_thread_info(RtPriorityThreadInfo); + +/// Promote a specific thread to real-time, with a C API. +/// +/// This is useful when the thread to promote cannot make some system calls necessary to promote +/// it. +/// +/// # Arguments +/// +/// `thread_info` - the information of the thread to promote to real-time, gather from calling +/// `atp_get_current_thread_info` on the thread to promote. +/// * `audio_buffer_frames` - the exact or an upper limit on the number of frames that have to be +/// rendered each callback, or 0 for a sensible default value. +/// * `audio_samplerate_hz` - the sample-rate for this audio stream, in Hz. +/// +/// # Return value +/// +/// A pointer to an `atp_handle` in case of success, NULL otherwise. +#[no_mangle] +pub unsafe extern "C" fn atp_promote_thread_to_real_time( + thread_info: *mut atp_thread_info, + audio_buffer_frames: u32, + audio_samplerate_hz: u32, +) -> *mut atp_handle { + let thread_info = &mut *thread_info; + match promote_thread_to_real_time(thread_info.0, audio_buffer_frames, audio_samplerate_hz) { + Ok(handle) => Box::into_raw(Box::new(atp_handle(handle))), + _ => std::ptr::null_mut(), + } +} + +/// Demote a thread promoted to from real-time, with a C API. +/// +/// # Arguments +/// +/// `handle` - an opaque struct received from a promoting function. +/// +/// # Return value +/// +/// 0 in case of success, non-zero otherwise. +#[no_mangle] +pub unsafe extern "C" fn atp_demote_thread_from_real_time(thread_info: *mut atp_thread_info) -> i32 { + if thread_info.is_null() { + return 1; + } + let thread_info = (*thread_info).0; + + match demote_thread_from_real_time(thread_info) { + Ok(_) => 0, + _ => 1, + } +} + +/// Set a real-time limit for the calling thread. +/// +/// # Arguments +/// +/// `audio_buffer_frames` - the number of frames the audio callback has to render each quantum. 0 +/// picks a rather high default value. +/// `audio_samplerate_hz` - the sample-rate of the audio stream. +/// +/// # Return value +/// +/// 0 in case of success, 1 otherwise. +#[no_mangle] +pub extern "C" fn atp_set_real_time_limit(audio_buffer_frames: u32, + audio_samplerate_hz: u32) -> i32 { + let r = set_real_time_hard_limit(audio_buffer_frames, audio_samplerate_hz); + if r.is_err() { + return 1; + } + 0 +} + +} +} + +/// Promote the calling thread thread to real-time priority. +/// +/// # Arguments +/// +/// * `audio_buffer_frames` - the exact or an upper limit on the number of frames that have to be +/// rendered each callback, or 0 for a sensible default value. +/// * `audio_samplerate_hz` - the sample-rate for this audio stream, in Hz. +/// +/// # Return value +/// +/// This function returns a `Result<RtPriorityHandle>`, which is an opaque struct to be passed to +/// `demote_current_thread_from_real_time` to revert to the previous thread priority. +pub fn promote_current_thread_to_real_time( + audio_buffer_frames: u32, + audio_samplerate_hz: u32, +) -> Result<RtPriorityHandle, AudioThreadPriorityError> { + if audio_samplerate_hz == 0 { + return Err(AudioThreadPriorityError::new("sample rate is zero")); + } + promote_current_thread_to_real_time_internal(audio_buffer_frames, audio_samplerate_hz) +} + +/// Demotes the calling thread from real-time priority. +/// +/// # Arguments +/// +/// * `handle` - An opaque struct returned from a successful call to +/// `promote_current_thread_to_real_time`. +/// +/// # Return value +/// +/// `Ok` in scase of success, `Err` otherwise. +pub fn demote_current_thread_from_real_time( + handle: RtPriorityHandle, +) -> Result<(), AudioThreadPriorityError> { + demote_current_thread_from_real_time_internal(handle) +} + +/// Opaque handle for the C API +#[allow(non_camel_case_types)] +pub struct atp_handle(RtPriorityHandle); + +/// Promote the calling thread thread to real-time priority, with a C API. +/// +/// # Arguments +/// +/// * `audio_buffer_frames` - the exact or an upper limit on the number of frames that have to be +/// rendered each callback, or 0 for a sensible default value. +/// * `audio_samplerate_hz` - the sample-rate for this audio stream, in Hz. +/// +/// # Return value +/// +/// This function returns `NULL` in case of error: if it couldn't bump the thread, or if the +/// `audio_samplerate_hz` is zero. It returns an opaque handle, to be passed to +/// `atp_demote_current_thread_from_real_time` to demote the thread. +/// +/// Additionaly, NULL can be returned in sandboxed processes on Linux, when DBUS cannot be used in +/// the process (for example because the socket to DBUS cannot be created). If this is the case, +/// it's necessary to get the information from the thread to promote and ask another process to +/// promote it (maybe via another privileged process). +#[no_mangle] +pub extern "C" fn atp_promote_current_thread_to_real_time( + audio_buffer_frames: u32, + audio_samplerate_hz: u32, +) -> *mut atp_handle { + match promote_current_thread_to_real_time(audio_buffer_frames, audio_samplerate_hz) { + Ok(handle) => Box::into_raw(Box::new(atp_handle(handle))), + _ => std::ptr::null_mut(), + } +} +/// Demotes the calling thread from real-time priority, with a C API. +/// +/// # Arguments +/// +/// * `atp_handle` - An opaque struct returned from a successful call to +/// `atp_promote_current_thread_to_real_time`. +/// +/// # Return value +/// +/// 0 in case of success, non-zero in case of error. +/// +/// # Safety +/// +/// Only to be used with a valid pointer from this library -- not after having released it via +/// atp_free_handle. +#[no_mangle] +pub unsafe extern "C" fn atp_demote_current_thread_from_real_time(handle: *mut atp_handle) -> i32 { + assert!(!handle.is_null()); + let handle = Box::from_raw(handle); + + match demote_current_thread_from_real_time(handle.0) { + Ok(_) => 0, + _ => 1, + } +} + +/// Frees a handle, with a C API. +/// +/// This is useful when it impractical to call `atp_demote_current_thread_from_real_time` on the +/// right thread. Access to the handle must be synchronized externaly, or the thread that was +/// promoted to real-time priority must have exited. +/// +/// # Arguments +/// +/// * `atp_handle` - An opaque struct returned from a successful call to +/// `atp_promote_current_thread_to_real_time`. +/// +/// # Return value +/// +/// 0 in case of success, non-zero in case of error. +/// +/// # Safety +/// +/// Should only be called to free something from this crate. +#[no_mangle] +pub unsafe extern "C" fn atp_free_handle(handle: *mut atp_handle) -> i32 { + if handle.is_null() { + return 1; + } + let _handle = Box::from_raw(handle); + 0 +} + +#[cfg(test)] +mod tests { + use super::*; + #[cfg(feature = "terminal-logging")] + use simple_logger; + #[test] + fn it_works() { + #[cfg(feature = "terminal-logging")] + simple_logger::init().unwrap(); + { + assert!(promote_current_thread_to_real_time(0, 0).is_err()); + } + { + match promote_current_thread_to_real_time(0, 44100) { + Ok(rt_prio_handle) => { + demote_current_thread_from_real_time(rt_prio_handle).unwrap(); + assert!(true); + } + Err(e) => { + eprintln!("{}", e); + assert!(false); + } + } + } + { + match promote_current_thread_to_real_time(512, 44100) { + Ok(rt_prio_handle) => { + demote_current_thread_from_real_time(rt_prio_handle).unwrap(); + assert!(true); + } + Err(e) => { + eprintln!("{}", e); + assert!(false); + } + } + } + { + match promote_current_thread_to_real_time(512, 44100) { + Ok(_) => { + assert!(true); + } + Err(e) => { + eprintln!("{}", e); + assert!(false); + } + } + // automatically deallocated, but not demoted until the thread exits. + } + } + cfg_if! { + if #[cfg(target_os = "linux")] { + use nix::unistd::*; + use nix::sys::signal::*; + + #[test] + fn test_linux_api() { + { + let info = get_current_thread_info().unwrap(); + match promote_thread_to_real_time(info, 512, 44100) { + Ok(_) => { + assert!(true); + } + Err(e) => { + eprintln!("{}", e); + assert!(false); + } + } + } + { + let info = get_current_thread_info().unwrap(); + let bytes = info.serialize(); + let info2 = RtPriorityThreadInfo::deserialize(bytes); + assert!(info == info2); + } + { + let info = get_current_thread_info().unwrap(); + let bytes = thread_info_serialize(info); + let info2 = thread_info_deserialize(bytes); + assert!(info == info2); + } + } + #[test] + fn test_remote_promotion() { + let (rd, wr) = pipe().unwrap(); + + match fork().expect("fork failed") { + ForkResult::Parent{ child } => { + eprintln!("Parent PID: {}", getpid()); + let mut bytes = [0_u8; std::mem::size_of::<RtPriorityThreadInfo>()]; + match read(rd, &mut bytes) { + Ok(_) => { + let info = RtPriorityThreadInfo::deserialize(bytes); + match promote_thread_to_real_time(info, 0, 44100) { + Ok(_) => { + eprintln!("thread promotion in the child from the parent succeeded"); + assert!(true); + } + Err(_) => { + eprintln!("promotion Err"); + kill(child, SIGKILL).expect("Could not kill the child?"); + assert!(false); + } + } + } + Err(e) => { + eprintln!("could not read from the pipe: {}", e); + } + } + kill(child, SIGKILL).expect("Could not kill the child?"); + } + ForkResult::Child => { + let r = set_real_time_hard_limit(0, 44100); + if r.is_err() { + eprintln!("Could not set RT limit, the test will fail."); + } + eprintln!("Child pid: {}", getpid()); + let info = get_current_thread_info().unwrap(); + let bytes = info.serialize(); + match write(wr, &bytes) { + Ok(_) => { + loop { + std::thread::sleep(std::time::Duration::from_millis(1000)); + eprintln!("child sleeping, waiting to be promoted..."); + } + } + Err(_) => { + eprintln!("write error on the pipe."); + } + } + } + } + } + } + } +} diff --git a/third_party/rust/audio_thread_priority/src/mach_sys.rs b/third_party/rust/audio_thread_priority/src/mach_sys.rs new file mode 100644 index 0000000000..f5798adf43 --- /dev/null +++ b/third_party/rust/audio_thread_priority/src/mach_sys.rs @@ -0,0 +1,36 @@ +/* automatically generated by rust-bindgen */ + +pub const THREAD_EXTENDED_POLICY: u32 = 1; +pub const THREAD_TIME_CONSTRAINT_POLICY: u32 = 2; +pub const THREAD_PRECEDENCE_POLICY: u32 = 3; +pub type __darwin_natural_t = ::std::os::raw::c_uint; +pub type __darwin_mach_port_name_t = __darwin_natural_t; +pub type __darwin_mach_port_t = __darwin_mach_port_name_t; +pub type boolean_t = ::std::os::raw::c_uint; +pub type natural_t = __darwin_natural_t; +pub type integer_t = ::std::os::raw::c_int; +pub type mach_port_t = __darwin_mach_port_t; +pub type thread_t = mach_port_t; +pub type thread_policy_flavor_t = natural_t; +pub type thread_policy_t = *mut integer_t; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct thread_extended_policy { + pub timeshare: boolean_t, +} +pub type thread_extended_policy_data_t = thread_extended_policy; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct thread_time_constraint_policy { + pub period: u32, + pub computation: u32, + pub constraint: u32, + pub preemptible: boolean_t, +} +pub type thread_time_constraint_policy_data_t = thread_time_constraint_policy; +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct thread_precedence_policy { + pub importance: integer_t, +} +pub type thread_precedence_policy_data_t = thread_precedence_policy; diff --git a/third_party/rust/audio_thread_priority/src/rt_linux.rs b/third_party/rust/audio_thread_priority/src/rt_linux.rs new file mode 100644 index 0000000000..e5c4f747d0 --- /dev/null +++ b/third_party/rust/audio_thread_priority/src/rt_linux.rs @@ -0,0 +1,311 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/* Widely copied from dbus-rs/dbus/examples/rtkit.rs */ + +extern crate dbus; +extern crate libc; + +use std::cmp; +use std::error::Error; +use std::io::Error as OSError; + +use dbus::{BusType, Connection, Message, MessageItem, Props}; + +use crate::AudioThreadPriorityError; + +const DBUS_SOCKET_TIMEOUT: i32 = 10_000; +const RT_PRIO_DEFAULT: u32 = 10; +// This is different from libc::pid_t, which is 32 bits, and is defined in sys/types.h. +#[allow(non_camel_case_types)] +type kernel_pid_t = libc::c_long; + +impl From<dbus::Error> for AudioThreadPriorityError { + fn from(error: dbus::Error) -> Self { + AudioThreadPriorityError::new(&format!( + "{}:{}", + error.name().unwrap_or("?"), + error.message().unwrap_or("?") + )) + } +} + +impl From<Box<dyn Error>> for AudioThreadPriorityError { + fn from(error: Box<dyn Error>) -> Self { + AudioThreadPriorityError::new(&error.to_string()) + } +} + +#[repr(C)] +#[derive(Clone, Copy)] +pub struct RtPriorityThreadInfoInternal { + /// System-wise thread id, use to promote the thread via dbus. + thread_id: kernel_pid_t, + /// Process-local thread id, used to restore scheduler characteristics. This information is not + /// useful in another process, but is useful tied to the `thread_id`, when back into the first + /// process. + pthread_id: libc::pthread_t, + /// The PID of the process containing `thread_id` below. + pid: libc::pid_t, + /// ... + policy: libc::c_int, +} + +impl RtPriorityThreadInfoInternal { + /// Serialize a RtPriorityThreadInfoInternal to a byte buffer. + pub fn serialize(&self) -> [u8; std::mem::size_of::<Self>()] { + unsafe { std::mem::transmute::<Self, [u8; std::mem::size_of::<Self>()]>(*self) } + } + /// Get an RtPriorityThreadInfoInternal from a byte buffer. + pub fn deserialize(bytes: [u8; std::mem::size_of::<Self>()]) -> Self { + unsafe { std::mem::transmute::<[u8; std::mem::size_of::<Self>()], Self>(bytes) } + } +} + +impl PartialEq for RtPriorityThreadInfoInternal { + fn eq(&self, other: &Self) -> bool { + self.thread_id == other.thread_id && self.pthread_id == other.pthread_id + } +} + +/*#[derive(Debug)]*/ +pub struct RtPriorityHandleInternal { + thread_info: RtPriorityThreadInfoInternal, +} + +fn item_as_i64(i: MessageItem) -> Result<i64, AudioThreadPriorityError> { + match i { + MessageItem::Int32(i) => Ok(i as i64), + MessageItem::Int64(i) => Ok(i), + _ => Err(AudioThreadPriorityError::new(&format!( + "Property is not integer ({:?})", + i + ))), + } +} + +fn rtkit_set_realtime(thread: u64, pid: u64, prio: u32) -> Result<(), Box<dyn Error>> { + let m = if unsafe { libc::getpid() as u64 } == pid { + let mut m = Message::new_method_call( + "org.freedesktop.RealtimeKit1", + "/org/freedesktop/RealtimeKit1", + "org.freedesktop.RealtimeKit1", + "MakeThreadRealtime", + )?; + m.append_items(&[thread.into(), prio.into()]); + m + } else { + let mut m = Message::new_method_call( + "org.freedesktop.RealtimeKit1", + "/org/freedesktop/RealtimeKit1", + "org.freedesktop.RealtimeKit1", + "MakeThreadRealtimeWithPID", + )?; + m.append_items(&[pid.into(), thread.into(), prio.into()]); + m + }; + let c = Connection::get_private(BusType::System)?; + c.send_with_reply_and_block(m, DBUS_SOCKET_TIMEOUT)?; + Ok(()) +} + +/// Returns the maximum priority, maximum real-time time slice, and the current real-time time +/// slice for this process. +fn get_limits() -> Result<(i64, u64, libc::rlimit64), AudioThreadPriorityError> { + let c = Connection::get_private(BusType::System)?; + + let p = Props::new( + &c, + "org.freedesktop.RealtimeKit1", + "/org/freedesktop/RealtimeKit1", + "org.freedesktop.RealtimeKit1", + DBUS_SOCKET_TIMEOUT, + ); + let mut current_limit = libc::rlimit64 { + rlim_cur: 0, + rlim_max: 0, + }; + + let max_prio = item_as_i64(p.get("MaxRealtimePriority")?)?; + if max_prio < 0 { + return Err(AudioThreadPriorityError::new( + "invalid negative MaxRealtimePriority", + )); + } + + let max_rttime = item_as_i64(p.get("RTTimeUSecMax")?)?; + if max_rttime < 0 { + return Err(AudioThreadPriorityError::new( + "invalid negative RTTimeUSecMax", + )); + } + + if unsafe { libc::getrlimit64(libc::RLIMIT_RTTIME, &mut current_limit) } < 0 { + return Err(AudioThreadPriorityError::new_with_inner( + "getrlimit64", + Box::new(OSError::last_os_error()), + )); + } + + Ok((max_prio, (max_rttime as u64), current_limit)) +} + +fn set_limits(request: u64, max: u64) -> Result<(), AudioThreadPriorityError> { + // Set a soft limit to the limit requested, to be able to handle going over the limit using + // SIGXCPU. Set the hard limit to the maxium slice to prevent getting SIGKILL. + let new_limit = libc::rlimit64 { + rlim_cur: request, + rlim_max: max, + }; + if unsafe { libc::setrlimit64(libc::RLIMIT_RTTIME, &new_limit) } < 0 { + return Err(AudioThreadPriorityError::new_with_inner( + "setrlimit64", + Box::new(OSError::last_os_error()), + )); + } + + Ok(()) +} + +pub fn promote_current_thread_to_real_time_internal( + audio_buffer_frames: u32, + audio_samplerate_hz: u32, +) -> Result<RtPriorityHandleInternal, AudioThreadPriorityError> { + let thread_info = get_current_thread_info_internal()?; + promote_thread_to_real_time_internal(thread_info, audio_buffer_frames, audio_samplerate_hz) +} + +pub fn demote_current_thread_from_real_time_internal( + rt_priority_handle: RtPriorityHandleInternal, +) -> Result<(), AudioThreadPriorityError> { + assert!(unsafe { libc::pthread_self() } == rt_priority_handle.thread_info.pthread_id); + + let param = unsafe { std::mem::zeroed::<libc::sched_param>() }; + + if unsafe { + libc::pthread_setschedparam( + rt_priority_handle.thread_info.pthread_id, + rt_priority_handle.thread_info.policy, + ¶m, + ) + } < 0 + { + return Err(AudioThreadPriorityError::new_with_inner( + "could not demote thread", + Box::new(OSError::last_os_error()), + )); + } + Ok(()) +} + +/// This can be called by sandboxed code, it only restores priority to what they were. +pub fn demote_thread_from_real_time_internal( + thread_info: RtPriorityThreadInfoInternal, +) -> Result<(), AudioThreadPriorityError> { + let param = unsafe { std::mem::zeroed::<libc::sched_param>() }; + + // https://github.com/rust-lang/libc/issues/1511 + const SCHED_RESET_ON_FORK: libc::c_int = 0x40000000; + + if unsafe { + libc::pthread_setschedparam( + thread_info.pthread_id, + libc::SCHED_OTHER | SCHED_RESET_ON_FORK, + ¶m, + ) + } < 0 + { + return Err(AudioThreadPriorityError::new_with_inner( + "could not demote thread", + Box::new(OSError::last_os_error()), + )); + } + Ok(()) +} + +/// Get the current thread information, as an opaque struct, that can be serialized and sent +/// accross processes. This is enough to capture the current state of the scheduling policy, and +/// an identifier to have another thread promoted to real-time. +pub fn get_current_thread_info_internal( +) -> Result<RtPriorityThreadInfoInternal, AudioThreadPriorityError> { + let thread_id = unsafe { libc::syscall(libc::SYS_gettid) }; + let pthread_id = unsafe { libc::pthread_self() }; + let mut param = unsafe { std::mem::zeroed::<libc::sched_param>() }; + let mut policy = 0; + + if unsafe { libc::pthread_getschedparam(pthread_id, &mut policy, &mut param) } < 0 { + return Err(AudioThreadPriorityError::new_with_inner( + "pthread_getschedparam", + Box::new(OSError::last_os_error()), + )); + } + + let pid = unsafe { libc::getpid() }; + + Ok(RtPriorityThreadInfoInternal { + pid, + thread_id, + pthread_id, + policy, + }) +} + +/// This set the RLIMIT_RTTIME resource to something other than "unlimited". It's necessary for the +/// rtkit request to succeed, and needs to hapen in the child. We can't get the real limit here, +/// because we don't have access to DBUS, so it is hardcoded to 200ms, which is the default in the +/// rtkit package. +pub fn set_real_time_hard_limit_internal( + audio_buffer_frames: u32, + audio_samplerate_hz: u32, +) -> Result<(), AudioThreadPriorityError> { + let buffer_frames = if audio_buffer_frames > 0 { + audio_buffer_frames + } else { + // 50ms slice. This "ought to be enough for anybody". + audio_samplerate_hz / 20 + }; + let budget_us = (buffer_frames * 1_000_000 / audio_samplerate_hz) as u64; + + // It's only necessary to set RLIMIT_RTTIME to something when in the child, skip it if it's a + // remoting call. + let (_, max_rttime, _) = get_limits()?; + + // Only take what we need, or cap at the system limit, no further. + let rttime_request = cmp::min(budget_us, max_rttime as u64); + set_limits(rttime_request, max_rttime)?; + + Ok(()) +} + +/// Promote a thread (possibly in another process) identified by its tid, to real-time. +pub fn promote_thread_to_real_time_internal( + thread_info: RtPriorityThreadInfoInternal, + audio_buffer_frames: u32, + audio_samplerate_hz: u32, +) -> Result<RtPriorityHandleInternal, AudioThreadPriorityError> { + let RtPriorityThreadInfoInternal { pid, thread_id, .. } = thread_info; + + let handle = RtPriorityHandleInternal { thread_info }; + + let (_, _, limits) = get_limits()?; + set_real_time_hard_limit_internal(audio_buffer_frames, audio_samplerate_hz)?; + + let r = rtkit_set_realtime(thread_id as u64, pid as u64, RT_PRIO_DEFAULT); + + match r { + Ok(_) => Ok(handle), + Err(e) => { + if unsafe { libc::setrlimit64(libc::RLIMIT_RTTIME, &limits) } < 0 { + return Err(AudioThreadPriorityError::new_with_inner( + "setrlimit64", + Box::new(OSError::last_os_error()), + )); + } + Err(AudioThreadPriorityError::new_with_inner( + "Thread promotion error", + e, + )) + } + } +} diff --git a/third_party/rust/audio_thread_priority/src/rt_mach.rs b/third_party/rust/audio_thread_priority/src/rt_mach.rs new file mode 100644 index 0000000000..7727b78f79 --- /dev/null +++ b/third_party/rust/audio_thread_priority/src/rt_mach.rs @@ -0,0 +1,162 @@ +use crate::mach_sys::*; +use crate::AudioThreadPriorityError; +use libc::{pthread_self, pthread_t}; +use log::info; +use mach::kern_return::{kern_return_t, KERN_SUCCESS}; +use mach::mach_time::{mach_timebase_info, mach_timebase_info_data_t}; +use mach::message::mach_msg_type_number_t; +use mach::port::mach_port_t; +use std::mem::size_of; + +extern "C" { + fn pthread_mach_thread_np(tid: pthread_t) -> mach_port_t; + // Those functions are commented out in thread_policy.h but somehow it works just fine !? + fn thread_policy_set( + thread: thread_t, + flavor: thread_policy_flavor_t, + policy_info: thread_policy_t, + count: mach_msg_type_number_t, + ) -> kern_return_t; + fn thread_policy_get( + thread: thread_t, + flavor: thread_policy_flavor_t, + policy_info: thread_policy_t, + count: &mut mach_msg_type_number_t, + get_default: &mut boolean_t, + ) -> kern_return_t; +} + +// can't use size_of in const fn just now in stable, use a macro for now. +macro_rules! THREAD_TIME_CONSTRAINT_POLICY_COUNT { + () => { + (size_of::<thread_time_constraint_policy_data_t>() / size_of::<integer_t>()) as u32 + }; +} + +#[derive(Debug)] +pub struct RtPriorityHandleInternal { + tid: mach_port_t, + previous_time_constraint_policy: thread_time_constraint_policy_data_t, +} + +impl Default for RtPriorityHandleInternal { + fn default() -> Self { + Self::new() + } +} + +impl RtPriorityHandleInternal { + pub fn new() -> RtPriorityHandleInternal { + RtPriorityHandleInternal { + tid: 0, + previous_time_constraint_policy: thread_time_constraint_policy_data_t { + period: 0, + computation: 0, + constraint: 0, + preemptible: 0, + }, + } + } +} + +pub fn demote_current_thread_from_real_time_internal( + rt_priority_handle: RtPriorityHandleInternal, +) -> Result<(), AudioThreadPriorityError> { + unsafe { + let mut h = rt_priority_handle; + let rv: kern_return_t = thread_policy_set( + h.tid, + THREAD_TIME_CONSTRAINT_POLICY, + (&mut h.previous_time_constraint_policy) as *mut _ as thread_policy_t, + THREAD_TIME_CONSTRAINT_POLICY_COUNT!(), + ); + if rv != KERN_SUCCESS as i32 { + return Err(AudioThreadPriorityError::new( + "thread demotion error: thread_policy_get: RT", + )); + } + + info!("thread {} priority restored.", h.tid); + } + + Ok(()) +} + +pub fn promote_current_thread_to_real_time_internal( + audio_buffer_frames: u32, + audio_samplerate_hz: u32, +) -> Result<RtPriorityHandleInternal, AudioThreadPriorityError> { + let mut rt_priority_handle = RtPriorityHandleInternal::new(); + + let buffer_frames = if audio_buffer_frames > 0 { + audio_buffer_frames + } else { + audio_samplerate_hz / 20 + }; + + unsafe { + let tid: mach_port_t = pthread_mach_thread_np(pthread_self()); + let mut time_constraints = thread_time_constraint_policy_data_t { + period: 0, + computation: 0, + constraint: 0, + preemptible: 0, + }; + + // Get current thread attributes, to revert back to the correct setting later if needed. + rt_priority_handle.tid = tid; + + // false: we want to get the current value, not the default value. If this is `false` after + // returning, it means there are no current settings because of other factor, and the + // default was returned instead. + let mut get_default: boolean_t = 0; + let mut count: mach_msg_type_number_t = THREAD_TIME_CONSTRAINT_POLICY_COUNT!(); + let mut rv: kern_return_t = thread_policy_get( + tid, + THREAD_TIME_CONSTRAINT_POLICY, + (&mut time_constraints) as *mut _ as thread_policy_t, + &mut count, + &mut get_default, + ); + + if rv != KERN_SUCCESS as i32 { + return Err(AudioThreadPriorityError::new( + "thread promotion error: thread_policy_get: time_constraint", + )); + } + + rt_priority_handle.previous_time_constraint_policy = time_constraints; + + let cb_duration = buffer_frames as f32 / (audio_samplerate_hz as f32) * 1000.; + // The multiplicators are somwhat arbitrary for now. + + let mut timebase_info = mach_timebase_info_data_t { denom: 0, numer: 0 }; + mach_timebase_info(&mut timebase_info); + + let ms2abs: f32 = ((timebase_info.denom as f32) / timebase_info.numer as f32) * 1000000.; + + // Computation time is half of constraint, per macOS 12 behaviour. + time_constraints = thread_time_constraint_policy_data_t { + period: (cb_duration * ms2abs) as u32, + computation: (cb_duration / 2.0 * ms2abs) as u32, + constraint: (cb_duration * ms2abs) as u32, + preemptible: 1, // true + }; + + rv = thread_policy_set( + tid, + THREAD_TIME_CONSTRAINT_POLICY, + (&mut time_constraints) as *mut _ as thread_policy_t, + THREAD_TIME_CONSTRAINT_POLICY_COUNT!(), + ); + if rv != KERN_SUCCESS as i32 { + return Err(AudioThreadPriorityError::new( + "thread promotion error: thread_policy_set: time_constraint", + )); + } + + info!("thread {} bumped to real time priority.", tid); + } + + Ok(rt_priority_handle) +} diff --git a/third_party/rust/audio_thread_priority/src/rt_win.rs b/third_party/rust/audio_thread_priority/src/rt_win.rs new file mode 100644 index 0000000000..f8f441c3ce --- /dev/null +++ b/third_party/rust/audio_thread_priority/src/rt_win.rs @@ -0,0 +1,99 @@ +#[cfg(feature = "windows")] +mod os { + pub use windows::Win32::Foundation::GetLastError; + pub use windows::Win32::Foundation::HANDLE; + pub use windows::Win32::Foundation::PSTR; + pub use windows::Win32::System::Threading::{ + AvRevertMmThreadCharacteristics, AvSetMmThreadCharacteristicsA, + }; + + pub fn ok(rv: windows::Win32::Foundation::BOOL) -> bool { + rv.as_bool() + } + + pub fn invalid_handle(handle: HANDLE) -> bool { + handle.is_invalid() + } +} +#[cfg(feature = "winapi")] +mod os { + pub use winapi::shared::ntdef::HANDLE; + pub use winapi::um::avrt::{AvRevertMmThreadCharacteristics, AvSetMmThreadCharacteristicsA}; + pub use winapi::um::errhandlingapi::GetLastError; + + pub fn ok(rv: winapi::shared::minwindef::BOOL) -> bool { + rv != 0 + } + + #[allow(non_snake_case)] + pub fn PSTR(ptr: *const u8) -> *const i8 { + ptr as _ + } + + pub fn invalid_handle(handle: HANDLE) -> bool { + handle.is_null() + } +} + +use crate::AudioThreadPriorityError; + +use log::info; + +#[derive(Debug)] +pub struct RtPriorityHandleInternal { + mmcss_task_index: u32, + task_handle: os::HANDLE, +} + +impl RtPriorityHandleInternal { + pub fn new(mmcss_task_index: u32, task_handle: os::HANDLE) -> RtPriorityHandleInternal { + RtPriorityHandleInternal { + mmcss_task_index, + task_handle, + } + } +} + +pub fn demote_current_thread_from_real_time_internal( + rt_priority_handle: RtPriorityHandleInternal, +) -> Result<(), AudioThreadPriorityError> { + let rv = unsafe { os::AvRevertMmThreadCharacteristics(rt_priority_handle.task_handle) }; + if !os::ok(rv) { + return Err(AudioThreadPriorityError::new(&format!( + "Unable to restore the thread priority ({:?})", + unsafe { os::GetLastError() } + ))); + } + + info!( + "task {} priority restored.", + rt_priority_handle.mmcss_task_index + ); + + Ok(()) +} + +pub fn promote_current_thread_to_real_time_internal( + _audio_buffer_frames: u32, + _audio_samplerate_hz: u32, +) -> Result<RtPriorityHandleInternal, AudioThreadPriorityError> { + let mut task_index = 0u32; + + let handle = + unsafe { os::AvSetMmThreadCharacteristicsA(os::PSTR("Audio\0".as_ptr()), &mut task_index) }; + let handle = RtPriorityHandleInternal::new(task_index, handle); + + if os::invalid_handle(handle.task_handle) { + return Err(AudioThreadPriorityError::new(&format!( + "Unable to restore the thread priority ({:?})", + unsafe { os::GetLastError() } + ))); + } + + info!( + "task {} bumped to real time priority.", + handle.mmcss_task_index + ); + + Ok(handle) +} |