pub struct System { /* private fields */ }
Expand description
Structs containing system’s information such as processes, memory and CPU.
use sysinfo::System;
if sysinfo::IS_SUPPORTED_SYSTEM {
println!("System: {:?}", System::new_all());
} else {
println!("This OS isn't supported (yet?).");
}
Implementations§
source§impl System
impl System
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new System
instance with nothing loaded.
Use one of the refresh methods (like refresh_all
) to update its internal information.
use sysinfo::System;
let s = System::new();
sourcepub fn new_all() -> Self
pub fn new_all() -> Self
Creates a new System
instance with everything loaded.
It is an equivalent of System::new_with_specifics
(
RefreshKind::everything
())
.
use sysinfo::System;
let s = System::new_all();
sourcepub fn new_with_specifics(refreshes: RefreshKind) -> Self
pub fn new_with_specifics(refreshes: RefreshKind) -> Self
Creates a new System
instance and refresh the data corresponding to the
given RefreshKind
.
use sysinfo::{ProcessRefreshKind, RefreshKind, System};
// We want to only refresh processes.
let mut system = System::new_with_specifics(
RefreshKind::new().with_processes(ProcessRefreshKind::everything()),
);
assert!(!system.processes().is_empty());
sourcepub fn refresh_specifics(&mut self, refreshes: RefreshKind)
pub fn refresh_specifics(&mut self, refreshes: RefreshKind)
Refreshes according to the given RefreshKind
. It calls the corresponding
“refresh_” methods.
use sysinfo::{ProcessRefreshKind, RefreshKind, System};
let mut s = System::new_all();
// Let's just update processes:
s.refresh_specifics(
RefreshKind::new().with_processes(ProcessRefreshKind::everything()),
);
sourcepub fn refresh_all(&mut self)
pub fn refresh_all(&mut self)
Refreshes all system and processes information.
It is the same as calling system.refresh_specifics(RefreshKind::everything())
.
Don’t forget to take a look at ProcessRefreshKind::everything
method to see what it
will update for processes more in details.
use sysinfo::System;
let mut s = System::new();
s.refresh_all();
sourcepub fn refresh_memory(&mut self)
pub fn refresh_memory(&mut self)
Refreshes RAM and SWAP usage.
It is the same as calling system.refresh_memory_specifics(MemoryRefreshKind::everything())
.
If you don’t want to refresh both, take a look at System::refresh_memory_specifics
.
use sysinfo::System;
let mut s = System::new();
s.refresh_memory();
sourcepub fn refresh_memory_specifics(&mut self, refresh_kind: MemoryRefreshKind)
pub fn refresh_memory_specifics(&mut self, refresh_kind: MemoryRefreshKind)
Refreshes system memory specific information.
use sysinfo::{MemoryRefreshKind, System};
let mut s = System::new();
s.refresh_memory_specifics(MemoryRefreshKind::new().with_ram());
sourcepub fn refresh_cpu_usage(&mut self)
pub fn refresh_cpu_usage(&mut self)
Refreshes CPUs usage.
⚠️ 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 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
system.refresh_cpu_specifics(CpuRefreshKind::new().with_cpu_usage())
.
use sysinfo::System;
let mut s = System::new_all();
s.refresh_cpu_usage();
sourcepub fn refresh_cpu_frequency(&mut self)
pub fn refresh_cpu_frequency(&mut self)
Refreshes CPUs frequency information.
Calling this method is the same as calling
system.refresh_cpu_specifics(CpuRefreshKind::new().with_frequency())
.
use sysinfo::System;
let mut s = System::new_all();
s.refresh_cpu_frequency();
sourcepub fn refresh_cpu(&mut self)
pub fn refresh_cpu(&mut self)
Refreshes all information related to 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 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
system.refresh_cpu_specifics(CpuRefreshKind::everything())
.
use sysinfo::System;
let mut s = System::new_all();
s.refresh_cpu();
sourcepub fn refresh_cpu_specifics(&mut self, refresh_kind: CpuRefreshKind)
pub fn refresh_cpu_specifics(&mut self, refresh_kind: CpuRefreshKind)
Refreshes CPUs specific information.
use sysinfo::{System, CpuRefreshKind};
let mut s = System::new_all();
s.refresh_cpu_specifics(CpuRefreshKind::everything());
sourcepub fn refresh_processes(&mut self)
pub fn refresh_processes(&mut self)
Gets all processes and updates their information.
It does the same as:
system.refresh_processes_specifics(
ProcessRefreshKind::new()
.with_memory()
.with_cpu()
.with_disk_usage()
.with_exe(UpdateKind::OnlyIfNotSet),
);
⚠️ On Linux, sysinfo
keeps the stat
files open by default. You can change this behaviour
by using set_open_files_limit
.
Example:
use sysinfo::System;
let mut s = System::new_all();
s.refresh_processes();
sourcepub fn refresh_processes_specifics(&mut self, refresh_kind: ProcessRefreshKind)
pub 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};
let mut s = System::new_all();
s.refresh_processes_specifics(ProcessRefreshKind::new());
sourcepub fn refresh_pids(&mut self, pids: &[Pid])
pub fn refresh_pids(&mut self, pids: &[Pid])
Gets specified processes and updates their information.
It does the same as:
system.refresh_pids_specifics(
&[Pid::from(1), Pid::from(2)],
ProcessRefreshKind::new()
.with_memory()
.with_cpu()
.with_disk_usage()
.with_exe(UpdateKind::OnlyIfNotSet),
);
⚠️ On Linux, sysinfo
keeps the stat
files open by default. You can change this behaviour
by using set_open_files_limit
.
Example:
use sysinfo::System;
let mut s = System::new_all();
s.refresh_processes();
sourcepub fn refresh_pids_specifics(
&mut self,
pids: &[Pid],
refresh_kind: ProcessRefreshKind
)
pub fn refresh_pids_specifics( &mut self, pids: &[Pid], refresh_kind: ProcessRefreshKind )
Gets specified 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::{Pid, ProcessRefreshKind, System};
let mut s = System::new_all();
s.refresh_pids_specifics(&[Pid::from(1), Pid::from(2)], ProcessRefreshKind::new());
sourcepub fn refresh_process(&mut self, pid: Pid) -> bool
pub 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:
system.refresh_process_specifics(
pid,
ProcessRefreshKind::new()
.with_memory()
.with_cpu()
.with_disk_usage()
.with_exe(UpdateKind::OnlyIfNotSet),
);
⚠️ On Linux, sysinfo
keeps the stat
files open by default. You can change this behaviour
by using set_open_files_limit
.
Example:
use sysinfo::{Pid, System};
let mut s = System::new_all();
s.refresh_process(Pid::from(1337));
sourcepub fn refresh_process_specifics(
&mut self,
pid: Pid,
refresh_kind: ProcessRefreshKind
) -> bool
pub 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.
⚠️ On Linux, sysinfo
keeps the stat
files open by default. You can change this behaviour
by using set_open_files_limit
.
use sysinfo::{Pid, ProcessRefreshKind, System};
let mut s = System::new_all();
s.refresh_process_specifics(Pid::from(1337), ProcessRefreshKind::new());
sourcepub fn processes(&self) -> &HashMap<Pid, Process>
pub fn processes(&self) -> &HashMap<Pid, Process>
Returns the process list.
use sysinfo::System;
let s = System::new_all();
for (pid, process) in s.processes() {
println!("{} {}", pid, process.name());
}
sourcepub fn process(&self, pid: Pid) -> Option<&Process>
pub 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, System};
let s = System::new_all();
if let Some(process) = s.process(Pid::from(1337)) {
println!("{}", process.name());
}
sourcepub fn processes_by_name<'a: 'b, 'b>(
&'a self,
name: &'b str
) -> impl Iterator<Item = &'a Process> + 'b
pub fn processes_by_name<'a: 'b, 'b>( &'a self, name: &'b str ) -> impl 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
System::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::System;
let s = System::new_all();
for process in s.processes_by_name("htop") {
println!("{} {}", process.pid(), process.name());
}
sourcepub fn processes_by_exact_name<'a: 'b, 'b>(
&'a self,
name: &'b str
) -> impl Iterator<Item = &'a Process> + 'b
pub fn processes_by_exact_name<'a: 'b, 'b>( &'a self, name: &'b str ) -> impl 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
System::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::System;
let s = System::new_all();
for process in s.processes_by_exact_name("htop") {
println!("{} {}", process.pid(), process.name());
}
sourcepub fn global_cpu_info(&self) -> &Cpu
pub 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 System::refresh_cpu
or
System::refresh_specifics
with cpu
enabled.
⚠️ Important ⚠️
Information like Cpu::brand
, Cpu::vendor_id
or Cpu::frequency
are not set on the “global” CPU.
use sysinfo::{CpuRefreshKind, RefreshKind, System};
let s = System::new_with_specifics(
RefreshKind::new().with_cpu(CpuRefreshKind::everything()),
);
println!("{}%", s.global_cpu_info().cpu_usage());
sourcepub fn cpus(&self) -> &[Cpu]
pub fn cpus(&self) -> &[Cpu]
Returns the list of the CPUs.
By default, the list of CPUs is empty until you call System::refresh_cpu
or
System::refresh_specifics
with cpu
enabled.
use sysinfo::{CpuRefreshKind, RefreshKind, System};
let s = System::new_with_specifics(
RefreshKind::new().with_cpu(CpuRefreshKind::everything()),
);
for cpu in s.cpus() {
println!("{}%", cpu.cpu_usage());
}
sourcepub fn physical_core_count(&self) -> Option<usize>
pub 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::System;
let s = System::new();
println!("{:?}", s.physical_core_count());
sourcepub fn total_memory(&self) -> u64
pub fn total_memory(&self) -> u64
Returns the RAM size in bytes.
use sysinfo::System;
let s = System::new_all();
println!("{} bytes", s.total_memory());
On Linux, if you want to see this information with the limit of your cgroup, take a look
at cgroup_limits
.
sourcepub fn free_memory(&self) -> u64
pub 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 available_memory
.
use sysinfo::System;
let s = System::new_all();
println!("{} bytes", s.free_memory());
sourcepub fn available_memory(&self) -> u64
pub 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 System::free_memory
returns the same value as this method.
use sysinfo::System;
let s = System::new_all();
println!("{} bytes", s.available_memory());
sourcepub fn used_memory(&self) -> u64
pub fn used_memory(&self) -> u64
Returns the amount of used RAM in bytes.
use sysinfo::System;
let s = System::new_all();
println!("{} bytes", s.used_memory());
sourcepub fn total_swap(&self) -> u64
pub fn total_swap(&self) -> u64
Returns the SWAP size in bytes.
use sysinfo::System;
let s = System::new_all();
println!("{} bytes", s.total_swap());
sourcepub fn free_swap(&self) -> u64
pub fn free_swap(&self) -> u64
Returns the amount of free SWAP in bytes.
use sysinfo::System;
let s = System::new_all();
println!("{} bytes", s.free_swap());
sourcepub fn used_swap(&self) -> u64
pub fn used_swap(&self) -> u64
Returns the amount of used SWAP in bytes.
use sysinfo::System;
let s = System::new_all();
println!("{} bytes", s.used_swap());
sourcepub fn cgroup_limits(&self) -> Option<CGroupLimits>
pub fn cgroup_limits(&self) -> Option<CGroupLimits>
Retrieves the limits for the current cgroup (if any), otherwise it returns None
.
This information is computed every time the method is called.
⚠️ You need to have run refresh_memory
at least once before
calling this method.
⚠️ This method is only implemented for Linux. It always returns None
for all other
systems.
use sysinfo::System;
let s = System::new_all();
println!("limits: {:?}", s.cgroup_limits());
sourcepub fn uptime() -> u64
pub fn uptime() -> u64
Returns system uptime (in seconds).
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("System running since {} seconds", System::uptime());
sourcepub fn boot_time() -> u64
pub fn boot_time() -> u64
Returns the time (in seconds) when the system booted since UNIX epoch.
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("System booted at {} seconds", System::boot_time());
sourcepub fn load_average() -> LoadAvg
pub fn load_average() -> LoadAvg
Returns the system load average value.
Important: this information is computed every time this function is called.
⚠️ This is currently not working on Windows.
use sysinfo::System;
let load_avg = System::load_average();
println!(
"one minute: {}%, five minutes: {}%, fifteen minutes: {}%",
load_avg.one,
load_avg.five,
load_avg.fifteen,
);
sourcepub fn name() -> Option<String>
pub fn name() -> Option<String>
Returns the system name.
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("OS: {:?}", System::name());
sourcepub fn kernel_version() -> Option<String>
pub fn kernel_version() -> Option<String>
Returns the system’s kernel version.
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("kernel version: {:?}", System::kernel_version());
sourcepub fn os_version() -> Option<String>
pub fn os_version() -> 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;
println!("OS version: {:?}", System::os_version());
sourcepub fn long_os_version() -> Option<String>
pub fn long_os_version() -> 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;
println!("Long OS Version: {:?}", System::long_os_version());
sourcepub fn distribution_id() -> String
pub fn distribution_id() -> 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;
println!("Distribution ID: {:?}", System::distribution_id());