Struct sysinfo::System

source ·
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

source

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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:

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

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:

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

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

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

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

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.

source

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

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

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

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

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

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

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

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

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

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

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

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

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

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

pub fn distribution_id() -> 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;

println!("Distribution ID: {:?}", System::distribution_id());
source

pub fn host_name() -> Option<String>

Returns the system hostname based off DNS.

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

use sysinfo::System;

println!("Hostname: {:?}", System::host_name());
source

pub fn cpu_arch() -> Option<String>

Returns the CPU architecture (eg. x86, amd64, aarch64, …).

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

use sysinfo::System;

println!("CPU Archicture: {:?}", System::cpu_arch());

Trait Implementations§

source§

impl Debug for System

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for System

source§

fn default() -> System

Returns the “default value” for a type. Read more
source§

impl Serialize for System

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for System

§

impl Send for System

§

impl Sync for System

§

impl Unpin for System

§

impl UnwindSafe for System

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.