-
Notifications
You must be signed in to change notification settings - Fork 676
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1148: Implement sched::sched_getaffinity() r=asomers a=thib-ack Hello, I found the function `sched::sched_setaffinity()` but I also needed to get the process affinity and I did not find the function `sched::sched_getaffinity()` So I added the function which maps [sched_getaffinity(2)](https://linux.die.net/man/2/sched_getaffinity) which "get a process's CPU affinity mask" to the sched.rs file. I hope the code match your guidelines (returned `Result<CpuSet>` instead of pointer parameter) If that's not the case, tell me, I will fix asap. I hope this will help ! Thanks, Thibaut Co-authored-by: Thibaut Ackermann <[email protected]>
- Loading branch information
Showing
4 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use nix::sched::{sched_getaffinity, sched_setaffinity, CpuSet}; | ||
use nix::unistd::Pid; | ||
|
||
#[test] | ||
fn test_sched_affinity() { | ||
// If pid is zero, then the mask of the calling process is returned. | ||
let initial_affinity = sched_getaffinity(Pid::from_raw(0)).unwrap(); | ||
let mut at_least_one_cpu = false; | ||
let mut last_valid_cpu = 0; | ||
for field in 0..CpuSet::count() { | ||
if initial_affinity.is_set(field).unwrap() { | ||
at_least_one_cpu = true; | ||
last_valid_cpu = field; | ||
} | ||
} | ||
assert!(at_least_one_cpu); | ||
|
||
// Now restrict the running CPU | ||
let mut new_affinity = CpuSet::new(); | ||
new_affinity.set(last_valid_cpu).unwrap(); | ||
sched_setaffinity(Pid::from_raw(0), &new_affinity).unwrap(); | ||
|
||
// And now re-check the affinity which should be only the one we set. | ||
let updated_affinity = sched_getaffinity(Pid::from_raw(0)).unwrap(); | ||
for field in 0..CpuSet::count() { | ||
// Should be set only for the CPU we set previously | ||
assert_eq!(updated_affinity.is_set(field).unwrap(), field==last_valid_cpu) | ||
} | ||
|
||
// Finally, reset the initial CPU set | ||
sched_setaffinity(Pid::from_raw(0), &initial_affinity).unwrap(); | ||
} |