AI prompts
base on Cross-platform library to fetch system information # sysinfo [![][img_crates]][crates] [![][img_doc]][doc]
`sysinfo` is a crate used to get a system's information.
## Supported OSes
It currently supports the following OSes (alphabetically sorted):
* Android
* FreeBSD
* iOS
* Linux
* macOS
* Raspberry Pi
* Windows
You can still use `sysinfo` on non-supported OSes, it'll simply do nothing and always return
empty values. You can check in your program directly if an OS is supported by checking the
[`IS_SUPPORTED_SYSTEM`] constant.
The minimum-supported version of `rustc` is **1.74**.
## Usage
If you want to migrate from an older version, don't hesitate to take a look at the
[CHANGELOG](https://github.com/GuillaumeGomez/sysinfo/blob/master/CHANGELOG.md) and at the
[migration guide](https://github.com/GuillaumeGomez/sysinfo/blob/master/migration_guide.md).
⚠️ 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:
```rust
use sysinfo::{
Components, Disks, Networks, System,
};
// Please note that we use "new_all" to ensure that all lists of
// CPUs and processes are filled!
let mut sys = System::new_all();
// First we update all information of our `System` struct.
sys.refresh_all();
println!("=> system:");
// RAM and swap information:
println!("total memory: {} bytes", sys.total_memory());
println!("used memory : {} bytes", sys.used_memory());
println!("total swap : {} bytes", sys.total_swap());
println!("used swap : {} bytes", sys.used_swap());
// Display system information:
println!("System name: {:?}", System::name());
println!("System kernel version: {:?}", System::kernel_version());
println!("System OS version: {:?}", System::os_version());
println!("System host name: {:?}", System::host_name());
// Number of CPUs:
println!("NB CPUs: {}", sys.cpus().len());
// Display processes ID, name na disk usage:
for (pid, process) in sys.processes() {
println!("[{pid}] {:?} {:?}", process.name(), process.disk_usage());
}
// We display all disks' information:
println!("=> disks:");
let disks = Disks::new_with_refreshed_list();
for disk in &disks {
println!("{disk:?}");
}
// Network interfaces name, total data received and total data transmitted:
let networks = Networks::new_with_refreshed_list();
println!("=> networks:");
for (interface_name, data) in &networks {
println!(
"{interface_name}: {} B (down) / {} B (up)",
data.total_received(),
data.total_transmitted(),
);
// If you want the amount of data received/transmitted since last call
// to `Networks::refresh`, use `received`/`transmitted`.
}
// Components temperature:
let components = Components::new_with_refreshed_list();
println!("=> components:");
for component in &components {
println!("{component:?}");
}
```
Please remember that to have some up-to-date information, you need to call the equivalent
`refresh` method. For example, for the CPU usage:
```rust,no_run
use sysinfo::System;
let mut sys = System::new();
loop {
sys.refresh_cpu_usage(); // Refreshing CPU usage.
for cpu in sys.cpus() {
print!("{}% ", cpu.cpu_usage());
}
// Sleeping to let time for the system to run for long
// enough to have useful information.
std::thread::sleep(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL);
}
```
By default, `sysinfo` uses multiple threads. However, this can increase the memory usage on some
platforms (macOS for example). The behavior can be disabled by setting `default-features = false`
in `Cargo.toml` (which disables the `multithread` cargo feature).
### 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 issues 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 quite some time on the first run.
If your program needs to use a lot of file descriptors, you'd better use:
```rust,no_run
sysinfo::set_open_files_limit(0);
```
as `sysinfo` keeps a number of file descriptors open to have better performance on some
targets when refreshing processes.
### 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:
```bash
> sudo apt-get install gcc-multilib-arm-linux-gnueabihf
```
Then configure cargo to use the corresponding toolchain:
```bash
cat << EOF > ~/.cargo/config
[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
EOF
```
Finally, cross compile:
```bash
rustup target add armv7-unknown-linux-gnueabihf
cargo build --target=armv7-unknown-linux-gnueabihf
```
### 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][sysinfo-blog] which explains how `sysinfo` extracts information
on the different systems.
[sysinfo-blog]: https://blog.guillaume-gomez.fr/articles/2021-09-06+sysinfo%3A+how+to+extract+systems%27+information
### 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:
```bash
> make
> ./simple
# If needed:
> LD_LIBRARY_PATH=target/debug/ ./simple
```
### Benchmarks
You can run the benchmarks locally with rust **nightly** by doing:
```bash
> cargo bench
```
## Donations
If you appreciate my work and want to support me, you can do it with
[github sponsors](https://github.com/sponsors/GuillaumeGomez) or with
[patreon](https://www.patreon.com/GuillaumeGomez).
[img_crates]: https://img.shields.io/crates/v/sysinfo.svg
[img_doc]: https://img.shields.io/badge/rust-documentation-blue.svg
[crates]: https://crates.io/crates/sysinfo
[doc]: https://docs.rs/sysinfo/
", Assign "at most 3 tags" to the expected json: {"id":"12322","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"