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

Miscellaneous updates #111

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

name = "num_cpus"
version = "1.13.0" # don't forget to update html_root_url
edition = "2018"
description = "Get the number of CPUs on a machine."
authors = ["Sean McArthur <sean@seanmonstar.com>"]
license = "MIT OR Apache-2.0"
Expand All @@ -10,6 +11,7 @@ documentation = "https://docs.rs/num_cpus"
keywords = ["cpu", "cpus", "cores"]
categories = ["hardware-support"]
readme = "README.md"
include = ["src/**/*", "LICENSE-*", "README.md", "CHANGELOG.md"]

[target.'cfg(not(windows))'.dependencies]
libc = "0.2.26"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ num_cpus = "1.0"
In your `main.rs` or `lib.rs`:

```rust
extern crate num_cpus;
use num_cpus;

// count logical cores this process could try to use
let num = num_cpus::get();
Expand Down
2 changes: 1 addition & 1 deletion examples/values.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extern crate num_cpus;
use num_cpus;

fn main() {
println!("Logical CPUs: {}", num_cpus::get());
Expand Down
94 changes: 52 additions & 42 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! current system.
//!
//! Sometimes the CPU will exaggerate the number of CPUs it contains, because it can use
//! [processor tricks] to deliver increased performance when there are more threads. This
//! [processor tricks] to deliver increased performance when there are more threads. This
//! crate provides methods to get both the logical and physical numbers of cores.
//!
//! This information can be used as a guide to how many tasks can be run in parallel.
Expand Down Expand Up @@ -40,7 +40,7 @@ extern crate hermit_abi;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "linux")]
use linux::{get_num_cpus, get_num_physical_cpus};
use crate::linux::{get_num_cpus, get_num_physical_cpus};

/// Returns the number of available CPUs of the current system.
///
Expand Down Expand Up @@ -93,7 +93,7 @@ pub fn get() -> usize {
/// let physical_cpus = num_cpus::get_physical();
/// if logical_cpus > physical_cpus {
/// println!("We have simultaneous multithreading with about {:.2} \
/// logical cores to 1 physical core.",
/// logical cores to 1 physical core.",
/// (logical_cpus as f64) / (physical_cpus as f64));
/// } else if logical_cpus == physical_cpus {
/// println!("Either we don't have simultaneous multithreading, or our \
Expand All @@ -110,8 +110,12 @@ pub fn get_physical() -> usize {
get_num_physical_cpus()
}


#[cfg(not(any(target_os = "linux", target_os = "windows", target_os="macos", target_os="openbsd")))]
#[cfg(not(any(
target_os = "linux",
target_os = "windows",
target_os = "macos",
target_os = "openbsd"
)))]
#[inline]
fn get_num_physical_cpus() -> usize {
// Not implemented, fall back
Expand All @@ -122,16 +126,16 @@ fn get_num_physical_cpus() -> usize {
fn get_num_physical_cpus() -> usize {
match get_num_physical_cpus_windows() {
Some(num) => num,
None => get_num_cpus()
None => get_num_cpus(),
}
}

#[cfg(target_os = "windows")]
fn get_num_physical_cpus_windows() -> Option<usize> {
// Inspired by https://msdn.microsoft.com/en-us/library/ms683194

use std::ptr;
use std::mem;
use std::ptr;

#[allow(non_upper_case_globals)]
const RelationProcessorCore: u32 = 0;
Expand All @@ -141,13 +145,13 @@ fn get_num_physical_cpus_windows() -> Option<usize> {
struct SYSTEM_LOGICAL_PROCESSOR_INFORMATION {
mask: usize,
relationship: u32,
_unused: [u64; 2]
_unused: [u64; 2],
}

extern "system" {
fn GetLogicalProcessorInformation(
info: *mut SYSTEM_LOGICAL_PROCESSOR_INFORMATION,
length: &mut u32
length: &mut u32,
) -> u32;
}

Expand Down Expand Up @@ -189,7 +193,8 @@ fn get_num_physical_cpus_windows() -> Option<usize> {
buf.set_len(count as usize);
}

let phys_proc_count = buf.iter()
let phys_proc_count = buf
.iter()
// Only interested in processor packages (physical processors.)
.filter(|proc_info| proc_info.relationship == RelationProcessorCore)
.count();
Expand Down Expand Up @@ -229,9 +234,7 @@ fn get_num_cpus() -> usize {
}
}

#[cfg(any(target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd"))]
#[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd"))]
fn get_num_cpus() -> usize {
use std::ptr;

Expand All @@ -244,12 +247,14 @@ fn get_num_cpus() -> usize {
if cpus < 1 {
let mut mib = [libc::CTL_HW, libc::HW_NCPU, 0, 0];
unsafe {
libc::sysctl(mib.as_mut_ptr(),
2,
&mut cpus as *mut _ as *mut _,
&mut cpus_size as *mut _ as *mut _,
ptr::null_mut(),
0);
libc::sysctl(
mib.as_mut_ptr(),
2,
&mut cpus as *mut _ as *mut _,
&mut cpus_size as *mut _ as *mut _,
ptr::null_mut(),
0,
);
}
if cpus < 1 {
cpus = 1;
Expand All @@ -268,12 +273,14 @@ fn get_num_cpus() -> usize {
let rc: libc::c_int;

unsafe {
rc = libc::sysctl(mib.as_mut_ptr(),
2,
&mut cpus as *mut _ as *mut _,
&mut cpus_size as *mut _ as *mut _,
ptr::null_mut(),
0);
rc = libc::sysctl(
mib.as_mut_ptr(),
2,
&mut cpus as *mut _ as *mut _,
&mut cpus_size as *mut _ as *mut _,
ptr::null_mut(),
0,
);
}
if rc < 0 {
cpus = 1;
Expand All @@ -291,20 +298,21 @@ fn get_num_physical_cpus() -> usize {
let rc: libc::c_int;

unsafe {
rc = libc::sysctl(mib.as_mut_ptr(),
2,
&mut cpus as *mut _ as *mut _,
&mut cpus_size as *mut _ as *mut _,
ptr::null_mut(),
0);
rc = libc::sysctl(
mib.as_mut_ptr(),
2,
&mut cpus as *mut _ as *mut _,
&mut cpus_size as *mut _ as *mut _,
ptr::null_mut(),
0,
);
}
if rc < 0 {
cpus = 1;
}
cpus as usize
}


#[cfg(target_os = "macos")]
fn get_num_physical_cpus() -> usize {
use std::ffi::CStr;
Expand All @@ -313,15 +321,17 @@ fn get_num_physical_cpus() -> usize {
let mut cpus: i32 = 0;
let mut cpus_size = std::mem::size_of_val(&cpus);

let sysctl_name = CStr::from_bytes_with_nul(b"hw.physicalcpu\0")
.expect("byte literal is missing NUL");
let sysctl_name =
CStr::from_bytes_with_nul(b"hw.physicalcpu\0").expect("byte literal is missing NUL");

unsafe {
if 0 != libc::sysctlbyname(sysctl_name.as_ptr(),
&mut cpus as *mut _ as *mut _,
&mut cpus_size as *mut _ as *mut _,
ptr::null_mut(),
0) {
if 0 != libc::sysctlbyname(
sysctl_name.as_ptr(),
&mut cpus as *mut _ as *mut _,
&mut cpus_size as *mut _ as *mut _,
ptr::null_mut(),
0,
) {
return get_num_cpus();
}
}
Expand All @@ -335,8 +345,8 @@ fn get_num_physical_cpus() -> usize {
target_os = "android",
target_os = "solaris",
target_os = "illumos",
target_os = "fuchsia")
)]
target_os = "fuchsia"
))]
fn get_num_cpus() -> usize {
// On ARM targets, processors could be turned off to save power.
// Use `_SC_NPROCESSORS_CONF` to get the real number.
Expand Down Expand Up @@ -391,7 +401,7 @@ fn get_num_cpus() -> usize {
pub abi: u32,
}

extern {
extern "C" {
fn get_system_info(info: *mut system_info) -> status_t;
}

Expand Down