Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement CpuArch #1126

Closed
wants to merge 53 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
fb69eae
#1118 implement CPU arch for Linux, freebsd and Mac.
Nov 2, 2023
0426c4a
Merge pull request #2 from GuillaumeGomez/master
njouanin Nov 2, 2023
95786da
Fix missing arch argument
Nov 2, 2023
ea3311f
Fix clone
Nov 2, 2023
6f477db
Add test
Nov 2, 2023
186828b
Add CpuArch Enum. Fix arch String on Mac / BSD
Nov 2, 2023
de7dd4b
Merge pull request #3 from GuillaumeGomez/master
njouanin Nov 2, 2023
90bacfe
Review + CI fixes
Nov 3, 2023
745731d
Merge branch 'GuillaumeGomez:master' into master
njouanin Nov 3, 2023
68ae8cd
Fix return type
Nov 3, 2023
df252e1
Fix CI
Nov 3, 2023
537350b
Fix CI
Nov 3, 2023
3b85280
Fix clippy error (use map)
Nov 3, 2023
3367d5f
Add missing argument
Nov 3, 2023
4e98412
Remove unnecessary clone()
Nov 3, 2023
c48fa9c
Add CpuArch missing arg
Nov 3, 2023
e5b48ab
Support unknown arch also
Nov 3, 2023
d2ce550
Fix last review
Nov 4, 2023
7a3389b
Update required rust version
Nov 4, 2023
d750282
Fix type to u8 as expected by from_bytes_until_nul
Nov 4, 2023
8a35fba
Fix CI
Nov 4, 2023
32910a0
Fix typo
Nov 4, 2023
9ded99a
Fix CI
Nov 4, 2023
1edea4d
Fix comment typo
GuillaumeGomez Oct 20, 2023
53eac4c
Add missing serde implementations
GuillaumeGomez Oct 21, 2023
53b8b56
Change `Disk::file_system()` to return `&OsStr`
chklauser Oct 20, 2023
b0abbe8
Bump MSRV to 1.65 (#1108)
chklauser Oct 22, 2023
aee8a9d
Fix new clippy lint
GuillaumeGomez Oct 24, 2023
9bf8779
Implement Display for DiskKind
jimmyruann Oct 24, 2023
5cdf44a
Add Sonoma to long OS version
Oct 31, 2023
b1954ee
fixed typos
fritzrehde Oct 30, 2023
9ea2310
Fix rustc "unused import" warning
GuillaumeGomez Oct 30, 2023
92cce04
1118 implement CPU arch for Linux, freebsd and Mac.
Nov 2, 2023
56e3e5e
Merge branch 'master' of github.com:njouanin/sysinfo
Nov 4, 2023
214b887
Move arch info to System
Nov 4, 2023
bbd0ec0
Remove arch stuff from cpu
Nov 4, 2023
575f180
Fix typo
Nov 4, 2023
e309a16
Fix clippy error
Nov 4, 2023
d79a64e
add missing use
Nov 4, 2023
1285ec0
Use String instead of &str
Nov 4, 2023
9b20794
Don't store arch
Nov 5, 2023
3f37140
Remove CpuArch
Nov 5, 2023
224566f
Fix clippy
Nov 5, 2023
2c23922
Merge branch 'GuillaumeGomez:master' into master
njouanin Nov 5, 2023
63196da
Remove unused import
Nov 5, 2023
69a4a81
Fix get_sys_value change
Nov 5, 2023
af60d97
Fix get_sys_value call on FreeBSD
Nov 5, 2023
3242ecc
Fix argument type
Nov 5, 2023
22ed661
Remove unused
Nov 5, 2023
77bda04
Review fix
Nov 5, 2023
78ee171
Move arch info to System
Nov 4, 2023
acde12b
Merge branch 'GuillaumeGomez:master' into master
njouanin Nov 5, 2023
cf03115
Merge branch 'master' of github.com:njouanin/sysinfo
Nov 5, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .cirrus.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
task:
name: rust 1.65 on freebsd 13
name: rust 1.69 on freebsd 13
freebsd_instance:
image: freebsd-13-1-release-amd64
setup_script:
- curl https://sh.rustup.rs -sSf --output rustup.sh
- sh rustup.sh -y --profile=minimal --default-toolchain=1.65
- sh rustup.sh -y --profile=minimal --default-toolchain=1.69
- . $HOME/.cargo/env
- rustup --version
- rustup component add clippy
Expand Down Expand Up @@ -37,14 +37,14 @@ task:
- FREEBSD_CI=1 cargo test --lib -j1 -- --ignored

task:
name: rust 1.65 on mac m1
name: rust 1.69 on mac m1
macos_instance:
image: ghcr.io/cirruslabs/macos-monterey-base:latest
setup_script:
- brew update
- brew install curl
- curl https://sh.rustup.rs -sSf --output rustup.sh
- sh rustup.sh -y --profile=minimal --default-toolchain=1.65
- sh rustup.sh -y --profile=minimal --default-toolchain=1.69
- source $HOME/.cargo/env
- rustup --version
- rustup component add clippy
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
- { os: 'ubuntu-latest', target: 'x86_64-linux-android', cross: true }
- { os: 'ubuntu-latest', target: 'i686-linux-android', cross: true }
toolchain:
- "1.65.0" # minimum supported rust version
- "1.69.0" # minimum supported rust version
- stable
- nightly
steps:
Expand Down Expand Up @@ -139,7 +139,7 @@ jobs:
- macos-latest
- windows-latest
toolchain:
- "1.65.0" # minimum supported rust version
- "1.69.0" # minimum supported rust version
- stable
- nightly
steps:
Expand Down Expand Up @@ -205,7 +205,7 @@ jobs:
strategy:
matrix:
toolchain:
- "1.65.0" # minimum supported rust version
- "1.69.0" # minimum supported rust version
- stable
steps:
- uses: actions/checkout@v2
Expand Down
78 changes: 42 additions & 36 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description = "Library to get system information such as processes, CPUs, disks,
repository = "https://github.com/GuillaumeGomez/sysinfo"
license = "MIT"
readme = "README.md"
rust-version = "1.65"
rust-version = "1.69"
exclude = ["/test-unknown"]
categories = ["filesystem", "os", "api-bindings"]
edition = "2018"
Expand All @@ -28,7 +28,13 @@ unknown-ci = []
features = ["serde"]
# Setting this default target to prevent `freebsd` to be the default one.
default-target = "x86_64-unknown-linux-gnu"
targets = ["i686-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-msvc", "i686-pc-windows-msvc", "x86_64-unknown-freebsd"]
targets = [
"i686-unknown-linux-gnu",
"x86_64-apple-darwin",
"x86_64-pc-windows-msvc",
"i686-pc-windows-msvc",
"x86_64-unknown-freebsd",
]
cargo-args = ["-Zbuild-std"]
rustdoc-args = ["--generate-link-to-definition"]

Expand All @@ -43,40 +49,40 @@ once_cell = "1.18"
[target.'cfg(windows)'.dependencies]
ntapi = "0.4"
windows = { version = "0.51", features = [
"Wdk_System_SystemInformation",
"Wdk_System_SystemServices",
"Wdk_System_Threading",
"Win32_Foundation",
"Win32_NetworkManagement_IpHelper",
"Win32_NetworkManagement_NetManagement",
"Win32_NetworkManagement_Ndis",
"Win32_Networking_WinSock",
"Win32_Security",
"Win32_Security_Authentication_Identity",
"Win32_Security_Authorization",
"Win32_Storage_FileSystem",
"Win32_System_Com",
"Win32_System_Diagnostics_Debug",
"Win32_System_IO",
"Win32_System_Ioctl",
"Win32_System_LibraryLoader",
"Win32_System_Kernel",
"Win32_System_Memory",
"Win32_System_Ole",
"Win32_System_Performance",
"Win32_System_Power",
"Win32_System_ProcessStatus",
"Win32_System_Registry",
"Win32_System_RemoteDesktop",
"Win32_System_Rpc",
"Win32_System_SystemInformation",
"Win32_System_SystemServices",
"Win32_System_Threading",
"Win32_System_Variant",
"Win32_System_WindowsProgramming",
"Win32_System_Wmi",
"Win32_UI_Shell",
]}
"Wdk_System_SystemInformation",
"Wdk_System_SystemServices",
"Wdk_System_Threading",
"Win32_Foundation",
"Win32_NetworkManagement_IpHelper",
"Win32_NetworkManagement_NetManagement",
"Win32_NetworkManagement_Ndis",
"Win32_Networking_WinSock",
"Win32_Security",
"Win32_Security_Authentication_Identity",
"Win32_Security_Authorization",
"Win32_Storage_FileSystem",
"Win32_System_Com",
"Win32_System_Diagnostics_Debug",
"Win32_System_IO",
"Win32_System_Ioctl",
"Win32_System_LibraryLoader",
"Win32_System_Kernel",
"Win32_System_Memory",
"Win32_System_Ole",
"Win32_System_Performance",
"Win32_System_Power",
"Win32_System_ProcessStatus",
"Win32_System_Registry",
"Win32_System_RemoteDesktop",
"Win32_System_Rpc",
"Win32_System_SystemInformation",
"Win32_System_SystemServices",
"Win32_System_Threading",
"Win32_System_Variant",
"Win32_System_WindowsProgramming",
"Win32_System_Wmi",
"Win32_UI_Shell",
] }

