Trait sysinfo::SystemExt

source ·
pub trait SystemExt: Sized + Debug + Default + Send + Sync {
    const IS_SUPPORTED: bool;
    const SUPPORTED_SIGNALS: &'static [Signal];
    const MINIMUM_CPU_UPDATE_INTERVAL: Duration;
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: 'b, 'b>( &'a self, name: &'b str ) -> Box<dyn Iterator<Item = &'a Process> + 'b> { ... } fn processes_by_exact_name<'a: 'b, 'b>( &'a self, name: &'b str ) -> Box<dyn Iterator<Item = &'a Process> + 'b> { ... } fn get_user_by_id(&self, user_id: &Uid) -> Option<&User> { ... }
}
Expand description

Contains all the methods of the System type.

Required Associated Constants§

source

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?).");
}
source

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);
source

const MINIMUM_CPU_UPDATE_INTERVAL: Duration

This is the minimum interval time used internally by sysinfo to refresh the CPU time.

⚠️ This value differs from one OS to another.

Why is this constant even needed?

If refreshed too often, the CPU usage of processes will be 0 whereas on Linux it’ll always be the maximum value (number of CPUs * 100).

Required Methods§

source

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();
source

fn refresh_memory(&mut self)

Refreshes RAM and SWAP usage.

use sysinfo::{System, SystemExt};

let mut s = System::new_all();
s.refresh_memory();
source

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());
source

fn refresh_components_list(&mut self)

Refreshes components list.

use sysinfo::{System, SystemExt};

let mut s = System::new();
s.refresh_components_list();
source

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());
source

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());
source

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();
source

fn refresh_users_list(&mut self)

Refreshes users list.

use sysinfo::{System, SystemExt};

let mut s = System::new_all();
s.refresh_users_list();
source

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());
}
source

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());
}
source

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());
source

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());
}
source

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());
source

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());
source

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());
source

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());
source

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());
source

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());
source

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());
source

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());
source

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());
}
source

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();
}
source

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());
}
source

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());
}
source

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();
}
source

fn sort_disks_by<F>(&mut self, compare: F)where F: FnMut(&Disk, &Disk) -> Ordering,

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.

source

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(),
    );
}
source

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();
source

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());
source

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());
source

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,
);
source

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());
source

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());
source

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());
source

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());
source

fn distribution_id(&self) -> String

Returns the distribution id as defined by os-release, or std::env::consts::OS.

See also

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());
source

fn host_name(&self) -> Option<String>

Returns the system hostname based off DNS

Important: this information is computed every time this function is called.

use sysinfo::{System, SystemExt};

let s = System::new();
println!("Hostname: {:?}", s.host_name());

Provided Methods§

source

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();
source

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();
source

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()),
);
source

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();
source

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();
source

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 200 ms, take a look at SystemExt::MINIMUM_CPU_UPDATE_INTERVAL for more information) to get accurate value 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();
source

fn refresh_components(&mut self)

Refreshes components’ temperature.

use sysinfo::{System, SystemExt};

let mut s = System::new_all();
s.refresh_components();
source

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();
source

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));
source

fn refresh_disks(&mut self)

Refreshes the listed disks’ information.

use sysinfo::{System, SystemExt};

let mut s = System::new_all();
s.refresh_disks();
source

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();
source

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();
source

fn processes_by_name<'a: 'b, 'b>( &'a self, name: &'b str ) -> Box<dyn Iterator<Item = &'a Process> + 'b>

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:

  1. It is limited to 15 characters.
  2. 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());
}
source

fn processes_by_exact_name<'a: 'b, 'b>( &'a self, name: &'b str ) -> Box<dyn Iterator<Item = &'a Process> + 'b>

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:

  1. It is limited to 15 characters.
  2. 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());
}
source

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));
    }
}

Implementors§