diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2022-12-02 12:57:41 +0100 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2022-12-02 12:59:13 +0100 |
commit | 1e6a5d1ff6e8ef5bf340502f74c4d5039cedc67a (patch) | |
tree | 59899facd4e1989db3f0370aa2e75d0748577d66 /src/libutil/cgroup.cc | |
parent | 1211e59a038379026496bbee4b203bbd66833b01 (diff) |
Clean up cgroup handling in getMaxCPU()
Also, don't assume in LocalDerivationGoal that cgroups are mounted on
/sys/fs/cgroup.
Diffstat (limited to 'src/libutil/cgroup.cc')
-rw-r--r-- | src/libutil/cgroup.cc | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/libutil/cgroup.cc b/src/libutil/cgroup.cc index f693d77be..a008481ca 100644 --- a/src/libutil/cgroup.cc +++ b/src/libutil/cgroup.cc @@ -2,6 +2,7 @@ #include "cgroup.hh" #include "util.hh" +#include "finally.hh" #include <chrono> #include <cmath> @@ -10,9 +11,25 @@ #include <thread> #include <dirent.h> +#include <mntent.h> namespace nix { +std::optional<Path> getCgroupFS() +{ + static auto res = [&]() -> std::optional<Path> { + auto fp = fopen("/proc/mounts", "r"); + if (!fp) return std::nullopt; + Finally delFP = [&]() { fclose(fp); }; + while (auto ent = getmntent(fp)) + if (std::string_view(ent->mnt_type) == "cgroup2") + return ent->mnt_dir; + + return std::nullopt; + }(); + return res; +} + // FIXME: obsolete, check for cgroup2 std::map<std::string, std::string> getCgroups(const Path & cgroupFile) { |