Merge pull request #9020 from poettering/physical-memory-cgroupsv2
make sure physical_memory() groks cgroupsv2
This commit is contained in:
+47
-7
@@ -405,6 +405,7 @@ uint64_t physical_memory(void) {
|
||||
uint64_t mem, lim;
|
||||
size_t ps;
|
||||
long sc;
|
||||
int r;
|
||||
|
||||
/* We return this as uint64_t in case we are running as 32bit process on a 64bit kernel with huge amounts of
|
||||
* memory.
|
||||
@@ -418,13 +419,40 @@ uint64_t physical_memory(void) {
|
||||
ps = page_size();
|
||||
mem = (uint64_t) sc * (uint64_t) ps;
|
||||
|
||||
if (cg_get_root_path(&root) < 0)
|
||||
r = cg_get_root_path(&root);
|
||||
if (r < 0) {
|
||||
log_debug_errno(r, "Failed to determine root cgroup, ignoring cgroup memory limit: %m");
|
||||
return mem;
|
||||
}
|
||||
|
||||
if (cg_get_attribute("memory", root, "memory.limit_in_bytes", &value))
|
||||
r = cg_all_unified();
|
||||
if (r < 0) {
|
||||
log_debug_errno(r, "Failed to determine root unified mode, ignoring cgroup memory limit: %m");
|
||||
return mem;
|
||||
}
|
||||
if (r > 0) {
|
||||
r = cg_get_attribute("memory", root, "memory.max", &value);
|
||||
if (r < 0) {
|
||||
log_debug_errno(r, "Failed to read memory.max cgroup attribute, ignoring cgroup memory limit: %m");
|
||||
return mem;
|
||||
}
|
||||
|
||||
if (safe_atou64(value, &lim) < 0)
|
||||
if (streq(value, "max"))
|
||||
return mem;
|
||||
} else {
|
||||
r = cg_get_attribute("memory", root, "memory.limit_in_bytes", &value);
|
||||
if (r < 0) {
|
||||
log_debug_errno(r, "Failed to read memory.limit_in_bytes cgroup attribute, ignoring cgroup memory limit: %m");
|
||||
return mem;
|
||||
}
|
||||
}
|
||||
|
||||
r = safe_atou64(value, &lim);
|
||||
if (r < 0) {
|
||||
log_debug_errno(r, "Failed to parse cgroup memory limit '%s', ignoring: %m", value);
|
||||
return mem;
|
||||
}
|
||||
if (lim == UINT64_MAX)
|
||||
return mem;
|
||||
|
||||
/* Make sure the limit is a multiple of our own page size */
|
||||
@@ -465,6 +493,7 @@ uint64_t system_tasks_max(void) {
|
||||
|
||||
uint64_t a = TASKS_MAX, b = TASKS_MAX;
|
||||
_cleanup_free_ char *root = NULL;
|
||||
int r;
|
||||
|
||||
/* Determine the maximum number of tasks that may run on this system. We check three sources to determine this
|
||||
* limit:
|
||||
@@ -475,13 +504,24 @@ uint64_t system_tasks_max(void) {
|
||||
*
|
||||
* And then pick the smallest of the three */
|
||||
|
||||
(void) procfs_tasks_get_limit(&a);
|
||||
r = procfs_tasks_get_limit(&a);
|
||||
if (r < 0)
|
||||
log_debug_errno(r, "Failed to read maximum number of tasks from /proc, ignoring: %m");
|
||||
|
||||
if (cg_get_root_path(&root) >= 0) {
|
||||
r = cg_get_root_path(&root);
|
||||
if (r < 0)
|
||||
log_debug_errno(r, "Failed to determine cgroup root path, ignoring: %m");
|
||||
else {
|
||||
_cleanup_free_ char *value = NULL;
|
||||
|
||||
if (cg_get_attribute("pids", root, "pids.max", &value) >= 0)
|
||||
(void) safe_atou64(value, &b);
|
||||
r = cg_get_attribute("pids", root, "pids.max", &value);
|
||||
if (r < 0)
|
||||
log_debug_errno(r, "Failed to read pids.max attribute of cgroup root, ignoring: %m");
|
||||
else if (!streq(value, "max")) {
|
||||
r = safe_atou64(value, &b);
|
||||
if (r < 0)
|
||||
log_debug_errno(r, "Failed to parse pids.max attribute of cgroup root, ignoring: %m");
|
||||
}
|
||||
}
|
||||
|
||||
return MIN3(TASKS_MAX,
|
||||
|
||||
Reference in New Issue
Block a user