Skip to content

Commit

Permalink
Update System::refresh_processes API to give control over when to r…
Browse files Browse the repository at this point in the history
…emove dead processes
  • Loading branch information
GuillaumeGomez committed Oct 3, 2024
1 parent 6f1d382 commit 7452b8d
Show file tree
Hide file tree
Showing 17 changed files with 299 additions and 162 deletions.
8 changes: 4 additions & 4 deletions benches/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ fn bench_refresh_all(b: &mut test::Bencher) {
fn bench_refresh_processes(b: &mut test::Bencher) {
let mut s = sysinfo::System::new();

s.refresh_processes(sysinfo::ProcessesToUpdate::All); // to load the whole processes list a first time.
s.refresh_processes(sysinfo::ProcessesToUpdate::All, true); // to load the whole processes list a first time.
b.iter(move || {
s.refresh_processes(sysinfo::ProcessesToUpdate::All);
s.refresh_processes(sysinfo::ProcessesToUpdate::All, true);
});
}

Expand All @@ -44,7 +44,7 @@ fn bench_refresh_processes(b: &mut test::Bencher) {
fn bench_first_refresh_processes(b: &mut test::Bencher) {
b.iter(move || {
let mut s = sysinfo::System::new();
s.refresh_processes(sysinfo::ProcessesToUpdate::All);
s.refresh_processes(sysinfo::ProcessesToUpdate::All, true);
});
}

Expand All @@ -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 = sysinfo::get_current_pid().expect("failed to get current pid");
b.iter(move || {
s.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]));
s.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true);
});
}

Expand Down
2 changes: 1 addition & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ fn interpret_input(
.take(1)
.next()
{
if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid])) != 0 {
if sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true) != 0 {
writeln!(&mut io::stdout(), "Process `{pid}` updated successfully");
} else {
writeln!(&mut io::stdout(), "Process `{pid}` couldn't be updated...");
Expand Down
52 changes: 26 additions & 26 deletions src/c_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub extern "C" fn sysinfo_refresh_memory(system: CSystem) {
let system: &mut System = system.borrow_mut();
system.refresh_memory();
}
Box::into_raw(system);
let _ = Box::into_raw(system);
}
}

Expand All @@ -68,7 +68,7 @@ pub extern "C" fn sysinfo_refresh_cpu(system: CSystem) {
let system: &mut System = system.borrow_mut();
system.refresh_cpu_usage();
}
Box::into_raw(system);
let _ = Box::into_raw(system);
}
}

Expand All @@ -82,7 +82,7 @@ pub extern "C" fn sysinfo_refresh_all(system: CSystem) {
let system: &mut System = system.borrow_mut();
system.refresh_all();
}
Box::into_raw(system);
let _ = Box::into_raw(system);
}
}

Expand All @@ -96,9 +96,9 @@ pub extern "C" fn sysinfo_refresh_processes(system: CSystem) {
let mut system: Box<System> = Box::from_raw(system as *mut System);
{
let system: &mut System = system.borrow_mut();
system.refresh_processes(ProcessesToUpdate::All);
system.refresh_processes(ProcessesToUpdate::All, true);
}
Box::into_raw(system);
let _ = Box::into_raw(system);
}
}

Expand All @@ -112,9 +112,9 @@ pub extern "C" fn sysinfo_refresh_process(system: CSystem, pid: PID) {
let mut system: Box<System> = Box::from_raw(system as *mut System);
{
let system: &mut System = system.borrow_mut();
system.refresh_processes(ProcessesToUpdate::Some(&[Pid::from_u32(pid as _)]));
system.refresh_processes(ProcessesToUpdate::Some(&[Pid::from_u32(pid as _)]), true);
}
Box::into_raw(system);
let _ = Box::into_raw(system);
}
}

Expand Down Expand Up @@ -144,7 +144,7 @@ pub extern "C" fn sysinfo_disks_refresh(disks: CDisks) {
let disks: &mut Disks = disks.borrow_mut();
disks.refresh();
}
Box::into_raw(disks);
let _ = Box::into_raw(disks);
}
}

Expand All @@ -158,7 +158,7 @@ pub extern "C" fn sysinfo_disks_refresh_list(disks: CDisks) {
let disks: &mut Disks = disks.borrow_mut();
disks.refresh_list();
}
Box::into_raw(disks);
let _ = Box::into_raw(disks);
}
}

Expand All @@ -169,7 +169,7 @@ pub extern "C" fn sysinfo_total_memory(system: CSystem) -> size_t {
unsafe {
let system: Box<System> = Box::from_raw(system as *mut System);
let ret = system.total_memory() as size_t;
Box::into_raw(system);
let _ = Box::into_raw(system);
ret
}
}
Expand All @@ -181,7 +181,7 @@ pub extern "C" fn sysinfo_free_memory(system: CSystem) -> size_t {
unsafe {
let system: Box<System> = Box::from_raw(system as *mut System);
let ret = system.free_memory() as size_t;
Box::into_raw(system);
let _ = Box::into_raw(system);
ret
}
}
Expand All @@ -192,7 +192,7 @@ pub extern "C" fn sysinfo_used_memory(system: CSystem) -> size_t {
assert!(!system.is_null());
let system: Box<System> = unsafe { Box::from_raw(system as *mut System) };
let ret = system.used_memory() as size_t;
Box::into_raw(system);
let _ = Box::into_raw(system);
ret
}

