Debian Codegraph @

src/test/test-procfs-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "errno-util.h"
4 #include "format-util.h"
5 #include "log.h"
6 #include "process-util.h"
7 #include "procfs-util.h"
8 #include "tests.h"
9 #include "time-util.h"
10
11 int main(int argc, char *argv[]) {
12 nsec_t nsec;
13 uint64_t v, pid_max, threads_max, limit;
14 int r;
15
16 test_setup_logging(LOG_DEBUG);
17
18 assert_se(procfs_cpu_get_usage(&nsec) >= 0);
19 log_info("Current system CPU time: %s", FORMAT_TIMESPAN(nsec/NSEC_PER_USEC, 1));
20
21 assert_se(procfs_memory_get_used(&v) >= 0);
22 log_info("Current memory usage: %s", FORMAT_BYTES(v));
23
24 assert_se(procfs_tasks_get_current(&v) >= 0);
25 log_info("Current number of tasks: %" PRIu64, v);
26
27 pid_max = TASKS_MAX;
28 r = procfs_get_pid_max(&pid_max);
29 if (r == -ENOENT || ERRNO_IS_NEG_PRIVILEGE(r))
30 return log_tests_skipped_errno(r, "can't get pid max");
31 assert(r >= 0);
32 log_info("kernel.pid_max: %"PRIu64, pid_max);
33
34 threads_max = TASKS_MAX;
35 r = procfs_get_threads_max(&threads_max);
36 if (r == -ENOENT || ERRNO_IS_NEG_PRIVILEGE(r))
37 return log_tests_skipped_errno(r, "can't get threads max");
38 assert(r >= 0);
39 log_info("kernel.threads-max: %"PRIu64, threads_max);
40
41 limit = MIN(pid_max - (pid_max > 0), threads_max);
42
43 assert_se(r >= 0);
44 log_info("Limit of tasks: %" PRIu64, limit);
45 assert_se(limit > 0);
46
47 /* This call should never fail, as we're trying to set it to the same limit */
48 assert(procfs_tasks_set_limit(limit) >= 0);
49
50 if (limit > 100) {
51 log_info("Reducing limit by one to %"PRIu64"…", limit-1);
52
53 r = procfs_tasks_set_limit(limit-1);
54 if (r == -ENOENT || ERRNO_IS_NEG_FS_WRITE_REFUSED(r))
55 return log_tests_skipped_errno(r, "can't set tasks limit");
56 assert_se(r >= 0);
57
58 assert_se(procfs_get_pid_max(&v) >= 0);
59 /* We never decrease the pid_max, so it shouldn't have changed */
60 assert_se(v == pid_max);
61
62 assert_se(procfs_get_threads_max(&v) >= 0);
63 assert_se(v == limit-1);
64
65 assert_se(procfs_tasks_set_limit(limit) >= 0);
66
67 assert_se(procfs_get_pid_max(&v) >= 0);
68 assert_se(v == pid_max);
69
70 assert_se(procfs_get_threads_max(&v) >= 0);
71 assert_se(v == limit);
72 }
73
74 return 0;
75 }