]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/limits-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
4 #include "cgroup-util.h"
5 #include "limits-util.h"
6 #include "memory-util.h"
7 #include "parse-util.h"
8 #include "process-util.h"
9 #include "procfs-util.h"
10 #include "string-util.h"
12 uint64_t physical_memory(void) {
13 _cleanup_free_
char *root
= NULL
, *value
= NULL
;
19 /* We return this as uint64_t in case we are running as 32bit process on a 64bit kernel with huge amounts of
22 * In order to support containers nicely that have a configured memory limit we'll take the minimum of the
23 * physically reported amount of memory and the limit configured for the root cgroup, if there is any. */
25 sc
= sysconf(_SC_PHYS_PAGES
);
29 mem
= (uint64_t) sc
* (uint64_t) ps
;
31 r
= cg_get_root_path(&root
);
33 log_debug_errno(r
, "Failed to determine root cgroup, ignoring cgroup memory limit: %m");
39 log_debug_errno(r
, "Failed to determine root unified mode, ignoring cgroup memory limit: %m");
43 r
= cg_get_attribute("memory", root
, "memory.max", &value
);
44 if (r
== -ENOENT
) /* Field does not exist on the system's top-level cgroup, hence don't
45 * complain. (Note that it might exist on our own root though, if we live
46 * in a cgroup namespace, hence check anyway instead of not even
50 log_debug_errno(r
, "Failed to read memory.max cgroup attribute, ignoring cgroup memory limit: %m");
54 if (streq(value
, "max"))
57 r
= cg_get_attribute("memory", root
, "memory.limit_in_bytes", &value
);
59 log_debug_errno(r
, "Failed to read memory.limit_in_bytes cgroup attribute, ignoring cgroup memory limit: %m");
64 r
= safe_atou64(value
, &lim
);
66 log_debug_errno(r
, "Failed to parse cgroup memory limit '%s', ignoring: %m", value
);
69 if (lim
== UINT64_MAX
)
72 /* Make sure the limit is a multiple of our own page size */
79 uint64_t physical_memory_scale(uint64_t v
, uint64_t max
) {
84 /* Returns the physical memory size, multiplied by v divided by max. Returns UINT64_MAX on overflow. On success
85 * the result is a multiple of the page size (rounds down). */
90 p
= physical_memory() / ps
;
106 uint64_t system_tasks_max(void) {
107 uint64_t a
= TASKS_MAX
, b
= TASKS_MAX
;
108 _cleanup_free_
char *root
= NULL
;
111 /* Determine the maximum number of tasks that may run on this system. We check three sources to determine this
114 * a) the maximum tasks value the kernel allows on this architecture
115 * b) the cgroups pids_max attribute for the system
116 * c) the kernel's configured maximum PID value
118 * And then pick the smallest of the three */
120 r
= procfs_tasks_get_limit(&a
);
122 log_debug_errno(r
, "Failed to read maximum number of tasks from /proc, ignoring: %m");
124 r
= cg_get_root_path(&root
);
126 log_debug_errno(r
, "Failed to determine cgroup root path, ignoring: %m");
128 r
= cg_get_attribute_as_uint64("pids", root
, "pids.max", &b
);
130 log_debug_errno(r
, "Failed to read pids.max attribute of cgroup root, ignoring: %m");
133 return MIN3(TASKS_MAX
,
134 a
<= 0 ? TASKS_MAX
: a
,
135 b
<= 0 ? TASKS_MAX
: b
);
138 uint64_t system_tasks_max_scale(uint64_t v
, uint64_t max
) {
143 /* Multiply the system's task value by the fraction v/max. Hence, if max==100 this calculates percentages
144 * relative to the system's maximum number of tasks. Returns UINT64_MAX on overflow. */
146 t
= system_tasks_max();
150 if (m
/ t
!= v
) /* overflow? */