Repository: GuillaumeGomez/sysinfo
Pull request #290 (2020-04-08 16:37:12)

Disk io


Patch:
diff --git a/Cargo.toml b/Cargo.toml
index 0f171f2..3be8e3a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "sysinfo"
-version = "0.12.0"
+version = "0.13.0"
 authors = ["Guillaume Gomez <guillaume1.gomez@gmail.com>"]
 
 description = "Library to get system information such as processes, processors, disks, components and networks"
diff --git a/README.md b/README.md
index 45551fe..ceaa270 100644
--- a/README.md
+++ b/README.md
@@ -47,9 +47,9 @@ cargo build --target=armv7-unknown-linux-gnueabihf
 You have an example into the `examples` folder. Just run `cargo run` inside the `examples` folder to start it. Otherwise, here is a little code sample:
 
 ```rust
-use sysinfo::{NetworkExt, NetworksExt, System, SystemExt};
+use sysinfo::{NetworkExt, NetworksExt, ProcessExt, System, SystemExt};
 
-let mut sys = System::new();
+let mut sys = System::new_all();
 
 // We display the disks:
 println!("=> disk list:");
@@ -78,6 +78,11 @@ println!("NB processors: {}", sys.get_processors().len());
 
 // To refresh all system information:
 sys.refresh_all();
+
+// We show the processes and some of their information:
+for (pid, process) in sys.get_processes() {
+    println!("[{}] {} {:?}", pid, process.name(), process.disk_usage());
+}
 ```
 
 ## C interface
@@ -108,21 +113,21 @@ Here are the current results:
 <details>
 
 ```text
-test bench_new                     ... bench:     186,638 ns/iter (+/- 8,603)
-test bench_new_all                 ... bench:  18,604,576 ns/iter (+/- 1,323,790)
-test bench_refresh_all             ... bench:   5,147,523 ns/iter (+/- 337,096)
-test bench_refresh_components      ... bench:      25,391 ns/iter (+/- 1,725)
-test bench_refresh_components_list ... bench:     381,490 ns/iter (+/- 18,237)
-test bench_refresh_cpu             ... bench:      13,397 ns/iter (+/- 880)
-test bench_refresh_disks           ... bench:       2,515 ns/iter (+/- 161)
-test bench_refresh_disks_list      ... bench:      50,986 ns/iter (+/- 5,301)
-test bench_refresh_memory          ... bench:      11,843 ns/iter (+/- 779)
-test bench_refresh_networks        ... bench:     250,242 ns/iter (+/- 68,482)
-test bench_refresh_networks_list   ... bench:     255,148 ns/iter (+/- 23,677)
-test bench_refresh_process         ... bench:     128,860 ns/iter (+/- 22,638)
-test bench_refresh_processes       ... bench:   4,714,965 ns/iter (+/- 309,573)
-test bench_refresh_system          ... bench:      52,044 ns/iter (+/- 4,510)
-test bench_refresh_users_list      ... bench:   2,095,043 ns/iter (+/- 648,385)
+test bench_new                     ... bench:     182,536 ns/iter (+/- 21,074)
+test bench_new_all                 ... bench:  19,911,714 ns/iter (+/- 1,612,109)
+test bench_refresh_all             ... bench:   5,649,643 ns/iter (+/- 444,129)
+test bench_refresh_components      ... bench:      25,293 ns/iter (+/- 1,748)
+test bench_refresh_components_list ... bench:     382,331 ns/iter (+/- 31,620)
+test bench_refresh_cpu             ... bench:      13,633 ns/iter (+/- 1,135)
+test bench_refresh_disks           ... bench:       2,509 ns/iter (+/- 75)
+test bench_refresh_disks_list      ... bench:      51,488 ns/iter (+/- 5,470)
+test bench_refresh_memory          ... bench:      12,941 ns/iter (+/- 3,023)
+test bench_refresh_networks        ... bench:     256,506 ns/iter (+/- 37,196)
+test bench_refresh_networks_list   ... bench:     266,751 ns/iter (+/- 54,535)
+test bench_refresh_process         ... bench:     117,372 ns/iter (+/- 8,732)
+test bench_refresh_processes       ... bench:   5,125,929 ns/iter (+/- 560,050)
+test bench_refresh_system          ... bench:      52,526 ns/iter (+/- 6,786)
+test bench_refresh_users_list      ... bench:   2,479,582 ns/iter (+/- 1,063,982)
 ```
 </details>
 
