Skip to content

Commit 65cd014

Browse files
committed
sys::prctl: adding calls about core scheduling for isolation based on
cookie.
1 parent b561476 commit 65cd014

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

changelog/2629.added.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `get_sched_core`, `set_sched_core`, `share_sched_core_from` and `share_sched_core_to` for Linux

src/sys/prctl.rs

+52
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
use crate::errno::Errno;
99
use crate::sys::signal::Signal;
10+
use crate::unistd::Pid;
1011
use crate::Result;
1112

1213
use libc::{c_int, c_ulong, c_void};
@@ -226,3 +227,54 @@ pub fn set_vma_anon_name(addr: NonNull<c_void>, length: NonZeroUsize, name: Opti
226227

227228
Errno::result(res).map(drop)
228229
}
230+
231+
/// Get the (scheduling) cookie of the calling thread
232+
pub fn get_sched_core() -> Result<u32> {
233+
let mut cookie: u32 = 0;
234+
let res = unsafe {
235+
libc::prctl(
236+
libc::PR_SCHED_CORE,
237+
libc::PR_SCHED_CORE_GET,
238+
&mut cookie, 0, 0)
239+
};
240+
241+
match Errno::result(res) {
242+
Ok(_) => Ok(cookie),
243+
Err(e) => Err(e),
244+
}
245+
}
246+
247+
/// Set a cookie for the calling thread
248+
pub fn set_sched_core(cookie: u32) -> Result<()> {
249+
let res = unsafe {
250+
libc::prctl(
251+
libc::PR_SCHED_CORE,
252+
libc::PR_SCHED_CORE_CREATE,
253+
&cookie, 0, 0)
254+
};
255+
256+
Errno::result(res).map(drop)
257+
}
258+
259+
/// Share the cookie with another thread
260+
pub fn share_sched_core_from(pid: Pid) -> Result<()> {
261+
let res = unsafe {
262+
libc::prctl(
263+
libc::PR_SCHED_CORE,
264+
libc::PR_SCHED_CORE_SHARE_FROM,
265+
&pid, 0, 0)
266+
};
267+
268+
Errno::result(res).map(drop)
269+
}
270+
/// Calling thread shares the same cookie with another thread
271+
pub fn share_sched_core_to(pid: Pid) -> Result<()> {
272+
let res = unsafe {
273+
libc::prctl(
274+
libc::PR_SCHED_CORE,
275+
libc::PR_SCHED_CORE_SHARE_TO,
276+
&pid, 0, 0)
277+
};
278+
279+
Errno::result(res).map(drop)
280+
}

test/sys/test_prctl.rs

+29
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,33 @@ mod test_prctl {
164164
.unwrap_or_default();
165165
prctl::set_vma_anon_name(ptr, sz, None).unwrap_or_default();
166166
}
167+
168+
#[test]
169+
fn test_sched_core() {
170+
use nix::errno::Errno;
171+
use nix::unistd::Pid;
172+
173+
let c: u32 = 1234;
174+
let p = Pid::from_raw(0);
175+
let mut err = prctl::set_sched_core(c).unwrap_err();
176+
match err {
177+
Errno::EINVAL | Errno::ESRCH | Errno::ENODEV => (),
178+
e => panic!("unexpected error {e}"),
179+
}
180+
err = prctl::get_sched_core().unwrap_err();
181+
match err {
182+
Errno::EINVAL | Errno::ESRCH | Errno::ENODEV => (),
183+
e => panic!("unexpected error {e}"),
184+
}
185+
err = prctl::share_sched_core_from(p).unwrap_err();
186+
match err {
187+
Errno::EINVAL | Errno::ESRCH | Errno::ENODEV => (),
188+
e => panic!("unexpected error {e}"),
189+
}
190+
err = prctl::share_sched_core_to(p).unwrap_err();
191+
match err {
192+
Errno::EINVAL | Errno::ESRCH | Errno::ENODEV => (),
193+
e => panic!("unexpected error {e}"),
194+
}
195+
}
167196
}

0 commit comments

Comments
 (0)