summaryrefslogtreecommitdiff
path: root/src/basic/procfs-util.c
blob: 38f9fa1212b1da8f0440de04320170f773a2a537 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* SPDX-License-Identifier: LGPL-2.1+ */

#include <errno.h>

#include "alloc-util.h"
#include "def.h"
#include "fd-util.h"
#include "fileio.h"
#include "parse-util.h"
#include "process-util.h"
#include "procfs-util.h"
#include "stdio-util.h"
#include "string-util.h"

int procfs_tasks_get_limit(uint64_t *ret) {
        _cleanup_free_ char *value = NULL;
        uint64_t pid_max, threads_max;
        int r;

        assert(ret);

        /* So there are two sysctl files that control the system limit of processes:
         *
         * 1. kernel.threads-max: this is probably the sysctl that makes more sense, as it directly puts a limit on
         *    concurrent tasks.
         *
         * 2. kernel.pid_max: this limits the numeric range PIDs can take, and thus indirectly also limits the number
         *    of concurrent threads. AFAICS it's primarily a compatibility concept: some crappy old code used a signed
         *    16bit type for PIDs, hence the kernel provides a way to ensure the PIDs never go beyond INT16_MAX by
         *    default.
         *
         * By default #2 is set to much lower values than #1, hence the limit people come into contact with first, as
         * it's the lowest boundary they need to bump when they want higher number of processes.
         *
         * Also note the weird definition of #2: PIDs assigned will be kept below this value, which means the number of
         * tasks that can be created is one lower, as PID 0 is not a valid process ID. */

        r = read_one_line_file("/proc/sys/kernel/pid_max", &value);
        if (r < 0)
                return r;

        r = safe_atou64(value, &pid_max);
        if (r < 0)
                return r;

        value = mfree(value);
        r = read_one_line_file("/proc/sys/kernel/threads-max", &value);
        if (r < 0)
                return r;

        r = safe_atou64(value, &threads_max);
        if (r < 0)
                return r;

        /* Subtract one from pid_max, since PID 0 is not a valid PID */
        *ret = MIN(pid_max-1, threads_max);
        return 0;
}

int procfs_tasks_set_limit(uint64_t limit) {
        char buffer[DECIMAL_STR_MAX(uint64_t)+1];
        _cleanup_free_ char *value = NULL;
        uint64_t pid_max;
        int r;

        if (limit == 0) /* This makes no sense, we are userspace and hence count as tasks too, and we want to live,
                         * hence the limit conceptually has to be above 0. Also, most likely if anyone asks for a zero
                         * limit they probably mean "no limit", hence let's better refuse this to avoid
                         * confusion. */
                return -EINVAL;

        /* The Linux kernel doesn't allow this value to go below 20, hence don't allow this either, higher values than
         * TASKS_MAX are not accepted by the pid_max sysctl. We'll treat anything this high as "unbounded" and hence
         * set it to the maximum. */
        limit = CLAMP(limit, 20U, TASKS_MAX);

        r = read_one_line_file("/proc/sys/kernel/pid_max", &value);
        if (r < 0)
                return r;
        r = safe_atou64(value, &pid_max);
        if (r < 0)
                return r;

        /* As pid_max is about the numeric pid_t range we'll bump it if necessary, but only ever increase it, never
         * decrease it, as threads-max is the much more relevant sysctl. */
        if (limit > pid_max-1) {
                sprintf(buffer, "%" PRIu64, limit+1); /* Add one, since PID 0 is not a valid PID */
                r = write_string_file("/proc/sys/kernel/pid_max", buffer, WRITE_STRING_FILE_DISABLE_BUFFER);
                if (r < 0)
                        return r;
        }

        sprintf(buffer, "%" PRIu64, limit);
        r = write_string_file("/proc/sys/kernel/threads-max", buffer, WRITE_STRING_FILE_DISABLE_BUFFER);
        if (r < 0) {
                uint64_t threads_max;

                /* Hmm, we couldn't write this? If so, maybe it was already set properly? In that case let's not
                 * generate an error */

                value = mfree(value);
                if (read_one_line_file("/proc/sys/kernel/threads-max", &value) < 0)
                        return r; /* return original error */

                if (safe_atou64(value, &threads_max) < 0)
                        return r; /* return original error */

                if (MIN(pid_max-1, threads_max) != limit)
                        return r; /* return original error */

                /* Yay! Value set already matches what we were trying to set, hence consider this a success. */
        }

        return 0;
}