Expand All @@ -203,7 +203,7 @@ pub extern "C" fn sysinfo_total_swap(system: CSystem) -> size_t {
unsafe {
let system: Box<System> = Box::from_raw(system as *mut System);
let ret = system.total_swap() as size_t;
Box::into_raw(system);
let _ = Box::into_raw(system);
ret
}
}
Expand All @@ -215,7 +215,7 @@ pub extern "C" fn sysinfo_free_swap(system: CSystem) -> size_t {
unsafe {
let system: Box<System> = Box::from_raw(system as *mut System);
let ret = system.free_swap() as size_t;
Box::into_raw(system);
let _ = Box::into_raw(system);
ret
}
}
Expand All @@ -227,7 +227,7 @@ pub extern "C" fn sysinfo_used_swap(system: CSystem) -> size_t {
unsafe {
let system: Box<System> = Box::from_raw(system as *mut System);
let ret = system.used_swap() as size_t;
Box::into_raw(system);
let _ = Box::into_raw(system);
ret
}
}
Expand Down Expand Up @@ -258,7 +258,7 @@ pub extern "C" fn sysinfo_networks_refresh_list(networks: CNetworks) {
let networks: &mut Networks = networks.borrow_mut();
networks.refresh_list();
}
Box::into_raw(networks);
let _ = Box::into_raw(networks);
}
}

Expand All @@ -272,7 +272,7 @@ pub extern "C" fn sysinfo_networks_refresh(networks: CNetworks) {
let networks: &mut Networks = networks.borrow_mut();
networks.refresh();
}
Box::into_raw(networks);
let _ = Box::into_raw(networks);
}
}

Expand All @@ -286,7 +286,7 @@ pub extern "C" fn sysinfo_networks_received(networks: CNetworks) -> size_t {
let ret = networks.iter().fold(0, |acc: size_t, (_, data)| {
acc.saturating_add(data.received() as size_t)
});
Box::into_raw(networks);
let _ = Box::into_raw(networks);
ret
}
}
Expand All @@ -301,7 +301,7 @@ pub extern "C" fn sysinfo_networks_transmitted(networks: CNetworks) -> size_t {
let ret = networks.iter().fold(0, |acc: size_t, (_, data)| {
acc.saturating_add(data.transmitted() as size_t)
});
Box::into_raw(networks);
let _ = Box::into_raw(networks);
ret
}
}
Expand Down Expand Up @@ -333,7 +333,7 @@ pub extern "C" fn sysinfo_cpus_usage(
}
*length = cpus.len() as c_uint - 1;
}
Box::into_raw(system);
let _ = Box::into_raw(system);
}
}

Expand Down Expand Up @@ -362,7 +362,7 @@ pub extern "C" fn sysinfo_processes(
}
entries.len() as size_t
};
Box::into_raw(system);
let _ = Box::into_raw(system);
len
}
} else {
Expand All @@ -386,7 +386,7 @@ pub extern "C" fn sysinfo_process_by_pid(system: CSystem, pid: PID) -> CProcess
} else {
std::ptr::null()
};
Box::into_raw(system);
let _ = Box::into_raw(system);
ret
}
}
Expand Down Expand Up @@ -534,7 +534,7 @@ pub extern "C" fn sysinfo_cpu_vendor_id(system: CSystem) -> RString {
} else {
std::ptr::null()
};
Box::into_raw(system);
let _ = Box::into_raw(system);
c_string
}
}
Expand All @@ -554,7 +554,7 @@ pub extern "C" fn sysinfo_cpu_brand(system: CSystem) -> RString {
} else {
std::ptr::null()
};
Box::into_raw(system);
let _ = Box::into_raw(system);
c_string
}
}
Expand All @@ -566,7 +566,7 @@ pub extern "C" fn sysinfo_cpu_physical_cores(system: CSystem) -> u32 {
unsafe {
let system: Box<System> = Box::from_raw(system as *mut System);
let count = system.physical_core_count().unwrap_or(0);
Box::into_raw(system);
let _ = Box::into_raw(system);
count as u32
}
}
Expand All @@ -582,7 +582,7 @@ pub extern "C" fn sysinfo_cpu_frequency(system: CSystem) -> u64 {
.first()
.map(|cpu| cpu.frequency())
.unwrap_or(0);
Box::into_raw(system);
let _ = Box::into_raw(system);
freq
}
}
Expand Down
Loading

0 comments on commit 7452b8d

Please sign in to comment.