-
-
Notifications
You must be signed in to change notification settings - Fork 91
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
Add call to check total number of CPUs? #94
Comments
Does the |
Ah, does it not take affinity into account? Still, it doesn't really help because I am interested in the number of CPU threads, not the number of cores. |
I'm not sure what you want, cpuid may be helpful. #[macro_use]
extern crate raw_cpuid;
fn main() {
/// For Intel Core I7 and later, use `0x0B` to determine number of processors.
///
/// https://www.scss.tcd.ie/Jeremy.Jones/CS4021/processor-identification-cpuid-instruction-note.pdf
/// 5.1.12 Processor Topology
let cpu_info = cpuid!(0x0B);
let threads_per_core = cpu_info.ebx & 0xFFFF;
println!("{}", threads_per_core);
} |
@kubo39 Cool! I suppose that is x86-only? And in a totally different crate? And what would the result be in a multi-CPU machine? Would I get just the number of cores on the physical CPU the current thread happens to execute on? |
AFAIK, such extension only exists on x86.
yes, this is raw-cpuid crate.
Perhaps It depends on cache level, please check the section 5.1.12 on https://www.scss.tcd.ie/Jeremy.Jones/CS4021/processor-identification-cpuid-instruction-note.pdf
Ah, I may have misunderstood. You just want system-wide logical processor count? |
@kubo39 Thanks a lot! That crate does indeed seem to do exactly what I want. |
On linux, num_cpus::get() currently returns 1 if the calling thread has its sched_affinity set to only one thread.
I have a use case where a main thread is pinned to core 0, and worker threads are pinned to cores 1, 2, 3 etc.
In this use case, the main thread can no longer use num_cpus::get() to determine the number of cpus in the system. In my case, I had a problem where a thread pool-implementation erroneously decided to use a single worker thread, because it thought the machine had only one CPU, since the pool was initialized on a thread which was pinned to a specific core.
I get the idea of returning "usable cores", but I think there is also a use case for a call to return the total number of cpus (the set which could be used in a call to sched_setaffinity).
Would you be open to pull request to add such a call?
The text was updated successfully, but these errors were encountered: