sysinfo

sysinfo is a crate used to get a system's information.
Supported OSes
It currently supports the following OSes (alphabetically sorted):
- Android
- FreeBSD
- NetBSD
- iOS
- Linux
- macOS
- Raspberry Pi
- Windows
You can still compile and use sysinfo on non-supported OSes, it'll simply do nothing.
The minimum-supported version of rustc is 1.95.
Usage
If you want to migrate from an older version, don't hesitate to take a look at the CHANGELOG and at the migration guide.
⚠️ Before any attempt to read the different structs' information, you need to update them to get up-to-date information because for most of them, it works on diff between the current value and the old one.
Which is why, it's much better to keep the same instance of [System] around instead of
recreating it multiple times.
You have an example into the examples folder. You can run it with cargo run --example simple.
Otherwise, here is a little code sample:
use ;
// Please note that we use "new_all" to ensure that all lists of
// CPUs and processes are filled!
println!;
// Display system information:
println!;
println!;
println!;
println!;
if let Ok = new_all
// We display all disks' information:
println!;
if let Ok = new_with_refreshed_list
// Network interfaces name, total data received and total data transmitted:
if let Ok = new_with_refreshed_list
// Components temperature:
if let Ok = new_with_refreshed_list
Please remember that to have some up-to-date information, you need to call the equivalent
refresh method. For example, for the CPU usage:
use System;
let Ok = new else ;
loop
The multithread feature (disabled by default) enables sysinfo to use more than
one thread to query information. It gives a performance boost, however, this can increase
the memory usage on some platforms (macOS for example).
Good practice / Performance tips
Most of the time, you don't want all information provided by sysinfo but just a subset of it.
In this case, it's recommended to use refresh_specifics(...) methods with only what you need
to have much better performance.
Another issue frequently encountered: unless you know what you're doing, it's almost all the
time better to instantiate the System struct once and use this one instance through your
program. The reason is because a lot of information needs a previous measure to be computed
(the CPU usage for example). Another example why it's much better: in case you want to list
all running processes, sysinfo needs to allocate all memory for the Process struct list,
which takes extra time on the first run.
Running on Raspberry Pi
It'll be difficult to build on Raspberry Pi. A good way-around is to cross-build, then send the executable to your Raspberry Pi.
First install the arm toolchain, for example on Ubuntu:
> sudo
Then configure cargo to use the corresponding toolchain:
Finally, cross compile:
Linux on Docker & Windows Subsystem for Linux (WSL)
Virtual Linux systems, such as those run through Docker and Windows Subsystem for Linux (WSL), do
not receive host hardware information via /sys/class/hwmon or /sys/class/thermal. As such,
querying for components may return no results (or unexpected results) when using this library on
virtual systems.
Use in binaries running inside the macOS or iOS Sandbox/stores
Apple has restrictions as to which APIs can be linked into binaries that are distributed through the app store.
By default, sysinfo is not compatible with these restrictions. You can use the apple-app-store
feature flag to disable the Apple prohibited features. This also enables the apple-sandbox feature.
In the case of applications using the sandbox outside of the app store, the apple-sandbox feature
can be used alone to avoid causing policy violations at runtime.
How it works
I wrote a blog post you can find here which explains how sysinfo extracts information
on the different systems.
Running tests
Because we're looking at system information, some tests have a better chance to succeed when there is a limited number of parallel running tests. To ensure they all pass, use:
C interface
It's possible to use this crate directly from C. Take a look at the Makefile and at the
examples/simple.c file.
To build the C example, just run:
> make
> ./simple
# If needed:
> LD_LIBRARY_PATH=target/debug/
Benchmarks
You can run the benchmarks locally with rust nightly by doing:
> cargo
Donations
If you appreciate my work and want to support me, you can do it with github sponsors or with patreon.