Skip to content

Commit

Permalink
Convert physical_core_count to an associated function
Browse files Browse the repository at this point in the history
  • Loading branch information
arpankapoor committed Dec 8, 2024
1 parent 1dc641e commit 87c9728
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 64 deletions.
2 changes: 1 addition & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ fn interpret_input(
writeln!(
&mut io::stdout(),
"number of physical cores: {}",
sys.physical_core_count()
System::physical_core_count()
.map(|c| c.to_string())
.unwrap_or_else(|| "Unknown".to_owned()),
);
Expand Down
18 changes: 6 additions & 12 deletions src/c_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,18 +531,6 @@ pub extern "C" fn sysinfo_cpu_brand(system: CSystem) -> RString {
}
}

/// Equivalent of [`system::physical_core_count()`].
#[no_mangle]
pub extern "C" fn sysinfo_cpu_physical_cores(system: CSystem) -> u32 {
assert!(!system.is_null());
unsafe {
let system: Box<System> = Box::from_raw(system as *mut System);
let count = system.physical_core_count().unwrap_or(0);
let _ = Box::into_raw(system);
count as u32
}
}

/// Equivalent of [`cpu::frequency()`].
#[no_mangle]
pub extern "C" fn sysinfo_cpu_frequency(system: CSystem) -> u64 {
Expand Down Expand Up @@ -608,3 +596,9 @@ pub extern "C" fn sysinfo_system_long_version() -> RString {
std::ptr::null()
}
}

/// Equivalent of [`system::physical_core_count()`].
#[no_mangle]
pub extern "C" fn sysinfo_cpu_physical_cores() -> u32 {
System::physical_core_count().unwrap_or(0)
}
46 changes: 22 additions & 24 deletions src/common/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,22 +504,6 @@ impl System {
self.inner.cpus()
}

/// Returns the number of physical cores on the CPU or `None` if it couldn't get it.
///
/// In case there are multiple CPUs, it will combine the physical core count of all the CPUs.
///
/// **Important**: this information is computed every time this function is called.
///
/// ```no_run
/// use sysinfo::System;
///
/// let s = System::new();
/// println!("{:?}", s.physical_core_count());
/// ```
pub fn physical_core_count(&self) -> Option<usize> {
self.inner.physical_core_count()
}

/// Returns the RAM size in bytes.
///
/// ```no_run
Expand Down Expand Up @@ -817,6 +801,22 @@ impl System {
pub fn cpu_arch() -> String {
SystemInner::cpu_arch().unwrap_or_else(|| std::env::consts::ARCH.to_owned())
}

/// Returns the number of physical cores on the CPU or `None` if it couldn't get it.
///
/// In case there are multiple CPUs, it will combine the physical core count of all the CPUs.
///
/// **Important**: this information is computed every time this function is called.
///
/// ```no_run
/// use sysinfo::System;
///
/// let s = System::new();
/// println!("{:?}", System::physical_core_count());
/// ```
pub fn physical_core_count() -> Option<usize> {
SystemInner::physical_core_count()
}
}

/// A struct representing system load average value.
Expand Down Expand Up @@ -2496,25 +2496,23 @@ mod test {
if IS_SUPPORTED_SYSTEM {
// The physical cores count is recomputed every time the function is called, so the
// information must be relevant even with nothing initialized.
let physical_cores_count = s
.physical_core_count()
.expect("failed to get number of physical cores");
let physical_cores_count =
System::physical_core_count().expect("failed to get number of physical cores");

s.refresh_cpu_usage();
// The cpus shouldn't be empty anymore.
assert!(!s.cpus().is_empty());

// In case we are running inside a VM, it's possible to not have a physical core, only
// logical ones, which is why we don't test `physical_cores_count > 0`.
let physical_cores_count2 = s
.physical_core_count()
.expect("failed to get number of physical cores");
let physical_cores_count2 =
System::physical_core_count().expect("failed to get number of physical cores");
assert!(physical_cores_count2 <= s.cpus().len());
assert_eq!(physical_cores_count, physical_cores_count2);
} else {
assert_eq!(s.physical_core_count(), None);
assert_eq!(System::physical_core_count(), None);
}
assert!(s.physical_core_count().unwrap_or(0) <= s.cpus().len());
assert!(System::physical_core_count().unwrap_or(0) <= s.cpus().len());
}

