1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-05 12:21:48 +02:00

Move daemon process into sub-cgroup

The daemon process is now moved into a new sub-cgroup called nix-daemon when the
daemon starts. This is necessary to abide by the no-processes-in-inner-nodes
rule, because the service cgroup becomes an inner node when the child cgroups
for the build are created (see LocalDerivationGoal::startBuilder()).

See #9675
This commit is contained in:
Parker Hoyes 2024-08-26 13:50:22 +00:00
parent 1073a8effa
commit 46b3188045
4 changed files with 75 additions and 12 deletions

View file

@ -6,6 +6,7 @@
#include <chrono>
#include <cmath>
#include <mutex>
#include <regex>
#include <unordered_set>
#include <thread>
@ -144,4 +145,35 @@ CgroupStats destroyCgroup(const Path & cgroup)
return destroyCgroup(cgroup, true);
}
std::string getCurrentCgroup()
{
auto cgroupFS = getCgroupFS();
if (!cgroupFS)
throw Error("cannot determine the cgroups file system");
auto ourCgroups = getCgroups("/proc/self/cgroup");
auto ourCgroup = ourCgroups[""];
if (ourCgroup == "")
throw Error("cannot determine cgroup name from /proc/self/cgroup");
return ourCgroup;
}
static std::optional<std::string> rootCgroup;
static std::mutex rootCgroupMutex;
std::string getRootCgroup()
{
{
std::lock_guard<std::mutex> guard(rootCgroupMutex);
if (rootCgroup)
return *rootCgroup;
}
auto current = getCurrentCgroup();
std::lock_guard<std::mutex> guard(rootCgroupMutex);
if (rootCgroup)
return *rootCgroup;
rootCgroup = current;
return current;
}
}

View file

@ -25,4 +25,13 @@ struct CgroupStats
*/
CgroupStats destroyCgroup(const Path & cgroup);
std::string getCurrentCgroup();
/**
* Get the cgroup that should be used as the parent when creating new
* sub-cgroups. The first time this is called, the current cgroup will be
* returned, and then all subsequent calls will return the original cgroup.
*/
std::string getRootCgroup();
}