Skip to content

Commit

Permalink
Remove Networks::refresh_list
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 30, 2024
1 parent f11680c commit d7d59c4
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 193 deletions.
14 changes: 2 additions & 12 deletions benches/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand All @@ -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);
});
}

Expand Down
2 changes: 1 addition & 1 deletion examples/simple.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" => {
Expand Down
16 changes: 1 addition & 15 deletions src/c_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Networks> = 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) {
Expand All @@ -270,7 +256,7 @@ pub extern "C" fn sysinfo_networks_refresh(networks: CNetworks) {
let mut networks: Box<Networks> = Box::from_raw(networks as *mut Networks);
{
let networks: &mut Networks = networks.borrow_mut();
networks.refresh();
networks.refresh(true);
}
let _ = Box::into_raw(networks);
}
Expand Down
46 changes: 13 additions & 33 deletions src/common/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}");
/// }
Expand All @@ -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;
Expand All @@ -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
}

Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
24 changes: 11 additions & 13 deletions src/unix/apple/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 11 additions & 9 deletions src/unix/freebsd/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
70 changes: 11 additions & 59 deletions src/unix/linux/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ fn refresh_networks_list_from_sysfs(
}
};
}

// Remove interfaces which are gone.
interfaces.retain(|_, d| d.inner.updated);
}
}

Expand All @@ -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);
}
}
Expand Down Expand Up @@ -188,53 +187,6 @@ pub(crate) struct NetworkDataInner {
}

impl NetworkDataInner {
fn update(&mut self, path: &str, data: &mut Vec<u8>) {
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)
}
Expand Down
4 changes: 1 addition & 3 deletions src/unknown/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit d7d59c4

Please sign in to comment.