int procfs_tasks_get_current(uint64_t *ret) {
        _cleanup_free_ char *value = NULL;
        const char *p, *nr;
        size_t n;
        int r;

        assert(ret);

        r = read_one_line_file("/proc/loadavg", &value);
        if (r < 0)
                return r;

        /* Look for the second part of the fourth field, which is separated by a slash from the first part. None of the
         * earlier fields use a slash, hence let's use this to find the right spot. */
        p = strchr(value, '/');
        if (!p)
                return -EINVAL;

        p++;
        n = strspn(p, DIGITS);
        nr = strndupa(p, n);

        return safe_atou64(nr, ret);
}

static uint64_t calc_gcd64(uint64_t a, uint64_t b) {

        while (b > 0) {
                uint64_t t;

                t = a % b;

                a = b;
                b = t;
        }

        return a;
}

int procfs_cpu_get_usage(nsec_t *ret) {
        _cleanup_free_ char *first_line = NULL;
        unsigned long user_ticks, nice_ticks, system_ticks, irq_ticks, softirq_ticks,
                guest_ticks = 0, guest_nice_ticks = 0;
        long ticks_per_second;
        uint64_t sum, gcd, a, b;
        const char *p;
        int r;

        assert(ret);

        r = read_one_line_file("/proc/stat", &first_line);
        if (r < 0)
                return r;

        p = first_word(first_line, "cpu");
        if (!p)
                return -EINVAL;

        if (sscanf(p, "%lu %lu %lu %*u %*u %lu %lu %*u %lu %lu",
                   &user_ticks,
                   &nice_ticks,
                   &system_ticks,
                   &irq_ticks,
                   &softirq_ticks,
                   &guest_ticks,
                   &guest_nice_ticks) < 5) /* we only insist on the first five fields */
                return -EINVAL;

        ticks_per_second = sysconf(_SC_CLK_TCK);
        if (ticks_per_second  < 0)
                return -errno;
        assert(ticks_per_second > 0);

        sum = (uint64_t) user_ticks + (uint64_t) nice_ticks + (uint64_t) system_ticks +
                (uint64_t) irq_ticks + (uint64_t) softirq_ticks +
                (uint64_t) guest_ticks + (uint64_t) guest_nice_ticks;

        /* Let's reduce this fraction before we apply it to avoid overflows when converting this to µsec */
        gcd = calc_gcd64(NSEC_PER_SEC, ticks_per_second);

        a = (uint64_t) NSEC_PER_SEC / gcd;
        b = (uint64_t) ticks_per_second / gcd;

        *ret = DIV_ROUND_UP((nsec_t) sum * (nsec_t) a, (nsec_t) b);
        return 0;
}

int procfs_memory_get(uint64_t *ret_total, uint64_t *ret_used) {
        uint64_t mem_total = UINT64_MAX, mem_free = UINT64_MAX;
        _cleanup_fclose_ FILE *f = NULL;
        int r;

        f = fopen("/proc/meminfo", "re");
        if (!f)
                return -errno;

        for (;;) {
                _cleanup_free_ char *line = NULL;
                uint64_t *v;
                char *p, *e;
                size_t n;

                r = read_line(f, LONG_LINE_MAX, &line);
                if (r < 0)
                        return r;
                if (r == 0)
                        return -EINVAL; /* EOF: Couldn't find one or both fields? */

                p = first_word(line, "MemTotal:");
                if (p)
                        v = &mem_total;
                else {
                        p = first_word(line, "MemFree:");
                        if (p)
                                v = &mem_free;
                        else
                                continue;
                }

                /* Determine length of numeric value */
                n = strspn(p, DIGITS);
                if (n == 0)
                        return -EINVAL;
                e = p + n;

                /* Ensure the line ends in " kB" */
                n = strspn(e, WHITESPACE);
                if (n == 0)
                        return -EINVAL;
                if (!streq(e + n, "kB"))
                        return -EINVAL;

                *e = 0;
                r = safe_atou64(p, v);
                if (r < 0)
                        return r;
                if (*v == UINT64_MAX)
                        return -EINVAL;

                if (mem_total != UINT64_MAX && mem_free != UINT64_MAX)
                        break;
        }

        if (mem_free > mem_total)
                return -EINVAL;

        if (ret_total)
                *ret_total = mem_total * 1024U;
        if (ret_used)
                *ret_used = (mem_total - mem_free) * 1024U;
        return 0;
}