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

Count processors via affinity mask on linux #38

Merged
merged 3 commits into from
May 25, 2017
Merged

Count processors via affinity mask on linux #38

merged 3 commits into from
May 25, 2017

Conversation

kubo39
Copy link
Contributor

@kubo39 kubo39 commented May 22, 2017

This Fixes #12

Copy link
Owner

@seanmonstar seanmonstar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for all this! This is impressive!

I just have some questions left in comments.

src/lib.rs Outdated
}

#[cfg(target_os = "linux")]
fn get_num_cpus() -> usize {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether using affinity by default is better, or if it'd be a sort of breaking change. Would one always always want the affinity, or could one conceivably want to just know the number or CPUs on the machine anyways?

Put another way, should this be a separate function, or just be the default and only implementation for linux? I'm not saying one or the other is correct, I'd be interested in rationale for either case.

src/lib.rs Outdated
target_os = "fuchsia",
)
#[cfg(feature = "nightly")]
#[feature(asm, naked_functions)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there large benefits to using this asm as opposed to not requiring unstable features and using the bit masking?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function exists in the standard library: https://doc.rust-lang.org/std/primitive.u64.html#method.count_ones

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just taken from my perfromance sensitive code written in C.
count_ones is just what I wanted, thanks!

@seanmonstar
Copy link
Owner

I've moved the question out of the line comment, so that edits don't make it disappear:

I wonder whether using affinity by default is better, or if it'd be a sort of breaking change. Would one always always want the affinity, or could one conceivably want to just know the number or CPUs on the machine anyways?

Put another way, should this be a separate function, or just be the default and only implementation for linux? I'm not saying one or the other is correct, I'd be interested in rationale for either case.

@kubo39
Copy link
Contributor Author

kubo39 commented May 23, 2017

** updated **

I researched some other language implementations on Linux.

Counts via affinity mask

golang

nproc(1)

Ruby

Counts by sysconf(_SC_NPROCESSORS_ONLN)

D

Both

python

ref: https://github.com/kubo39/cpucount

@seanmonstar
Copy link
Owner

I'm slightly inclined to follow golang, but welcome other opinions. I also wonder if that's breaking change (or a "bug fix").

@seanmonstar
Copy link
Owner

My current feelings are:

  • get() should check affinity. It's the "default" function to call, and I assume most people using it are just wanting to know how many threads or processes they should spawn, so it's actually bad to report more CPUs than the process can actually use.
  • get_physical() is already there for those who need that specific datum.
  • get_all() or something could be added later if there is really a need to know ALL cores, physical and virtual.

So, I'm considering merging this as-is, unless there's other thoughts.

@brson
Copy link

brson commented May 25, 2017

@seanmonstar I agree that the default behavior should give you a reasonable number of threads to spawn.

I'm not sure about whether it's a breaking change or not, but I'd say it should get a major bump.

@pedrocr
Copy link
Contributor

pedrocr commented May 25, 2017

@seanmonstar the reason for get_physical() is to get the same thing as get() but without hyperthreading. I agree that it makes sense that get() returns the threads you can use but then get_physical() should return the physical threads you can use. Otherwise it's inconsistent. So I think it makes more sense to do 4 functions having normal/physical versions of with/without affinity and have the default be the affinity ones. So:

  • get() is all threads with affinity
  • get_physical() is all physically sepparate threads with affinity
  • get_all() is all threads without affinity
  • get_all_physical() is the current get_physical()

To do this what we're missing is the get_physical() implementation where affinity is taken into account.

@seanmonstar
Copy link
Owner

@pedrocr that makes sense, thanks for the insight! I've opened #40 for tracking that.

@seanmonstar seanmonstar merged commit 68f3382 into seanmonstar:master May 25, 2017
@seanmonstar
Copy link
Owner

Thanks @kubo39! Excellent work!

@kubo39 kubo39 deleted the cpu-affinity branch May 25, 2017 23:33
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

Successfully merging this pull request may close these issues.

5 participants