aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/globals.cc
diff options
context:
space:
mode:
authorAlex Wied <centromere@users.noreply.github.com>2022-07-19 02:09:46 -0400
committerAlex Wied <centromere@users.noreply.github.com>2022-07-19 16:25:53 -0400
commit722de8ddcc875c7e8e9a228f9d88454bae31fd40 (patch)
tree6ec425a1ab1a10c41c7a74ad0aa85c775b4dfe44 /src/libstore/globals.cc
parent1af5d798a4d10a07e995d420a759f2fe752b583a (diff)
libstore/globals.cc: Move cgroup detection to libutil
Diffstat (limited to 'src/libstore/globals.cc')
-rw-r--r--src/libstore/globals.cc54
1 files changed, 6 insertions, 48 deletions
diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc
index 48df839fa..d724897bb 100644
--- a/src/libstore/globals.cc
+++ b/src/libstore/globals.cc
@@ -11,11 +11,6 @@
#include <dlfcn.h>
#include <sys/utsname.h>
-#if __linux__
-#include <mntent.h>
-#include <cmath>
-#endif
-
#include <nlohmann/json.hpp>
@@ -119,50 +114,13 @@ std::vector<Path> getUserConfigFiles()
unsigned int Settings::getDefaultCores()
{
- unsigned int concurrency = std::max(1U, std::thread::hardware_concurrency());
-
- #if __linux__
- FILE *fp = fopen("/proc/mounts", "r");
- if (!fp)
- return concurrency;
-
- Strings cgPathParts;
-
- struct mntent *ent;
- while ((ent = getmntent(fp))) {
- std::string mountType, mountPath;
-
- mountType = ent->mnt_type;
- mountPath = ent->mnt_dir;
-
- if (mountType == "cgroup2") {
- cgPathParts.push_back(mountPath);
- break;
- }
- }
-
- fclose(fp);
-
- if (cgPathParts.size() > 0 && pathExists("/proc/self/cgroup")) {
- std::string currentCgroup = readFile("/proc/self/cgroup");
- Strings cgValues = tokenizeString<Strings>(currentCgroup, ":");
- cgPathParts.push_back(trim(cgValues.back(), "\n"));
- cgPathParts.push_back("cpu.max");
- std::string fullCgPath = canonPath(concatStringsSep("/", cgPathParts));
-
- if (pathExists(fullCgPath)) {
- std::string cpuMax = readFile(fullCgPath);
- std::vector<std::string> cpuMaxParts = tokenizeString<std::vector<std::string>>(cpuMax, " ");
- std::string quota = cpuMaxParts[0];
- std::string period = trim(cpuMaxParts[1], "\n");
-
- if (quota != "max")
- concurrency = std::ceil(std::stoi(quota) / std::stof(period));
- }
- }
- #endif
+ const unsigned int concurrency = std::max(1U, std::thread::hardware_concurrency());
+ const unsigned int maxCPU = getMaxCPU();
- return concurrency;
+ if (maxCPU > 0)
+ return maxCPU;
+ else
+ return concurrency;
}
StringSet Settings::getDefaultSystemFeatures()