// This test only exists to ensure that the `Display` and `Debug` traits are implemented on the
Expand Down
2 changes: 1 addition & 1 deletion src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl serde::Serialize for crate::System {
state.serialize_field("global_cpu_usage", &self.global_cpu_usage())?;
state.serialize_field("cpus", &self.cpus())?;

state.serialize_field("physical_core_count", &self.physical_core_count())?;
state.serialize_field("physical_core_count", &Self::physical_core_count())?;
state.serialize_field("total_memory", &self.total_memory())?;
state.serialize_field("free_memory", &self.free_memory())?;
state.serialize_field("available_memory", &self.available_memory())?;
Expand Down
2 changes: 1 addition & 1 deletion src/sysinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ size_t sysinfo_networks_transmitted(CNetworks networks);
RString sysinfo_cpu_vendor_id(CSystem system);
RString sysinfo_cpu_brand(CSystem system);
uint64_t sysinfo_cpu_frequency(CSystem system);
uint32_t sysinfo_cpu_physical_cores(CSystem system);

RString sysinfo_system_name();
RString sysinfo_system_kernel_version();
RString sysinfo_system_version();
RString sysinfo_system_host_name();
RString sysinfo_system_long_version();
uint32_t sysinfo_cpu_physical_cores();

void sysinfo_rstring_free(RString str);
8 changes: 4 additions & 4 deletions src/unix/apple/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,6 @@ impl SystemInner {
&self.cpus.cpus
}

pub(crate) fn physical_core_count(&self) -> Option<usize> {
physical_core_count()
}

pub(crate) fn total_memory(&self) -> u64 {
self.mem_total
}
Expand Down Expand Up @@ -507,6 +503,10 @@ impl SystemInner {
}
}
}

pub(crate) fn physical_core_count() -> Option<usize> {
physical_core_count()
}
}

fn get_system_info(value: c_int, default: Option<&str>) -> Option<String> {
Expand Down
8 changes: 4 additions & 4 deletions src/unix/freebsd/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,6 @@ impl SystemInner {
&self.cpus.cpus
}

pub(crate) fn physical_core_count(&self) -> Option<usize> {
physical_core_count()
}

pub(crate) fn total_memory(&self) -> u64 {
self.mem_total
}
Expand Down Expand Up @@ -268,6 +264,10 @@ impl SystemInner {
}
}
}

pub(crate) fn physical_core_count() -> Option<usize> {
physical_core_count()
}
}

impl SystemInner {
Expand Down
8 changes: 4 additions & 4 deletions src/unix/linux/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,6 @@ impl SystemInner {
&self.cpus.cpus
}

pub(crate) fn physical_core_count(&self) -> Option<usize> {
get_physical_core_count()
}

pub(crate) fn total_memory(&self) -> u64 {
self.mem_total
}
Expand Down Expand Up @@ -503,6 +499,10 @@ impl SystemInner {
}
}

pub(crate) fn physical_core_count() -> Option<usize> {
get_physical_core_count()
}

pub(crate) fn refresh_cpu_list(&mut self, refresh_kind: CpuRefreshKind) {
self.cpus = CpusWrapper::new();
self.refresh_cpu_specifics(refresh_kind);
Expand Down
8 changes: 4 additions & 4 deletions src/unknown/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ impl SystemInner {
&[]
}

pub(crate) fn physical_core_count(&self) -> Option<usize> {
None
}

pub(crate) fn total_memory(&self) -> u64 {
0
}
Expand Down Expand Up @@ -145,4 +141,8 @@ impl SystemInner {
pub(crate) fn cpu_arch() -> Option<String> {
None
}

pub(crate) fn physical_core_count() -> Option<usize> {
None
}
}
8 changes: 4 additions & 4 deletions src/windows/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,6 @@ impl SystemInner {
self.cpus.cpus()
}

pub(crate) fn physical_core_count(&self) -> Option<usize> {
get_physical_core_count()
}

pub(crate) fn total_memory(&self) -> u64 {
self.mem_total
}
Expand Down Expand Up @@ -506,6 +502,10 @@ impl SystemInner {
}
}
}

pub(crate) fn physical_core_count() -> Option<usize> {
get_physical_core_count()
}
}

pub(crate) fn is_proc_running(handle: HANDLE) -> bool {
Expand Down
3 changes: 1 addition & 2 deletions tests/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ fn test_cpu() {
#[test]
fn test_physical_core_numbers() {
if sysinfo::IS_SUPPORTED_SYSTEM {
let s = sysinfo::System::new();
let count = s.physical_core_count();
let count = sysinfo::System::physical_core_count();
assert_ne!(count, None);
assert!(count.unwrap() > 0);
}
Expand Down
4 changes: 1 addition & 3 deletions tests/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ fn should_skip() -> bool {
return true;
}

let s = sysinfo::System::new_all();

// If we don't have any physical core present, it's very likely that we're inside a VM...
s.physical_core_count().unwrap_or_default() == 0
sysinfo::System::physical_core_count().unwrap_or_default() == 0
}

#[test]
Expand Down

0 comments on commit 87c9728

Please sign in to comment.