sysinfo是用于获取系统信息的板条箱。
它目前支持以下OS(按字母顺序排序):
您仍然可以在非支撑OSS上使用sysinfo ,它将简单地做任何事情并始终返回空值。您可以通过检查[ IS_SUPPORTED_SYSTEM ]常数来直接检查程序。
rustc的最低支持版本为1.74 。
如果您想从旧版本迁移,请随时查看ChangElog和迁移指南。
这就是为什么要保持同一[ System ]的实例,而不是多次重新创建它。
您有一个示例中的examples文件夹。您可以使用cargo run --example simple运行它。
否则,这是一个小的代码示例:
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:?}" ) ;
}请记住,要获得一些最新信息,您需要调用同等的refresh方法。例如,对于CPU使用:
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 ) ;
}默认情况下, sysinfo使用多个线程。但是,这可以增加某些平台上的内存使用量(例如MACOS)。可以通过在Cargo.toml中设置default-features = false来禁用该行为(该行为可以禁用multithread货物功能)。
在大多数情况下,您不希望sysinfo提供的所有信息,而只是其中的一部分。在这种情况下,建议使用refresh_specifics(...)方法,只有您需要更好的性能。
另一个问题经常遇到:除非您知道自己在做什么,否则几乎最好是实例化System结构并通过程序使用此实例。原因是因为许多信息需要先前要计算的措施(例如,CPU使用情况)。另一个示例为什么要好得多:如果您想列出所有运行过程, sysinfo需要为Process结构列表分配所有内存,这在第一次运行中需要很长时间。
如果您的程序需要使用大量文件描述符,则最好使用:
sysinfo :: set_open_files_limit ( 0 ) ;随着sysinfo保持许多文件描述符,在刷新过程时在某些目标上具有更好的性能。
在覆盆子pi上很难建立。一个很好的方法是跨构建,然后将可执行文件发送到覆盆子Pi。
首先安装ARM工具链,例如在Ubuntu上:
> sudo apt-get install gcc-multilib-arm-linux-gnueabihf然后配置货物以使用相应的工具链:
cat << EOF > ~/.cargo/config
[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"
EOF最后,交叉编译:
rustup target add armv7-unknown-linux-gnueabihf
cargo build --target=armv7-unknown-linux-gnueabihf虚拟Linux系统,例如通过Docker和Windows子系统运行的Linux(WSL)的系统,无法通过/sys/class/hwmon或/hwmon或/sys/class/thermal接收主机硬件信息。因此,在虚拟系统上使用此库时,对组件的查询可能不会返回结果(或意外结果)。
苹果对哪些API可以链接到通过App Store分发的二进制文件有限制。默认情况下, sysinfo与这些限制不兼容。您可以使用apple-app-store功能标志来禁用Apple禁止的功能。这也启用了apple-sandbox功能。对于使用App Store外部的Sandbox的应用程序,可以单独使用apple-sandbox功能,以避免在运行时造成违反政策违规行为。
我写了一篇博客文章,您可以在此处找到,其中解释了sysinfo如何提取有关不同系统的信息。
可以直接从C Makefile使用此板条examples/simple.c
要构建C示例,只需运行:
> make
> ./simple
# If needed:
> LD_LIBRARY_PATH=target/debug/ ./simple您可以通过每晚的Rust在本地运行基准:
> cargo bench如果您感谢我的工作并想支持我,则可以与Github赞助商或Patreon一起做。