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

Add call to check total number of CPUs? #94

Open
avl opened this issue Feb 19, 2020 · 6 comments
Open

Add call to check total number of CPUs? #94

avl opened this issue Feb 19, 2020 · 6 comments

Comments

@avl
Copy link

avl commented Feb 19, 2020

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?

@seanmonstar
Copy link
Owner

Does the num_cpus::get_physical() serve your needs?

@avl
Copy link
Author

avl commented Feb 20, 2020

Does the num_cpus::get_physical() serve your needs?

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.

@kubo39
Copy link
Contributor

kubo39 commented Feb 22, 2020

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);
}

@avl
Copy link
Author

avl commented Feb 25, 2020

@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?

@kubo39
Copy link
Contributor

kubo39 commented Feb 25, 2020

@avl

I suppose that is x86-only?

AFAIK, such extension only exists on x86.

And in a totally different crate?

yes, this is raw-cpuid crate.

And what would the result be in a multi-CPU machine?

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

Would I get just the number of cores on the physical CPU the current thread happens to execute on?

Ah, I may have misunderstood. You just want system-wide logical processor count?
If so, heim-rs may be preferable.

@avl
Copy link
Author

avl commented Feb 25, 2020

@kubo39 Thanks a lot! That crate does indeed seem to do exactly what I want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants