Skip to content
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 Process disk usage (bytes read/written) #248

Closed
wants to merge 12 commits into from
60 changes: 31 additions & 29 deletions src/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,35 +559,37 @@ pub(crate) fn get_system_computation_time() -> ULARGE_INTEGER {
}

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();
if r.is_some(){
let du = r.unwrap().get_disk_usage().ok();
if du.is_some(){
let du = du.unwrap();
if du.is_some(){
let report = du.unwrap().get_report().ok();
if report.is_some(){
let report = report.unwrap();
if report.is_some(){
let report = report.unwrap();
let read_bytes = report.get_bytes_read_count().ok();
let write_bytes = report.get_bytes_written_count().ok();
if read_bytes.is_some(){
p.read_bytes = read_bytes.unwrap() as u64;
}
if write_bytes.is_some(){
p.written_bytes = write_bytes.unwrap() as u64;
}
}
}
}
}

}

}
let diag_info = ProcessDiagnosticInfo::try_get_for_process_id(p.pid as u32).ok();
match diag_info{
Some(diag_info) => match diag_info{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());

Some(diag_info) => match diag_info.get_disk_usage().ok(){
Some(disk_usage) => match disk_usage{
Some(disk_usage) => match disk_usage.get_report().ok(){
Some(report) => match report{
Some(report) => {
let read_bytes = report.get_bytes_read_count().ok();
let write_bytes = report.get_bytes_written_count().ok();
match read_bytes{
Some(read_bytes) => p.read_bytes = read_bytes as u64,
None => {}
};
match write_bytes{
Some(write_bytes) => p.written_bytes = write_bytes as u64,
None => {}
};
},
None => {}
},
None => {}
},
None => {}
},
None => {}
},
None => {}
},
None => {}
};
}

pub(crate) fn compute_cpu_usage(p: &mut Process, nb_processors: u64, now: ULARGE_INTEGER) {
Expand Down
13 changes: 4 additions & 9 deletions tests/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn test_process() {

#[test]
fn test_process_disk_usage(){
use sysinfo::{ProcessExt, SystemExt};
use sysinfo::{ProcessExt, SystemExt, get_current_pid};
use std::fs::File;
use std::fs;
use std::io::prelude::*;
Expand All @@ -31,13 +31,8 @@ fn test_process_disk_usage(){
file.write_all(b"This is a test file\nwith test data.\n").unwrap();
}
fs::remove_file("test.txt").ok();
let mut system = sysinfo::System::new();
system.refresh_processes();
let process_list = system.get_process_list();
let mut write_bytes: u64 = 0;
for p in process_list.values(){
write_bytes += p.written_bytes();
}
let system = sysinfo::System::new();
let p = system.get_process(get_current_pid().expect("Failed retrieving current pid.")).expect("failed to get process");

assert!(write_bytes > 0);
assert!(p.written_bytes() > 0);
}