Struct System
pub struct System { /* private fields */ }
dep_sysinfo
only.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§
§impl System
impl System
pub fn new() -> System
pub fn new() -> System
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();
pub fn new_all() -> System
pub fn new_all() -> System
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();
pub fn new_with_specifics(refreshes: RefreshKind) -> System
pub fn new_with_specifics(refreshes: RefreshKind) -> System
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::nothing().with_processes(ProcessRefreshKind::everything()),
);
assert!(!system.processes().is_empty());
pub 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::nothing().with_processes(ProcessRefreshKind::everything()),
);
pub 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();
pub 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();
pub 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::nothing().with_ram());
pub 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::nothing().with_cpu_usage())
.
use sysinfo::System;
let mut s = System::new_all();
// Wait a bit because CPU usage is based on diff.
std::thread::sleep(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL);
// Refresh CPUs again.
s.refresh_cpu_usage();
pub 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::nothing().with_frequency())
.
use sysinfo::System;
let mut s = System::new_all();
s.refresh_cpu_frequency();
pub fn refresh_cpu_list(&mut self, refresh_kind: CpuRefreshKind)
pub fn refresh_cpu_list(&mut self, refresh_kind: CpuRefreshKind)
Refreshes the list of CPU.
Normally, this should almost never be needed as it’s pretty rare for a computer to add a CPU while running, but it’s possible on some computers which shutdown CPU if the load is low enough.
The refresh_kind
argument tells what information you want to be retrieved
for each CPU.
use sysinfo::{CpuRefreshKind, System};
let mut s = System::new_all();
// We already have the list of CPU filled, but we want to recompute it
// in case new CPUs were added.
s.refresh_cpu_list(CpuRefreshKind::everything());
pub fn refresh_cpu_all(&mut self)
pub fn refresh_cpu_all(&mut self)
Refreshes all information related to CPUs information.
If you only want the CPU usage, use System::refresh_cpu_usage
instead.
⚠️ Please note that the result will 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_all();
pub 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());
pub fn refresh_processes(
&mut self,
processes_to_update: ProcessesToUpdate<'_>,
remove_dead_processes: bool,
) -> usize ⓘ
pub fn refresh_processes( &mut self, processes_to_update: ProcessesToUpdate<'_>, remove_dead_processes: bool, ) -> usize ⓘ
Gets all processes and updates their information.
It does the same as:
system.refresh_processes_specifics(
ProcessesToUpdate::All,
true,
ProcessRefreshKind::nothing()
.with_memory()
.with_cpu()
.with_disk_usage()
.with_exe(UpdateKind::OnlyIfNotSet),
);
⚠️ remove_dead_processes
works as follows: if an updated process is dead, then it is
removed. So if you refresh pids 1, 2 and 3. If 2 and 7 are dead, only 2 will be removed
since 7 is not part of the update.
⚠️ On Linux, sysinfo
keeps the stat
files open by default. You can change this behaviour
by using set_open_files_limit
.
Example:
use sysinfo::{ProcessesToUpdate, System};
let mut s = System::new_all();
s.refresh_processes(ProcessesToUpdate::All, true);
pub fn refresh_processes_specifics(
&mut self,
processes_to_update: ProcessesToUpdate<'_>,
remove_dead_processes: bool,
refresh_kind: ProcessRefreshKind,
) -> usize ⓘ
pub fn refresh_processes_specifics( &mut self, processes_to_update: ProcessesToUpdate<'_>, remove_dead_processes: bool, refresh_kind: ProcessRefreshKind, ) -> usize ⓘ
Gets all processes and updates the specified information.
Returns the number of updated processes.
⚠️ remove_dead_processes
works as follows: if an updated process is dead, then it is
removed. So if you refresh pids 1, 2 and 3. If 2 and 7 are dead, only 2 will be removed
since 7 is not part of the update.
⚠️ On Linux, sysinfo
keeps the stat
files open by default. You can change this behaviour
by using set_open_files_limit
.
use sysinfo::{ProcessesToUpdate, ProcessRefreshKind, System};
let mut s = System::new_all();
s.refresh_processes_specifics(
ProcessesToUpdate::All,
true,
ProcessRefreshKind::everything(),
);
pub 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());
}
pub 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());
}
pub fn processes_by_name<'a, 'b>(
&'a self,
name: &'b OsStr,
) -> impl Iterator<Item = &'a Process> + 'bwhere
'a: 'b,
pub fn processes_by_name<'a, 'b>(
&'a self,
name: &'b OsStr,
) -> impl Iterator<Item = &'a Process> + 'bwhere
'a: '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".as_ref()) {
println!("{} {:?}", process.pid(), process.name());
}
pub fn processes_by_exact_name<'a, 'b>(
&'a self,
name: &'b OsStr,
) -> impl Iterator<Item = &'a Process> + 'bwhere
'a: 'b,
pub fn processes_by_exact_name<'a, 'b>(
&'a self,
name: &'b OsStr,
) -> impl Iterator<Item = &'a Process> + 'bwhere
'a: '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".as_ref()) {
println!("{} {:?}", process.pid(), process.name());
}
pub fn global_cpu_usage(&self) -> f32 ⓘ
pub fn global_cpu_usage(&self) -> f32 ⓘ
Returns “global” CPUs usage (aka the addition of all the CPUs).
To have up-to-date information, you need to call System::refresh_cpu_specifics
or
System::refresh_specifics
with cpu
enabled.
use sysinfo::{CpuRefreshKind, RefreshKind, System};
let mut s = System::new_with_specifics(
RefreshKind::nothing().with_cpu(CpuRefreshKind::everything()),
);
// Wait a bit because CPU usage is based on diff.
std::thread::sleep(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL);
// Refresh CPUs again to get actual value.
s.refresh_cpu_usage();
println!("{}%", s.global_cpu_usage());
pub 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_specifics
or
System::refresh_specifics
with cpu
enabled.
use sysinfo::{CpuRefreshKind, RefreshKind, System};
let mut s = System::new_with_specifics(
RefreshKind::nothing().with_cpu(CpuRefreshKind::everything()),
);
// Wait a bit because CPU usage is based on diff.
std::thread::sleep(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL);
// Refresh CPUs again to get actual value.
s.refresh_cpu_usage();
for cpu in s.cpus() {
println!("{}%", cpu.cpu_usage());
}
pub 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());
pub 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
.
pub 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());
pub 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());
pub 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());
pub 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());
pub 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());
pub 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());
pub 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());
pub 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());
pub 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());
pub 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,
);
pub fn name() -> Option<String> ⓘ
pub fn name() -> Option<String> ⓘ
Returns the system name.
example platform | value of System::name() |
---|---|
linux laptop | “Ubuntu” |
android phone | “Pixel 9 Pro” |
apple laptop | “Darwin” |
windows server | “Windows” |
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("OS: {:?}", System::name());
pub fn kernel_version() -> Option<String> ⓘ
pub fn kernel_version() -> Option<String> ⓘ
Returns the system’s kernel version.
example platform | value of System::kernel_version() |
---|---|
linux laptop | “6.8.0-48-generic” |
android phone | “6.1.84-android14-11” |
apple laptop | “24.1.0” |
windows server | “20348” |
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("kernel version: {:?}", System::kernel_version());
pub fn os_version() -> Option<String> ⓘ
pub fn os_version() -> Option<String> ⓘ
Returns the system version (e.g. for macOS this will return 15.1 rather than the kernel version).
example platform | value of System::os_version() |
---|---|
linux laptop | “24.04” |
android phone | “15” |
apple laptop | “15.1.1” |
windows server | “10 (20348)” |
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("OS version: {:?}", System::os_version());
pub fn long_os_version() -> Option<String> ⓘ
pub fn long_os_version() -> Option<String> ⓘ
Returns the system long os version.
example platform | value of System::long_os_version() |
---|---|
linux laptop | “Linux (Ubuntu 24.04)” |
android phone | “Android 15 on Pixel 9 Pro” |
apple laptop | “macOS 15.1.1 Sequoia” |
windows server | “Windows Server 2022 Datacenter” |
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("Long OS Version: {:?}", System::long_os_version());
pub 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
example platform | value of System::distribution_id() |
---|---|
linux laptop | “ubuntu” |
android phone | “android” |
apple laptop | “macos” |
windows server | “windows” |
Important: this information is computed every time this function is called.
use sysinfo::System;
println!("Distribution ID: {:?}", System::distribution_id());
Trait Implementations§
Auto Trait Implementations§
impl Freeze for System
impl RefUnwindSafe for System
impl Send for System
impl Sync for System
impl Unpin for System
impl UnwindSafe for System
Blanket Implementations§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> ByteSized for T
impl<T> ByteSized for T
Source§const BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
Source§fn byte_align(&self) -> usize ⓘ
fn byte_align(&self) -> usize ⓘ
Source§fn ptr_size_ratio(&self) -> [usize; 2]
fn ptr_size_ratio(&self) -> [usize; 2]
Source§impl<T, R> Chain<R> for Twhere
T: ?Sized,
impl<T, R> Chain<R> for Twhere
T: ?Sized,
Source§impl<T> ExtAny for T
impl<T> ExtAny for T
Source§fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
Source§impl<T> ExtMem for Twhere
T: ?Sized,
impl<T> ExtMem for Twhere
T: ?Sized,
Source§const NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
Source§fn mem_align_of_val(&self) -> usize ⓘ
fn mem_align_of_val(&self) -> usize ⓘ
Source§fn mem_size_of_val(&self) -> usize ⓘ
fn mem_size_of_val(&self) -> usize ⓘ
Source§fn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
true
if dropping values of this type matters. Read moreSource§fn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where
Self: Sized,
self
without running its destructor. Read moreSource§fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
Source§unsafe fn mem_zeroed<T>() -> T
unsafe fn mem_zeroed<T>() -> T
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§fn mem_as_bytes(&self) -> &[u8] ⓘ
fn mem_as_bytes(&self) -> &[u8] ⓘ
unsafe_slice
only.§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> Hook for T
impl<T> Hook for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError> ⓘ
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError> ⓘ
§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out
indicating that a T
is niched.