diff --git a/src/common/mod.rs b/src/common/mod.rs index acc96013e..d83dd9fe3 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -11,6 +11,46 @@ pub(crate) mod system; #[cfg(feature = "user")] pub(crate) mod user; +/// Type containing read and written bytes. +/// +/// It is returned by [`Process::disk_usage`][crate::Process::disk_usage] and [`Disk::usage`][crate::Disk::usage]. +/// +#[cfg_attr(not(all(feature = "system", feature = "disk")), doc = "```ignore")] +/// ```no_run +/// use sysinfo::{Disks, System}; +/// +/// let s = System::new_all(); +/// for (pid, process) in s.processes() { +/// let disk_usage = process.disk_usage(); +/// println!("[{}] read bytes : new/total => {}/{} B", +/// pid, +/// disk_usage.read_bytes, +/// disk_usage.total_read_bytes, +/// ); +/// println!("[{}] written bytes: new/total => {}/{} B", +/// pid, +/// disk_usage.written_bytes, +/// disk_usage.total_written_bytes, +/// ); +/// } +/// +/// let disks = Disks::new_with_refreshed_list(); +/// for disk in disks.list() { +/// println!("[{:?}] disk usage: {:?}", disk.name(), disk.usage()); +/// } +/// ``` +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd)] +pub struct DiskUsage { + /// Total number of written bytes. + pub total_written_bytes: u64, + /// Number of written bytes since the last refresh. + pub written_bytes: u64, + /// Total number of read bytes. + pub total_read_bytes: u64, + /// Number of read bytes since the last refresh. + pub read_bytes: u64, +} + macro_rules! xid { ($(#[$outer:meta])+ $name:ident, $type:ty $(, $trait:ty)?) => { #[cfg(any(feature = "system", feature = "user"))] diff --git a/src/common/system.rs b/src/common/system.rs index 1409becd1..71c71e5b1 100644 --- a/src/common/system.rs +++ b/src/common/system.rs @@ -6,6 +6,7 @@ use std::fmt; use std::path::Path; use std::str::FromStr; +use crate::common::DiskUsage; use crate::{CpuInner, Gid, ProcessInner, SystemInner, Uid}; /// Structs containing system's information such as processes, memory and CPU. @@ -938,44 +939,6 @@ pub struct CGroupLimits { pub rss: u64, } -/// Type containing read and written bytes. -/// -/// It is returned by [`Process::disk_usage`][crate::Process::disk_usage]. -/// -/// ⚠️ Files might be cached in memory by your OS, meaning that reading/writing them might not -/// increase the `read_bytes`/`written_bytes` values. You can find more information about it -/// in the `proc_pid_io` manual (`man proc_pid_io` on unix platforms). -/// -/// ```no_run -/// use sysinfo::System; -/// -/// let s = System::new_all(); -/// for (pid, process) in s.processes() { -/// let disk_usage = process.disk_usage(); -/// println!("[{}] read bytes : new/total => {}/{} B", -/// pid, -/// disk_usage.read_bytes, -/// disk_usage.total_read_bytes, -/// ); -/// println!("[{}] written bytes: new/total => {}/{} B", -/// pid, -/// disk_usage.written_bytes, -/// disk_usage.total_written_bytes, -/// ); -/// } -/// ``` -#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd)] -pub struct DiskUsage { - /// Total number of written bytes. - pub total_written_bytes: u64, - /// Number of written bytes since the last refresh. - pub written_bytes: u64, - /// Total number of read bytes. - pub total_read_bytes: u64, - /// Number of read bytes since the last refresh. - pub read_bytes: u64, -} - /// Enum describing the different status of a process. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ProcessStatus { diff --git a/src/lib.rs b/src/lib.rs index 4f208e258..9c12c2e6e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,9 +76,9 @@ pub use crate::common::disk::{Disk, DiskKind, Disks}; pub use crate::common::network::{IpNetwork, MacAddr, NetworkData, Networks}; #[cfg(feature = "system")] pub use crate::common::system::{ - get_current_pid, CGroupLimits, Cpu, CpuRefreshKind, DiskUsage, LoadAvg, MemoryRefreshKind, Pid, - Process, ProcessRefreshKind, ProcessStatus, ProcessesToUpdate, RefreshKind, Signal, System, - ThreadKind, UpdateKind, + get_current_pid, CGroupLimits, Cpu, CpuRefreshKind, LoadAvg, MemoryRefreshKind, Pid, Process, + ProcessRefreshKind, ProcessStatus, ProcessesToUpdate, RefreshKind, Signal, System, ThreadKind, + UpdateKind, }; #[cfg(feature = "user")] pub use crate::common::user::{Group, Groups, User, Users}; @@ -87,6 +87,9 @@ pub use crate::common::{Gid, Uid}; #[cfg(feature = "system")] pub use crate::sys::{MINIMUM_CPU_UPDATE_INTERVAL, SUPPORTED_SIGNALS}; +#[cfg(any(feature = "system", feature = "disk"))] +pub use crate::common::DiskUsage; + #[cfg(feature = "user")] pub(crate) use crate::common::user::GroupInner; #[cfg(feature = "user")] diff --git a/src/unix/apple/disk.rs b/src/unix/apple/disk.rs index b80cbd789..2ac36ac17 100644 --- a/src/unix/apple/disk.rs +++ b/src/unix/apple/disk.rs @@ -73,6 +73,7 @@ impl DiskInner { } pub(crate) fn refresh(&mut self) -> bool { + #[cfg(target_os = "macos")] let Some((read_bytes, written_bytes)) = self .bsd_name .as_ref() @@ -81,6 +82,8 @@ impl DiskInner { sysinfo_debug!("Failed to update disk i/o stats"); return false; }; + #[cfg(not(target_os = "macos"))] + let (read_bytes, written_bytes) = (0, 0); self.old_read_bytes = self.read_bytes; self.old_written_bytes = self.written_bytes; diff --git a/src/unix/freebsd/disk.rs b/src/unix/freebsd/disk.rs index c19ce8f8e..b4194e33f 100644 --- a/src/unix/freebsd/disk.rs +++ b/src/unix/freebsd/disk.rs @@ -1,6 +1,6 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use crate::{Disk, DiskKind}; +use crate::{Disk, DiskKind, DiskUsage}; use std::ffi::{OsStr, OsString}; use std::os::unix::ffi::OsStringExt; diff --git a/src/unknown/disk.rs b/src/unknown/disk.rs index 901c3114b..7190547b0 100644 --- a/src/unknown/disk.rs +++ b/src/unknown/disk.rs @@ -1,6 +1,6 @@ // Take a look at the license at the top of the repository in the LICENSE file. -use crate::{Disk, DiskKind}; +use crate::{Disk, DiskKind, DiskUsage}; use std::{ffi::OsStr, path::Path};