From d7d59c4b80431bb89b42b152051f7065c0e03881 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 30 Nov 2024 23:19:58 +0100 Subject: [PATCH] Remove `Networks::refresh_list` --- benches/basic.rs | 14 ++------ examples/simple.c | 2 +- examples/simple.rs | 2 +- src/c_interface.rs | 16 +-------- src/common/network.rs | 46 +++++++----------------- src/unix/apple/network.rs | 24 ++++++------- src/unix/freebsd/network.rs | 20 ++++++----- src/unix/linux/network.rs | 70 ++++++------------------------------- src/unknown/network.rs | 4 +-- src/windows/network.rs | 57 +++++++----------------------- tests/network.rs | 4 +-- 11 files changed, 66 insertions(+), 193 deletions(-) diff --git a/benches/basic.rs b/benches/basic.rs index 6b050fc91..e552ba17a 100644 --- a/benches/basic.rs +++ b/benches/basic.rs @@ -86,7 +86,7 @@ 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_list(true); }); } @@ -96,17 +96,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..f848f0c05 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -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..ae771606f 100644 --- a/src/c_interface.rs +++ b/src/c_interface.rs @@ -248,20 +248,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 +256,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/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/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/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/network.rs b/src/unix/linux/network.rs index 6f3208aac..186899b86 100644 --- a/src/unix/linux/network.rs +++ b/src/unix/linux/network.rs @@ -112,9 +112,6 @@ fn refresh_networks_list_from_sysfs( } }; } - - // Remove interfaces which are gone. - interfaces.retain(|_, d| d.inner.updated); } } @@ -133,16 +130,18 @@ 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) { + pub(crate) fn refresh(&mut self, remove_not_listed_interfaces: bool) { refresh_networks_list_from_sysfs(&mut self.interfaces, Path::new("/sys/class/net/")); + 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); } } @@ -188,53 +187,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) } 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/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/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); } }