sysinfo 는 시스템 정보를 얻는 데 사용되는 상자입니다.
현재 다음 OS를 지원합니다 (알파벳순 정렬) :
지원되지 않는 OS에서 여전히 sysinfo 사용할 수 있습니다. 단순히 아무것도하지 않고 항상 빈 값을 반환합니다. [ IS_SUPPORTED_SYSTEM ] 상수를 확인하여 OS가 지원되는 경우 프로그램을 직접 확인할 수 있습니다.
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 프로세스를 새로 고칠 때 일부 대상에서 더 나은 성능을 갖도록 여러 파일 설명자를 열어 두십시오.
Raspberry Pi를 구축하기가 어려울 것입니다. 좋은 방법은 교차 구축을 한 다음 실행 파일을 Raspberry Pi에 보내는 것입니다.
예를 들어 Ubuntu에서 Arm Toolchain을 설치하십시오.
> 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 Docker 및 WSL (Windows Subsystem for Linux)과 같은 가상 Linux 시스템은 /sys/class/hwmon 또는 /sys/class/thermal 통해 호스트 하드웨어 정보를받지 않습니다. 따라서 구성 요소에 대한 쿼리는 가상 시스템 에서이 라이브러리를 사용할 때 결과 (또는 예기치 않은 결과)를 반환 할 수 없습니다.
Apple은 API를 App Store를 통해 배포하는 바이너리에 어떤 API를 연결할 수 있는지에 대한 제한이 있습니다. 기본적으로 sysinfo 이러한 제한과 호환되지 않습니다. apple-app-store 기능 플래그를 사용하여 Apple 금지 기능을 비활성화 할 수 있습니다. 또한 apple-sandbox 기능도 가능합니다. App Store 외부의 샌드 박스를 사용하는 응용 프로그램의 경우 apple-sandbox 기능을 단독으로 사용하여 런타임에 정책 위반을 유발하지 않을 수 있습니다.
sysinfo 다른 시스템에 대한 정보를 추출하는 방법을 설명하는 블로그 게시물을 작성했습니다.
C에서 직접이 상자를 직접 사용할 수 있습니다. Makefile 과 examples/simple.c 파일을 살펴보십시오.
C 예제를 작성하려면 그냥 실행하십시오.
> make
> ./simple
# If needed:
> LD_LIBRARY_PATH=target/debug/ ./simple벤치 마크를 밤마다 Rust로 로컬로 실행할 수 있습니다.
> cargo bench내 작업에 감사하고 저를 지원하고 싶다면 Github 스폰서 나 Patreon과 함께 할 수 있습니다.