Implement sched::sched_getaffinity()#1148
Conversation
32feaef to
8f9956b
Compare
asomers
left a comment
There was a problem hiding this comment.
Could you also please add a CHANGELOG entry and a test?
| libc::sched_getaffinity( | ||
| pid.into(), | ||
| mem::size_of::<CpuSet>() as libc::size_t, | ||
| & mut cpuset.cpu_set, |
There was a problem hiding this comment.
Style nit: there should be no space between & and mut.
| ) | ||
| }; | ||
|
|
||
| match res { |
There was a problem hiding this comment.
I think you can do this more simply by Errno::result(res).and(Ok(cpuset))
30aad7c to
dde8156
Compare
Thanks |
dde8156 to
d6fbf81
Compare
asomers
left a comment
There was a problem hiding this comment.
Could you please also add docs for sched_getaffinity? And since you're here, it would be great if you could do the same for sched_setaffinity and for the CpuSet methods.
| 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..(8 * mem::size_of::<libc::cpu_set_t>()) { |
There was a problem hiding this comment.
What does the magic number 8 mean? You should remove it if possible, or define it as a const if not.
There was a problem hiding this comment.
This value comes from the CpuSet::set() and CpuSet::is_set() methods.
https://github.com/nix-rust/nix/blob/master/src/sched.rs#L63
pub fn is_set(&self, field: usize) -> Result<bool> {
if field >= 8 * mem::size_of::<libc::cpu_set_t>() {
Err(Error::Sys(Errno::EINVAL))
} else {
Ok(unsafe { libc::CPU_ISSET(field, &self.cpu_set) })
}
}That's because there is no easy way to know the size of a CpuSet and in the test I want to check each bits of the CpuSet.
I can do differently:
- add a method
CpuSet::size()which will return this value. - call
CpuSet::is_set()until I get aErrno::EINVAL
What do you prefer ?
There was a problem hiding this comment.
Better to add a method to CpuSet. But I would call it count, because that's what the C macros call it.
| @@ -0,0 +1,36 @@ | |||
|
|
|||
| #[cfg(any(target_os = "android", | |||
There was a problem hiding this comment.
You should move the #[cfg()] into test/test.rs, for consistency with other files.
5895dc1 to
47e9413
Compare
|
Hi, I'll be AFK for the next 2 weeks unfortunately. I'll continue to work on this when I come back. Thanks, |
No, I disgree. Return arguments are very C-ish. It's much Rustier to use return values. I doubt there will be any performance difference, because rustc is able to do what C++ people call "return by move". |
|
You'll have to rebase to fix the build error on nightly. |
47e9413 to
3d6ba05
Compare
asomers
left a comment
There was a problem hiding this comment.
I like the docs! I think we're getting close now.
| } | ||
|
|
||
| /// Return the maximum number of CPU in CpuSet | ||
| #[inline] |
There was a problem hiding this comment.
You can remove the #[inline]. The compiler is smart enough to figure it out for itself.
| /// `field` is the CPU id to remove | ||
| pub fn unset(&mut self, field: usize) -> Result<()> { | ||
| if field >= 8 * mem::size_of::<libc::cpu_set_t>() { | ||
| CpuSet::count(); |
There was a problem hiding this comment.
oops, copy-paste mistake
3d6ba05 to
9934abc
Compare
| /// | ||
| /// Binding the current thread to CPU 0 can be done as follows: | ||
| /// | ||
| /// ```rust |
There was a problem hiding this comment.
I would probably make this rust,no_run. Otherwise, you'll be changing the affinity of the doc test process.
sched_getaffinity(2) get a process's CPU affinity mask
9934abc to
18c2038
Compare
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 <thibaut@ackermann.dev>
Build succeeded
|
Hello,
I found the function
sched::sched_setaffinity()but I also needed to get the process affinity and I did not find the functionsched::sched_getaffinity()So I added the function which maps sched_getaffinity(2) 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