build-cores: detect cores automatically if set to 0#13402
build-cores: detect cores automatically if set to 0#13402xokdvium merged 1 commit intoNixOS:masterfrom
Conversation
This is rather surprising, since This patch (288 lines) seems overly complicated for such a trivial change. I would just do something like: In particular, having both a unit test and a functional test seems overkill. (Functional tests are almost always preferred over unit tests.) |
013a0ef to
e5b7784
Compare
It is in the millisecond range, as is sourcing setup.sh. It slowed it down by roughly 10-15% if I remember correctly.
I applied your suggestion, which removed most code. Also I removed the unit tests. |
So the issue that is addressed here is that the "builder" (a.k.a. the shell script) shouldn't need to "guess" the level of parallelism? Also the special semantics of |
Correct!
Done
Yes it behaves exactly the same, I just thought that having 0 as a default is easier to follow. But I can change it back if you prefer. |
It just seems that this does extra unnecessary work ( It also looks like https://en.cppreference.com/w/cpp/thread/thread/hardware_concurrency.html
So this really doesn't correspond to the number of logical cores. Do you think this could be a problem? Since nix itself would be the sole source of truth for the level of concurrency it's important to get the default right. |
On my system it does correctly return the number of logical cores trying the snippet of the reference you linked: $ cat > main.c
#include <iostream>
#include <thread>
int main()
{
unsigned int n = std::thread::hardware_concurrency();
std::cout << n << " concurrent threads are supported.\n";
}
$ g++ main.c && ./main
16 concurrent threads are supported.I have 8 physical cores and 16 logical ones, so it seems correct. |
Yeah, I suspect it does the right thing for most cases, though there might be some nuances. So I guess it's fine. |
|
I have to correct one of my statements made in the original description above:
This is not true, nixos was setting |
|
Also this needs a release note. |
Done |
xokdvium
left a comment
There was a problem hiding this comment.
Also worth noting that the Advanced Topics documentation also mentions that 0 cores implies autodetection: https://nix.dev/manual/nix/2.29/advanced-topics/cores-vs-jobs.html
NIX_BUILD_CORES is equal to cores, unless cores equals 0, in which case NIX_BUILD_CORES will be the total number of cores in the system.
As such, this can probably be considered a bugfix?
Also briefly discussed in the team meeting:
This seems like a good first step towards ironing out the semantics of user-facing settings. Related to #11143.
This changes makes nix detect a machines available cores automatically whenever build-cores is set to 0. So far, nix simply passed NIX_BUILD_CORES=0 whenever build-cores is set to 0. (only when build-cores is unset it was detecting cores automatically) The behavior of passing NIX_BUILD_CORES=0 leads to a performance penalty when sourcing nixpkgs' generic builder's `setup.sh`, as setup.sh has to execute `nproc`. This significantly slows down sourcing of setup.sh
|
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: |
This change makes nix detect a machines available cores automatically whenever
coresis set to 0.So far, nix simply passed NIX_BUILD_CORES=0 whenever build-cores is set to 0. (only when
coresis unset it was detecting cores automatically)The behavior of passing NIX_BUILD_CORES=0 leads to a performance penalty when sourcing nixpkgs' generic builder's
setup.sh, as setup.sh has to executenproc. This significantly slows down sourcing of setup.shThis affects all nixos machines since the introduction of the nix settings module a couple releases ago which sets(It was set to 0 in nixos since at least 6 years)coresto 0 be default.Add 👍 to pull requests you find important.
The Nix maintainer team uses a GitHub project board to schedule and track reviews.