Skip to content

Commit

Permalink
Move arch info to System
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas committed Nov 4, 2023
1 parent 56e3e5e commit 214b887
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 147 deletions.
52 changes: 24 additions & 28 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 Expand Up @@ -3391,22 +3403,6 @@ impl Cpu {
pub fn frequency(&self) -> u64 {
self.inner.frequency()
}

/// Returns the CPU's arch (e.g. x86_64, arm64, ...).
///
/// ```no_run
/// use sysinfo::{System, RefreshKind, CpuRefreshKind};
///
/// let s = System::new_with_specifics(
/// RefreshKind::new().with_cpu(CpuRefreshKind::everything()),
/// );
/// for cpu in s.cpus() {
/// println!("{:?}", cpu.arch());
/// }
/// ```
pub fn arch(&self) -> CpuArch {
self.inner.arch()
}
}

/// Enumeration of CPU architectures
Expand All @@ -3421,17 +3417,17 @@ pub enum CpuArch {
/// `mips64` platform
MIPS64,
/// `powerpc` platform
POWERPC,
PowerPC,
/// `powerpc64` platform
POWERPC64,
PowerPC64,
/// `riscv32` platform
RISCV32,
/// `riscv64` platform
RISCV64,
/// `wasm` platform
WASM,
Wasm,
/// `wasm64` platform
WASM64,
Wasm64,
/// `x86` platform
X86,
/// `x64_64` AMD/Intel platform
Expand All @@ -3449,12 +3445,12 @@ impl From<&str> for CpuArch {
"aarch64" => Self::ARM64,
"mips" => Self::MIPS,
"mips64" => Self::MIPS64,
"powerpc" => Self::POWERPC,
"powerpc64" => Self::POWERPC64,
"powerpc" => Self::PowerPC,
"powerpc64" => Self::PowerPC64,
"riscv32" => Self::RISCV32,
"riscv64" => Self::RISCV64,
"wasm" => Self::WASM,
"wasm64" => Self::WASM64,
"wasm" => Self::Wasm,
"wasm64" => Self::Wasm64,
"x86" => Self::X86,
"x86_64" => Self::X86_64,
_ => Self::UNKNOWN,
Expand All @@ -3469,12 +3465,12 @@ impl Display for CpuArch {
Self::ARM64 => String::from("arm64"),
Self::MIPS => String::from("mips"),
Self::MIPS64 => String::from("mips64"),
Self::POWERPC => String::from("powerpc"),
Self::POWERPC64 => String::from("powerpc64"),
Self::PowerPC => String::from("powerpc"),
Self::PowerPC64 => String::from("powerpc64"),
Self::RISCV32 => String::from("riscv32"),
Self::RISCV64 => String::from("riscv64"),
Self::WASM => String::from("wasm"),
Self::WASM64 => String::from("wasm64"),
Self::Wasm => String::from("wasm"),
Self::Wasm64 => String::from("wasm64"),
Self::X86 => String::from("x86"),
Self::X86_64 => String::from("x86_64"),
Self::UNKNOWN => String::from("unknown"),
Expand Down
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,7 @@ mod test {
if !IS_SUPPORTED {
return;
}
let mut s = System::new();
s.refresh_cpu_usage();
assert!(s.cpus().iter().any(|c| !(c.arch() == CpuArch::UNKNOWN)));
let s = System::new();
assert!(!(s.cpu_arch() == None));
}
}
37 changes: 2 additions & 35 deletions src/unix/apple/cpu.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::sys::utils::{get_sys_value, get_sys_value_by_name};
use crate::{Cpu, CpuArch, CpuRefreshKind};
use crate::{Cpu, CpuRefreshKind};

use libc::{c_char, c_int, c_void, host_processor_info, mach_port_t, mach_task_self};
use libc::{c_char, c_void, host_processor_info, mach_port_t, mach_task_self};
use std::mem;
use std::ops::Deref;
use std::sync::Arc;
Expand All @@ -24,7 +24,6 @@ impl CpusWrapper {
0,
String::new(),
String::new(),
CpuArch::UNKNOWN,
),
},
cpus: Vec::new(),
Expand Down Expand Up @@ -113,7 +112,6 @@ pub(crate) struct CpuInner {
frequency: u64,
vendor_id: String,
brand: String,
arch: CpuArch,
}

impl CpuInner {
Expand All @@ -123,7 +121,6 @@ impl CpuInner {
frequency: u64,
vendor_id: String,
brand: String,
arch: CpuArch,
) -> Self {
Self {
name,
Expand All @@ -132,7 +129,6 @@ impl CpuInner {
frequency,
vendor_id,
brand,
arch,
}
}

Expand Down Expand Up @@ -172,10 +168,6 @@ impl CpuInner {
pub(crate) fn brand(&self) -> &str {
&self.brand
}

pub(crate) fn arch(&self) -> CpuArch {
self.arch
}
}

pub(crate) unsafe fn get_cpu_frequency() -> u64 {
Expand Down Expand Up @@ -295,7 +287,6 @@ pub(crate) fn init_cpus(
} else {
global_cpu.frequency()
};
let arch = unsafe { get_cpu_arch() };

unsafe {
if !get_sys_value(
Expand All @@ -319,7 +310,6 @@ pub(crate) fn init_cpus(
frequency,
vendor_id.clone(),
brand.clone(),
arch,
),
};
if refresh_kind.cpu_usage() {
Expand Down Expand Up @@ -381,29 +371,6 @@ pub(crate) fn get_vendor_id_and_brand() -> (String, String) {
(vendor, get_sysctl_str(b"machdep.cpu.brand_string\0"))
}

pub(crate) unsafe fn get_cpu_arch() -> CpuArch {
use std::ffi::CStr;
let mut mib: [c_int; 2] = [libc::CTL_HW, libc::HW_MACHINE_ARCH];
let mut arch_str: [u8; 32] = [0; 32];

if get_sys_value(
libc::CTL_HW as _,
libc::HW_MACHINE as _,
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) => CpuArch::from(arch),
Err(_) => CpuArch::UNKNOWN,
})
.unwrap_or_else(|_| CpuArch::UNKNOWN)
} else {
CpuArch::UNKNOWN
}
}

#[cfg(test)]
mod test {
use crate::*;
Expand Down
31 changes: 31 additions & 0 deletions src/unix/apple/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub(crate) struct SystemInner {
#[cfg(all(target_os = "macos", not(feature = "apple-sandbox")))]
clock_info: Option<crate::sys::macos::system::SystemTimeInfo>,
cpus: CpusWrapper,
arch: Option<String>,
}

pub(crate) struct Wrap<'a>(pub UnsafeCell<&'a mut HashMap<Pid, Process>>);
Expand Down Expand Up @@ -94,6 +95,7 @@ impl SystemInner {
#[cfg(all(target_os = "macos", not(feature = "apple-sandbox")))]
clock_info: crate::sys::macos::system::SystemTimeInfo::new(port),
cpus: CpusWrapper::new(),
arch: get_cpu_arch(),
}
}
}
Expand Down Expand Up @@ -428,6 +430,10 @@ impl SystemInner {
pub(crate) fn distribution_id(&self) -> String {
std::env::consts::OS.to_owned()
}

pub(crate) fn cpu_arch(&self) -> Option<String> {
self.arch.clone()
}
}

#[cfg(all(target_os = "macos", not(feature = "apple-sandbox")))]
Expand Down Expand Up @@ -496,3 +502,28 @@ 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 mib: [c_int; 2] = [libc::CTL_HW, libc::HW_MACHINE_ARCH];
let mut arch_str: [u8; 32] = [0; 32];

unsafe {
if get_sys_value(
libc::CTL_HW as _,
libc::HW_MACHINE as _,
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
}
}
}
32 changes: 1 addition & 31 deletions src/unix/freebsd/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::sys::utils::{
get_sys_value_array, get_sys_value_by_name, get_sys_value_str_by_name, init_mib, VecSwitcher,
};
use crate::{Cpu, CpuArch, CpuRefreshKind};
use crate::{Cpu, CpuRefreshKind};

