Add Process disk usage (bytes read/written)#248
Add Process disk usage (bytes read/written)#248bvaisvil wants to merge 12 commits intoGuillaumeGomez:masterfrom
Conversation
tests/process.rs
Outdated
| system.refresh_processes(); | ||
| let process_list = system.get_process_list(); | ||
| let mut write_bytes: u64 = 0; | ||
| for p in process_list.values(){ |
There was a problem hiding this comment.
In here, if any process wrote any data, it'll be > 0. I assume this is not what you want to test. So instead, just get the current pid and do:
let p = system.get_process(sysinfo::get_current_pid().expect("failed to get current pid")).expect("no such process");
assert!(p.written_bytes() > 0);
src/windows/process.rs
Outdated
| pub(crate) fn get_disk_usage(p: &mut Process){ | ||
| let r = ProcessDiagnosticInfo::try_get_for_process_id(p.pid as u32).ok(); | ||
| if r.is_some(){ | ||
| let r = r.unwrap(); |
There was a problem hiding this comment.
Please never use unwrap. If needed, create an inner function expecting a result and then chain ? calls.
There was a problem hiding this comment.
sure, I can do that. Thought if I converted it to an option and then check if it was Some that unwrap won't cause a panic, perhaps I am wrong about that.
There was a problem hiding this comment.
In that case, you can use if let.
|
Thanks for the PR! This looks promising. A few things though: please run |
|
Benches for Linux and Mac seem okay. mac before mac after Linux Before Linux After Windows Before: And After: |
src/windows/process.rs
Outdated
| } | ||
| let diag_info = ProcessDiagnosticInfo::try_get_for_process_id(p.pid as u32).ok(); | ||
| match diag_info{ | ||
| Some(diag_info) => match diag_info{ |
There was a problem hiding this comment.
Isn't of this match cascade, you can add a new macro:
macro_rules! safe_unwrap {
($x:expr) => {
match $x {
Some(x) => x,
None => return,
}
}And you can use it like this:
let diag_info = safe_unwrap!(diag_info.get_disk_usage().ok());
src/sysinfo.rs
Outdated
| #![crate_type = "lib"] | ||
| #![crate_type = "rlib"] | ||
| #![deny(missing_docs)] | ||
| #![warn(missing_docs)] |
There was a problem hiding this comment.
Please don't change this one.
|
Latest commits restore the |
Put them in the same file where they're used. :) (unless they're used in multiple files, then create a |
|
I looked into using a WMI query. I can't find a query that will give the number of bytes written or read by a process. I also looked into having the I'm open to any ideas ... |
You can wrap the field into an |
|
I took over this PR in #287. I kept your commits but rebased and fixed the few things that were not smooth enough. Again: thanks a lot for starting this! :) |
This PR adds two new fields to process,
read_bytesandwritten_bytesand accessor methods forProcessExtby the same name for MacOS, Linux, and Windows. In addition, I've added a test for the new functionality.Please note: For windows, this adds a new dependency with
winrt. This was the only way I could find to get the information.