• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/H03-May-2022-183169

benches/H03-May-2022-154120

examples/H03-May-2022-522483

src/H03-May-2022-12,7589,127

tests/H03-May-2022-344262

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D01-Jan-197094 66

.gitignoreH A D29-Nov-1973825 4837

ADDING_NEW_PLATFORMS.mdH A D29-Nov-1973144 96

CHANGELOG.mdH A D29-Nov-19737.2 KiB274170

Cargo.lockH A D01-Jan-19707.4 KiB295261

Cargo.tomlH A D01-Jan-19701.9 KiB5649

Cargo.toml.orig-cargoH A D29-Nov-19731.5 KiB4635

LICENSEH A D29-Nov-19731.1 KiB2317

MakefileH A D29-Nov-1973801 4625

README.mdH A D29-Nov-19739 KiB245185

build.rsH A D29-Nov-1973695 2015

README.md

1# sysinfo ![img_github_ci] [![][img_crates]][crates] [![][img_doc]][doc]
2
3`sysinfo` is a crate used to get a system's information.
4
5## Supported Oses
6
7It currently supports the following OSes (alphabetically sorted):
8
9 * Android
10 * iOS
11 * Linux
12 * macOS
13 * Raspberry Pi
14 * Windows
15
16You can still use `sysinfo` on non-supported OSes, it'll simply do nothing and always return
17empty values. You can check in your program directly if an OS is supported by checking the
18[`SystemExt::IS_SUPPORTED`] constant.
19
20The minimum-supported version of `rustc` is **1.54**.
21
22## Usage
23
24⚠️ Before any attempt to read the different structs' information, you need to update them to
25get up-to-date information because for most of them, it works on diff between the current value
26and the old one.
27
28Which is why, it's much better to keep the same instance of [`System`] around instead of
29recreating it multiple times.
30
31You have an example into the `examples` folder. You can run it with `cargo run --example simple`.
32
33Otherwise, here is a little code sample:
34
35```rust
36use sysinfo::{NetworkExt, NetworksExt, ProcessExt, System, SystemExt};
37
38// Please note that we use "new_all" to ensure that all list of
39// components, network interfaces, disks and users are already
40// filled!
41let mut sys = System::new_all();
42
43// First we update all information of our `System` struct.
44sys.refresh_all();
45
46// We display all disks' information:
47println!("=> disks:");
48for disk in sys.disks() {
49    println!("{:?}", disk);
50}
51
52// Network interfaces name, data received and data transmitted:
53println!("=> networks:");
54for (interface_name, data) in sys.networks() {
55    println!("{}: {}/{} B", interface_name, data.received(), data.transmitted());
56}
57
58// Components temperature:
59println!("=> components:");
60for component in sys.components() {
61    println!("{:?}", component);
62}
63
64println!("=> system:");
65// RAM and swap information:
66println!("total memory: {} KB", sys.total_memory());
67println!("used memory : {} KB", sys.used_memory());
68println!("total swap  : {} KB", sys.total_swap());
69println!("used swap   : {} KB", sys.used_swap());
70
71// Display system information:
72println!("System name:             {:?}", sys.name());
73println!("System kernel version:   {:?}", sys.kernel_version());
74println!("System OS version:       {:?}", sys.os_version());
75println!("System host name:        {:?}", sys.host_name());
76
77// Number of processors:
78println!("NB processors: {}", sys.processors().len());
79
80// Display processes ID, name na disk usage:
81for (pid, process) in sys.processes() {
82    println!("[{}] {} {:?}", pid, process.name(), process.disk_usage());
83}
84
85```
86
87By default, `sysinfo` uses multiple threads. However, this can increase the memory usage on some
88platforms (macOS for example). The behavior can be disabled by setting `default-features = false`
89in `Cargo.toml` (which disables the `multithread` cargo feature).
90
91### Running on Raspberry Pi
92
93It'll be difficult to build on Raspberry Pi. A good way-around is to cross-build, then send the
94executable to your Raspberry Pi.
95
96First install the arm toolchain, for example on Ubuntu:
97
98```bash
99> sudo apt-get install gcc-multilib-arm-linux-gnueabihf`
100```
101
102Then configure cargo to use the corresponding toolchain:
103
104```bash
105cat << EOF > ~/.cargo/config
106[target.armv7-unknown-linux-gnueabihf]
107linker = "arm-linux-gnueabihf-gcc"
108EOF
109```
110
111Finally, cross compile:
112
113```bash
114rustup target add armv7-unknown-linux-gnueabihf
115cargo build --target=armv7-unknown-linux-gnueabihf
116```
117
118### Linux on Docker & Windows Subsystem for Linux (WSL)
119
120Virtual Linux systems, such as those run through Docker and Windows Subsystem for Linux (WSL), do
121not receive host hardware information via `/sys/class/hwmon` or `/sys/class/thermal`. As such,
122querying for components may return no results (or unexpected results) when using this library on
123virtual systems.
124
125### Use in binaries running inside the macOS or iOS Sandbox/stores
126
127Apple has restrictions as to which APIs can be linked into binaries that are distributed through the app store.
128By default, `sysinfo` is not compatible with these restrictions. You can use the `apple-app-store`
129feature flag to disable the Apple prohibited features. This also enables the `apple-sandbox` feature.
130In the case of applications using the sandbox outside of the app store, the `apple-sandbox` feature
131can be used alone to avoid causing policy violations at runtime.
132
133### How it works
134
135I wrote a blog post you can find [here][sysinfo-blog] which explains how `sysinfo` extracts information
136on the diffent systems.
137
138[sysinfo-blog]: https://blog.guillaume-gomez.fr/articles/2021-09-06+sysinfo%3A+how+to+extract+systems%27+information
139
140### C interface
141
142It's possible to use this crate directly from C. Take a look at the `Makefile` and at the
143`examples/simple.c` file.
144
145To build the C example, just run:
146
147```bash
148> make
149> ./simple
150# If needed:
151> LD_LIBRARY_PATH=target/release/ ./simple
152```
153
154### Benchmarks
155
156You can run the benchmarks locally with rust **nightly** by doing:
157
158```bash
159> cargo bench
160```
161
162Here are the current results:
163
164**Linux**
165
166<details>
167
168```text
169test bench_new                     ... bench:     182,536 ns/iter (+/- 21,074)
170test bench_new_all                 ... bench:  19,911,714 ns/iter (+/- 1,612,109)
171test bench_refresh_all             ... bench:   5,649,643 ns/iter (+/- 444,129)
172test bench_refresh_components      ... bench:      25,293 ns/iter (+/- 1,748)
173test bench_refresh_components_list ... bench:     382,331 ns/iter (+/- 31,620)
174test bench_refresh_cpu             ... bench:      13,633 ns/iter (+/- 1,135)
175test bench_refresh_disks           ... bench:       2,509 ns/iter (+/- 75)
176test bench_refresh_disks_list      ... bench:      51,488 ns/iter (+/- 5,470)
177test bench_refresh_memory          ... bench:      12,941 ns/iter (+/- 3,023)
178test bench_refresh_networks        ... bench:     256,506 ns/iter (+/- 37,196)
179test bench_refresh_networks_list   ... bench:     266,751 ns/iter (+/- 54,535)
180test bench_refresh_process         ... bench:     117,372 ns/iter (+/- 8,732)
181test bench_refresh_processes       ... bench:   5,125,929 ns/iter (+/- 560,050)
182test bench_refresh_system          ... bench:      52,526 ns/iter (+/- 6,786)
183test bench_refresh_users_list      ... bench:   2,479,582 ns/iter (+/- 1,063,982)
184```
185</details>
186
187**Windows**
188
189<details>
190
191```text
192test bench_new                     ... bench:   7,119,215 ns/iter (+/- 283,002)
193test bench_new_all                 ... bench:  27,364,010 ns/iter (+/- 1,353,879)
194test bench_refresh_all             ... bench:   3,125,085 ns/iter (+/- 92,479)
195test bench_refresh_components      ... bench:   1,239,478 ns/iter (+/- 45,790)
196test bench_refresh_components_list ... bench:   3,197,295 ns/iter (+/- 91,662)
197test bench_refresh_cpu             ... bench:      24,973 ns/iter (+/- 1,844)
198test bench_refresh_disks           ... bench:      52,321 ns/iter (+/- 1,533)
199test bench_refresh_disks_list      ... bench:     114,756 ns/iter (+/- 3,900)
200test bench_refresh_memory          ... bench:         581 ns/iter (+/- 25)
201test bench_refresh_networks        ... bench:      35,231 ns/iter (+/- 2,210)
202test bench_refresh_networks_list   ... bench:     661,170 ns/iter (+/- 56,636)
203test bench_refresh_process         ... bench:       1,531 ns/iter (+/- 154)
204test bench_refresh_processes       ... bench:   1,070,742 ns/iter (+/- 57,539)
205test bench_refresh_system          ... bench:   1,303,291 ns/iter (+/- 44,538)
206test bench_refresh_users_list      ... bench:   2,340,562 ns/iter (+/- 83,992)
207```
208</details>
209
210**macOS**
211
212<details>
213
214```text
215test bench_new                     ... bench:      87,569 ns/iter (+/- 11,078)
216test bench_new_all                 ... bench:  21,445,081 ns/iter (+/- 523,973)
217test bench_refresh_all             ... bench:   1,915,573 ns/iter (+/- 296,132)
218test bench_refresh_components      ... bench:     293,904 ns/iter (+/- 63,492)
219test bench_refresh_components_list ... bench:     894,462 ns/iter (+/- 161,599)
220test bench_refresh_cpu             ... bench:       8,636 ns/iter (+/- 1,244)
221test bench_refresh_disks           ... bench:         937 ns/iter (+/- 97)
222test bench_refresh_disks_list      ... bench:      25,116 ns/iter (+/- 990)
223test bench_refresh_memory          ... bench:       2,172 ns/iter (+/- 67)
224test bench_refresh_networks        ... bench:     183,552 ns/iter (+/- 2,253)
225test bench_refresh_networks_list   ... bench:     183,623 ns/iter (+/- 11,183)
226test bench_refresh_process         ... bench:       5,571 ns/iter (+/- 443)
227test bench_refresh_processes       ... bench:     764,125 ns/iter (+/- 28,568)
228test bench_refresh_system          ... bench:     333,610 ns/iter (+/- 53,204)
229test bench_refresh_users_list      ... bench:  16,816,081 ns/iter (+/- 1,039,374)
230```
231</details>
232
233## Donations
234
235If you appreciate my work and want to support me, you can do it with
236[github sponsors](https://github.com/sponsors/GuillaumeGomez) or with
237[patreon](https://www.patreon.com/GuillaumeGomez).
238
239[img_github_ci]: https://github.com/GuillaumeGomez/sysinfo/workflows/CI/badge.svg
240[img_crates]: https://img.shields.io/crates/v/sysinfo.svg
241[img_doc]: https://img.shields.io/badge/rust-documentation-blue.svg
242
243[crates]: https://crates.io/crates/sysinfo
244[doc]: https://docs.rs/sysinfo/
245