@@ -131,21 +136,21 @@ test bench_refresh_users_list      ... bench:   2,095,043 ns/iter (+/- 648,385)
 <details>
 
 ```text
-test bench_new                     ... bench:   7,335,755 ns/iter (+/- 469,000)
-test bench_new_all                 ... bench:  32,233,480 ns/iter (+/- 1,567,239)
-test bench_refresh_all             ... bench:   1,433,015 ns/iter (+/- 126,322)
-test bench_refresh_components      ... bench:           1 ns/iter (+/- 0)
-test bench_refresh_components_list ... bench:   9,835,060 ns/iter (+/- 407,072)
-test bench_refresh_cpu             ... bench:      33,873 ns/iter (+/- 2,177)
-test bench_refresh_disks           ... bench:      58,951 ns/iter (+/- 6,128)
-test bench_refresh_disks_list      ... bench:     125,199 ns/iter (+/- 2,741)
-test bench_refresh_memory          ... bench:       1,004 ns/iter (+/- 56)
-test bench_refresh_networks        ... bench:      39,013 ns/iter (+/- 2,676)
-test bench_refresh_networks_list   ... bench:   1,341,850 ns/iter (+/- 78,258)
-test bench_refresh_process         ... bench:       2,116 ns/iter (+/- 58)
-test bench_refresh_processes       ... bench:   1,032,447 ns/iter (+/- 57,695)
-test bench_refresh_system          ... bench:      35,374 ns/iter (+/- 3,200)
-test bench_refresh_users_list      ... bench:   3,321,140 ns/iter (+/- 135,160)
+test bench_new                     ... bench:   7,119,215 ns/iter (+/- 283,002)
+test bench_new_all                 ... bench:  27,364,010 ns/iter (+/- 1,353,879)
+test bench_refresh_all             ... bench:   3,125,085 ns/iter (+/- 92,479)
+test bench_refresh_components      ... bench:   1,239,478 ns/iter (+/- 45,790)
+test bench_refresh_components_list ... bench:   3,197,295 ns/iter (+/- 91,662)
+test bench_refresh_cpu             ... bench:      24,973 ns/iter (+/- 1,844)
+test bench_refresh_disks           ... bench:      52,321 ns/iter (+/- 1,533)
+test bench_refresh_disks_list      ... bench:     114,756 ns/iter (+/- 3,900)
+test bench_refresh_memory          ... bench:         581 ns/iter (+/- 25)
+test bench_refresh_networks        ... bench:      35,231 ns/iter (+/- 2,210)
+test bench_refresh_networks_list   ... bench:     661,170 ns/iter (+/- 56,636)
+test bench_refresh_process         ... bench:       1,531 ns/iter (+/- 154)
+test bench_refresh_processes       ... bench:   1,070,742 ns/iter (+/- 57,539)
+test bench_refresh_system          ... bench:   1,303,291 ns/iter (+/- 44,538)
+test bench_refresh_users_list      ... bench:   2,340,562 ns/iter (+/- 83,992)
 ```
 </details>
 
