pub trait SystemExt: Sized + Debug + Default + Send + Sync {
const IS_SUPPORTED: bool;
const SUPPORTED_SIGNALS: &'static [Signal];
Show 48 methods
fn new_with_specifics(refreshes: RefreshKind) -> Self;
fn refresh_memory(&mut self);
fn refresh_cpu(&mut self);
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_processor_info(&self) -> &Processor;
fn processors(&self) -> &[Processor];
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 disks(&self) -> &[Disk];
fn users(&self) -> &[User];
fn disks_mut(&mut self) -> &mut [Disk];
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 host_name(&self) -> Option<String>;
fn new() -> Self { ... }
fn new_all() -> Self { ... }
fn refresh_specifics(&mut self, refreshes: RefreshKind) { ... }
fn refresh_system(&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 refresh_all(&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> { ... }
}
Expand description
Contains all the methods of the System
type.
Associated Constants
const 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?).");
}
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
fn 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();
fn 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();
fn refresh_cpu(&mut self)
fn refresh_cpu(&mut self)
Refreshes CPU usage.
use sysinfo::{System, SystemExt};
let mut s = System::new_all();
s.refresh_cpu();
fn 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();
fn 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());
fn 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());
fn refresh_disks_list(&mut self)
fn refresh_disks_list(&mut self)
The disk list will be emptied then completely recomputed.
use sysinfo::{System, SystemExt};
let mut s = System::new_all();
s.refresh_disks_list();
fn 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();
Returns the process list.
use sysinfo::{ProcessExt, System, SystemExt};
let s = System::new_all();
for (pid, process) in s.processes() {
println!("{} {}", pid, process.name());
}
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());
}
fn global_processor_info(&self) -> &Processor
fn global_processor_info(&self) -> &Processor
Returns “global” processors information (aka the addition of all the processors).
To have up-to-date information, you need to call SystemExt::refresh_cpu
or
SystemExt::refresh_specifics
with cpu
enabled.
use sysinfo::{ProcessorExt, RefreshKind, System, SystemExt};
let s = System::new_with_specifics(RefreshKind::new().with_cpu());
println!("{}%", s.global_processor_info().cpu_usage());
fn processors(&self) -> &[Processor]
fn processors(&self) -> &[Processor]
Returns the list of the processors.
By default, the list of processors is empty until you call SystemExt::refresh_cpu
or
SystemExt::refresh_specifics
with cpu
enabled.
use sysinfo::{ProcessorExt, RefreshKind, System, SystemExt};
let s = System::new_with_specifics(RefreshKind::new().with_cpu());
for processor in s.processors() {
println!("{}%", processor.cpu_usage());
}
fn physical_core_count(&self) -> Option<usize>
fn physical_core_count(&self) -> Option<usize>
Returns the number of physical cores on the processor 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::{ProcessorExt, System, SystemExt};
let s = System::new();
println!("{:?}", s.physical_core_count());
fn total_memory(&self) -> u64
fn total_memory(&self) -> u64
Returns the RAM size in KB.
use sysinfo::{System, SystemExt};
let s = System::new_all();
println!("{} KB", s.total_memory());
fn free_memory(&self) -> u64
fn free_memory(&self) -> u64
Returns the amount of free RAM in KB.
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!("{} KB", s.free_memory());
fn available_memory(&self) -> u64
fn available_memory(&self) -> u64
Returns the amount of available RAM in KB.
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!("{} KB", s.available_memory());
fn used_memory(&self) -> u64
fn used_memory(&self) -> u64
Returns the amound of used RAM in KB.
use sysinfo::{System, SystemExt};
let s = System::new_all();
println!("{} KB", s.used_memory());
fn total_swap(&self) -> u64
fn total_swap(&self) -> u64
Returns the SWAP size in KB.
use sysinfo::{System, SystemExt};
let s = System::new_all();
println!("{} KB", s.total_swap());
Returns the amount of free SWAP in KB.
use sysinfo::{System, SystemExt};
let s = System::new_all();
println!("{} KB", s.free_swap());
Returns the amount of used SWAP in KB.
use sysinfo::{System, SystemExt};
let s = System::new_all();
println!("{} KB", s.used_swap());
fn 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());
}
fn 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();
}
Returns the disks list.
use sysinfo::{DiskExt, System, SystemExt};
let s = System::new_all();
for disk in s.disks() {
println!("{:?}", disk.name());
}
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());
}
Returns the disks list.
use sysinfo::{DiskExt, System, SystemExt};
let mut s = System::new_all();
for disk in s.disks_mut() {
disk.refresh();
}
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(),
);
}
fn 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();
Returns system uptime (in seconds).
use sysinfo::{System, SystemExt};
let s = System::new_all();
println!("System running since {} seconds", s.uptime());
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());
fn 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,
);
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());
fn 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());
fn 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());
fn 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());
Provided methods
Creates a new System
instance with nothing loaded except the processors list. 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();
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();
fn 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()),
);
fn 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();
fn 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();
fn 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();
fn 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));
fn 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();
fn 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();
fn 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();
fn 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();
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
.
use sysinfo::{ProcessExt, System, SystemExt};
let s = System::new_all();
for process in s.processes_by_name("htop") {
println!("{} {}", process.pid(), process.name());
}
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
.
use sysinfo::{ProcessExt, System, SystemExt};
let s = System::new_all();
for process in s.processes_by_exact_name("htop") {
println!("{} {}", process.pid(), process.name());
}