Repository: GuillaumeGomez/sysinfo
Pull request #536 (2021-07-08 20:15:37)

Fix memory information gathering on linux and add test for system memory


Patch:
diff --git a/src/lib.rs b/src/lib.rs
index bddef8d..94420f8 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -198,16 +198,45 @@ pub fn set_open_files_limit(mut _new_limit: isize) -> bool {
 mod test {
     use crate::*;
 
+    #[test]
+    fn check_process_memory_usage() {
+        let mut s = System::new();
+        s.refresh_all();
+
+        if System::IS_SUPPORTED {
+            // No process should have 0 as memory usage.
+            assert!(!s.processes().iter().all(|(_, proc_)| proc_.memory() == 0));
+        } else {
+            // There should be no process, but if there is one, its memory usage should be 0.
+            assert!(s.processes().iter().all(|(_, proc_)| proc_.memory() == 0));
+        }
+    }
+
     #[test]
     fn check_memory_usage() {
-        // We don't want to test on unsupported systems.
+        let mut s = System::new();
+
+        assert_eq!(s.total_memory(), 0);
+        assert_eq!(s.free_memory(), 0);
+        assert_eq!(s.available_memory(), 0);
+        assert_eq!(s.used_memory(), 0);
+        assert_eq!(s.total_swap(), 0);
+        assert_eq!(s.free_swap(), 0);
+        assert_eq!(s.used_swap(), 0);
+
+        s.refresh_memory();
         if System::IS_SUPPORTED {
-            let mut s = System::new();
-            s.refresh_all();
-            assert_eq!(
-                s.processes().iter().all(|(_, proc_)| proc_.memory() == 0),
-                false
-            );
+            assert!(s.total_memory() > 0);
+            assert!(s.used_memory() > 0);
+            if s.total_swap() > 0 {
+                // I think it's pretty safe to assume that there is still some swap left...
+                assert!(s.free_swap() > 0);
+            }
+        } else {
+            assert_eq!(s.total_memory(), 0);
+            assert_eq!(s.used_memory(), 0);
+            assert_eq!(s.total_swap(), 0);
+            assert_eq!(s.free_swap(), 0);
         }
     }
 
diff --git a/src/linux/system.rs b/src/linux/system.rs
index 066d80d..a2c276d 100644
--- a/src/linux/system.rs
+++ b/src/linux/system.rs
@@ -300,7 +300,8 @@ impl SystemExt for System {
     fn refresh_memory(&mut self) {
         if let Ok(data) = get_all_data("/proc/meminfo", 16_385) {
             for line in data.split('\n') {
-                let field = match line.split(':').next() {
+                let mut iter = line.split(':');
+                let field = match iter.next() {
                     Some("MemTotal") => &mut self.mem_total,
                     Some("MemFree") => &mut self.mem_free,
                     Some("MemAvailable") => &mut self.mem_available,
@@ -311,7 +312,7 @@ impl SystemExt for System {
                     Some("SwapFree") => &mut self.swap_free,
                     _ => continue,
                 };
-                if let Some(val_str) = line.rsplit(' ').nth(1) {
+                if let Some(val_str) = iter.next().and_then(|s| s.trim_start().split(' ').next()) {
                     if let Ok(value) = u64::from_str(val_str) {
                         // /proc/meminfo reports KiB, though it says "kB". Convert it.
                         *field = value * 128 / 125;


Test patch:
diff --git a/tests/process.rs b/tests/process.rs
index f30047c..a3a5f2f 100644
--- a/tests/process.rs
+++ b/tests/process.rs
@@ -79,7 +79,7 @@ fn unix_like_cmd() {
     s.refresh_processes();
     assert!(!s.processes().is_empty());
     let process = s.process(p.id() as sysinfo::Pid).unwrap();
-    if process.cmd() != &["sleep", "3"] {
+    if process.cmd() != ["sleep", "3"] {
         panic!("cmd not equivalent to`[sleep, 3]`: {:?}", process);
     }
 }
diff --git a/tests/processor.rs b/tests/processor.rs
index 1f9a783..a8533ee 100644
--- a/tests/processor.rs
+++ b/tests/processor.rs
@@ -17,11 +17,7 @@ fn test_processor() {
         let s = sysinfo::System::new_all();
         assert!(!s.processors().is_empty());
 
-        assert!(s.processors()[0]
-            .brand()
-            .chars()
-            .find(|c| *c == '\0')
-            .is_none())
+        assert!(!s.processors()[0].brand().chars().any(|c| c == '\0'));
     }
 }