@@ -154,21 +159,21 @@ test bench_refresh_users_list      ... bench:   3,321,140 ns/iter (+/- 135,160)
 <details>
 
 ```text
-test bench_new                     ... bench:      86,404 ns/iter (+/- 9,402)
-test bench_new_all                 ... bench:  21,123,771 ns/iter (+/- 570,722)
-test bench_refresh_all             ... bench:   1,757,683 ns/iter (+/- 203,234)
-test bench_refresh_components      ... bench:     325,560 ns/iter (+/- 41,068)
-test bench_refresh_components_list ... bench:     989,827 ns/iter (+/- 221,093)
-test bench_refresh_cpu             ... bench:       8,535 ns/iter (+/- 487)
-test bench_refresh_disks           ... bench:         939 ns/iter (+/- 33)
-test bench_refresh_disks_lists     ... bench:      25,093 ns/iter (+/- 2,080)
-test bench_refresh_memory          ... bench:       2,174 ns/iter (+/- 55)
-test bench_refresh_networks        ... bench:     181,558 ns/iter (+/- 7,325)
-test bench_refresh_networks_list   ... bench:     180,410 ns/iter (+/- 2,414)
-test bench_refresh_process         ... bench:       5,570 ns/iter (+/- 431)
-test bench_refresh_processes       ... bench:     683,455 ns/iter (+/- 14,995)
-test bench_refresh_system          ... bench:     362,875 ns/iter (+/- 172,547)
-test bench_refresh_users_list      ... bench:  16,783,834 ns/iter (+/- 465,111)
+test bench_new                     ... bench:      87,569 ns/iter (+/- 11,078)
+test bench_new_all                 ... bench:  21,445,081 ns/iter (+/- 523,973)
+test bench_refresh_all             ... bench:   1,915,573 ns/iter (+/- 296,132)
+test bench_refresh_components      ... bench:     293,904 ns/iter (+/- 63,492)
+test bench_refresh_components_list ... bench:     894,462 ns/iter (+/- 161,599)
+test bench_refresh_cpu             ... bench:       8,636 ns/iter (+/- 1,244)
+test bench_refresh_disks           ... bench:         937 ns/iter (+/- 97)
+test bench_refresh_disks_list      ... bench:      25,116 ns/iter (+/- 990)
+test bench_refresh_memory          ... bench:       2,172 ns/iter (+/- 67)
+test bench_refresh_networks        ... bench:     183,552 ns/iter (+/- 2,253)
+test bench_refresh_networks_list   ... bench:     183,623 ns/iter (+/- 11,183)
+test bench_refresh_process         ... bench:       5,571 ns/iter (+/- 443)
+test bench_refresh_processes       ... bench:     764,125 ns/iter (+/- 28,568)
+test bench_refresh_system          ... bench:     333,610 ns/iter (+/- 53,204)
+test bench_refresh_users_list      ... bench:  16,816,081 ns/iter (+/- 1,039,374)
 ```
 </details>
 
diff --git a/benches/basic.rs b/benches/basic.rs
index b146493..ab106b1 100644
--- a/benches/basic.rs
+++ b/benches/basic.rs
@@ -57,7 +57,7 @@ fn bench_refresh_process(b: &mut test::Bencher) {
     // to be sure it'll exist for at least as long as we run
     let pid = get_current_pid().expect("failed to get current pid");
     b.iter(move || {
-        s.refresh_process(pid);
+        s.refresh_process(pid, );
     });
 }
 
diff --git a/examples/src/simple.rs b/examples/src/simple.rs
index 1ffcf41..a40a09b 100644
--- a/examples/src/simple.rs
+++ b/examples/src/simple.rs
@@ -113,7 +113,7 @@ fn print_help() {
     );
     writeln!(
         &mut io::stdout(),
-        "boot_time           : Displays system boot time"
+        "boot_time          : Displays system boot time"
     );
     writeln!(
         &mut io::stdout(),
diff --git a/src/common.rs b/src/common.rs
index 3655cb2..da53b4e 100644
--- a/src/common.rs
+++ b/src/common.rs
@@ -52,6 +52,9 @@ assert_eq!(r.", stringify!($name), "(), false);
 
 let r = r.with_", stringify!($name), "();
 assert_eq!(r.", stringify!($name), "(), true);
+
+let r = r.without_", stringify!($name), "();
+assert_eq!(r.", stringify!($name), "(), false);
 ```"),
                     pub fn $name(&self) -> bool {
                         self.$name
@@ -402,14 +405,14 @@ impl UserExt for User {
 
 /// Type containing read and written bytes.
 ///
-/// It is returned by [`ProcessExt::get_disk_usage`][crate::ProcessExt::get_disk_usage].
+/// It is returned by [`ProcessExt::disk_usage`][crate::ProcessExt::disk_usage].
 ///
 /// ```no_run
 /// use sysinfo::{ProcessExt, System, SystemExt};
 ///
 /// let s = System::new_all();
 /// for (pid, process) in s.get_processes() {
-///     let disk_usage = process.get_disk_usage();
+///     let disk_usage = process.disk_usage();
 ///     println!("[{}] read bytes   : new/total => {}/{}",
 ///         pid,
 ///         disk_usage.read_bytes,
diff --git a/src/debug.rs b/src/debug.rs
index e70e987..bd2da70 100644
--- a/src/debug.rs
+++ b/src/debug.rs
@@ -81,6 +81,7 @@ impl fmt::Debug for Process {
             .field("CPU usage", &self.cpu_usage())
             .field("status", &self.status())
             .field("root", &self.root())
+            .field("disk_usage", &self.disk_usage())
             .finish()
     }
 }
