Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove some refresh list #1406

Merged
merged 2 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 2 additions & 21 deletions benches/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

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

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
4 changes: 2 additions & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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" => {
Expand All @@ -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
32 changes: 2 additions & 30 deletions src/c_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,7 @@ pub extern "C" fn sysinfo_disks_refresh(disks: CDisks) {
let mut disks: Box<Disks> = 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<Disks> = 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);
}
Expand Down Expand Up @@ -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<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 +242,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
67 changes: 10 additions & 57 deletions src/common/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}");
/// }
Expand Down Expand Up @@ -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};
Expand All @@ -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
}

Expand Down Expand Up @@ -326,69 +325,23 @@ impl Disks {
/// Refreshes the listed disks' information.
///
/// Equivalent to <code>[Disks::refresh_specifics]\([DiskRefreshKind::everything]\())</code>.
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 <code>[Disks::refresh_list_specifics]\([DiskRefreshKind::everything]\())</code>.
///
/// ```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);
}
}

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
2 changes: 0 additions & 2 deletions src/sysinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Loading
Loading