Skip to content

Commit

Permalink
Remove Disks::refresh_list
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 30, 2024
1 parent d7d59c4 commit eb5de0e
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 164 deletions.
11 changes: 1 addition & 10 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(true);
disks.refresh(true);
});
}

Expand Down
2 changes: 1 addition & 1 deletion 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 Down
16 changes: 1 addition & 15 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
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
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
42 changes: 17 additions & 25 deletions src/unix/apple/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
}
}

Expand All @@ -207,11 +215,7 @@ impl crate::DisksInner {
}
}

unsafe fn get_list(
container: &mut Vec<Disk>,
remove_not_listed_disks: bool,
refresh_kind: DiskRefreshKind,
) {
unsafe fn get_list(container: &mut Vec<Disk>, refresh_kind: DiskRefreshKind) {
let raw_disks = {
let count = libc::getfsstat(ptr::null_mut(), 0, libc::MNT_NOWAIT);
if count < 1 {
Expand Down Expand Up @@ -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<core_foundation_sys::array::__CFArray>;
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down
13 changes: 3 additions & 10 deletions src/unix/freebsd/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand All @@ -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 {
Expand Down Expand Up @@ -322,7 +316,6 @@ fn get_disks_mapping() -> HashMap<String, String> {

pub unsafe fn get_all_list(
container: &mut Vec<Disk>,
add_new_disks: bool,
remove_not_listed_disks: bool,
refresh_kind: DiskRefreshKind,
) {
Expand Down Expand Up @@ -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.
Expand Down
44 changes: 15 additions & 29 deletions src/unix/linux/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

Expand Down Expand Up @@ -329,12 +334,7 @@ fn find_type_for_device_name(device_name: &OsStr) -> DiskKind {
}
}

fn get_all_list(
container: &mut Vec<Disk>,
remove_not_listed_disks: bool,
content: &str,
refresh_kind: DiskRefreshKind,
) {
fn get_all_list(container: &mut Vec<Disk>, 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/") {
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 1 addition & 5 deletions src/unknown/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,14 @@ impl DisksInner {
self.disks
}

pub(crate) fn refresh_list_specifics(
pub(crate) fn refresh_specifics(
&mut self,
_remove_not_listed_disks: bool,
_refreshes: DiskRefreshKind,
) {
// Does nothing.
}

pub(crate) fn refresh_specifics(&mut self, _refreshes: DiskRefreshKind) {
// Does nothing.
}

pub(crate) fn list(&self) -> &[Disk] {
&self.disks
}
Expand Down
Loading

0 comments on commit eb5de0e

Please sign in to comment.