Repository: GuillaumeGomez/sysinfo (language: rust)

Issue:
Network: transferred/received isn't actively updating on GNU/Linux
If you like my work, please consider sponsoring me!

**Describe the bug**
Hi there! I'm trying out sysinfo example (taken from the README) and noticed that network interfaces don't show how much data is transferred/received. I have a single (ethernet, enp2s0) interface.
```
=> networks:
enp2s0: 0/0 B
lo: 0/0 B
```
I tried doing a couple of network activities, but these values never got updated. Currently, I'm running Debian 12 (with 6.1.0-17-amd64 kernel).

**To Reproduce**
1. Run a Debian 12 VM.
2. Compile the example file (in README) in that VM.
3. Do some network activities, and execute the binary.

Any ideas? Or am I missing something? 


Discussion / hints:
The example is definitely not great. I should have used [total_received](https://docs.rs/sysinfo/latest/sysinfo/struct.NetworkData.html#method.total_received) and [total_transmitted](https://docs.rs/sysinfo/latest/sysinfo/struct.NetworkData.html#method.total_transmitted) instead...

Are you interested into sending a PR to improve the README example?
> The example is definitely not great. I should have used [total_received](https://docs.rs/sysinfo/latest/sysinfo/struct.NetworkData.html#method.total_received) and [total_transmitted](https://docs.rs/sysinfo/latest/sysinfo/struct.NetworkData.html#method.total_transmitted) instead...

total_{received, transmitted} seems to work fine, thank you!

> Are you interested into sending a PR to improve the README example?

Sure :sweat_smile: 

Curious, why does sysinfo have `transmitted` and `received` functions while there's already `total_transmitted` and `total_received` exist?
Because it allows you to know how much data was received/transmitted since last update without having to store this information yourself.

Fix patch:
diff --git a/README.md b/README.md
index 26244eea6..6ece94bc4 100644
--- a/README.md
+++ b/README.md
@@ -78,11 +78,17 @@ for disk in &disks {
     println!("{disk:?}");
 }
 
-// Network interfaces name, data received and data transmitted:
+// Network interfaces name, total data received and total data transmitted:
 let networks = Networks::new_with_refreshed_list();
 println!("=> networks:");
 for (interface_name, data) in &networks {
-    println!("{interface_name}: {}/{} B", data.received(), data.transmitted());
+    println!(
+        "{interface_name}: {} B (down) / {} B (up)",
+        data.total_received(),
+        data.total_transmitted(),
+    );
+    // If you want the amount of data received/transmitted since last call
+    // to `Networks::refresh`, use `received`/`transmitted`.
 }
 
 // Components temperature:
diff --git a/src/common.rs b/src/common.rs
index 798ab1a93..753b6b0ec 100644
--- a/src/common.rs
+++ b/src/common.rs
@@ -2034,7 +2034,7 @@ impl Networks {
     /// 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 user is added or removed, this method won't take it into account. Use
+    /// ⚠️ 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
@@ -2077,10 +2077,19 @@ pub struct NetworkData {
 impl NetworkData {
     /// Returns the number of received bytes since the last refresh.
     ///
+    /// If you want the total number of bytes received, take a look at the
+    /// [`total_received`](NetworkData::total_received) method.
+    ///
     /// ```no_run
     /// use sysinfo::Networks;
+    /// use std::{thread, time};
+    ///
+    /// let mut networks = Networks::new_with_refreshed_list();
+    /// // Waiting a bit to get data from network...
+    /// thread::sleep(time::Duration::from_millis(10));
+    /// // Refreshing again to generate diff.
+    /// networks.refresh();
     ///
-    /// let networks = Networks::new_with_refreshed_list();
     /// for (interface_name, network) in &networks {
     ///     println!("in: {} B", network.received());
     /// }
@@ -2091,6 +2100,9 @@ impl NetworkData {
 
     /// Returns the total number of received bytes.
     ///
+    /// If you want the amount of received bytes since the last refresh, take a look at the
+    /// [`received`](NetworkData::received) method.
+    ///
     /// ```no_run
     /// use sysinfo::Networks;
     ///
@@ -2105,10 +2117,19 @@ impl NetworkData {
 
     /// Returns the number of transmitted bytes since the last refresh.
     ///
+    /// If you want the total number of bytes transmitted, take a look at the
+    /// [`total_transmitted`](NetworkData::total_transmitted) method.
+    ///
     /// ```no_run
     /// use sysinfo::Networks;
+    /// use std::{thread, time};
+    ///
+    /// let mut networks = Networks::new_with_refreshed_list();
+    /// // Waiting a bit to get data from network...
+    /// thread::sleep(time::Duration::from_millis(10));
+    /// // Refreshing again to generate diff.
+    /// networks.refresh();
     ///
-    /// let networks = Networks::new_with_refreshed_list();
     /// for (interface_name, network) in &networks {
     ///     println!("out: {} B", network.transmitted());
     /// }
@@ -2119,6 +2140,9 @@ impl NetworkData {
 
     /// Returns the total number of transmitted bytes.
     ///
+    /// If you want the amount of transmitted bytes since the last refresh, take a look at the
+    /// [`transmitted`](NetworkData::transmitted) method.
+    ///
     /// ```no_run
     /// use sysinfo::Networks;
     ///
@@ -2133,10 +2157,19 @@ impl NetworkData {
 
     /// Returns the number of incoming packets since the last refresh.
     ///
+    /// If you want the total number of packets received, take a look at the
+    /// [`total_packets_received`](NetworkData::total_packets_received) method.
+    ///
     /// ```no_run
     /// use sysinfo::Networks;
+    /// use std::{thread, time};
+    ///
+    /// let mut networks = Networks::new_with_refreshed_list();
+    /// // Waiting a bit to get data from network...
+    /// thread::sleep(time::Duration::from_millis(10));
+    /// // Refreshing again to generate diff.
+    /// networks.refresh();
     ///
-    /// let networks = Networks::new_with_refreshed_list();
     /// for (interface_name, network) in &networks {
     ///     println!("in: {}", network.packets_received());
     /// }
@@ -2147,6 +2180,9 @@ impl NetworkData {
 
     /// Returns the total number of incoming packets.
     ///
+    /// If you want the amount of received packets since the last refresh, take a look at the
+    /// [`packets_received`](NetworkData::packets_received) method.
+    ///
     /// ```no_run
     /// use sysinfo::Networks;
     ///
@@ -2161,10 +2197,19 @@ impl NetworkData {
 
     /// Returns the number of outcoming packets since the last refresh.
     ///
+    /// If you want the total number of packets transmitted, take a look at the
+    /// [`total_packets_transmitted`](NetworkData::total_packets_transmitted) method.
+    ///
     /// ```no_run
     /// use sysinfo::Networks;
+    /// use std::{thread, time};
+    ///
+    /// let mut networks = Networks::new_with_refreshed_list();
+    /// // Waiting a bit to get data from network...
+    /// thread::sleep(time::Duration::from_millis(10));
+    /// // Refreshing again to generate diff.
+    /// networks.refresh();
     ///
-    /// let networks = Networks::new_with_refreshed_list();
     /// for (interface_name, network) in &networks {
     ///     println!("out: {}", network.packets_transmitted());
     /// }
@@ -2175,6 +2220,9 @@ impl NetworkData {
 
     /// Returns the total number of outcoming packets.
     ///
+    /// If you want the amount of transmitted packets since the last refresh, take a look at the
+    /// [`packets_transmitted`](NetworkData::packets_transmitted) method.
+    ///
     /// ```no_run
     /// use sysinfo::Networks;
     ///
@@ -2189,10 +2237,19 @@ impl NetworkData {
 
     /// Returns the number of incoming errors since the last refresh.
     ///
+    /// If you want the total number of errors on received packets, take a look at the
+    /// [`total_errors_on_received`](NetworkData::total_errors_on_received) method.
+    ///
     /// ```no_run
     /// use sysinfo::Networks;
+    /// use std::{thread, time};
+    ///
+    /// let mut networks = Networks::new_with_refreshed_list();
+    /// // Waiting a bit to get data from network...
+    /// thread::sleep(time::Duration::from_millis(10));
+    /// // Refreshing again to generate diff.
+    /// networks.refresh();
     ///
-    /// let networks = Networks::new_with_refreshed_list();
     /// for (interface_name, network) in &networks {
     ///     println!("in: {}", network.errors_on_received());
     /// }
@@ -2203,6 +2260,9 @@ impl NetworkData {
 
     /// Returns the total number of incoming errors.
     ///
+    /// If you want the amount of errors on received packets since the last refresh, take a look at
+    /// the [`errors_on_received`](NetworkData::errors_on_received) method.
+    ///
     /// ```no_run
     /// use sysinfo::Networks;
     ///
@@ -2217,10 +2277,19 @@ impl NetworkData {
 
     /// Returns the number of outcoming errors since the last refresh.
     ///
+    /// If you want the total number of errors on transmitted packets, take a look at the
+    /// [`total_errors_on_transmitted`](NetworkData::total_errors_on_transmitted) method.
+    ///
     /// ```no_run
     /// use sysinfo::Networks;
+    /// use std::{thread, time};
+    ///
+    /// let mut networks = Networks::new_with_refreshed_list();
+    /// // Waiting a bit to get data from network...
+    /// thread::sleep(time::Duration::from_millis(10));
+    /// // Refreshing again to generate diff.
+    /// networks.refresh();
     ///
-    /// let networks = Networks::new_with_refreshed_list();
     /// for (interface_name, network) in &networks {
     ///     println!("out: {}", network.errors_on_transmitted());
     /// }
@@ -2231,6 +2300,9 @@ impl NetworkData {
 
     /// Returns the total number of outcoming errors.
     ///
+    /// If you want the amount of errors on transmitted packets since the last refresh, take a look at
+    /// the [`errors_on_transmitted`](NetworkData::errors_on_transmitted) method.
+    ///
     /// ```no_run
     /// use sysinfo::Networks;
     ///


Test patch: