]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/limits-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
6 #include "cgroup-util.h"
7 #include "limits-util.h"
8 #include "memory-util.h"
9 #include "parse-util.h"
10 #include "process-util.h"
11 #include "procfs-util.h"
12 #include "string-util.h"
14 uint64_t physical_memory(void) {
15 _cleanup_free_
char *root
= NULL
, *value
= NULL
;
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
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. */
27 sc
= sysconf(_SC_PHYS_PAGES
);
31 mem
= (uint64_t) sc
* (uint64_t) ps
;
33 r
= cg_get_root_path(&root
);
35 log_debug_errno(r
, "Failed to determine root cgroup, ignoring cgroup memory limit: %m");
41 log_debug_errno(r
, "Failed to determine root unified mode, ignoring cgroup memory limit: %m");
45 r
= cg_get_attribute("memory", root
, "memory.max", &value
);
46 if (r
== -ENOENT
) /* Field does not exist on the system's top-level cgroup, hence don't
47 * complain. (Note that it might exist on our own root though, if we live
48 * in a cgroup namespace, hence check anyway instead of not even
52 log_debug_errno(r
, "Failed to read memory.max cgroup attribute, ignoring cgroup memory limit: %m");
56 if (streq(value
, "max"))
59 r
= cg_get_attribute("memory", root
, "memory.limit_in_bytes", &value
);
61 log_debug_errno(r
, "Failed to read memory.limit_in_bytes cgroup attribute, ignoring cgroup memory limit: %m");
66 r
= safe_atou64(value
, &lim
);
68 log_debug_errno(r
, "Failed to parse cgroup memory limit '%s', ignoring: %m", value
);
71 if (lim
== UINT64_MAX
)
74 /* Make sure the limit is a multiple of our own page size */
81 uint64_t physical_memory_scale(uint64_t v
, uint64_t max
) {
84 /* Shortcut two special cases */
88 return physical_memory();
92 /* Returns the physical memory size, multiplied by v divided by max. Returns UINT64_MAX on overflow. On success
93 * the result is a multiple of the page size (rounds down). */
98 p
= physical_memory() / ps
;
101 if (v
> UINT64_MAX
/ p
)
107 if (m
> UINT64_MAX
/ ps
)
113 uint64_t system_tasks_max(void) {
114 uint64_t a
= TASKS_MAX
, b
= TASKS_MAX
, c
= TASKS_MAX
;
115 _cleanup_free_
char *root
= NULL
;
118 /* Determine the maximum number of tasks that may run on this system. We check three sources to
119 * determine this limit:
121 * a) kernel.threads-max sysctl: the maximum number of tasks (threads) the kernel allows.
123 * This puts a direct limit on the number of concurrent tasks.
125 * b) kernel.pid_max sysctl: the maximum PID value.
127 * This limits the numeric range PIDs can take, and thus indirectly also limits the number of
128 * concurrent threads. It's primarily a compatibility concept: some crappy old code used a signed
129 * 16-bit type for PIDs, hence the kernel provides a way to ensure the PIDs never go beyond
130 * INT16_MAX by default.
132 * Also note the weird definition: PIDs assigned will be kept below this value, which means
133 * the number of tasks that can be created is one lower, as PID 0 is not a valid process ID.
135 * c) pids.max on the root cgroup: the kernel's configured maximum number of tasks.
137 * and then pick the smallest of the three.
139 * By default pid_max is set to much lower values than threads-max, hence the limit people come into
140 * contact with first, as it's the lowest boundary they need to bump when they want higher number of
144 r
= procfs_get_threads_max(&a
);
146 log_debug_errno(r
, "Failed to read kernel.threads-max, ignoring: %m");
148 r
= procfs_get_pid_max(&b
);
150 log_debug_errno(r
, "Failed to read kernel.pid_max, ignoring: %m");
152 /* Subtract one from pid_max, since PID 0 is not a valid PID */
155 r
= cg_get_root_path(&root
);
157 log_debug_errno(r
, "Failed to determine cgroup root path, ignoring: %m");
159 r
= cg_get_attribute_as_uint64("pids", root
, "pids.max", &c
);
161 log_debug_errno(r
, "Failed to read pids.max attribute of root cgroup, ignoring: %m");
164 return MIN3(a
, b
, c
);
167 uint64_t system_tasks_max_scale(uint64_t v
, uint64_t max
) {
170 /* Shortcut two special cases */
174 return system_tasks_max();
178 /* Multiply the system's task value by the fraction v/max. Hence, if max==100 this calculates percentages
179 * relative to the system's maximum number of tasks. Returns UINT64_MAX on overflow. */
181 t
= system_tasks_max();
184 if (v
> UINT64_MAX
/ t
) /* overflow? */