From 5a1215504286eae036b797ba4e40c9ef16bfb1c8 Mon Sep 17 00:00:00 2001 From: zetwhite Date: Sun, 19 Sep 2021 03:24:33 +0900 Subject: [PATCH 1/4] fix posix.sched_param SlotConstructor : to make argument value bound by both keyword and position --- vm/src/stdlib/os.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index bcc50da1b4..97e99f7177 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -2496,10 +2496,19 @@ mod posix { } } + #[derive(FromArgs)] + pub struct SchedParamArg { + #[pyarg(any)] + sched_priority: PyObjectRef, + } + impl SlotConstructor for SchedParam { - type Args = SchedParam; - fn py_new(cls: PyTypeRef, sched_param: Self::Args, vm: &VirtualMachine) -> PyResult { - sched_param.into_pyresult_with_type(vm, cls) + type Args = SchedParamArg; + fn py_new(cls: PyTypeRef, arg: Self::Args, vm: &VirtualMachine) -> PyResult { + SchedParam { + sched_priority: arg.sched_priority, + } + .into_pyresult_with_type(vm, cls) } } From 421389eca6d39c0481fdd0fdc4824e76f3a371fe Mon Sep 17 00:00:00 2001 From: zetwhite Date: Sun, 19 Sep 2021 03:27:12 +0900 Subject: [PATCH 2/4] posix : add sched_setscheduler and sched_getscheduler --- vm/src/stdlib/os.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 97e99f7177..96ef4f96a3 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -2494,6 +2494,25 @@ mod posix { sched_priority_repr.as_str() )) } + + #[cfg(any( + target_os = "linux", + target_os = "netbsd", + target_os = "freebsd", + target_os = "android" + ))] + fn try_to_libc(&self, vm: &VirtualMachine) -> PyResult { + let priority = self.sched_priority.clone(); + let priority_type = priority.class().name(); + let value = priority.downcast::().map_err(|_| { + vm.new_type_error(format!( + "an integer is required (got type {})", + priority_type + )) + })?; + let sched_priority = int::try_to_primitive(value.as_bigint(), vm)?; + Ok(libc::sched_param { sched_priority }) + } } #[derive(FromArgs)] @@ -2512,6 +2531,55 @@ mod posix { } } + #[cfg(any( + target_os = "linux", + target_os = "netbsd", + target_os = "freebsd", + target_os = "android" + ))] + #[derive(FromArgs)] + struct SchedSetschedulerArgs { + #[pyarg(positional)] + pid: i32, + #[pyarg(positional)] + policy: i32, + #[pyarg(positional)] + sched_param_obj: PyRef, + } + + #[cfg(any( + target_os = "linux", + target_os = "netbsd", + target_os = "freebsd", + target_os = "android" + ))] + #[pyfunction] + fn sched_getscheduler(pid: libc::pid_t, vm: &VirtualMachine) -> PyResult { + let policy = unsafe { libc::sched_getscheduler(pid) }; + if policy == -1 { + Err(errno_err(vm)) + } else { + Ok(policy) + } + } + + #[cfg(any( + target_os = "linux", + target_os = "netbsd", + target_os = "freebsd", + target_os = "android" + ))] + #[pyfunction] + fn sched_setscheduler(args: SchedSetschedulerArgs, vm: &VirtualMachine) -> PyResult { + let libc_sched_param = args.sched_param_obj.try_to_libc(vm)?; + let policy = unsafe { libc::sched_setscheduler(args.pid, args.policy, &libc_sched_param) }; + if policy == -1 { + Err(errno_err(vm)) + } else { + Ok(policy) + } + } + #[pyfunction] fn get_inheritable(fd: RawFd, vm: &VirtualMachine) -> PyResult { use nix::fcntl::fcntl; From e1719f8437b3225a9b023d955b2bade25a859ab8 Mon Sep 17 00:00:00 2001 From: zetwhite Date: Sun, 19 Sep 2021 03:29:27 +0900 Subject: [PATCH 3/4] posix : add sched_setparam and sched_getparam --- vm/src/stdlib/os.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 96ef4f96a3..8588f2caa7 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -2580,6 +2580,56 @@ mod posix { } } + #[cfg(any( + target_os = "linux", + target_os = "netbsd", + target_os = "freebsd", + target_os = "android" + ))] + #[pyfunction] + fn sched_getparam(pid: libc::pid_t, vm: &VirtualMachine) -> PyResult { + let mut libc_sched_param = libc::sched_param { sched_priority: 0 }; + let ret = unsafe { libc::sched_getparam(pid, &mut libc_sched_param) }; + if ret == -1 { + Err(errno_err(vm)) + } else { + Ok(SchedParam { + sched_priority: BigInt::from(libc_sched_param.sched_priority).into_pyobject(vm), + }) + } + } + + #[cfg(any( + target_os = "linux", + target_os = "netbsd", + target_os = "freebsd", + target_os = "android" + ))] + #[derive(FromArgs)] + struct SetSchedParamArgs { + #[pyarg(positional)] + pid: i32, + #[pyarg(positional)] + sched_param_obj: PyRef, + } + + #[cfg(any( + target_os = "linux", + target_os = "netbsd", + target_os = "freebsd", + target_os = "android" + ))] + #[pyfunction] + fn sched_setparam(args: SetSchedParamArgs, vm: &VirtualMachine) -> PyResult { + let libc_sched_param = args.sched_param_obj.try_to_libc(vm)?; + let ret = unsafe { libc::sched_setparam(args.pid, &libc_sched_param) }; + if ret == -1 { + Err(errno_err(vm)) + } else { + Ok(ret) + } + } + #[pyfunction] fn get_inheritable(fd: RawFd, vm: &VirtualMachine) -> PyResult { use nix::fcntl::fcntl; From 0b38820db6d7c9f36f63b37f9f6c80237f8c63a1 Mon Sep 17 00:00:00 2001 From: zetwhite Date: Fri, 24 Sep 2021 02:05:42 +0900 Subject: [PATCH 4/4] fix os.sched_getparam : make to use MaybeUninit Union --- vm/src/stdlib/os.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/vm/src/stdlib/os.rs b/vm/src/stdlib/os.rs index 8588f2caa7..c90bc1b981 100644 --- a/vm/src/stdlib/os.rs +++ b/vm/src/stdlib/os.rs @@ -2588,15 +2588,16 @@ mod posix { ))] #[pyfunction] fn sched_getparam(pid: libc::pid_t, vm: &VirtualMachine) -> PyResult { - let mut libc_sched_param = libc::sched_param { sched_priority: 0 }; - let ret = unsafe { libc::sched_getparam(pid, &mut libc_sched_param) }; - if ret == -1 { - Err(errno_err(vm)) - } else { - Ok(SchedParam { - sched_priority: BigInt::from(libc_sched_param.sched_priority).into_pyobject(vm), - }) - } + let param = unsafe { + let mut param = std::mem::MaybeUninit::uninit(); + if -1 == libc::sched_getparam(pid, param.as_mut_ptr()) { + return Err(errno_err(vm)); + } + param.assume_init() + }; + Ok(SchedParam { + sched_priority: param.sched_priority.into_pyobject(vm), + }) } #[cfg(any( @@ -2606,7 +2607,7 @@ mod posix { target_os = "android" ))] #[derive(FromArgs)] - struct SetSchedParamArgs { + struct SchedSetParamArgs { #[pyarg(positional)] pid: i32, #[pyarg(positional)] @@ -2620,7 +2621,7 @@ mod posix { target_os = "android" ))] #[pyfunction] - fn sched_setparam(args: SetSchedParamArgs, vm: &VirtualMachine) -> PyResult { + fn sched_setparam(args: SchedSetParamArgs, vm: &VirtualMachine) -> PyResult { let libc_sched_param = args.sched_param_obj.try_to_libc(vm)?; let ret = unsafe { libc::sched_setparam(args.pid, &libc_sched_param) }; if ret == -1 {