[target.'cfg(not(any(target_os = "unknown", target_arch = "wasm32")))'.dependencies]
libc = "^0.2.150"
Expand Down
12 changes: 12 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,18 @@ impl System {
pub fn host_name(&self) -> Option<String> {
self.inner.host_name()
}

/// Returns the CPU architecture (eg. x86, amd64, aarch64, ...)
///
/// ```no_run
/// use sysinfo::System;
///
/// let s = System::new();
/// println!("CPU Archicture: {:?}", s.cpu_arch());
/// ```
pub fn cpu_arch(&self) -> Option<String> {
self.inner.cpu_arch()
}
}

/// Struct containing information of a process.
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,4 +520,10 @@ mod test {
.iter()
.any(|(_, process)| !process.cmd().is_empty()));
}

#[test]
fn check_cpu_arch() {
let s = System::new();
assert_eq!(s.cpu_arch().is_some(), IS_SUPPORTED);
}
}
27 changes: 27 additions & 0 deletions src/unix/apple/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ impl SystemInner {
pub(crate) fn distribution_id(&self) -> String {
std::env::consts::OS.to_owned()
}

pub(crate) fn cpu_arch(&self) -> Option<String> {
get_cpu_arch()
}
}

fn get_system_info(value: c_int, default: Option<&str>) -> Option<String> {
Expand Down Expand Up @@ -451,3 +455,26 @@ fn get_system_info(value: c_int, default: Option<&str>) -> Option<String> {
}
}
}

