1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
9 #include "parse-util.h"
10 #include "process-util.h"
11 #include "procfs-util.h"
12 #include "stdio-util.h"
13 #include "string-util.h"
15 int procfs_tasks_get_limit(uint64_t *ret
) {
16 _cleanup_free_
char *value
= NULL
;
17 uint64_t pid_max
, threads_max
;
22 /* So there are two sysctl files that control the system limit of processes:
24 * 1. kernel.threads-max: this is probably the sysctl that makes more sense, as it directly puts a limit on
27 * 2. kernel.pid_max: this limits the numeric range PIDs can take, and thus indirectly also limits the number
28 * of concurrent threads. AFAICS it's primarily a compatibility concept: some crappy old code used a signed
29 * 16bit type for PIDs, hence the kernel provides a way to ensure the PIDs never go beyond INT16_MAX by
32 * By default #2 is set to much lower values than #1, hence the limit people come into contact with first, as
33 * it's the lowest boundary they need to bump when they want higher number of processes.
35 * Also note the weird definition of #2: PIDs assigned will be kept below this value, which means the number of
36 * tasks that can be created is one lower, as PID 0 is not a valid process ID. */
38 r
= read_one_line_file("/proc/sys/kernel/pid_max", &value
);
42 r
= safe_atou64(value
, &pid_max
);
47 r
= read_one_line_file("/proc/sys/kernel/threads-max", &value
);
51 r
= safe_atou64(value
, &threads_max
);
55 /* Subtract one from pid_max, since PID 0 is not a valid PID */
56 *ret
= MIN(pid_max
-1, threads_max
);
60 int procfs_tasks_set_limit(uint64_t limit
) {
61 char buffer
[DECIMAL_STR_MAX(uint64_t)+1];
62 _cleanup_free_
char *value
= NULL
;
66 if (limit
== 0) /* This makes no sense, we are userspace and hence count as tasks too, and we want to live,
67 * hence the limit conceptually has to be above 0. Also, most likely if anyone asks for a zero
68 * limit he/she probably means "no limit", hence let's better refuse this to avoid
72 /* The Linux kernel doesn't allow this value to go below 20, hence don't allow this either, higher values than
73 * TASKS_MAX are not accepted by the pid_max sysctl. We'll treat anything this high as "unbounded" and hence
74 * set it to the maximum. */
75 limit
= CLAMP(limit
, 20U, TASKS_MAX
);
77 r
= read_one_line_file("/proc/sys/kernel/pid_max", &value
);
80 r
= safe_atou64(value
, &pid_max
);
84 /* As pid_max is about the numeric pid_t range we'll bump it if necessary, but only ever increase it, never
85 * decrease it, as threads-max is the much more relevant sysctl. */
86 if (limit
> pid_max
-1) {
87 sprintf(buffer
, "%" PRIu64
, limit
+1); /* Add one, since PID 0 is not a valid PID */
88 r
= write_string_file("/proc/sys/kernel/pid_max", buffer
, WRITE_STRING_FILE_DISABLE_BUFFER
);
93 sprintf(buffer
, "%" PRIu64
, limit
);
94 r
= write_string_file("/proc/sys/kernel/threads-max", buffer
, WRITE_STRING_FILE_DISABLE_BUFFER
);
98 /* Hmm, we couldn't write this? If so, maybe it was already set properly? In that case let's not
99 * generate an error */
101 value
= mfree(value
);
102 if (read_one_line_file("/proc/sys/kernel/threads-max", &value
) < 0)
103 return r
; /* return original error */
105 if (safe_atou64(value
, &threads_max
) < 0)
106 return r
; /* return original error */
108 if (MIN(pid_max
-1, threads_max
) != limit
)
109 return r
; /* return original error */
111 /* Yay! Value set already matches what we were trying to set, hence consider this a success. */
117 int procfs_tasks_get_current(uint64_t *ret
) {
118 _cleanup_free_
char *value
= NULL
;
125 r
= read_one_line_file("/proc/loadavg", &value
);
129 /* Look for the second part of the fourth field, which is separated by a slash from the first part. None of the
130 * earlier fields use a slash, hence let's use this to find the right spot. */
131 p
= strchr(value
, '/');
136 n
= strspn(p
, DIGITS
);
139 return safe_atou64(nr
, ret
);
142 static uint64_t calc_gcd64(uint64_t a
, uint64_t b
) {
156 int procfs_cpu_get_usage(nsec_t
*ret
) {
157 _cleanup_free_
char *first_line
= NULL
;
158 unsigned long user_ticks
, nice_ticks
, system_ticks
, irq_ticks
, softirq_ticks
,
159 guest_ticks
= 0, guest_nice_ticks
= 0;
160 long ticks_per_second
;
161 uint64_t sum
, gcd
, a
, b
;
167 r
= read_one_line_file("/proc/stat", &first_line
);
171 p
= first_word(first_line
, "cpu");
175 if (sscanf(p
, "%lu %lu %lu %*u %*u %lu %lu %*u %lu %lu",
182 &guest_nice_ticks
) < 5) /* we only insist on the first five fields */
185 ticks_per_second
= sysconf(_SC_CLK_TCK
);
186 if (ticks_per_second
< 0)
188 assert(ticks_per_second
> 0);
190 sum
= (uint64_t) user_ticks
+ (uint64_t) nice_ticks
+ (uint64_t) system_ticks
+
191 (uint64_t) irq_ticks
+ (uint64_t) softirq_ticks
+
192 (uint64_t) guest_ticks
+ (uint64_t) guest_nice_ticks
;
194 /* Let's reduce this fraction before we apply it to avoid overflows when converting this to µsec */
195 gcd
= calc_gcd64(NSEC_PER_SEC
, ticks_per_second
);
197 a
= (uint64_t) NSEC_PER_SEC
/ gcd
;
198 b
= (uint64_t) ticks_per_second
/ gcd
;
200 *ret
= DIV_ROUND_UP((nsec_t
) sum
* (nsec_t
) a
, (nsec_t
) b
);
204 int procfs_memory_get(uint64_t *ret_total
, uint64_t *ret_used
) {
205 uint64_t mem_total
= UINT64_MAX
, mem_free
= UINT64_MAX
;
206 _cleanup_fclose_
FILE *f
= NULL
;
209 f
= fopen("/proc/meminfo", "re");
214 _cleanup_free_
char *line
= NULL
;
219 r
= read_line(f
, LONG_LINE_MAX
, &line
);
223 return -EINVAL
; /* EOF: Couldn't find one or both fields? */
225 p
= first_word(line
, "MemTotal:");
229 p
= first_word(line
, "MemFree:");
236 /* Determine length of numeric value */
237 n
= strspn(p
, DIGITS
);
242 /* Ensure the line ends in " kB" */
243 n
= strspn(e
, WHITESPACE
);
246 if (!streq(e
+ n
, "kB"))
250 r
= safe_atou64(p
, v
);
253 if (*v
== UINT64_MAX
)
256 if (mem_total
!= UINT64_MAX
&& mem_free
!= UINT64_MAX
)
260 if (mem_free
> mem_total
)
264 *ret_total
= mem_total
* 1024U;
266 *ret_used
= (mem_total
- mem_free
) * 1024U;