-
Notifications
You must be signed in to change notification settings - Fork 681
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a sysinfo wrapper #922
Conversation
src/sys/sysinfo.rs
Outdated
} | ||
|
||
/// Current number of processes. | ||
pub fn process_count(&self) -> u32 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why you picked u32 instead of u16 here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The max. process count on x86-64 Linux doesn't fit in a u16
. The default does, but PID_MAX_LIMIT
doesn't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's defined as a __u16
in the Linux header file and in libc. Maybe the kernel can support >65536 processes, but sysinfo can't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that's odd. I guess I can change it to use u16
then. Both the libc
crate and man page use unsigned short
though, which can technically be a u32
.
src/sys/sysinfo.rs
Outdated
|
||
/// Returns the amount of completely unused RAM in Bytes. | ||
/// | ||
/// "Unused" in this context means that the RAM in neither actively used by programs, nor by the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
80 columns per line, please.
src/sys/sysinfo.rs
Outdated
} | ||
|
||
fn scale_mem(&self, units: libc::c_ulong) -> u64 { | ||
(units * self.0.mem_unit as libc::c_ulong) as u64 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this multiplication is overflowing on 32-bit architectures.
src/sys/sysinfo.rs
Outdated
/// [See `sysinfo(2)`](http://man7.org/linux/man-pages/man2/sysinfo.2.html). | ||
pub fn sysinfo() -> Result<SysInfo> { | ||
let mut info: libc::sysinfo = unsafe { mem::uninitialized() }; | ||
match unsafe { libc::sysinfo(&mut info) } { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified to let res = libc::sysinfo(...); Errno::result(res)
src/sys/sysinfo.rs
Outdated
} | ||
|
||
/// Current number of processes. | ||
pub fn process_count(&self) -> u32 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's defined as a __u16
in the Linux header file and in libc. Maybe the kernel can support >65536 processes, but sysinfo can't.
src/sys/sysinfo.rs
Outdated
pub fn ram_unused(&self) -> u64 { | ||
self.scale_mem(self.0.freeram) | ||
} | ||
|
||
fn scale_mem(&self, units: libc::c_ulong) -> u64 { | ||
(units * self.0.mem_unit as libc::c_ulong) as u64 | ||
(units as u64 * self.0.mem_unit as u64) as u64 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The final "as u64" is superfluous.
Ok, I'm satisfied. All it needs now is a squash. We can't squash ourselves, because we use bors. |
|
bors r+ |
👎 Rejected by code reviews |
Silly me. I have to hit "approve" before telling bors to merge. bors r+ |
922: Add a sysinfo wrapper r=asomers a=jonas-schievink Closes #505 Returned values were also inspected manually to be correct. Co-authored-by: Jonas Schievink <[email protected]>
Closes #505
Returned values were also inspected manually to be correct.