pub(crate) fn get_cpu_arch() -> Option<String> {
use std::ffi::CStr;
let mut arch_str: [u8; 32] = [0; 32];

unsafe {
let mut mib = [libc::CTL_HW as _, libc::HW_MACHINE as _];
if get_sys_value(
mem::size_of::<[u8; 32]>(),
arch_str.as_mut_ptr() as *mut _,
&mut mib,
) {
CStr::from_bytes_until_nul(&arch_str)
.map(|res| match res.to_str() {
Ok(arch) => Some(arch.to_string()),
Err(_) => None,
})
.unwrap_or_else(|_| None)
} else {
None
}
}
}
1 change: 1 addition & 0 deletions src/unix/freebsd/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ impl CpusWrapper {
// We get the CPU vendor ID in here.
let vendor_id =
get_sys_value_str_by_name(b"hw.model\0").unwrap_or_else(|| "<unknown>".to_owned());

for pos in 0..self.nb_cpus {
if refresh_kind.frequency() {
unsafe {
Expand Down
22 changes: 22 additions & 0 deletions src/unix/freebsd/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ impl SystemInner {
pub(crate) fn distribution_id(&self) -> String {
std::env::consts::OS.to_owned()
}
pub(crate) fn cpu_arch(&self) -> Option<String> {
get_cpu_arch()
}
}

impl SystemInner {
Expand Down Expand Up @@ -595,3 +598,22 @@ impl Drop for SystemInfo {
}
}
}

pub(crate) fn get_cpu_arch() -> Option<String> {
use std::ffi::CStr;
let mut arch_str: [u8; 32] = [0; 32];

unsafe {
let mib = [libc::CTL_HW as _, libc::HW_MACHINE as _];
if get_sys_value(&mib, &mut arch_str) {
CStr::from_bytes_until_nul(&arch_str)
.map(|res| match res.to_str() {
Ok(arch) => Some(arch.to_string()),
Err(_) => None,
})
.unwrap_or_else(|_| None)
} else {
None
}
}
}
24 changes: 24 additions & 0 deletions src/unix/linux/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,9 @@ impl SystemInner {
get_system_info_android(InfoType::DistributionID)
.unwrap_or_else(|| std::env::consts::OS.to_owned())
}
pub(crate) fn cpu_arch(&self) -> Option<String> {
get_cpu_arch()
}
}

fn read_u64(filename: &str) -> Option<u64> {
Expand Down Expand Up @@ -640,6 +643,27 @@ fn get_system_info_android(info: InfoType) -> Option<String> {
}
}

fn get_cpu_arch() -> Option<String> {
let mut raw = std::mem::MaybeUninit::<libc::utsname>::zeroed();

unsafe {
if libc::uname(raw.as_mut_ptr()) == 0 {
let info = raw.assume_init();

let machine = info
.machine
.iter()
.filter(|c| **c != 0)
.map(|c| *c as u8 as char)
.collect::<String>();

Some(machine)
} else {
None
}
}
}

#[cfg(test)]
mod test {
#[cfg(target_os = "android")]
Expand Down
3 changes: 3 additions & 0 deletions src/unknown/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,7 @@ impl SystemInner {
pub(crate) fn host_name(&self) -> Option<String> {
None
}
pub(crate) fn cpu_arch(&self) -> Option<String> {
None
}
}
21 changes: 20 additions & 1 deletion src/windows/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ use windows::Wdk::System::SystemInformation::{NtQuerySystemInformation, SystemPr
use windows::Win32::Foundation::{HANDLE, STATUS_INFO_LENGTH_MISMATCH, STILL_ACTIVE};
use windows::Win32::System::ProcessStatus::{K32GetPerformanceInfo, PERFORMANCE_INFORMATION};
use windows::Win32::System::Registry::HKEY_LOCAL_MACHINE;
use windows::Win32::System::SystemInformation;
use windows::Win32::System::SystemInformation::{
ComputerNamePhysicalDnsHostname, GetComputerNameExW, GetTickCount64, GlobalMemoryStatusEx,
MEMORYSTATUSEX,
MEMORYSTATUSEX, SYSTEM_INFO,
};
use windows::Win32::System::Threading::GetExitCodeProcess;

Expand Down Expand Up @@ -449,6 +450,9 @@ impl SystemInner {
pub(crate) fn distribution_id(&self) -> String {
std::env::consts::OS.to_owned()
}
pub(crate) fn cpu_arch(&self) -> Option<String> {
get_cpu_arch()
}
}

pub(crate) fn is_proc_running(handle: HANDLE) -> bool {
Expand Down Expand Up @@ -540,3 +544,18 @@ fn get_dns_hostname() -> Option<String> {
sysinfo_debug!("Failed to get computer hostname");
None
}

fn get_cpu_arch() -> Option<String> {
// https://docs.microsoft.com/fr-fr/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info
unsafe {
let info = SYSTEM_INFO::default();
match info.Anonymous.Anonymous.wProcessorArchitecture {
SystemInformation::PROCESSOR_ARCHITECTURE_INTEL => Some("x86".to_string()),
SystemInformation::PROCESSOR_ARCHITECTURE_ARM => Some("arm".to_string()),
SystemInformation::PROCESSOR_ARCHITECTURE_AMD64 => Some("x86_64".to_string()),
SystemInformation::PROCESSOR_ARCHITECTURE_ARM64 => Some("arm64".to_string()),
SystemInformation::PROCESSOR_ARCHITECTURE_ARM32_ON_WIN64 => Some("arm".to_string()),
_ => None,
}
}
}
1 change: 1 addition & 0 deletions tests/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fn test_cpu() {
assert!(s.cpus().iter().any(|c| !c.brand().is_empty()));
}
assert!(s.cpus().iter().any(|c| !c.vendor_id().is_empty()));
assert!(s.cpu_arch().is_some());
}

#[test]
Expand Down
Loading