diff options
author | Eelco Dolstra <edolstra@gmail.com> | 2022-11-18 16:59:36 +0100 |
---|---|---|
committer | Eelco Dolstra <edolstra@gmail.com> | 2022-11-18 16:59:36 +0100 |
commit | e6b71f84a0a766429fdceaf188ea0167e36a20d9 (patch) | |
tree | a927096578f9b47314b72e06ff62def8cdccca38 | |
parent | fa68eb367e79297bb1c0451cd92ad18a06edce96 (diff) |
Use cgroup.kill to quickly kill cgroups
-rw-r--r-- | src/libstore/cgroup.cc | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/src/libstore/cgroup.cc b/src/libstore/cgroup.cc index 2a485f0f9..f693d77be 100644 --- a/src/libstore/cgroup.cc +++ b/src/libstore/cgroup.cc @@ -35,9 +35,19 @@ static CgroupStats destroyCgroup(const Path & cgroup, bool returnStats) { if (!pathExists(cgroup)) return {}; - if (!pathExists(cgroup + "/cgroup.procs")) + auto procsFile = cgroup + "/cgroup.procs"; + + if (!pathExists(procsFile)) throw Error("'%s' is not a cgroup", cgroup); + /* Use the fast way to kill every process in a cgroup, if + available. */ + auto killFile = cgroup + "/cgroup.kill"; + if (pathExists(killFile)) + writeFile(killFile, "1"); + + /* Otherwise, manually kill every process in the subcgroups and + this cgroup. */ for (auto & entry : readDirectory(cgroup)) { if (entry.type != DT_DIR) continue; destroyCgroup(cgroup + "/" + entry.name, false); @@ -48,7 +58,7 @@ static CgroupStats destroyCgroup(const Path & cgroup, bool returnStats) std::unordered_set<pid_t> pidsShown; while (true) { - auto pids = tokenizeString<std::vector<std::string>>(readFile(cgroup + "/cgroup.procs")); + auto pids = tokenizeString<std::vector<std::string>>(readFile(procsFile)); if (pids.empty()) break; |