From 50c838f40a36298c7961cb0eebea8f8c11ff57f4 Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Thu, 29 May 2025 13:43:39 -0700 Subject: [PATCH 01/26] start adding wrappers for sched_set/get methods --- src/sched.rs | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/src/sched.rs b/src/sched.rs index 617d00493f..33f4c99581 100644 --- a/src/sched.rs +++ b/src/sched.rs @@ -316,6 +316,91 @@ mod sched_affinity { } } +#[cfg(linux_android)] +pub use self::sched_priority::*; + +#[cfg(linux_android)] +mod sched_priority { + use crate::errno::Errno; + use crate::unistd::Pid; + use crate::Result; + use libc::{self, c_int}; + + #[repr(C)] + pub struct SchedParam { + pub sched_priority: c_int, + } + impl From for libc::sched_param { + fn from(param: SchedParam) -> Self { + libc::sched_param { + sched_priority: param.sched_priority, + } + } + } + impl From for SchedParam { + fn from(param: libc::sched_param) -> Self { + SchedParam { + sched_priority: param.sched_priority, + } + } + } + + libc_enum! { + pub enum Scheduler { + SCHED_OTHER, + SCHED_FIFO, + SCHED_RR, + SCHED_BATCH, + SCHED_IDLE, + SCHED_DEADLINE, + } + impl TryFrom + } + + pub fn sched_get_priority_max(sched: Scheduler) -> Result { + let res = unsafe { libc::sched_get_priority_max(sched as c_int) }; + Errno::result(res).map(|int| int as c_int) + } + + pub fn sched_get_priority_min(sched: Scheduler) -> Result { + let res = unsafe { libc::sched_get_priority_min(sched as c_int) }; + Errno::result(res).map(|int| int as c_int) + } + + pub fn sched_getscheduler(pid: Pid) -> Result { + let res = unsafe { libc::sched_getscheduler(pid.into()) }; + + Errno::result(res).and_then(|sched| Scheduler::try_from(sched)) + } + + pub fn sched_setscheduler( + pid: Pid, + sched: Scheduler, + param: SchedParam, + ) -> Result<()> { + let param: libc::sched_param = param.into(); + let res = unsafe { + libc::sched_setscheduler(pid.into(), sched as c_int, ¶m) + }; + + Errno::result(res).map(drop) + } + + pub fn sched_getparam(pid: Pid) -> Result { + let mut param = libc::sched_param { sched_priority: 0 }; + let res = unsafe { libc::sched_getparam(pid.into(), &mut param) }; + + Errno::result(res).map(|_| param.into()) + } + + pub fn sched_setparam(pid: Pid, param: SchedParam) -> Result<()> { + let param: libc::sched_param = param.into(); + let res = unsafe { libc::sched_setparam(pid.into(), ¶m) }; + + Errno::result(res).map(drop) + } +} + /// Explicitly yield the processor to other threads. /// /// [Further reading](https://pubs.opengroup.org/onlinepubs/9699919799/functions/sched_yield.html) From 9ab5fa6217a9bdc71163572fe9d98c5a4b2667a0 Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Thu, 29 May 2025 13:48:08 -0700 Subject: [PATCH 02/26] from_priority --- src/sched.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/sched.rs b/src/sched.rs index 33f4c99581..c1c0c690a8 100644 --- a/src/sched.rs +++ b/src/sched.rs @@ -330,6 +330,15 @@ mod sched_priority { pub struct SchedParam { pub sched_priority: c_int, } + + impl SchedParam { + pub fn from_priority(priority: c_int) -> Self { + SchedParam { + sched_priority: priority, + } + } + } + impl From for libc::sched_param { fn from(param: SchedParam) -> Self { libc::sched_param { From 96d7fabb59942702e6206b3bd8f8239ad579fa83 Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Thu, 29 May 2025 16:24:44 -0700 Subject: [PATCH 03/26] add tests for sched priority --- test/test_sched.rs | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/test/test_sched.rs b/test/test_sched.rs index c52616b8bb..3a63d2e7eb 100644 --- a/test/test_sched.rs +++ b/test/test_sched.rs @@ -1,4 +1,7 @@ -use nix::sched::{sched_getaffinity, sched_getcpu, sched_setaffinity, CpuSet}; +use nix::sched::{ + sched_getaffinity, sched_getcpu, sched_getparam, sched_setaffinity, + sched_setscheduler, CpuSet, SchedParam, +}; use nix::unistd::Pid; #[test] @@ -37,3 +40,39 @@ fn test_sched_affinity() { // Finally, reset the initial CPU set sched_setaffinity(Pid::from_raw(0), &initial_affinity).unwrap(); } + +#[test] +fn test_sched_priority() { + let pid = Pid::from_raw(0); + let sched = sched_getscheduler(pid).unwrap(); + // default is NORMAL aka OTHER + assert_eq!(sched, Scheduler::SCHED_OTHER); + + let priority = sched_getparam(pid).unwrap().sched_priority; + assert_eq!(priority, 0); + + let max = sched_get_priority_max(Scheduler::SCHED_FIFO).unwrap(); + let min = sched_get_priority_min(Scheduler::SCHED_FIFO).unwrap(); + + // can't set priority unless process has correct capabilities and PREEMPT_RT kernel + match sched_setscheduler( + pid, + Scheduler::SCHED_FIFO, + SchedParam::from_priority(max), + ) { + Ok(_) => { + assert_eq!(sched_getscheduler(pid).unwrap(), Scheduler::SCHED_FIFO); + assert_eq!(sched_getparam(pid).unwrap().sched_priority, max); + } + Err(nix::Error::Sys(nix::Error::EPERM)) => { + // expected, assert that it didn't change + assert_eq!( + sched_getscheduler(pid).unwrap(), + Scheduler::SCHED_OTHER + ); + } + Err(e) => { + panic!("unexpected error: {}", e); + } + } +} From bec28bde2b9dddf52735ecf0052fa29fbebb2be8 Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Thu, 29 May 2025 16:35:01 -0700 Subject: [PATCH 04/26] switch to maybe uninit --- src/sched.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/sched.rs b/src/sched.rs index c1c0c690a8..5710ae623b 100644 --- a/src/sched.rs +++ b/src/sched.rs @@ -321,6 +321,8 @@ pub use self::sched_priority::*; #[cfg(linux_android)] mod sched_priority { + use std::mem::MaybeUninit; + use crate::errno::Errno; use crate::unistd::Pid; use crate::Result; @@ -396,10 +398,11 @@ mod sched_priority { } pub fn sched_getparam(pid: Pid) -> Result { - let mut param = libc::sched_param { sched_priority: 0 }; - let res = unsafe { libc::sched_getparam(pid.into(), &mut param) }; + let mut param: MaybeUninit = MaybeUninit::uninit(); + let res = + unsafe { libc::sched_getparam(pid.into(), param.as_mut_ptr()) }; - Errno::result(res).map(|_| param.into()) + Errno::result(res).map(|_| unsafe { param.assume_init() }.into()) } pub fn sched_setparam(pid: Pid, param: SchedParam) -> Result<()> { From e14de14ca66b80779c51a880be6c77cb7079afec Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Thu, 29 May 2025 16:47:44 -0700 Subject: [PATCH 05/26] fix compile --- src/sched.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/sched.rs b/src/sched.rs index 5710ae623b..6ef135f30b 100644 --- a/src/sched.rs +++ b/src/sched.rs @@ -326,15 +326,16 @@ mod sched_priority { use crate::errno::Errno; use crate::unistd::Pid; use crate::Result; - use libc::{self, c_int}; + use libc; #[repr(C)] + #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct SchedParam { - pub sched_priority: c_int, + pub sched_priority: libc::c_int, } impl SchedParam { - pub fn from_priority(priority: c_int) -> Self { + pub fn from_priority(priority: libc::c_int) -> Self { SchedParam { sched_priority: priority, } @@ -357,6 +358,7 @@ mod sched_priority { } libc_enum! { + #[repr(i32)] pub enum Scheduler { SCHED_OTHER, SCHED_FIFO, @@ -365,17 +367,17 @@ mod sched_priority { SCHED_IDLE, SCHED_DEADLINE, } - impl TryFrom + impl TryFrom } - pub fn sched_get_priority_max(sched: Scheduler) -> Result { - let res = unsafe { libc::sched_get_priority_max(sched as c_int) }; - Errno::result(res).map(|int| int as c_int) + pub fn sched_get_priority_max(sched: Scheduler) -> Result { + let res = unsafe { libc::sched_get_priority_max(sched as libc::c_int) }; + Errno::result(res).map(|int| int as libc::c_int) } - pub fn sched_get_priority_min(sched: Scheduler) -> Result { - let res = unsafe { libc::sched_get_priority_min(sched as c_int) }; - Errno::result(res).map(|int| int as c_int) + pub fn sched_get_priority_min(sched: Scheduler) -> Result { + let res = unsafe { libc::sched_get_priority_min(sched as libc::c_int) }; + Errno::result(res).map(|int| int as libc::c_int) } pub fn sched_getscheduler(pid: Pid) -> Result { @@ -391,7 +393,7 @@ mod sched_priority { ) -> Result<()> { let param: libc::sched_param = param.into(); let res = unsafe { - libc::sched_setscheduler(pid.into(), sched as c_int, ¶m) + libc::sched_setscheduler(pid.into(), sched as libc::c_int, ¶m) }; Errno::result(res).map(drop) From 55c1d7fe8326f38c189b2a58a68cf7cf71020fe4 Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Thu, 29 May 2025 17:21:37 -0700 Subject: [PATCH 06/26] start adding docs + changelog --- changelog/2640.added.md | 1 + src/sched.rs | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 changelog/2640.added.md diff --git a/changelog/2640.added.md b/changelog/2640.added.md new file mode 100644 index 0000000000..0c2c3957ec --- /dev/null +++ b/changelog/2640.added.md @@ -0,0 +1 @@ +Added new api methods for working with PREEMPT_RT thread priorities. \ No newline at end of file diff --git a/src/sched.rs b/src/sched.rs index 6ef135f30b..7e4176f0b7 100644 --- a/src/sched.rs +++ b/src/sched.rs @@ -359,33 +359,57 @@ mod sched_priority { libc_enum! { #[repr(i32)] + /// The type of scheduler for use with [`sched_getscheduler`] and [`sched_setscheduler`]. + /// See [man_sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html) for more details + /// on the differences in behavior. pub enum Scheduler { + /// The default scheduler on non-realtime linux - also known as SCHED_NORMAL. SCHED_OTHER, + /// The realtime FIFO scheduler. All FIFO threads have priority higher than 0 and + /// preempt SCHED_OTHER threads. Threads are executed in priority order, using + /// first-in-first-out lists to handle two threads with the same priority. SCHED_FIFO, + /// Round-robin scheduler SCHED_RR, + /// Batch scheduler, similar to SCHED_OTHER but assumes the thread is CPU intensive. + /// The kernel applies a mild penalty to switching to this thread. + /// As of Linux 2.6.16, the only valid priority is 0. SCHED_BATCH, + /// The idle scheduler only executes the thread when there are idle CPUs. SCHED_IDLE + /// threads have no progress guarantees. SCHED_IDLE, + /// Deadline scheduler, attempting to provide guaranteed latency for requests. + /// See the [linux kernel docs](https://docs.kernel.org/scheduler/sched-deadline.html) + /// for details. SCHED_DEADLINE, } impl TryFrom } + /// Get the highest priority value for a given scheduler. pub fn sched_get_priority_max(sched: Scheduler) -> Result { let res = unsafe { libc::sched_get_priority_max(sched as libc::c_int) }; Errno::result(res).map(|int| int as libc::c_int) } + /// Get the lowest priority value for a given scheduler. pub fn sched_get_priority_min(sched: Scheduler) -> Result { let res = unsafe { libc::sched_get_priority_min(sched as libc::c_int) }; Errno::result(res).map(|int| int as libc::c_int) } + /// Get the current scheduler in use for a given process or thread. + /// Using `Pid::from_raw(0)` will fetch the scheduler for the current thread. pub fn sched_getscheduler(pid: Pid) -> Result { let res = unsafe { libc::sched_getscheduler(pid.into()) }; Errno::result(res).and_then(|sched| Scheduler::try_from(sched)) } + /// Set the scheduler for a given process or thread. + /// Using `Pid::from_raw(0)` will set the scheduler for the current thread. + /// + /// pub fn sched_setscheduler( pid: Pid, sched: Scheduler, From f68a980641c877e12600dee058d6500e7b942c60 Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Thu, 29 May 2025 17:41:46 -0700 Subject: [PATCH 07/26] docs --- src/sched.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/sched.rs b/src/sched.rs index 7e4176f0b7..94794d1de8 100644 --- a/src/sched.rs +++ b/src/sched.rs @@ -330,11 +330,15 @@ mod sched_priority { #[repr(C)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] + /// Schedule parameters for a thread (currently only priority is supported). pub struct SchedParam { + /// Priority of the current schedule. pub sched_priority: libc::c_int, } impl SchedParam { + /// Create schedule parameters with a given priority. Priority must be between + /// min and max for the chosen schedule type. pub fn from_priority(priority: libc::c_int) -> Self { SchedParam { sched_priority: priority, @@ -399,17 +403,22 @@ mod sched_priority { } /// Get the current scheduler in use for a given process or thread. - /// Using `Pid::from_raw(0)` will fetch the scheduler for the current thread. + /// Using `Pid::from_raw(0)` will fetch the scheduler for the calling thread. pub fn sched_getscheduler(pid: Pid) -> Result { let res = unsafe { libc::sched_getscheduler(pid.into()) }; Errno::result(res).and_then(|sched| Scheduler::try_from(sched)) } - /// Set the scheduler for a given process or thread. - /// Using `Pid::from_raw(0)` will set the scheduler for the current thread. - /// - /// + /// Set the scheduler and parameters for a given process or thread. + /// Using `Pid::from_raw(0)` will set the scheduler for the calling thread. + /// + /// SCHED_OTHER, SCHED_IDLE and SCHED_BATCH only support a priority of `0`, and can be used + /// outside a Linux PREEMPT_RT context. + /// + /// SCHED_FIFO and SCHED_RR allow priorities between the min and max inclusive. + /// + /// SCHED_DEADLINE cannot be set with this function, libc::sched_setattr must be used instead. pub fn sched_setscheduler( pid: Pid, sched: Scheduler, @@ -423,6 +432,8 @@ mod sched_priority { Errno::result(res).map(drop) } + /// Get the schedule parameters (currently only priority) for a given thread. + /// Using `Pid::from_raw(0)` will return the parameters for the calling thread. pub fn sched_getparam(pid: Pid) -> Result { let mut param: MaybeUninit = MaybeUninit::uninit(); let res = @@ -431,6 +442,11 @@ mod sched_priority { Errno::result(res).map(|_| unsafe { param.assume_init() }.into()) } + /// Set the schedule parameters (currently only priority) for a given thread. + /// Using `Pid::from_raw(0)` will return the parameters for the calling thread. + /// + /// Changing the priority to something other than `0` requires using a SCHED_FIFO or SCHED_RR + /// and using a Linux kernel with PREEMPT_RT enabled. pub fn sched_setparam(pid: Pid, param: SchedParam) -> Result<()> { let param: libc::sched_param = param.into(); let res = unsafe { libc::sched_setparam(pid.into(), ¶m) }; From 79b061a12f6088794cef64f13265752bdf1356c7 Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Thu, 29 May 2025 17:44:18 -0700 Subject: [PATCH 08/26] fix test imports --- test/test_sched.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/test_sched.rs b/test/test_sched.rs index 3a63d2e7eb..8d5528d7d3 100644 --- a/test/test_sched.rs +++ b/test/test_sched.rs @@ -1,7 +1,5 @@ -use nix::sched::{ - sched_getaffinity, sched_getcpu, sched_getparam, sched_setaffinity, - sched_setscheduler, CpuSet, SchedParam, -}; +use nix::errno::Errno; +use nix::sched::{sched_get_priority_max, sched_get_priority_min, sched_getaffinity, sched_getcpu, sched_getparam, sched_getscheduler, sched_setaffinity, sched_setscheduler, CpuSet, SchedParam, Scheduler}; use nix::unistd::Pid; #[test] @@ -64,7 +62,7 @@ fn test_sched_priority() { assert_eq!(sched_getscheduler(pid).unwrap(), Scheduler::SCHED_FIFO); assert_eq!(sched_getparam(pid).unwrap().sched_priority, max); } - Err(nix::Error::Sys(nix::Error::EPERM)) => { + Err(Errno::EPERM) => { // expected, assert that it didn't change assert_eq!( sched_getscheduler(pid).unwrap(), From b97ea0c33f67c4a0fa8c8a4f5c9b786b7572cdc3 Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Thu, 29 May 2025 17:46:03 -0700 Subject: [PATCH 09/26] autocommit --- test/test_sched.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_sched.rs b/test/test_sched.rs index 8d5528d7d3..cb830aca7d 100644 --- a/test/test_sched.rs +++ b/test/test_sched.rs @@ -50,7 +50,7 @@ fn test_sched_priority() { assert_eq!(priority, 0); let max = sched_get_priority_max(Scheduler::SCHED_FIFO).unwrap(); - let min = sched_get_priority_min(Scheduler::SCHED_FIFO).unwrap(); + let _ = sched_get_priority_min(Scheduler::SCHED_FIFO).unwrap(); // can't set priority unless process has correct capabilities and PREEMPT_RT kernel match sched_setscheduler( From 8ee150938c9e03a04994b83f2b5d918c6b281535 Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Thu, 29 May 2025 17:49:49 -0700 Subject: [PATCH 10/26] fmt? --- src/sched.rs | 2 +- test/test_sched.rs | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/sched.rs b/src/sched.rs index 94794d1de8..09a318cf05 100644 --- a/src/sched.rs +++ b/src/sched.rs @@ -407,7 +407,7 @@ mod sched_priority { pub fn sched_getscheduler(pid: Pid) -> Result { let res = unsafe { libc::sched_getscheduler(pid.into()) }; - Errno::result(res).and_then(|sched| Scheduler::try_from(sched)) + Errno::result(res).and_then(Scheduler::try_from) } /// Set the scheduler and parameters for a given process or thread. diff --git a/test/test_sched.rs b/test/test_sched.rs index cb830aca7d..ddef7fb39a 100644 --- a/test/test_sched.rs +++ b/test/test_sched.rs @@ -1,5 +1,9 @@ use nix::errno::Errno; -use nix::sched::{sched_get_priority_max, sched_get_priority_min, sched_getaffinity, sched_getcpu, sched_getparam, sched_getscheduler, sched_setaffinity, sched_setscheduler, CpuSet, SchedParam, Scheduler}; +use nix::sched::{ + sched_get_priority_max, sched_get_priority_min, sched_getaffinity, + sched_getcpu, sched_getparam, sched_getscheduler, sched_setaffinity, + sched_setscheduler, CpuSet, SchedParam, Scheduler, +}; use nix::unistd::Pid; #[test] From f6368e5b6a92eb0085c07c77ad6ab6b4eceb512c Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Thu, 29 May 2025 17:53:05 -0700 Subject: [PATCH 11/26] fix musl? --- src/sched.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/sched.rs b/src/sched.rs index 09a318cf05..368a1bb913 100644 --- a/src/sched.rs +++ b/src/sched.rs @@ -316,9 +316,11 @@ mod sched_affinity { } } -#[cfg(linux_android)] +// musl has additional sched_param fields that we don't support yet +#[cfg(all(linux_android, not(target_env = "musl")))] pub use self::sched_priority::*; +#[cfg(all(linux_android, not(target_env = "musl")))] #[cfg(linux_android)] mod sched_priority { use std::mem::MaybeUninit; From 4cd4fef1659bc6a6bbb3ef71112749c29ac83dc1 Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Thu, 29 May 2025 17:54:05 -0700 Subject: [PATCH 12/26] disable test on musl --- test/test_sched.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test_sched.rs b/test/test_sched.rs index ddef7fb39a..61599e1c64 100644 --- a/test/test_sched.rs +++ b/test/test_sched.rs @@ -43,6 +43,7 @@ fn test_sched_affinity() { sched_setaffinity(Pid::from_raw(0), &initial_affinity).unwrap(); } +#[cfg(not(target_env = "musl"))] #[test] fn test_sched_priority() { let pid = Pid::from_raw(0); From df112d8efdf0621fbe3e16085b4f59cf532f08be Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Thu, 29 May 2025 17:57:41 -0700 Subject: [PATCH 13/26] autocommit --- test/test_sched.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/test_sched.rs b/test/test_sched.rs index 61599e1c64..c27a67449b 100644 --- a/test/test_sched.rs +++ b/test/test_sched.rs @@ -1,9 +1,10 @@ use nix::errno::Errno; +#[cfg(not(target_env = "musl"))] use nix::sched::{ - sched_get_priority_max, sched_get_priority_min, sched_getaffinity, - sched_getcpu, sched_getparam, sched_getscheduler, sched_setaffinity, - sched_setscheduler, CpuSet, SchedParam, Scheduler, + sched_get_priority_max, sched_get_priority_min, sched_getparam, + sched_getscheduler, sched_setscheduler, SchedParam, Scheduler, }; +use nix::sched::{sched_getaffinity, sched_getcpu, sched_setaffinity, CpuSet}; use nix::unistd::Pid; #[test] From b01b93853c81c890ab4aa015fa7a0d66c9fb08af Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Thu, 29 May 2025 18:01:47 -0700 Subject: [PATCH 14/26] autocommit --- test/test_sched.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/test_sched.rs b/test/test_sched.rs index c27a67449b..a9cf6f3862 100644 --- a/test/test_sched.rs +++ b/test/test_sched.rs @@ -1,4 +1,3 @@ -use nix::errno::Errno; #[cfg(not(target_env = "musl"))] use nix::sched::{ sched_get_priority_max, sched_get_priority_min, sched_getparam, @@ -68,7 +67,7 @@ fn test_sched_priority() { assert_eq!(sched_getscheduler(pid).unwrap(), Scheduler::SCHED_FIFO); assert_eq!(sched_getparam(pid).unwrap().sched_priority, max); } - Err(Errno::EPERM) => { + Err(nix::errno::Errno::EPERM) => { // expected, assert that it didn't change assert_eq!( sched_getscheduler(pid).unwrap(), From aa916e441de5a61018e4418360a77dc39fad5a19 Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Fri, 30 May 2025 11:14:51 -0700 Subject: [PATCH 15/26] exclude ohos too --- src/sched.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sched.rs b/src/sched.rs index 368a1bb913..d44ff06a8a 100644 --- a/src/sched.rs +++ b/src/sched.rs @@ -317,11 +317,10 @@ mod sched_affinity { } // musl has additional sched_param fields that we don't support yet -#[cfg(all(linux_android, not(target_env = "musl")))] +#[cfg(all(linux_android, not(target_env = "musl"), not(target_env = "ohos")))] pub use self::sched_priority::*; -#[cfg(all(linux_android, not(target_env = "musl")))] -#[cfg(linux_android)] +#[cfg(all(linux_android, not(target_env = "musl"), not(target_env = "ohos")))] mod sched_priority { use std::mem::MaybeUninit; @@ -333,6 +332,7 @@ mod sched_priority { #[repr(C)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] /// Schedule parameters for a thread (currently only priority is supported). + /// This is a wrapper around `libc::sched_param` pub struct SchedParam { /// Priority of the current schedule. pub sched_priority: libc::c_int, From 5b6c2242503ad66761536aca9932d0c146141409 Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Fri, 30 May 2025 11:15:37 -0700 Subject: [PATCH 16/26] exclude ohos from test --- test/test_sched.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_sched.rs b/test/test_sched.rs index a9cf6f3862..8a95cd8b4a 100644 --- a/test/test_sched.rs +++ b/test/test_sched.rs @@ -43,7 +43,7 @@ fn test_sched_affinity() { sched_setaffinity(Pid::from_raw(0), &initial_affinity).unwrap(); } -#[cfg(not(target_env = "musl"))] +#[cfg(any(not(target_env = "musl"), not(target_env = "ohos")))] #[test] fn test_sched_priority() { let pid = Pid::from_raw(0); From 101538ce424099cfae354d67c5414c5c77905bbf Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Fri, 30 May 2025 12:22:31 -0700 Subject: [PATCH 17/26] autocommit --- src/sched.rs | 17 +++++++++++++++++ test/test_sched.rs | 12 ++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/sched.rs b/src/sched.rs index d44ff06a8a..70ae65ee96 100644 --- a/src/sched.rs +++ b/src/sched.rs @@ -354,6 +354,23 @@ mod sched_priority { sched_priority: param.sched_priority, } } + + // #[cfg(any(target_env = "musl", target_env = "ohos"))] + // fn from(param: SchedParam) -> Self { + // // note: non-priority values are dummy values used by deadline scheduler, + // // don't use this abstraction if you're using deadline. + // let zero_ts = libc::timespec { + // tv_sec: 0, + // tv_nsec: 0, + // }; + // libc::sched_param { + // sched_priority: param.sched_priority, + // sched_ss_low_priority: 0, + // sched_ss_repl_period: zero_ts.clone(), + // sched_ss_init_budget: zero_ts, + // sched_ss_max_repl: 0, + // } + // } } impl From for SchedParam { fn from(param: libc::sched_param) -> Self { diff --git a/test/test_sched.rs b/test/test_sched.rs index 8a95cd8b4a..012bfcefa4 100644 --- a/test/test_sched.rs +++ b/test/test_sched.rs @@ -1,8 +1,3 @@ -#[cfg(not(target_env = "musl"))] -use nix::sched::{ - sched_get_priority_max, sched_get_priority_min, sched_getparam, - sched_getscheduler, sched_setscheduler, SchedParam, Scheduler, -}; use nix::sched::{sched_getaffinity, sched_getcpu, sched_setaffinity, CpuSet}; use nix::unistd::Pid; @@ -43,9 +38,14 @@ fn test_sched_affinity() { sched_setaffinity(Pid::from_raw(0), &initial_affinity).unwrap(); } -#[cfg(any(not(target_env = "musl"), not(target_env = "ohos")))] +#[cfg(not(any(target_env = "musl", target_env = "ohos")))] #[test] fn test_sched_priority() { + use nix::sched::{ + sched_get_priority_max, sched_get_priority_min, sched_getparam, + sched_getscheduler, sched_setscheduler, SchedParam, Scheduler, + }; + let pid = Pid::from_raw(0); let sched = sched_getscheduler(pid).unwrap(); // default is NORMAL aka OTHER From 1c4c20e8f97a9048e4a17d2bbcec331dc789e320 Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Fri, 30 May 2025 12:34:24 -0700 Subject: [PATCH 18/26] fmt?... --- src/sched.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/sched.rs b/src/sched.rs index 70ae65ee96..81f643585f 100644 --- a/src/sched.rs +++ b/src/sched.rs @@ -317,10 +317,18 @@ mod sched_affinity { } // musl has additional sched_param fields that we don't support yet -#[cfg(all(linux_android, not(target_env = "musl"), not(target_env = "ohos")))] +#[cfg(all( + linux_android, + not(target_env = "musl"), + not(target_env = "ohos") +))] pub use self::sched_priority::*; -#[cfg(all(linux_android, not(target_env = "musl"), not(target_env = "ohos")))] +#[cfg(all( + linux_android, + not(target_env = "musl"), + not(target_env = "ohos") +))] mod sched_priority { use std::mem::MaybeUninit; From eac42d033288ee19ff0356dd7d9534fd4658d3b8 Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Fri, 30 May 2025 12:36:51 -0700 Subject: [PATCH 19/26] filter to only linux_android, sigh --- test/test_sched.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_sched.rs b/test/test_sched.rs index 012bfcefa4..bcc452901a 100644 --- a/test/test_sched.rs +++ b/test/test_sched.rs @@ -38,7 +38,7 @@ fn test_sched_affinity() { sched_setaffinity(Pid::from_raw(0), &initial_affinity).unwrap(); } -#[cfg(not(any(target_env = "musl", target_env = "ohos")))] +#[cfg(all(linux_android, not(target_env = "musl"), not(target_env = "ohos")))] #[test] fn test_sched_priority() { use nix::sched::{ From be2f549764e876066ff6dfdc629fe20cede56f1c Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Fri, 30 May 2025 12:45:51 -0700 Subject: [PATCH 20/26] wtf fmt --- src/sched.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/sched.rs b/src/sched.rs index 81f643585f..22ff53c572 100644 --- a/src/sched.rs +++ b/src/sched.rs @@ -324,11 +324,7 @@ mod sched_affinity { ))] pub use self::sched_priority::*; -#[cfg(all( - linux_android, - not(target_env = "musl"), - not(target_env = "ohos") -))] +#[cfg(all(linux_android, not(target_env = "musl"), not(target_env = "ohos")))] mod sched_priority { use std::mem::MaybeUninit; From c1882cf48f6038b432d82f9385ed982f0c32a08e Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Fri, 6 Jun 2025 16:01:50 -0400 Subject: [PATCH 21/26] switch sched name for android --- src/sched.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sched.rs b/src/sched.rs index 22ff53c572..cb5392e1ab 100644 --- a/src/sched.rs +++ b/src/sched.rs @@ -390,8 +390,8 @@ mod sched_priority { /// See [man_sched(7)](https://man7.org/linux/man-pages/man7/sched.7.html) for more details /// on the differences in behavior. pub enum Scheduler { - /// The default scheduler on non-realtime linux - also known as SCHED_NORMAL. - SCHED_OTHER, + /// The default scheduler on non-realtime linux - also known as SCHED_OTHER. + SCHED_NORMAL, /// The realtime FIFO scheduler. All FIFO threads have priority higher than 0 and /// preempt SCHED_OTHER threads. Threads are executed in priority order, using /// first-in-first-out lists to handle two threads with the same priority. From b8ba4ab13440ced8c1281c8b53371b2bcf882a3f Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Fri, 6 Jun 2025 16:13:15 -0400 Subject: [PATCH 22/26] fix test compile --- test/test_sched.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_sched.rs b/test/test_sched.rs index bcc452901a..a41f6b9da3 100644 --- a/test/test_sched.rs +++ b/test/test_sched.rs @@ -49,7 +49,7 @@ fn test_sched_priority() { let pid = Pid::from_raw(0); let sched = sched_getscheduler(pid).unwrap(); // default is NORMAL aka OTHER - assert_eq!(sched, Scheduler::SCHED_OTHER); + assert_eq!(sched, Scheduler::SCHED_NORMAL); let priority = sched_getparam(pid).unwrap().sched_priority; assert_eq!(priority, 0); @@ -71,7 +71,7 @@ fn test_sched_priority() { // expected, assert that it didn't change assert_eq!( sched_getscheduler(pid).unwrap(), - Scheduler::SCHED_OTHER + Scheduler::SCHED_NORMAL ); } Err(e) => { From 872095886a5fde91867ca2c2f226dcc1302cec18 Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Fri, 20 Jun 2025 11:04:07 -0400 Subject: [PATCH 23/26] clippy --- test/test_sched.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_sched.rs b/test/test_sched.rs index a41f6b9da3..4349efdbe9 100644 --- a/test/test_sched.rs +++ b/test/test_sched.rs @@ -75,7 +75,7 @@ fn test_sched_priority() { ); } Err(e) => { - panic!("unexpected error: {}", e); + panic!("unexpected error: {e}"); } } } From 660529dd1f04dbe5fa85cd58ef4bf2c02850ffc8 Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Fri, 20 Jun 2025 11:21:54 -0400 Subject: [PATCH 24/26] code and comment cleanup --- src/sched.rs | 28 ++++++---------------------- test/test_sched.rs | 4 ++++ 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/src/sched.rs b/src/sched.rs index cb5392e1ab..751d3e8761 100644 --- a/src/sched.rs +++ b/src/sched.rs @@ -316,7 +316,7 @@ mod sched_affinity { } } -// musl has additional sched_param fields that we don't support yet +// musl & ohos have additional sched_param fields that we don't support yet #[cfg(all( linux_android, not(target_env = "musl"), @@ -335,8 +335,8 @@ mod sched_priority { #[repr(C)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] - /// Schedule parameters for a thread (currently only priority is supported). - /// This is a wrapper around `libc::sched_param` + /// Schedule parameters for a thread. Priority is the only supported parameter by the kernel + /// at the moment. This is a wrapper around `libc::sched_param` pub struct SchedParam { /// Priority of the current schedule. pub sched_priority: libc::c_int, @@ -359,22 +359,6 @@ mod sched_priority { } } - // #[cfg(any(target_env = "musl", target_env = "ohos"))] - // fn from(param: SchedParam) -> Self { - // // note: non-priority values are dummy values used by deadline scheduler, - // // don't use this abstraction if you're using deadline. - // let zero_ts = libc::timespec { - // tv_sec: 0, - // tv_nsec: 0, - // }; - // libc::sched_param { - // sched_priority: param.sched_priority, - // sched_ss_low_priority: 0, - // sched_ss_repl_period: zero_ts.clone(), - // sched_ss_init_budget: zero_ts, - // sched_ss_max_repl: 0, - // } - // } } impl From for SchedParam { fn from(param: libc::sched_param) -> Self { @@ -393,10 +377,10 @@ mod sched_priority { /// The default scheduler on non-realtime linux - also known as SCHED_OTHER. SCHED_NORMAL, /// The realtime FIFO scheduler. All FIFO threads have priority higher than 0 and - /// preempt SCHED_OTHER threads. Threads are executed in priority order, using + /// preempt SCHED_NORMAL threads. Threads are executed in priority order, using /// first-in-first-out lists to handle two threads with the same priority. SCHED_FIFO, - /// Round-robin scheduler + /// Round-robin scheduler, similar to SCHED_FIFO but with a time quantum. SCHED_RR, /// Batch scheduler, similar to SCHED_OTHER but assumes the thread is CPU intensive. /// The kernel applies a mild penalty to switching to this thread. @@ -441,7 +425,7 @@ mod sched_priority { /// /// SCHED_FIFO and SCHED_RR allow priorities between the min and max inclusive. /// - /// SCHED_DEADLINE cannot be set with this function, libc::sched_setattr must be used instead. + /// SCHED_DEADLINE cannot be set with this function, `libc::sched_setattr` must be used instead. pub fn sched_setscheduler( pid: Pid, sched: Scheduler, diff --git a/test/test_sched.rs b/test/test_sched.rs index 4349efdbe9..81fde80524 100644 --- a/test/test_sched.rs +++ b/test/test_sched.rs @@ -73,6 +73,10 @@ fn test_sched_priority() { sched_getscheduler(pid).unwrap(), Scheduler::SCHED_NORMAL ); + assert_eq!( + sched_getparam(pid).unwrap().sched_priority, + 0 + ); } Err(e) => { panic!("unexpected error: {e}"); From 2389cc6e22f5665af9e2d5717f6f951014c8abad Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Fri, 20 Jun 2025 13:10:22 -0400 Subject: [PATCH 25/26] fmt --- test/test_sched.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test/test_sched.rs b/test/test_sched.rs index 81fde80524..df97bb1fec 100644 --- a/test/test_sched.rs +++ b/test/test_sched.rs @@ -73,10 +73,7 @@ fn test_sched_priority() { sched_getscheduler(pid).unwrap(), Scheduler::SCHED_NORMAL ); - assert_eq!( - sched_getparam(pid).unwrap().sched_priority, - 0 - ); + assert_eq!(sched_getparam(pid).unwrap().sched_priority, 0); } Err(e) => { panic!("unexpected error: {e}"); From fd81fdc4408719e44b09f1d8286e99ff82bd9f0e Mon Sep 17 00:00:00 2001 From: Alexander Hill Date: Fri, 20 Jun 2025 13:12:12 -0400 Subject: [PATCH 26/26] manual fmt, sigh --- src/sched.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sched.rs b/src/sched.rs index 751d3e8761..ce714ac551 100644 --- a/src/sched.rs +++ b/src/sched.rs @@ -358,8 +358,8 @@ mod sched_priority { sched_priority: param.sched_priority, } } - } + impl From for SchedParam { fn from(param: libc::sched_param) -> Self { SchedParam {