Skip to content

Commit

Permalink
Merge pull request #38 from kubo39/cpu-affinity
Browse files Browse the repository at this point in the history
Count processors via affinity mask on linux
  • Loading branch information
seanmonstar authored May 25, 2017
2 parents 953d444 + ac028f3 commit 68f3382
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,31 @@ fn get_num_cpus() -> usize {
cpus as usize
}

#[cfg(
any(
target_os = "linux",
target_os = "nacl",
target_os = "macos",
target_os = "ios",
target_os = "android",
target_os = "solaris",
target_os = "fuchsia",
)
#[cfg(target_os = "linux")]
fn get_num_cpus() -> usize {
let mut set: libc::cpu_set_t = unsafe { std::mem::zeroed() };
if unsafe { libc::sched_getaffinity(0, std::mem::size_of::<libc::cpu_set_t>(), &mut set) } == 0 {
let mut count: u32 = 0;
for i in 0..libc::CPU_SETSIZE as usize {
if unsafe { libc::CPU_ISSET(i, &set) } {
count += 1
}
}
count as usize
} else {
unsafe {
libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as usize
}
}
}

#[cfg(any(
target_os = "nacl",
target_os = "macos",
target_os = "ios",
target_os = "android",
target_os = "solaris",
target_os = "fuchsia")
)]
fn get_num_cpus() -> usize {
unsafe {
Expand Down

0 comments on commit 68f3382

Please sign in to comment.