Skip to content

Commit

Permalink
fix: refresh process memory on Windows on first update
Browse files Browse the repository at this point in the history
The first time a process is refreshed on windows, its memory values are
not fetched. This is only done on a second refresh.

Fix this my moving the refresh memory into the process update function.
  • Loading branch information
vthib committed Oct 12, 2023
1 parent a728002 commit 12e3a0a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ impl ProcessInner {
if refresh_kind.disk_usage() {
update_disk_usage(self);
}
update_memory(self);
self.run_time = now.saturating_sub(self.start_time());
self.updated = true;
}
Expand Down
3 changes: 1 addition & 2 deletions src/windows/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{CpuRefreshKind, LoadAvg, Pid, ProcessRefreshKind};

use crate::sys::cpu::*;
use crate::sys::process::{get_start_time, update_memory};
use crate::sys::process::get_start_time;
use crate::sys::tools::*;
use crate::sys::utils::{get_now, get_reg_string_value, get_reg_value_u32};
use crate::{Process, ProcessInner};
Expand Down Expand Up @@ -473,7 +473,6 @@ fn refresh_existing_process(
} else {
return Some(false);
}
update_memory(proc_);
proc_.update(refresh_kind, nb_cpus, now);
proc_.updated = false;
Some(true)
Expand Down
16 changes: 16 additions & 0 deletions tests/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,19 @@ fn test_process_creds() {
true
}));
}

// Regression test for <https://github.com/GuillaumeGomez/sysinfo/issues/1084>
#[test]
fn test_process_memory_refresh() {
// Ensure the process memory is available on the first refresh.
let mut s = sysinfo::System::new();

// Refresh our own process
let pid = Pid::from_u32(std::process::id());
s.refresh_process_specifics(pid, sysinfo::ProcessRefreshKind::new());

let proc = s.process(pid).unwrap();
// Check that the memory values re not empty.
assert!(proc.memory() > 0);
assert!(proc.virtual_memory() > 0);
}

0 comments on commit 12e3a0a

Please sign in to comment.