NixOS / nix

Nix, the purely functional package manager
https://nixos.org/
GNU Lesser General Public License v2.1
12.43k stars 1.5k forks source link

Configure niceness of build tasks #11088

Open andir opened 3 months ago

andir commented 3 months ago

Is your feature request related to a problem? Please describe.

One of the features that I miss from Gentoo many years ago was a good control of CPU priority of local builds vs. the priority of my user space. I used to be able to recompile the entire world with just a couple of CPU cores while using my computer at the same time. Ever since I switched to Nix builds have the tendency to use all available CPU resources (as they should do, if they are available). The missing feature was any kind of nice-ness setting on the nix side to ensure that builds aren't eating away my interactive CPU session slices.

I've since carried a very simple patch in my Nix builds:

diff --git a/src/libstore/unix/build/local-derivation-goal.cc b/src/libstore/unix/build/local-derivation-goal.cc
index a99439738..ce2d236ab 100644
--- a/src/libstore/unix/build/local-derivation-goal.cc
+++ b/src/libstore/unix/build/local-derivation-goal.cc
@@ -1730,6 +1730,7 @@ void LocalDerivationGoal::runChild()
         } catch (SystemError &) { }

 #if __linux__
+        nice(15);
         if (useChroot) {

             userNamespaceSync.writeSide = -1;

This is a very simplistic approach but solved the problem for me (most of the time).

Describe the solution you'd like I'd like to discuss with upstream what a proper solution could look like. These days we have more facilities available to control CPU priorities etc via cgroups and other interfaces.

Ideally a local setting (/etc/nix/ only?) would allow me to configure the above used niceness value or a similar knob. I am aware that I can configure the amount of CPU cores and the amount of job concurrency but this isn't the same. I want builds to finish as fast a possible on an otherwise idle desktop / laptop while occasionally yielding CPU time for browsing the web, listening to music and getting work done.

Once the CPU priority topic has been discussed we could have a discussion on memory consumption. Adjusting OOM scores such that Nix builds are killed rather than my interactive session (that drives the build and thus kills the build anyway).

Describe alternatives you've considered The alternative is that I keep on carrying that patch forward and just do my own thing. Not preferred but it wouldn't be the end of the world... It is just one more patch.

Priorities

Add :+1: to issues you find important.

vikanezrimaya commented 2 months ago

Related idea: set niceness of a build depending on the niceness of the process that requested the build. That way I could launch nixos-rebuild with a certain niceness value and have it be propagated the same way niceness is propagated from parent to child on launch.

This could also allow setting niceness of a remote build, provided one has a way to renice the nix-store --stdio process (limits.conf(5) maybe?).