pub trait SystemExt:
Sized
+ Debug
+ Default
+ Send
+ Sync {
const IS_SUPPORTED: bool;
const SUPPORTED_SIGNALS: &'static [Signal];
Show 52 methods
// Required methods
fn new_with_specifics(refreshes: RefreshKind) -> Self;
fn refresh_memory(&mut self);
fn refresh_cpu_specifics(&mut self, refresh_kind: CpuRefreshKind);
fn refresh_components_list(&mut self);
fn refresh_processes_specifics(&mut self, refresh_kind: ProcessRefreshKind);
fn refresh_process_specifics(
&mut self,
pid: Pid,
refresh_kind: ProcessRefreshKind,
) -> bool;
fn refresh_disks_list(&mut self);
fn refresh_users_list(&mut self);
fn processes(&self) -> &HashMap<Pid, Process>;
fn process(&self, pid: Pid) -> Option<&Process>;
fn global_cpu_info(&self) -> &Cpu;
fn cpus(&self) -> &[Cpu];
fn physical_core_count(&self) -> Option<usize>;
fn total_memory(&self) -> u64;
fn free_memory(&self) -> u64;
fn available_memory(&self) -> u64;
fn used_memory(&self) -> u64;
fn total_swap(&self) -> u64;
fn free_swap(&self) -> u64;
fn used_swap(&self) -> u64;
fn components(&self) -> &[Component];
fn components_mut(&mut self) -> &mut [Component];
fn users(&self) -> &[User];
fn disks(&self) -> &[Disk];
fn disks_mut(&mut self) -> &mut [Disk];
fn sort_disks_by<F>(&mut self, compare: F)
where F: FnMut(&Disk, &Disk) -> Ordering;
fn networks(&self) -> &Networks;
fn networks_mut(&mut self) -> &mut Networks;
fn uptime(&self) -> u64;
fn boot_time(&self) -> u64;
fn load_average(&self) -> LoadAvg;
fn name(&self) -> Option<String>;
fn kernel_version(&self) -> Option<String>;
fn os_version(&self) -> Option<String>;
fn long_os_version(&self) -> Option<String>;
fn distribution_id(&self) -> String;
fn host_name(&self) -> Option<String>;
// Provided methods
fn new() -> Self { ... }
fn new_all() -> Self { ... }
fn refresh_specifics(&mut self, refreshes: RefreshKind) { ... }
fn refresh_all(&mut self) { ... }
fn refresh_system(&mut self) { ... }
fn refresh_cpu(&mut self) { ... }
fn refresh_components(&mut self) { ... }
fn refresh_processes(&mut self) { ... }
fn refresh_process(&mut self, pid: Pid) -> bool { ... }
fn refresh_disks(&mut self) { ... }
fn refresh_networks(&mut self) { ... }
fn refresh_networks_list(&mut self) { ... }
fn processes_by_name<'a>(
&'a self,
name: &'a str,
) -> Box<dyn Iterator<Item = &'a Process> + 'a> { ... }
fn processes_by_exact_name<'a>(
&'a self,
name: &'a str,
) -> Box<dyn Iterator<Item = &'a Process> + 'a> { ... }
fn get_user_by_id(&self, user_id: &Uid) -> Option<&User> { ... }
}
Expand description
Contains all the methods of the System
type.
Required Associated Constants§
sourceconst IS_SUPPORTED: bool
const IS_SUPPORTED: bool
Returns true
if this OS is supported. Please refer to the
crate-level documentation to get the list of supported OSes.
use sysinfo::{System, SystemExt};
if System::IS_SUPPORTED {
println!("This OS is supported!");
} else {
println!("This OS isn't supported (yet?).");
}
sourceconst SUPPORTED_SIGNALS: &'static [Signal]
const SUPPORTED_SIGNALS: &'static [Signal]
Returns the list of the supported signals on this system (used by
ProcessExt::kill_with
).
use sysinfo::{System, SystemExt};
println!("supported signals: {:?}", System::SUPPORTED_SIGNALS);
Required Methods§
sourcefn new_with_specifics(refreshes: RefreshKind) -> Self
fn new_with_specifics(refreshes: RefreshKind) -> Self
Creates a new System
instance and refresh the data corresponding to the
given RefreshKind
.
use sysinfo::{RefreshKind, System, SystemExt};
// We want everything except disks.
let mut system = System::new_with_specifics(RefreshKind::everything().without_disks_list());
assert_eq!(system.disks().len(), 0);
assert!(system.processes().len() > 0);
// If you want the disks list afterwards, just call the corresponding
// "refresh_disks_list":
system.refresh_disks_list();
let disks = system.disks();
sourcefn refresh_memory(&mut self)
fn refresh_memory(&mut self)
Refreshes RAM and SWAP usage.
use sysinfo::{System, SystemExt};
let mut s = System::new_all();
s.refresh_memory();
sourcefn refresh_cpu_specifics(&mut self, refresh_kind: CpuRefreshKind)
fn refresh_cpu_specifics(&mut self, refresh_kind: CpuRefreshKind)
Refreshes CPUs specific information.
Please note that it doesn’t recompute disks list, components list, network interfaces list nor users list.
use sysinfo::{System, SystemExt, CpuRefreshKind};
let mut s = System::new_all();
s.refresh_cpu_specifics(CpuRefreshKind::everything());
sourcefn refresh_components_list(&mut self)
fn refresh_components_list(&mut self)
Refreshes components list.
use sysinfo::{System, SystemExt};
let mut s = System::new();
s.refresh_components_list();
sourcefn refresh_processes_specifics(&mut self, refresh_kind: ProcessRefreshKind)
fn refresh_processes_specifics(&mut self, refresh_kind: ProcessRefreshKind)
Gets all processes and updates the specified information.
⚠️ On Linux, sysinfo
keeps the stat
files open by default. You can change this behaviour
by using set_open_files_limit
.
use sysinfo::{ProcessRefreshKind, System, SystemExt};
let mut s = System::new_all();
s.refresh_processes_specifics(ProcessRefreshKind::new());
sourcefn refresh_process_specifics(
&mut self,
pid: Pid,
refresh_kind: ProcessRefreshKind,
) -> bool
fn refresh_process_specifics( &mut self, pid: Pid, refresh_kind: ProcessRefreshKind, ) -> bool
Refreshes only the process corresponding to pid
. Returns false
if the process doesn’t
exist (it will NOT be removed from the processes if it doesn’t exist anymore). If it
isn’t listed yet, it’ll be added.
use sysinfo::{Pid, ProcessRefreshKind, System, SystemExt};
let mut s = System::new_all();
s.refresh_process_specifics(Pid::from(1337), ProcessRefreshKind::new());
sourcefn refresh_disks_list(&mut self)
fn refresh_disks_list(&mut self)
The disk list will be emptied then completely recomputed.
§Linux
⚠️ On linux, the NFS file
systems are ignored and the information of a mounted NFS cannot be obtained
via SystemExt::refresh_disks_list
. This is due to the fact that I/O function
statvfs
used by SystemExt::refresh_disks_list
is blocking and
may hang in some cases,
requiring to call systemctl stop
to terminate the NFS service from the remote
server in some cases.
use sysinfo::{System, SystemExt};
let mut s = System::new_all();
s.refresh_disks_list();
sourcefn refresh_users_list(&mut self)
fn refresh_users_list(&mut self)
Refreshes users list.
use sysinfo::{System, SystemExt};
let mut s = System::new_all();
s.refresh_users_list();
sourcefn processes(&self) -> &HashMap<Pid, Process>
fn processes(&self) -> &HashMap<Pid, Process>
Returns the process list.
use sysinfo::{ProcessExt, System, SystemExt};
let s = System::new_all();
for (pid, process) in s.processes() {
println!("{} {}", pid, process.name());
}
sourcefn process(&self, pid: Pid) -> Option<&Process>
fn process(&self, pid: Pid) -> Option<&Process>
Returns the process corresponding to the given pid or None
if no such process exists.
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{}", process.name());
}
sourcefn global_cpu_info(&self) -> &Cpu
fn global_cpu_info(&self) -> &Cpu
Returns “global” cpus information (aka the addition of all the CPUs).
To have up-to-date information, you need to call SystemExt::refresh_cpu
or
SystemExt::refresh_specifics
with cpu
enabled.
use sysinfo::{CpuRefreshKind, CpuExt, RefreshKind, System, SystemExt};
let s = System::new_with_specifics(
RefreshKind::new().with_cpu(CpuRefreshKind::everything()),
);
println!("{}%", s.global_cpu_info().cpu_usage());
sourcefn cpus(&self) -> &[Cpu]
fn cpus(&self) -> &[Cpu]
Returns the list of the CPUs.
By default, the list of cpus is empty until you call SystemExt::refresh_cpu
or
SystemExt::refresh_specifics
with cpu
enabled.
use sysinfo::{CpuRefreshKind, CpuExt, RefreshKind, System, SystemExt};
let s = System::new_with_specifics(
RefreshKind::new().with_cpu(CpuRefreshKind::everything()),
);
for cpu in s.cpus() {
println!("{}%", cpu.cpu_usage());
}
sourcefn physical_core_count(&self) -> Option<usize>
fn physical_core_count(&self) -> Option<usize>
Returns the number of physical cores on the CPU or None
if it couldn’t get it.
In case there are multiple CPUs, it will combine the physical core count of all the CPUs.
Important: this information is computed every time this function is called.
use sysinfo::{CpuExt, System, SystemExt};
let s = System::new();
println!("{:?}", s.physical_core_count());
sourcefn total_memory(&self) -> u64
fn total_memory(&self) -> u64
Returns the RAM size in bytes.
use sysinfo::{System, SystemExt};
let s = System::new_all();
println!("{} bytes", s.total_memory());
sourcefn free_memory(&self) -> u64
fn free_memory(&self) -> u64
Returns the amount of free RAM in bytes.
Generally, “free” memory refers to unallocated memory whereas “available” memory refers to memory that is available for (re)use.
Side note: Windows doesn’t report “free” memory so this method returns the same value
as get_available_memory
.
use sysinfo::{System, SystemExt};
let s = System::new_all();
println!("{} bytes", s.free_memory());
sourcefn available_memory(&self) -> u64
fn available_memory(&self) -> u64
Returns the amount of available RAM in bytes.
Generally, “free” memory refers to unallocated memory whereas “available” memory refers to memory that is available for (re)use.
⚠️ Windows and FreeBSD don’t report “available” memory so SystemExt::free_memory
returns the same value as this method.
use sysinfo::{System, SystemExt};
let s = System::new_all();
println!("{} bytes", s.available_memory());
sourcefn used_memory(&self) -> u64
fn used_memory(&self) -> u64
Returns the amount of used RAM in bytes.
use sysinfo::{System, SystemExt};
let s = System::new_all();
println!("{} bytes", s.used_memory());
sourcefn total_swap(&self) -> u64
fn total_swap(&self) -> u64
Returns the SWAP size in bytes.
use sysinfo::{System, SystemExt};
let s = System::new_all();
println!("{} bytes", s.total_swap());
sourcefn free_swap(&self) -> u64
fn free_swap(&self) -> u64
Returns the amount of free SWAP in bytes.
use sysinfo::{System, SystemExt};
let s = System::new_all();
println!("{} bytes", s.free_swap());
sourcefn used_swap(&self) -> u64
fn used_swap(&self) -> u64
Returns the amount of used SWAP in bytes.
use sysinfo::{System, SystemExt};
let s = System::new_all();
println!("{} bytes", s.used_swap());
sourcefn components(&self) -> &[Component]
fn components(&self) -> &[Component]
Returns the components list.
use sysinfo::{ComponentExt, System, SystemExt};
let s = System::new_all();
for component in s.components() {
println!("{}: {}°C", component.label(), component.temperature());
}
sourcefn components_mut(&mut self) -> &mut [Component]
fn components_mut(&mut self) -> &mut [Component]
Returns a mutable components list.
use sysinfo::{ComponentExt, System, SystemExt};
let mut s = System::new_all();
for component in s.components_mut() {
component.refresh();
}
sourcefn users(&self) -> &[User]
fn users(&self) -> &[User]
Returns the users list.
use sysinfo::{System, SystemExt, UserExt};
let mut s = System::new_all();
for user in s.users() {
println!("{} is in {} groups", user.name(), user.groups().len());
}
sourcefn disks(&self) -> &[Disk]
fn disks(&self) -> &[Disk]
Returns the disks list.
use sysinfo::{DiskExt, System, SystemExt};
let s = System::new_all();
for disk in s.disks() {
println!("{:?}", disk.name());
}
sourcefn disks_mut(&mut self) -> &mut [Disk]
fn disks_mut(&mut self) -> &mut [Disk]
Returns the disks list.
use sysinfo::{DiskExt, System, SystemExt};
let mut s = System::new_all();
for disk in s.disks_mut() {
disk.refresh();
}
sourcefn sort_disks_by<F>(&mut self, compare: F)
fn sort_disks_by<F>(&mut self, compare: F)
Sort the disk list with the provided callback.
Internally, it is using the slice::sort_unstable_by
function, so please refer to it
for implementation details.
⚠️ If you use SystemExt::refresh_disks_list
, you need to use this method before using
SystemExt::disks
or SystemExt::disks_mut
if you want them to be sorted.
sourcefn networks(&self) -> &Networks
fn networks(&self) -> &Networks
Returns the network interfaces object.
use sysinfo::{NetworkExt, NetworksExt, System, SystemExt};
let s = System::new_all();
let networks = s.networks();
for (interface_name, data) in networks {
println!(
"[{}] in: {}, out: {}",
interface_name,
data.received(),
data.transmitted(),
);
}
sourcefn networks_mut(&mut self) -> &mut Networks
fn networks_mut(&mut self) -> &mut Networks
Returns a mutable access to network interfaces.
use sysinfo::{NetworkExt, NetworksExt, System, SystemExt};
let mut s = System::new_all();
let networks = s.networks_mut();
networks.refresh_networks_list();
sourcefn uptime(&self) -> u64
fn uptime(&self) -> u64
Returns system uptime (in seconds).
use sysinfo::{System, SystemExt};
let s = System::new_all();
println!("System running since {} seconds", s.uptime());
sourcefn boot_time(&self) -> u64
fn boot_time(&self) -> u64
Returns the time (in seconds) when the system booted since UNIX epoch.
use sysinfo::{System, SystemExt};
let s = System::new();
println!("System booted at {} seconds", s.boot_time());
sourcefn load_average(&self) -> LoadAvg
fn load_average(&self) -> LoadAvg
Returns the system load average value.
use sysinfo::{System, SystemExt};
let s = System::new_all();
let load_avg = s.load_average();
println!(
"one minute: {}%, five minutes: {}%, fifteen minutes: {}%",
load_avg.one,
load_avg.five,
load_avg.fifteen,
);
sourcefn name(&self) -> Option<String>
fn name(&self) -> Option<String>
Returns the system name.
Important: this information is computed every time this function is called.
use sysinfo::{System, SystemExt};
let s = System::new();
println!("OS: {:?}", s.name());
sourcefn kernel_version(&self) -> Option<String>
fn kernel_version(&self) -> Option<String>
Returns the system’s kernel version.
Important: this information is computed every time this function is called.
use sysinfo::{System, SystemExt};
let s = System::new();
println!("kernel version: {:?}", s.kernel_version());
sourcefn os_version(&self) -> Option<String>
fn os_version(&self) -> Option<String>
Returns the system version (e.g. for MacOS this will return 11.1 rather than the kernel version).
Important: this information is computed every time this function is called.
use sysinfo::{System, SystemExt};
let s = System::new();
println!("OS version: {:?}", s.os_version());
sourcefn long_os_version(&self) -> Option<String>
fn long_os_version(&self) -> Option<String>
Returns the system long os version (e.g “MacOS 11.2 BigSur”).
Important: this information is computed every time this function is called.
use sysinfo::{System, SystemExt};
let s = System::new();
println!("Long OS Version: {:?}", s.long_os_version());
sourcefn distribution_id(&self) -> String
fn distribution_id(&self) -> String
Returns the distribution id as defined by os-release,
or std::env::consts::OS
.
See also
- https://www.freedesktop.org/software/systemd/man/os-release.html#ID=
- https://doc.rust-lang.org/std/env/consts/constant.OS.html
Important: this information is computed every time this function is called.
use sysinfo::{System, SystemExt};
let s = System::new();
println!("Distribution ID: {:?}", s.distribution_id());
Provided Methods§
sourcefn new() -> Self
fn new() -> Self
Creates a new System
instance with nothing loaded. If you want to
load components, network interfaces or the disks, you’ll have to use the
refresh_*_list
methods. SystemExt::refresh_networks_list
for
example.
Use the refresh_all
method to update its internal information (or any of the refresh_
method).
use sysinfo::{System, SystemExt};
let s = System::new();
sourcefn new_all() -> Self
fn new_all() -> Self
Creates a new System
instance with everything loaded.
It is an equivalent of SystemExt::new_with_specifics
(
RefreshKind::everything
())
.
use sysinfo::{System, SystemExt};
let s = System::new_all();
sourcefn refresh_specifics(&mut self, refreshes: RefreshKind)
fn refresh_specifics(&mut self, refreshes: RefreshKind)
Refreshes according to the given RefreshKind
. It calls the corresponding
“refresh_” methods.
use sysinfo::{ProcessRefreshKind, RefreshKind, System, SystemExt};
let mut s = System::new_all();
// Let's just update networks and processes:
s.refresh_specifics(
RefreshKind::new().with_networks().with_processes(ProcessRefreshKind::everything()),
);
sourcefn refresh_all(&mut self)
fn refresh_all(&mut self)
Refreshes all system, processes, disks and network interfaces information.
Please note that it doesn’t recompute disks list, components list, network interfaces list nor users list.
use sysinfo::{System, SystemExt};
let mut s = System::new_all();
s.refresh_all();
sourcefn refresh_system(&mut self)
fn refresh_system(&mut self)
Refreshes system information (RAM, swap, CPU usage and components’ temperature).
If you want some more specific refreshes, you might be interested into looking at
refresh_memory
, refresh_cpu
and refresh_components
.
use sysinfo::{System, SystemExt};
let mut s = System::new_all();
s.refresh_system();
sourcefn refresh_cpu(&mut self)
fn refresh_cpu(&mut self)
Refreshes CPUs information.
⚠️ Please note that the result will very likely be inaccurate at the first call. You need to call this method at least twice (with a bit of time between each call, like 200ms) to get accurate values as it uses previous results to compute the next value.
Calling this method is the same as calling
refresh_cpu_specifics(CpuRefreshKind::new().with_cpu_usage())
.
use sysinfo::{System, SystemExt};
let mut s = System::new_all();
s.refresh_cpu();
sourcefn refresh_components(&mut self)
fn refresh_components(&mut self)
Refreshes components’ temperature.
use sysinfo::{System, SystemExt};
let mut s = System::new_all();
s.refresh_components();
sourcefn refresh_processes(&mut self)
fn refresh_processes(&mut self)
Gets all processes and updates their information.
It does the same as system.refresh_processes_specifics(ProcessRefreshKind::everything())
.
⚠️ On Linux, sysinfo
keeps the stat
files open by default. You can change this behaviour
by using set_open_files_limit
.
use sysinfo::{System, SystemExt};
let mut s = System::new_all();
s.refresh_processes();
sourcefn refresh_process(&mut self, pid: Pid) -> bool
fn refresh_process(&mut self, pid: Pid) -> bool
Refreshes only the process corresponding to pid
. Returns false
if the process doesn’t
exist (it will NOT be removed from the processes if it doesn’t exist anymore). If it
isn’t listed yet, it’ll be added.
It is the same as calling
sys.refresh_process_specifics(pid, ProcessRefreshKind::everything())
.
use sysinfo::{Pid, System, SystemExt};
let mut s = System::new_all();
s.refresh_process(Pid::from(1337));
sourcefn refresh_disks(&mut self)
fn refresh_disks(&mut self)
Refreshes the listed disks’ information.
use sysinfo::{System, SystemExt};
let mut s = System::new_all();
s.refresh_disks();
sourcefn refresh_networks(&mut self)
fn refresh_networks(&mut self)
Refreshes networks data.
use sysinfo::{System, SystemExt};
let mut s = System::new_all();
s.refresh_networks();
It is a shortcut for:
use sysinfo::{NetworksExt, System, SystemExt};
let mut s = System::new_all();
let networks = s.networks_mut();
networks.refresh();
sourcefn refresh_networks_list(&mut self)
fn refresh_networks_list(&mut self)
The network list will be updated: removing not existing anymore interfaces and adding new ones.
use sysinfo::{System, SystemExt};
let mut s = System::new_all();
s.refresh_networks_list();
This is a shortcut for:
use sysinfo::{NetworksExt, System, SystemExt};
let mut s = System::new_all();
let networks = s.networks_mut();
networks.refresh_networks_list();
sourcefn processes_by_name<'a>(
&'a self,
name: &'a str,
) -> Box<dyn Iterator<Item = &'a Process> + 'a>
fn processes_by_name<'a>( &'a self, name: &'a str, ) -> Box<dyn Iterator<Item = &'a Process> + 'a>
Returns an iterator of process containing the given name
.
If you want only the processes with exactly the given name
, take a look at
SystemExt::processes_by_exact_name
.
⚠️ Important ⚠️
On linux, there are two things to know about processes’ name:
- It is limited to 15 characters.
- It is not always the exe name.
use sysinfo::{ProcessExt, System, SystemExt};
let s = System::new_all();
for process in s.processes_by_name("htop") {
println!("{} {}", process.pid(), process.name());
}
sourcefn processes_by_exact_name<'a>(
&'a self,
name: &'a str,
) -> Box<dyn Iterator<Item = &'a Process> + 'a>
fn processes_by_exact_name<'a>( &'a self, name: &'a str, ) -> Box<dyn Iterator<Item = &'a Process> + 'a>
Returns an iterator of processes with exactly the given name
.
If you instead want the processes containing name
, take a look at
SystemExt::processes_by_name
.
⚠️ Important ⚠️
On linux, there are two things to know about processes’ name:
- It is limited to 15 characters.
- It is not always the exe name.
use sysinfo::{ProcessExt, System, SystemExt};
let s = System::new_all();
for process in s.processes_by_exact_name("htop") {
println!("{} {}", process.pid(), process.name());
}
sourcefn get_user_by_id(&self, user_id: &Uid) -> Option<&User>
fn get_user_by_id(&self, user_id: &Uid) -> Option<&User>
Returns the User
matching the given user_id
.
Important: The user list must be filled before using this method, otherwise it will
always return None
(through the refresh_*
methods).
It is a shorthand for:
let s = System::new_all();
s.users().find(|user| user.id() == user_id);
Full example:
use sysinfo::{Pid, ProcessExt, System, SystemExt};
let mut s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
if let Some(user_id) = process.user_id() {
eprintln!("User for process 1337: {:?}", s.get_user_by_id(user_id));
}
}