| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "cgroup-util.h" |
| 7 |
#include "limits-util.h" |
| 8 |
#include "log.h" |
| 9 |
#include "memory-util.h" |
| 10 |
#include "parse-util.h" |
| 11 |
#include "process-util.h" |
| 12 |
#include "procfs-util.h" |
| 13 |
|
| 14 |
uint64_t physical_memory(void) { |
| 15 |
_cleanup_free_ char *root = NULL, *value = NULL; |
| 16 |
uint64_t mem, lim; |
| 17 |
size_t ps; |
| 18 |
long sc; |
| 19 |
int r; |
| 20 |
|
| 21 |
/* We return this as uint64_t in case we are running as 32-bit process on a 64-bit kernel with huge amounts of |
| 22 |
* memory. |
| 23 |
* |
| 24 |
* In order to support containers nicely that have a configured memory limit we'll take the minimum of the |
| 25 |
* physically reported amount of memory and the limit configured for the root cgroup, if there is any. */ |
| 26 |
|
| 27 |
sc = sysconf(_SC_PHYS_PAGES); |
| 28 |
assert(sc > 0); |
| 29 |
|
| 30 |
ps = page_size(); |
| 31 |
/* Physical page count times page size cannot realistically overflow uint64_t, |
| 32 |
* but use MUL_SAFE to make this obvious to static analyzers. */ |
| 33 |
assert_se(MUL_SAFE(&mem, (uint64_t) sc, (uint64_t) ps)); |
| 34 |
|
| 35 |
r = cg_get_root_path(&root); |
| 36 |
if (r < 0) { |
| 37 |
log_debug_errno(r, "Failed to determine root cgroup, ignoring cgroup memory limit: %m"); |
| 38 |
return mem; |
| 39 |
} |
| 40 |
|
| 41 |
r = cg_get_attribute(root, "memory.max", &value); |
| 42 |
if (r == -ENOENT) /* Field does not exist on the system's top-level cgroup, hence don't |
| 43 |
* complain. (Note that it might exist on our own root though, if we live |
| 44 |
* in a cgroup namespace, hence check anyway instead of not even |
| 45 |
* trying.) */ |
| 46 |
return mem; |
| 47 |
if (r < 0) { |
| 48 |
log_debug_errno(r, "Failed to read memory.max cgroup attribute, ignoring cgroup memory limit: %m"); |
| 49 |
return mem; |
| 50 |
} |
| 51 |
|
| 52 |
if (streq(value, "max")) |
| 53 |
return mem; |
| 54 |
|
| 55 |
r = safe_atou64(value, &lim); |
| 56 |
if (r < 0) { |
| 57 |
log_debug_errno(r, "Failed to parse cgroup memory limit '%s', ignoring: %m", value); |
| 58 |
return mem; |
| 59 |
} |
| 60 |
if (lim == UINT64_MAX) |
| 61 |
return mem; |
| 62 |
|
| 63 |
/* Make sure the limit is a multiple of our own page size */ |
| 64 |
lim /= ps; |
| 65 |
lim *= ps; |
| 66 |
|
| 67 |
return MIN(mem, lim); |
| 68 |
} |
| 69 |
|
| 70 |
uint64_t physical_memory_scale(uint64_t v, uint64_t max) { |
| 71 |
uint64_t p, m, ps; |
| 72 |
|
| 73 |
/* Shortcut two special cases */ |
| 74 |
if (v == 0) |
| 75 |
return 0; |
| 76 |
if (v == max) |
| 77 |
return physical_memory(); |
| 78 |
|
| 79 |
assert(max > 0); |
| 80 |
|
| 81 |
/* Returns the physical memory size, multiplied by v divided by max. Returns UINT64_MAX on overflow. On success |
| 82 |
* the result is a multiple of the page size (rounds down). */ |
| 83 |
|
| 84 |
ps = page_size(); |
| 85 |
assert(ps > 0); |
| 86 |
|
| 87 |
p = physical_memory() / ps; |
| 88 |
assert(p > 0); |
| 89 |
|
| 90 |
if (v > UINT64_MAX / p) |
| 91 |
return UINT64_MAX; |
| 92 |
|
| 93 |
m = p * v; |
| 94 |
m /= max; |
| 95 |
|
| 96 |
if (m > UINT64_MAX / ps) |
| 97 |
return UINT64_MAX; |
| 98 |
|
| 99 |
return m * ps; |
| 100 |
} |
| 101 |
|
| 102 |
uint64_t system_tasks_max(void) { |
| 103 |
uint64_t a = TASKS_MAX, b = TASKS_MAX, c = TASKS_MAX; |
| 104 |
_cleanup_free_ char *root = NULL; |
| 105 |
int r; |
| 106 |
|
| 107 |
/* Determine the maximum number of tasks that may run on this system. We check three sources to |
| 108 |
* determine this limit: |
| 109 |
* |
| 110 |
* a) kernel.threads-max sysctl: the maximum number of tasks (threads) the kernel allows. |
| 111 |
* |
| 112 |
* This puts a direct limit on the number of concurrent tasks. |
| 113 |
* |
| 114 |
* b) kernel.pid_max sysctl: the maximum PID value. |
| 115 |
* |
| 116 |
* This limits the numeric range PIDs can take, and thus indirectly also limits the number of |
| 117 |
* concurrent threads. It's primarily a compatibility concept: some crappy old code used a signed |
| 118 |
* 16-bit type for PIDs, hence the kernel provides a way to ensure the PIDs never go beyond |
| 119 |
* INT16_MAX by default. |
| 120 |
* |
| 121 |
* Also note the weird definition: PIDs assigned will be kept below this value, which means |
| 122 |
* the number of tasks that can be created is one lower, as PID 0 is not a valid process ID. |
| 123 |
* |
| 124 |
* c) pids.max on the root cgroup: the kernel's configured maximum number of tasks. |
| 125 |
* |
| 126 |
* and then pick the smallest of the three. |
| 127 |
* |
| 128 |
* By default pid_max is set to much lower values than threads-max, hence the limit people come into |
| 129 |
* contact with first, as it's the lowest boundary they need to bump when they want higher number of |
| 130 |
* processes. |
| 131 |
*/ |
| 132 |
|
| 133 |
r = procfs_get_threads_max(&a); |
| 134 |
if (r < 0) |
| 135 |
log_debug_errno(r, "Failed to read kernel.threads-max, ignoring: %m"); |
| 136 |
|
| 137 |
r = procfs_get_pid_max(&b); |
| 138 |
if (r < 0) |
| 139 |
log_debug_errno(r, "Failed to read kernel.pid_max, ignoring: %m"); |
| 140 |
else if (b > 0) |
| 141 |
/* Subtract one from pid_max, since PID 0 is not a valid PID */ |
| 142 |
b--; |
| 143 |
|
| 144 |
r = cg_get_root_path(&root); |
| 145 |
if (r < 0) |
| 146 |
log_debug_errno(r, "Failed to determine cgroup root path, ignoring: %m"); |
| 147 |
else { |
| 148 |
/* We'll have the "pids.max" attribute on the our root cgroup only if we are in a |
| 149 |
* CLONE_NEWCGROUP namespace. On the top-level namespace this attribute is missing, hence |
| 150 |
* suppress any message about that */ |
| 151 |
r = cg_get_attribute_as_uint64(root, "pids.max", &c); |
| 152 |
if (r < 0 && r != -ENODATA) |
| 153 |
log_debug_errno(r, "Failed to read pids.max attribute of root cgroup, ignoring: %m"); |
| 154 |
} |
| 155 |
|
| 156 |
return MIN3(a, b, c); |
| 157 |
} |
| 158 |
|
| 159 |
uint64_t system_tasks_max_scale(uint64_t v, uint64_t max) { |
| 160 |
uint64_t t, m; |
| 161 |
|
| 162 |
/* Shortcut two special cases */ |
| 163 |
if (v == 0) |
| 164 |
return 0; |
| 165 |
if (v == max) |
| 166 |
return system_tasks_max(); |
| 167 |
|
| 168 |
assert(max > 0); |
| 169 |
|
| 170 |
/* Multiply the system's task value by the fraction v/max. Hence, if max==100 this calculates percentages |
| 171 |
* relative to the system's maximum number of tasks. Returns UINT64_MAX on overflow. */ |
| 172 |
|
| 173 |
t = system_tasks_max(); |
| 174 |
assert(t > 0); |
| 175 |
|
| 176 |
if (v > UINT64_MAX / t) /* overflow? */ |
| 177 |
return UINT64_MAX; |
| 178 |
|
| 179 |
m = t * v; |
| 180 |
return m / max; |
| 181 |
} |