]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/limits-util.c
update TODO
[thirdparty/systemd.git] / src / basic / limits-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
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"
11
12 uint64_t physical_memory(void) {
13 _cleanup_free_ char *root = NULL, *value = NULL;
14 uint64_t mem, lim;
15 size_t ps;
16 long sc;
17 int r;
18
19 /* We return this as uint64_t in case we are running as 32bit process on a 64bit kernel with huge amounts of
20 * memory.
21 *
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. */
24
25 sc = sysconf(_SC_PHYS_PAGES);
26 assert(sc > 0);
27
28 ps = page_size();
29 mem = (uint64_t) sc * (uint64_t) ps;
30
31 r = cg_get_root_path(&root);
32 if (r < 0) {
33 log_debug_errno(r, "Failed to determine root cgroup, ignoring cgroup memory limit: %m");
34 return mem;
35 }
36
37 r = cg_all_unified();
38 if (r < 0) {
39 log_debug_errno(r, "Failed to determine root unified mode, ignoring cgroup memory limit: %m");
40 return mem;
41 }
42 if (r > 0) {
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
47 * trying.) */
48 return mem;
49 if (r < 0) {
50 log_debug_errno(r, "Failed to read memory.max cgroup attribute, ignoring cgroup memory limit: %m");
51 return mem;
52 }
53
54 if (streq(value, "max"))
55 return mem;
56 } else {
57 r = cg_get_attribute("memory", root, "memory.limit_in_bytes", &value);
58 if (r < 0) {
59 log_debug_errno(r, "Failed to read memory.limit_in_bytes cgroup attribute, ignoring cgroup memory limit: %m");
60 return mem;
61 }
62 }
63
64 r = safe_atou64(value, &lim);
65 if (r < 0) {
66 log_debug_errno(r, "Failed to parse cgroup memory limit '%s', ignoring: %m", value);
67 return mem;
68 }
69 if (lim == UINT64_MAX)
70 return mem;
71
72 /* Make sure the limit is a multiple of our own page size */
73 lim /= ps;
74 lim *= ps;
75
76 return MIN(mem, lim);
77 }
78
79 uint64_t physical_memory_scale(uint64_t v, uint64_t max) {
80 uint64_t p, m, ps, r;
81
82 assert(max > 0);
83
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). */
86
87 ps = page_size();
88 assert(ps > 0);
89
90 p = physical_memory() / ps;
91 assert(p > 0);
92
93 m = p * v;
94 if (m / p != v)
95 return UINT64_MAX;
96
97 m /= max;
98
99 r = m * ps;
100 if (r / ps != m)
101 return UINT64_MAX;
102
103 return r;
104 }
105
106 uint64_t system_tasks_max(void) {
107 uint64_t a = TASKS_MAX, b = TASKS_MAX;
108 _cleanup_free_ char *root = NULL;
109 int r;
110
111 /* Determine the maximum number of tasks that may run on this system. We check three sources to determine this
112 * limit:
113 *
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
117 *
118 * And then pick the smallest of the three */
119
120 r = procfs_tasks_get_limit(&a);
121 if (r < 0)
122 log_debug_errno(r, "Failed to read maximum number of tasks from /proc, ignoring: %m");
123
124 r = cg_get_root_path(&root);
125 if (r < 0)
126 log_debug_errno(r, "Failed to determine cgroup root path, ignoring: %m");
127 else {
128 r = cg_get_attribute_as_uint64("pids", root, "pids.max", &b);
129 if (r < 0)
130 log_debug_errno(r, "Failed to read pids.max attribute of cgroup root, ignoring: %m");
131 }
132
133 return MIN3(TASKS_MAX,
134 a <= 0 ? TASKS_MAX : a,
135 b <= 0 ? TASKS_MAX : b);
136 }
137
138 uint64_t system_tasks_max_scale(uint64_t v, uint64_t max) {
139 uint64_t t, m;
140
141 assert(max > 0);
142
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. */
145
146 t = system_tasks_max();
147 assert(t > 0);
148
149 m = t * v;
150 if (m / t != v) /* overflow? */
151 return UINT64_MAX;
152
153 return m / max;
154 }