SystemInfo

Struct SystemInfo 

Source
pub struct SystemInfo { /* private fields */ }
Expand description

Information about the system resources

Implementations§

Source§

impl SystemInfo

Source

pub fn new() -> Self

Create a new SystemInfo by querying actual system resources

Source

pub fn free_memory(&self) -> u64

returns the amount of free memory in bytes

Source

pub fn get_cpu_cores(&self) -> usize

returns the number of cores in the system

Source

pub fn get_cpu_usage(&self) -> f32

Get the current CPU load as a fraction (0.0 to 1.0)

Source

pub fn get_free_memory_mb(&self) -> usize

returns the amount of free memory in MB

Source

pub fn get_ram_memory_usage(&self) -> f32

returns the normalized (0.0 to 1.0) memory usage

Source

pub fn get_current_load(&self) -> f32

Get the current system load as a fraction (0.0 to 1.0)

Source

pub fn memory_usage(&self) -> f32

Get memory usage in bytes

Methods from Deref<Target = System>§

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>( &'a self, name: &'b OsStr, ) -> impl Iterator<Item = &'a Process> + 'b
where '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:

  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".as_ref()) {
    println!("{} {:?}", process.pid(), process.name());
}
Source

pub fn processes_by_exact_name<'a, 'b>( &'a self, name: &'b OsStr, ) -> impl Iterator<Item = &'a Process> + 'b
where '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:

  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".as_ref()) {
    println!("{} {:?}", process.pid(), process.name());
}
Source

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

Trait Implementations§

Source§

impl Clone for SystemInfo

Source§

fn clone(&self) -> SystemInfo

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SystemInfo

Source§

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

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

impl Default for SystemInfo

Source§

fn default() -> Self

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

impl Deref for SystemInfo

Source§

type Target = System

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Display for SystemInfo

Source§

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

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

impl From<System> for SystemInfo

Source§

fn from(system: System) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsWeight<T> for T
where T: Clone + IntoWeight<T>,

Source§

fn as_weight(&self) -> Weight<T>

Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ErrorKind for T
where T: Send + Sync + Debug + Display,

Source§

fn __private__(&self) -> Seal

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<K, S> Identity<K> for S
where S: Borrow<K>, K: Identifier,

Source§

type Item = S

Source§

fn get(&self) -> &<S as Identity<K>>::Item

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> IntoWeight<T> for T

Source§

impl<A, B, C> PercentDifference<B> for A
where A: Clone, B: Sub<A, Output = C>, C: Div<A, Output = C>,

Source§

type Output = C

Source§

fn percent_diff(self, rhs: B) -> <A as PercentDifference<B>>::Output

Computes the percent difference between two values.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<Q> RawState for Q
where Q: Send + Sync + Debug,

Source§

fn __private__(&self) -> Seal

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

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

Performs the conversion.
Source§

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

Source§

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

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

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

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more