use libc::{c_int, c_ulong};

Expand Down Expand Up @@ -67,7 +67,6 @@ 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());
let arch = unsafe { get_cpu_arch() };

for pos in 0..self.nb_cpus {
if refresh_kind.frequency() {
Expand Down Expand Up @@ -142,7 +141,6 @@ pub(crate) struct CpuInner {
name: String,
pub(crate) vendor_id: String,
pub(crate) frequency: u64,
pub(crate) arch: CpuArch,
}

impl CpuInner {
Expand All @@ -152,7 +150,6 @@ impl CpuInner {
name,
vendor_id,
frequency,
arch,
}
}

Expand All @@ -175,10 +172,6 @@ impl CpuInner {
pub(crate) fn brand(&self) -> &str {
""
}

pub(crate) fn arch(&self) -> CpuArch {
self.arch
}
}

pub(crate) fn physical_core_count() -> Option<usize> {
Expand All @@ -205,26 +198,3 @@ unsafe fn get_frequency_for_cpu(cpu_nb: usize) -> u64 {
}
frequency as _
}

pub(crate) unsafe fn get_cpu_arch() -> CpuArch {
use std::ffi::CStr;
let mut mib: [c_int; 2] = [libc::CTL_HW, libc::HW_MACHINE_ARCH];
let mut arch_str: [u8; 32] = [0; 32];

if get_sys_value(
libc::CTL_HW as _,
libc::HW_MACHINE as _,
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) => CpuArch::from(arch),
Err(_) => CpuArch::UNKNOWN,
})
.unwrap_or_else(|_| CpuArch::UNKNOWN)
} else {
CpuArch::UNKNOWN
}
}
30 changes: 30 additions & 0 deletions src/unix/freebsd/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub(crate) struct SystemInner {
boot_time: u64,
system_info: SystemInfo,
cpus: CpusWrapper,
arch: Option<String>,
}

impl SystemInner {
Expand All @@ -41,6 +42,7 @@ impl SystemInner {
boot_time: boot_time(),
system_info: SystemInfo::new(),
cpus: CpusWrapper::new(),
arch: get_cpu_arch(),
}
}

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

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

pub(crate) fn get_cpu_arch() -> Option<String> {
use std::ffi::CStr;
let mut mib: [c_int; 2] = [libc::CTL_HW, libc::HW_MACHINE_ARCH];
let mut arch_str: [u8; 32] = [0; 32];

unsafe {
if get_sys_value(
libc::CTL_HW as _,
libc::HW_MACHINE as _,
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
}
}
}
Loading

0 comments on commit 214b887

Please sign in to comment.