diff --git a/src/linux/process.rs b/src/linux/process.rs
index f2fbe69..fbeaacd 100644
--- a/src/linux/process.rs
+++ b/src/linux/process.rs
@@ -225,7 +225,7 @@ impl ProcessExt for Process {
         self.cpu_usage
     }
 
-    fn get_disk_usage(&self) -> DiskUsage {
+    fn disk_usage(&self) -> DiskUsage {
         DiskUsage {
             written_bytes: self.written_bytes - self.old_written_bytes,
             total_written_bytes: self.written_bytes,
diff --git a/src/mac/process.rs b/src/mac/process.rs
index 41adebf..0e86bf4 100644
--- a/src/mac/process.rs
+++ b/src/mac/process.rs
@@ -314,7 +314,7 @@ impl ProcessExt for Process {
         self.cpu_usage
     }
 
-    fn get_disk_usage(&self) -> DiskUsage {
+    fn disk_usage(&self) -> DiskUsage {
         DiskUsage {
             read_bytes: self.read_bytes - self.old_read_bytes,
             total_read_bytes: self.read_bytes,
diff --git a/src/traits.rs b/src/traits.rs
index 69ac35e..922ba76 100644
--- a/src/traits.rs
+++ b/src/traits.rs
@@ -306,7 +306,7 @@ pub trait ProcessExt: Debug {
     ///
     /// let s = System::new();
     /// if let Some(process) = s.get_process(1337) {
-    ///     let disk_usage = process.get_disk_usage();
+    ///     let disk_usage = process.disk_usage();
     ///     println!("read bytes   : new/total => {}/{}",
     ///         disk_usage.read_bytes,
     ///         disk_usage.total_read_bytes,
@@ -317,7 +317,7 @@ pub trait ProcessExt: Debug {
     ///     );
     /// }
     /// ```
-    fn get_disk_usage(&self) -> DiskUsage;
+    fn disk_usage(&self) -> DiskUsage;
 }
 
 /// Contains all the methods of the [`Processor`][crate::Processor] struct.
diff --git a/src/unknown/process.rs b/src/unknown/process.rs
index b55884f..5803d07 100644
--- a/src/unknown/process.rs
+++ b/src/unknown/process.rs
@@ -81,7 +81,7 @@ impl ProcessExt for Process {
         0.0
     }
 
-    fn get_disk_usage(&self) -> DiskUsage {
+    fn disk_usage(&self) -> DiskUsage {
         DiskUsage::default()
     }
 }
diff --git a/src/windows/process.rs b/src/windows/process.rs
index a5e9945..5172e09 100644
--- a/src/windows/process.rs
+++ b/src/windows/process.rs
@@ -396,7 +396,7 @@ impl ProcessExt for Process {
         self.cpu_usage
     }
 
-    fn get_disk_usage(&self) -> DiskUsage {
+    fn disk_usage(&self) -> DiskUsage {
         DiskUsage {
             written_bytes: self.written_bytes - self.old_written_bytes,
             total_written_bytes: self.written_bytes,
@@ -654,11 +654,6 @@ pub fn get_handle(p: &Process) -> HANDLE {
     *p.handle
 }
 
-pub fn update_proc_info(p: &mut Process) {
-    update_memory(p);
-    update_disk_usage(p);
-}
-
 pub fn update_disk_usage(p: &mut Process) {
     let mut counters = MaybeUninit::<IO_COUNTERS>::uninit();
     let ret = unsafe { GetProcessIoCounters(*p.handle, counters.as_mut_ptr()) };
diff --git a/src/windows/system.rs b/src/windows/system.rs
index 185cc6c..7821469 100644
--- a/src/windows/system.rs
+++ b/src/windows/system.rs
@@ -23,7 +23,7 @@ use SystemExt;
 use User;
 
 use windows::process::{
-    compute_cpu_usage, get_handle, get_system_computation_time, update_disk_usage, update_proc_info,
+    compute_cpu_usage, get_handle, get_system_computation_time, update_disk_usage, update_memory,
     Process,
 };
 use windows::tools::*;
@@ -172,7 +172,7 @@ impl SystemExt for System {
 
     fn refresh_process(&mut self, pid: Pid) -> bool {
         if self.process_list.contains_key(&pid) {
-            if refresh_existing_process(self, pid, true) == false {
+            if refresh_existing_process(self, pid) == false {
                 self.process_list.remove(&pid);
                 return false;
             }
@@ -394,19 +394,18 @@ fn is_proc_running(handle: HANDLE) -> bool {
     !(ret == FALSE || exit_code != STILL_ACTIVE)
 }
 
-fn refresh_existing_process(s: &mut System, pid: Pid, compute_cpu: bool) -> bool {
+fn refresh_existing_process(s: &mut System, pid: Pid) -> bool {
     if let Some(ref mut entry) = s.process_list.get_mut(&(pid as usize)) {
         if !is_proc_running(get_handle(entry)) {
             return false;
         }
-        update_proc_info(entry);
-        if compute_cpu {
-            compute_cpu_usage(
-                entry,
-                s.processors.len() as u64,
-                get_system_computation_time(),
-            );
-        }
+        update_memory(entry);
+        update_disk_usage(entry);
+        compute_cpu_usage(
+            entry,
+            s.processors.len() as u64,
+            get_system_computation_time(),
+        );
         true
     } else {
         false


Test patch:
diff --git a/tests/disk_list.rs b/tests/disk_list.rs
index 9b49dbf..9c4b8c5 100644
--- a/tests/disk_list.rs
+++ b/tests/disk_list.rs
@@ -10,7 +10,6 @@ extern crate sysinfo;
 fn test_disks() {
     use sysinfo::SystemExt;
 
-    let s = sysinfo::System::new();
-    println!("total memory: {}", s.get_total_memory());
-    println!("total cpu cores: {}", s.get_processors().len());
+    let s = sysinfo::System::new_all();
+    assert!(s.get_disks().len() > 0);
 }
diff --git a/tests/process.rs b/tests/process.rs
index b5b2dff..0ffcb93 100644
--- a/tests/process.rs
+++ b/tests/process.rs
@@ -78,7 +78,7 @@ fn test_process_disk_usage() {
     use std::fs;
     use std::fs::File;
     use std::io::prelude::*;
-    use sysinfo::{get_current_pid, ProcessExt, SystemExt};
+        use sysinfo::{get_current_pid, ProcessExt, SystemExt};
     {
         let mut file = File::create("test.txt").unwrap();
         file.write_all(b"This is a test file\nwith test data.\n")
@@ -94,13 +94,13 @@ fn test_process_disk_usage() {
         .expect("failed to get process");
 
     assert!(
-        p.get_disk_usage().total_written_bytes > 0,
+        p.disk_usage().total_written_bytes > 0,
         "found {} total written bytes...",
-        p.get_disk_usage().total_written_bytes
+        p.disk_usage().total_written_bytes
     );
     assert!(
-        p.get_disk_usage().written_bytes > 0,
+        p.disk_usage().written_bytes > 0,
         "found {} written bytes...",
-        p.get_disk_usage().written_bytes
+        p.disk_usage().written_bytes
     );
 }