diff --git a/benches/basic.rs b/benches/basic.rs index 6b050fc91..5148d2911 100644 --- a/benches/basic.rs +++ b/benches/basic.rs @@ -77,16 +77,7 @@ fn bench_refresh_disks(b: &mut test::Bencher) { let mut disks = sysinfo::Disks::new_with_refreshed_list(); b.iter(move || { - disks.refresh(); - }); -} - -#[bench] -fn bench_refresh_disks_list(b: &mut test::Bencher) { - let mut disks = sysinfo::Disks::new_with_refreshed_list(); - - b.iter(move || { - disks.refresh_list(false); + disks.refresh(true); }); } @@ -96,17 +87,7 @@ fn bench_refresh_networks(b: &mut test::Bencher) { let mut n = sysinfo::Networks::new_with_refreshed_list(); b.iter(move || { - n.refresh(); - }); -} - -#[cfg(feature = "network")] -#[bench] -fn bench_refresh_networks_list(b: &mut test::Bencher) { - let mut n = sysinfo::Networks::new_with_refreshed_list(); - - b.iter(move || { - n.refresh_list(); + n.refresh(true); }); } diff --git a/examples/simple.c b/examples/simple.c index f3d4f09f0..c0b2b86f6 100644 --- a/examples/simple.c +++ b/examples/simple.c @@ -61,7 +61,7 @@ int main() { CNetworks networks = sysinfo_networks_init(); sysinfo_refresh_all(system); - sysinfo_networks_refresh_list(networks); + sysinfo_networks_refresh(networks); printf("os name: %s\n", sysinfo_system_name(system)); printf("os version: %s\n", sysinfo_system_version(system)); diff --git a/examples/simple.rs b/examples/simple.rs index cd10074ee..ee2250481 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -158,7 +158,7 @@ fn interpret_input( "help" => print_help(), "refresh_disks" => { writeln!(&mut io::stdout(), "Refreshing disk list..."); - disks.refresh_list(true); + disks.refresh(true); writeln!(&mut io::stdout(), "Done."); } "refresh_users" => { @@ -168,7 +168,7 @@ fn interpret_input( } "refresh_networks" => { writeln!(&mut io::stdout(), "Refreshing network list..."); - networks.refresh_list(); + networks.refresh(true); writeln!(&mut io::stdout(), "Done."); } "refresh_components" => { diff --git a/src/c_interface.rs b/src/c_interface.rs index ecdee1706..eb4d13130 100644 --- a/src/c_interface.rs +++ b/src/c_interface.rs @@ -142,21 +142,7 @@ pub extern "C" fn sysinfo_disks_refresh(disks: CDisks) { let mut disks: Box = Box::from_raw(disks as *mut Disks); { let disks: &mut Disks = disks.borrow_mut(); - disks.refresh(); - } - let _ = Box::into_raw(disks); - } -} - -/// Equivalent of [`Disks::refresh_list()`][crate::Disks#method.refresh_list]. -#[no_mangle] -pub extern "C" fn sysinfo_disks_refresh_list(disks: CDisks) { - assert!(!disks.is_null()); - unsafe { - let mut disks: Box = Box::from_raw(disks as *mut Disks); - { - let disks: &mut Disks = disks.borrow_mut(); - disks.refresh_list(true); + disks.refresh(true); } let _ = Box::into_raw(disks); } @@ -248,20 +234,6 @@ pub extern "C" fn sysinfo_networks_destroy(networks: CNetworks) { } } -/// Equivalent of [`Networks::refresh_list()`][crate::Networks#method.refresh_list]. -#[no_mangle] -pub extern "C" fn sysinfo_networks_refresh_list(networks: CNetworks) { - assert!(!networks.is_null()); - unsafe { - let mut networks: Box = Box::from_raw(networks as *mut Networks); - { - let networks: &mut Networks = networks.borrow_mut(); - networks.refresh_list(); - } - let _ = Box::into_raw(networks); - } -} - /// Equivalent of [`Networks::refresh()`][crate::Networks#method.refresh]. #[no_mangle] pub extern "C" fn sysinfo_networks_refresh(networks: CNetworks) { @@ -270,7 +242,7 @@ pub extern "C" fn sysinfo_networks_refresh(networks: CNetworks) { let mut networks: Box = Box::from_raw(networks as *mut Networks); { let networks: &mut Networks = networks.borrow_mut(); - networks.refresh(); + networks.refresh(true); } let _ = Box::into_raw(networks); } diff --git a/src/common/disk.rs b/src/common/disk.rs index 04986f86b..83996da85 100644 --- a/src/common/disk.rs +++ b/src/common/disk.rs @@ -249,7 +249,7 @@ impl Disks { /// use sysinfo::Disks; /// /// let mut disks = Disks::new(); - /// disks.refresh_list(false); + /// disks.refresh(false); /// for disk in disks.list() { /// println!("{disk:?}"); /// } @@ -277,8 +277,7 @@ impl Disks { } /// Creates a new [`Disks`][crate::Disks] type with the disk list loaded - /// and refreshed according to the given [`DiskRefreshKind`]. It is a combination of - /// [`Disks::new`] and [`Disks::refresh_list_specifics`]. + /// and refreshed according to the given [`DiskRefreshKind`]. /// /// ```no_run /// use sysinfo::{Disks, DiskRefreshKind}; @@ -290,7 +289,7 @@ impl Disks { /// ``` pub fn new_with_refreshed_list_specifics(refreshes: DiskRefreshKind) -> Self { let mut disks = Self::new(); - disks.refresh_list_specifics(false, refreshes); + disks.refresh_specifics(false, refreshes); disks } @@ -326,69 +325,23 @@ impl Disks { /// Refreshes the listed disks' information. /// /// Equivalent to [Disks::refresh_specifics]\([DiskRefreshKind::everything]\()). - pub fn refresh(&mut self) { - self.refresh_specifics(DiskRefreshKind::everything()); + pub fn refresh(&mut self, remove_not_listed_disks: bool) { + self.inner + .refresh_specifics(remove_not_listed_disks, DiskRefreshKind::everything()); } - /// Refreshes the listed disks' information according to the given [`DiskRefreshKind`]. - /// - /// ⚠️ If a disk is added or removed, this method won't take it into account. Use - /// [`Disks::refresh_list`] instead. - /// - /// ⚠️ If you didn't call [`Disks::refresh_list`] beforehand, this method will do nothing as - /// the disk list will be empty. + /// Refreshes the disks' information according to the given [`DiskRefreshKind`]. /// /// ```no_run /// use sysinfo::Disks; /// /// let mut disks = Disks::new_with_refreshed_list(); /// // We wait some time...? - /// disks.refresh(); - /// ``` - pub fn refresh_specifics(&mut self, refreshes: DiskRefreshKind) { - self.inner.refresh_specifics(refreshes); - } - - /// The disk list will be emptied then completely recomputed. - /// - /// Equivalent to [Disks::refresh_list_specifics]\([DiskRefreshKind::everything]\()). - /// - /// ```no_run - /// use sysinfo::Disks; - /// - /// let mut disks = Disks::new(); - /// disks.refresh_list(true); - /// ``` - pub fn refresh_list(&mut self, remove_not_listed_disks: bool) { - self.refresh_list_specifics(remove_not_listed_disks, DiskRefreshKind::everything()); - } - - /// The disk list will be emptied then completely recomputed according to the given - /// [`DiskRefreshKind`]. - /// - /// ## Linux - /// - /// ⚠️ On Linux, the [NFS](https://en.wikipedia.org/wiki/Network_File_System) file - /// systems are ignored and the information of a mounted NFS **cannot** be obtained - /// via [`Disks::refresh_list_specifics`]. This is due to the fact that I/O function - /// `statvfs` used by [`Disks::refresh_list_specifics`] is blocking and - /// [may hang](https://github.com/GuillaumeGomez/sysinfo/pull/876) in some cases, - /// requiring to call `systemctl stop` to terminate the NFS service from the remote - /// server in some cases. - /// - /// ```no_run - /// use sysinfo::{Disks, DiskRefreshKind}; - /// - /// let mut disks = Disks::new(); - /// disks.refresh_list_specifics(true, DiskRefreshKind::nothing()); + /// disks.refresh(true); /// ``` - pub fn refresh_list_specifics( - &mut self, - remove_not_listed_disks: bool, - refreshes: DiskRefreshKind, - ) { + pub fn refresh_specifics(&mut self, remove_not_listed_disks: bool, refreshes: DiskRefreshKind) { self.inner - .refresh_list_specifics(remove_not_listed_disks, refreshes); + .refresh_specifics(remove_not_listed_disks, refreshes); } } diff --git a/src/common/network.rs b/src/common/network.rs index 023993c19..5a9e672a9 100644 --- a/src/common/network.rs +++ b/src/common/network.rs @@ -44,7 +44,7 @@ impl Networks { /// use sysinfo::Networks; /// /// let mut networks = Networks::new(); - /// networks.refresh_list(); + /// networks.refresh(true); /// for (interface_name, network) in &networks { /// println!("[{interface_name}]: {network:?}"); /// } @@ -56,8 +56,7 @@ impl Networks { } /// Creates a new [`Networks`][crate::Networks] type with the network interfaces - /// list loaded. It is a combination of [`Networks::new`] and - /// [`Networks::refresh_list`]. + /// list loaded. /// /// ```no_run /// use sysinfo::Networks; @@ -69,7 +68,7 @@ impl Networks { /// ``` pub fn new_with_refreshed_list() -> Self { let mut networks = Self::new(); - networks.refresh_list(); + networks.refresh(false); networks } @@ -87,36 +86,17 @@ impl Networks { self.inner.list() } - /// Refreshes the network interfaces list. - /// - /// ```no_run - /// use sysinfo::Networks; - /// - /// let mut networks = Networks::new(); - /// networks.refresh_list(); - /// ``` - pub fn refresh_list(&mut self) { - self.inner.refresh_list() - } - - /// Refreshes the network interfaces' content. If you didn't run [`Networks::refresh_list`] - /// before, calling this method won't do anything as no interfaces are present. - /// - /// ⚠️ If a network interface is added or removed, this method won't take it into account. Use - /// [`Networks::refresh_list`] instead. - /// - /// ⚠️ If you didn't call [`Networks::refresh_list`] beforehand, this method will do nothing - /// as the network list will be empty. + /// Refreshes the network interfaces. /// /// ```no_run /// use sysinfo::Networks; /// /// let mut networks = Networks::new_with_refreshed_list(); /// // Wait some time...? Then refresh the data of each network. - /// networks.refresh(); + /// networks.refresh(true); /// ``` - pub fn refresh(&mut self) { - self.inner.refresh() + pub fn refresh(&mut self, remove_not_listed_interfaces: bool) { + self.inner.refresh(remove_not_listed_interfaces) } } @@ -156,7 +136,7 @@ impl NetworkData { /// // Waiting a bit to get data from network... /// thread::sleep(time::Duration::from_millis(10)); /// // Refreshing again to generate diff. - /// networks.refresh(); + /// networks.refresh(true); /// /// for (interface_name, network) in &networks { /// println!("in: {} B", network.received()); @@ -196,7 +176,7 @@ impl NetworkData { /// // Waiting a bit to get data from network... /// thread::sleep(time::Duration::from_millis(10)); /// // Refreshing again to generate diff. - /// networks.refresh(); + /// networks.refresh(true); /// /// for (interface_name, network) in &networks { /// println!("out: {} B", network.transmitted()); @@ -236,7 +216,7 @@ impl NetworkData { /// // Waiting a bit to get data from network... /// thread::sleep(time::Duration::from_millis(10)); /// // Refreshing again to generate diff. - /// networks.refresh(); + /// networks.refresh(true); /// /// for (interface_name, network) in &networks { /// println!("in: {}", network.packets_received()); @@ -276,7 +256,7 @@ impl NetworkData { /// // Waiting a bit to get data from network... /// thread::sleep(time::Duration::from_millis(10)); /// // Refreshing again to generate diff. - /// networks.refresh(); + /// networks.refresh(true); /// /// for (interface_name, network) in &networks { /// println!("out: {}", network.packets_transmitted()); @@ -316,7 +296,7 @@ impl NetworkData { /// // Waiting a bit to get data from network... /// thread::sleep(time::Duration::from_millis(10)); /// // Refreshing again to generate diff. - /// networks.refresh(); + /// networks.refresh(true); /// /// for (interface_name, network) in &networks { /// println!("in: {}", network.errors_on_received()); @@ -356,7 +336,7 @@ impl NetworkData { /// // Waiting a bit to get data from network... /// thread::sleep(time::Duration::from_millis(10)); /// // Refreshing again to generate diff. - /// networks.refresh(); + /// networks.refresh(true); /// /// for (interface_name, network) in &networks { /// println!("out: {}", network.errors_on_transmitted()); diff --git a/src/sysinfo.h b/src/sysinfo.h index 20a9832e3..01f6ce368 100644 --- a/src/sysinfo.h +++ b/src/sysinfo.h @@ -33,7 +33,6 @@ void sysinfo_refresh_process(CSystem system, PID pid); CDisks sysinfo_disks_init(void); void sysinfo_disks_destroy(CDisks disks); void sysinfo_disks_refresh(CDisks disks); -void sysinfo_disks_refresh_list(CDisks disks); size_t sysinfo_total_memory(CSystem system); size_t sysinfo_free_memory(CSystem system); @@ -57,7 +56,6 @@ size_t sysinfo_process_virtual_memory(CProcess process); RString sysinfo_process_executable_path(CProcess process); RString sysinfo_process_root_directory(CProcess process); RString sysinfo_process_current_directory(CProcess process); -void sysinfo_networks_refresh_list(CNetworks networks); void sysinfo_networks_refresh(CNetworks networks); size_t sysinfo_networks_received(CNetworks networks); size_t sysinfo_networks_transmitted(CNetworks networks); diff --git a/src/unix/apple/disk.rs b/src/unix/apple/disk.rs index 812f3637a..bedc1504e 100644 --- a/src/unix/apple/disk.rs +++ b/src/unix/apple/disk.rs @@ -178,7 +178,7 @@ impl crate::DisksInner { } } - pub(crate) fn refresh_list_specifics( + pub(crate) fn refresh_specifics( &mut self, remove_not_listed_disks: bool, refresh_kind: DiskRefreshKind, @@ -187,14 +187,22 @@ impl crate::DisksInner { // SAFETY: We don't keep any Objective-C objects around because we // don't make any direct Objective-C calls in this code. with_autorelease(|| { - get_list(&mut self.disks, remove_not_listed_disks, refresh_kind); + get_list(&mut self.disks, refresh_kind); }) } - } - pub(crate) fn refresh_specifics(&mut self, refresh_kind: DiskRefreshKind) { - for disk in self.list_mut() { - disk.refresh_specifics(refresh_kind); + if remove_not_listed_disks { + self.disks.retain_mut(|disk| { + if !disk.inner.updated { + return false; + } + disk.inner.updated = false; + true + }); + } else { + for c in self.disks.iter_mut() { + c.inner.updated = false; + } } } @@ -207,11 +215,7 @@ impl crate::DisksInner { } } -unsafe fn get_list( - container: &mut Vec, - remove_not_listed_disks: bool, - refresh_kind: DiskRefreshKind, -) { +unsafe fn get_list(container: &mut Vec, refresh_kind: DiskRefreshKind) { let raw_disks = { let count = libc::getfsstat(ptr::null_mut(), 0, libc::MNT_NOWAIT); if count < 1 { @@ -329,20 +333,6 @@ unsafe fn get_list( container.push(disk); } } - - if remove_not_listed_disks { - container.retain_mut(|disk| { - if !disk.inner.updated { - return false; - } - disk.inner.updated = false; - true - }); - } else { - for c in container.iter_mut() { - c.inner.updated = false; - } - } } type RetainedCFArray = CFReleaser; @@ -499,6 +489,7 @@ unsafe fn new_disk( (None, None) }; + // We update the existing disk here to prevent having another call to get `storage` info. if let Some(disk) = disk { let disk = &mut disk.inner; if let Some(total_space) = total_space { @@ -508,6 +499,7 @@ unsafe fn new_disk( disk.available_space = available_space; } disk.refresh_io(refresh_kind); + disk.refresh_kind(refresh_kind); disk.updated = true; return None; } diff --git a/src/unix/apple/network.rs b/src/unix/apple/network.rs index 802cde412..e1a3b7164 100644 --- a/src/unix/apple/network.rs +++ b/src/unix/apple/network.rs @@ -79,22 +79,23 @@ impl NetworksInner { &self.interfaces } - pub(crate) fn refresh_list(&mut self) { - for (_, data) in self.interfaces.iter_mut() { - data.inner.updated = false; + pub(crate) fn refresh(&mut self, remove_not_listed_interfaces: bool) { + self.update_networks(); + if remove_not_listed_interfaces { + self.interfaces.retain(|_, i| { + if !i.inner.updated { + return false; + } + i.inner.updated = false; + true + }); } - self.update_networks(true); - self.interfaces.retain(|_, data| data.inner.updated); refresh_networks_addresses(&mut self.interfaces); } - pub(crate) fn refresh(&mut self) { - self.update_networks(false); - } - #[allow(clippy::cast_ptr_alignment)] #[allow(clippy::uninit_vec)] - fn update_networks(&mut self, insert: bool) { + fn update_networks(&mut self) { let mib = &mut [CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST2, 0]; let mib2 = &mut [ CTL_NET, @@ -193,9 +194,6 @@ impl NetworksInner { interface.updated = true; } hash_map::Entry::Vacant(e) => { - if !insert { - continue; - } let current_in; let current_out; let packets_in; diff --git a/src/unix/freebsd/disk.rs b/src/unix/freebsd/disk.rs index 080434498..4aff58ee2 100644 --- a/src/unix/freebsd/disk.rs +++ b/src/unix/freebsd/disk.rs @@ -91,12 +91,12 @@ impl crate::DisksInner { } } - pub(crate) fn refresh_list_specifics( + pub(crate) fn refresh_specifics( &mut self, remove_not_listed_disks: bool, refresh_kind: DiskRefreshKind, ) { - unsafe { get_all_list(&mut self.disks, true, remove_not_listed_disks, refresh_kind) } + unsafe { get_all_list(&mut self.disks, remove_not_listed_disks, refresh_kind) } } pub(crate) fn list(&self) -> &[Disk] { @@ -106,12 +106,6 @@ impl crate::DisksInner { pub(crate) fn list_mut(&mut self) -> &mut [Disk] { &mut self.disks } - - pub(crate) fn refresh_specifics(&mut self, refresh_kind: DiskRefreshKind) { - unsafe { - get_all_list(&mut self.disks, false, false, refresh_kind); - } - } } trait GetValues { @@ -322,7 +316,6 @@ fn get_disks_mapping() -> HashMap { pub unsafe fn get_all_list( container: &mut Vec, - add_new_disks: bool, remove_not_listed_disks: bool, refresh_kind: DiskRefreshKind, ) { @@ -386,7 +379,7 @@ pub unsafe fn get_all_list( // I/O usage is updated for all disks at once at the end. refresh_disk(&mut disk.inner, refresh_kind.without_io_usage()); disk.inner.updated = true; - } else if add_new_disks { + } else { let dev_mount_point = c_buf_to_utf8_str(&fs_info.f_mntfromname).unwrap_or(""); // USB keys and CDs are removable. diff --git a/src/unix/freebsd/network.rs b/src/unix/freebsd/network.rs index b28ea9aa8..eb15898a5 100644 --- a/src/unix/freebsd/network.rs +++ b/src/unix/freebsd/network.rs @@ -29,19 +29,21 @@ impl NetworksInner { &self.interfaces } - pub(crate) fn refresh_list(&mut self) { + pub(crate) fn refresh(&mut self, remove_not_listed_interfaces: bool) { unsafe { self.refresh_interfaces(true); } - // Remove interfaces which are gone. - self.interfaces.retain(|_, n| n.inner.updated); - refresh_networks_addresses(&mut self.interfaces); - } - - pub(crate) fn refresh(&mut self) { - unsafe { - self.refresh_interfaces(false); + if remove_not_listed_interfaces { + // Remove interfaces which are gone. + self.interfaces.retain(|_, i| { + if !i.inner.updated { + return false; + } + i.inner.updated = false; + true + }); } + refresh_networks_addresses(&mut self.interfaces); } unsafe fn refresh_interfaces(&mut self, refresh_all: bool) { diff --git a/src/unix/linux/disk.rs b/src/unix/linux/disk.rs index 2dfb227d8..9d4927279 100644 --- a/src/unix/linux/disk.rs +++ b/src/unix/linux/disk.rs @@ -148,24 +148,29 @@ impl crate::DisksInner { } } - pub(crate) fn refresh_list_specifics( + pub(crate) fn refresh_specifics( &mut self, remove_not_listed_disks: bool, refresh_kind: DiskRefreshKind, ) { get_all_list( &mut self.disks, - remove_not_listed_disks, &get_all_utf8_data("/proc/mounts", 16_385).unwrap_or_default(), refresh_kind, - ) - } + ); - pub(crate) fn refresh_specifics(&mut self, refresh_kind: DiskRefreshKind) { - let procfs_disk_stats = disk_stats(&refresh_kind); - for disk in self.list_mut() { - disk.inner - .efficient_refresh(refresh_kind, &procfs_disk_stats, false); + if remove_not_listed_disks { + self.disks.retain_mut(|disk| { + if !disk.inner.updated { + return false; + } + disk.inner.updated = false; + true + }); + } else { + for c in self.disks.iter_mut() { + c.inner.updated = false; + } } } @@ -329,12 +334,7 @@ fn find_type_for_device_name(device_name: &OsStr) -> DiskKind { } } -fn get_all_list( - container: &mut Vec, - remove_not_listed_disks: bool, - content: &str, - refresh_kind: DiskRefreshKind, -) { +fn get_all_list(container: &mut Vec, content: &str, refresh_kind: DiskRefreshKind) { // The goal of this array is to list all removable devices (the ones whose name starts with // "usb-"). let removable_entries = match fs::read_dir("/dev/disk/by-id/") { @@ -421,20 +421,6 @@ fn get_all_list( refresh_kind, )); } - - if remove_not_listed_disks { - container.retain_mut(|disk| { - if !disk.inner.updated { - return false; - } - disk.inner.updated = false; - true - }); - } else { - for c in container.iter_mut() { - c.inner.updated = false; - } - } } /// Disk IO stat information from `/proc/diskstats` file. diff --git a/src/unix/linux/network.rs b/src/unix/linux/network.rs index 6f3208aac..d36190498 100644 --- a/src/unix/linux/network.rs +++ b/src/unix/linux/network.rs @@ -40,6 +40,7 @@ fn read>(parent: P, path: &str, data: &mut Vec) -> u64 { fn refresh_networks_list_from_sysfs( interfaces: &mut HashMap, + remove_not_listed_interfaces: bool, sysfs_net: &Path, ) { if let Ok(dir) = std::fs::read_dir(sysfs_net) { @@ -112,9 +113,18 @@ fn refresh_networks_list_from_sysfs( } }; } - + } + // We do this here because `refresh_networks_list_remove_interface` test is checking that + // this is working as expected. + if remove_not_listed_interfaces { // Remove interfaces which are gone. - interfaces.retain(|_, d| d.inner.updated); + interfaces.retain(|_, i| { + if !i.inner.updated { + return false; + } + i.inner.updated = false; + true + }); } } @@ -133,16 +143,12 @@ impl NetworksInner { &self.interfaces } - pub(crate) fn refresh(&mut self) { - let mut v = vec![0; 30]; - - for (interface_name, data) in self.interfaces.iter_mut() { - data.inner.update(interface_name, &mut v); - } - } - - pub(crate) fn refresh_list(&mut self) { - refresh_networks_list_from_sysfs(&mut self.interfaces, Path::new("/sys/class/net/")); + pub(crate) fn refresh(&mut self, remove_not_listed_interfaces: bool) { + refresh_networks_list_from_sysfs( + &mut self.interfaces, + remove_not_listed_interfaces, + Path::new("/sys/class/net/"), + ); refresh_networks_addresses(&mut self.interfaces); } } @@ -188,53 +194,6 @@ pub(crate) struct NetworkDataInner { } impl NetworkDataInner { - fn update(&mut self, path: &str, data: &mut Vec) { - let path = &Path::new("/sys/class/net/").join(path).join("statistics"); - old_and_new!(self, rx_bytes, old_rx_bytes, read(path, "rx_bytes", data)); - old_and_new!(self, tx_bytes, old_tx_bytes, read(path, "tx_bytes", data)); - old_and_new!( - self, - rx_packets, - old_rx_packets, - read(path, "rx_packets", data) - ); - old_and_new!( - self, - tx_packets, - old_tx_packets, - read(path, "tx_packets", data) - ); - old_and_new!( - self, - rx_errors, - old_rx_errors, - read(path, "rx_errors", data) - ); - old_and_new!( - self, - tx_errors, - old_tx_errors, - read(path, "tx_errors", data) - ); - // old_and_new!( - // self, - // rx_compressed, - // old_rx_compressed, - // read(path, "rx_compressed", data) - // ); - // old_and_new!( - // self, - // tx_compressed, - // old_tx_compressed, - // read(path, "tx_compressed", data) - // ); - let mtu_path = &Path::new("/sys/class/net/").join(path); - let mtu = read(mtu_path, "mtu", data); - if self.mtu != mtu { - self.mtu = mtu; - } - } - pub(crate) fn received(&self) -> u64 { self.rx_bytes.saturating_sub(self.old_rx_bytes) } @@ -310,12 +269,12 @@ mod test { let mut interfaces = HashMap::new(); - refresh_networks_list_from_sysfs(&mut interfaces, sys_net_dir.path()); + refresh_networks_list_from_sysfs(&mut interfaces, false, sys_net_dir.path()); assert_eq!(interfaces.keys().collect::>(), ["itf1"]); fs::create_dir(sys_net_dir.path().join("itf2")).expect("failed to create subdirectory"); - refresh_networks_list_from_sysfs(&mut interfaces, sys_net_dir.path()); + refresh_networks_list_from_sysfs(&mut interfaces, false, sys_net_dir.path()); let mut itf_names: Vec = interfaces.keys().map(|n| n.to_owned()).collect(); itf_names.sort(); assert_eq!(itf_names, ["itf1", "itf2"]); @@ -332,14 +291,14 @@ mod test { let mut interfaces = HashMap::new(); - refresh_networks_list_from_sysfs(&mut interfaces, sys_net_dir.path()); + refresh_networks_list_from_sysfs(&mut interfaces, false, sys_net_dir.path()); let mut itf_names: Vec = interfaces.keys().map(|n| n.to_owned()).collect(); itf_names.sort(); assert_eq!(itf_names, ["itf1", "itf2"]); fs::remove_dir(&itf1_dir).expect("failed to remove subdirectory"); - refresh_networks_list_from_sysfs(&mut interfaces, sys_net_dir.path()); + refresh_networks_list_from_sysfs(&mut interfaces, true, sys_net_dir.path()); assert_eq!(interfaces.keys().collect::>(), ["itf2"]); } } diff --git a/src/unknown/disk.rs b/src/unknown/disk.rs index 99bc6a634..a65e2589a 100644 --- a/src/unknown/disk.rs +++ b/src/unknown/disk.rs @@ -65,7 +65,7 @@ impl DisksInner { self.disks } - pub(crate) fn refresh_list_specifics( + pub(crate) fn refresh_specifics( &mut self, _remove_not_listed_disks: bool, _refreshes: DiskRefreshKind, @@ -73,10 +73,6 @@ impl DisksInner { // Does nothing. } - pub(crate) fn refresh_specifics(&mut self, _refreshes: DiskRefreshKind) { - // Does nothing. - } - pub(crate) fn list(&self) -> &[Disk] { &self.disks } diff --git a/src/unknown/network.rs b/src/unknown/network.rs index c76fbd204..b67d4daa7 100644 --- a/src/unknown/network.rs +++ b/src/unknown/network.rs @@ -19,9 +19,7 @@ impl NetworksInner { &self.interfaces } - pub(crate) fn refresh_list(&mut self) {} - - pub(crate) fn refresh(&mut self) {} + pub(crate) fn refresh(&mut self, _remove_not_listed_interfaces: bool) {} } pub(crate) struct NetworkDataInner; diff --git a/src/windows/disk.rs b/src/windows/disk.rs index 46e6606c9..7fe11ef49 100644 --- a/src/windows/disk.rs +++ b/src/windows/disk.rs @@ -232,7 +232,7 @@ impl DisksInner { self.disks } - pub(crate) fn refresh_list_specifics( + pub(crate) fn refresh_specifics( &mut self, remove_not_listed_disks: bool, refreshes: DiskRefreshKind, @@ -242,12 +242,6 @@ impl DisksInner { } } - pub(crate) fn refresh_specifics(&mut self, refreshes: DiskRefreshKind) { - for disk in self.list_mut() { - disk.refresh_specifics(refreshes); - } - } - pub(crate) fn list(&self) -> &[Disk] { &self.disks } diff --git a/src/windows/network.rs b/src/windows/network.rs index c89aee071..386d24526 100644 --- a/src/windows/network.rs +++ b/src/windows/network.rs @@ -5,10 +5,8 @@ use crate::{IpNetwork, MacAddr, NetworkData}; use std::collections::{hash_map, HashMap}; -use windows::Win32::NetworkManagement::IpHelper::{ - FreeMibTable, GetIfEntry2, GetIfTable2, MIB_IF_ROW2, MIB_IF_TABLE2, -}; -use windows::Win32::NetworkManagement::Ndis::{MediaConnectStateDisconnected, NET_LUID_LH}; +use windows::Win32::NetworkManagement::IpHelper::{FreeMibTable, GetIfTable2, MIB_IF_TABLE2}; +use windows::Win32::NetworkManagement::Ndis::MediaConnectStateDisconnected; macro_rules! old_and_new { ($ty_:expr, $name:ident, $old:ident, $new_val:expr) => {{ @@ -32,7 +30,7 @@ impl NetworksInner { &self.interfaces } - pub(crate) fn refresh_list(&mut self) { + pub(crate) fn refresh(&mut self, remove_not_listed_interfaces: bool) { let mut table: *mut MIB_IF_TABLE2 = std::ptr::null_mut(); unsafe { @@ -127,7 +125,6 @@ impl NetworksInner { e.insert(NetworkData { inner: NetworkDataInner { - id: ptr.InterfaceLuid, current_out: ptr.OutOctets, old_out: ptr.OutOctets, current_in: ptr.InOctets, @@ -151,50 +148,22 @@ impl NetworksInner { } FreeMibTable(table as _); } - // Remove interfaces which are gone. - self.interfaces.retain(|_, d| d.inner.updated); - // Refresh all interfaces' addresses. - refresh_networks_addresses(&mut self.interfaces); - } - - pub(crate) fn refresh(&mut self) { - let entry = std::mem::MaybeUninit::::zeroed(); - - unsafe { - let mut entry = entry.assume_init(); - for (_, interface) in self.interfaces.iter_mut() { - let interface = &mut interface.inner; - entry.InterfaceLuid = interface.id; - entry.InterfaceIndex = 0; // to prevent the function to pick this one as index - if GetIfEntry2(&mut entry).is_err() { - continue; - } - old_and_new!(interface, current_out, old_out, entry.OutOctets); - old_and_new!(interface, current_in, old_in, entry.InOctets); - old_and_new!( - interface, - packets_in, - old_packets_in, - entry.InUcastPkts.saturating_add(entry.InNUcastPkts) - ); - old_and_new!( - interface, - packets_out, - old_packets_out, - entry.OutUcastPkts.saturating_add(entry.OutNUcastPkts) - ); - old_and_new!(interface, errors_in, old_errors_in, entry.InErrors); - old_and_new!(interface, errors_out, old_errors_out, entry.OutErrors); - if interface.mtu != entry.Mtu as u64 { - interface.mtu = entry.Mtu as u64 + if remove_not_listed_interfaces { + // Remove interfaces which are gone. + self.interfaces.retain(|_, i| { + if !i.inner.updated { + return false; } - } + i.inner.updated = false; + true + }); } + // Refresh all interfaces' addresses. + refresh_networks_addresses(&mut self.interfaces); } } pub(crate) struct NetworkDataInner { - id: NET_LUID_LH, current_out: u64, old_out: u64, current_in: u64, diff --git a/tests/disk.rs b/tests/disk.rs index 94af64c2a..73ddc16d1 100644 --- a/tests/disk.rs +++ b/tests/disk.rs @@ -21,7 +21,7 @@ fn test_disks() { let mut disks = sysinfo::Disks::new(); assert!(disks.list().is_empty()); - disks.refresh_list(false); + disks.refresh(false); assert!(!disks.list().is_empty()); } @@ -125,7 +125,7 @@ fn test_disk_refresh_kind() { // load with minimal `DiskRefreshKind`, then refresh for added detail should also work! let mut disks = Disks::new_with_refreshed_list_specifics(DiskRefreshKind::nothing()); - disks.refresh_specifics(refreshes); + disks.refresh_specifics(false, refreshes); assertions("incremental", &disks); } } @@ -168,7 +168,7 @@ fn test_disks_usage() { // Wait a bit just in case sleep(std::time::Duration::from_millis(500)); - disks.refresh(); + disks.refresh(false); // Depending on the OS and how disks are configured, the disk usage may be the exact same // across multiple disks. To account for this, collect the disk usages and dedup. diff --git a/tests/network.rs b/tests/network.rs index 3e753d70c..5888a91f9 100644 --- a/tests/network.rs +++ b/tests/network.rs @@ -10,9 +10,7 @@ fn test_networks() { if sysinfo::IS_SUPPORTED_SYSTEM { let mut n = Networks::new(); assert_eq!(n.iter().count(), 0); - n.refresh(); - assert_eq!(n.iter().count(), 0); - n.refresh_list(); + n.refresh(false); assert!(n.iter().count() > 0); } }