summaryrefslogtreecommitdiff
path: root/test/test_sched.rs
diff options
context:
space:
mode:
authorThibaut Ackermann <thibaut@ackermann.dev>2019-10-30 21:55:27 +0100
committerThibaut Ackermann <thibaut@ackermann.dev>2019-11-21 08:36:53 +0100
commit18c20381b094d7d8273d1a961052c22493c6b21c (patch)
tree45d85c77d0e616e221f380ad2125a68b0e878165 /test/test_sched.rs
parent3c7a23b52d238bd502392883a55c5b792af3a05e (diff)
downloadnix-18c20381b094d7d8273d1a961052c22493c6b21c.zip
Implement sched::sched_getaffinity()
sched_getaffinity(2) get a process's CPU affinity mask
Diffstat (limited to 'test/test_sched.rs')
-rw-r--r--test/test_sched.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/test/test_sched.rs b/test/test_sched.rs
new file mode 100644
index 00000000..922196a3
--- /dev/null
+++ b/test/test_sched.rs
@@ -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();
+}