File: | build-scan/../src/cgtop/cgtop.c |
Warning: | line 849, column 29 Null pointer passed to 1st parameter expecting 'nonnull' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |||
2 | ||||
3 | #include <alloca.h> | |||
4 | #include <errno(*__errno_location ()).h> | |||
5 | #include <getopt.h> | |||
6 | #include <signal.h> | |||
7 | #include <stdint.h> | |||
8 | #include <stdlib.h> | |||
9 | #include <string.h> | |||
10 | #include <unistd.h> | |||
11 | ||||
12 | #include "sd-bus.h" | |||
13 | ||||
14 | #include "alloc-util.h" | |||
15 | #include "bus-error.h" | |||
16 | #include "bus-util.h" | |||
17 | #include "cgroup-show.h" | |||
18 | #include "cgroup-util.h" | |||
19 | #include "fd-util.h" | |||
20 | #include "fileio.h" | |||
21 | #include "hashmap.h" | |||
22 | #include "parse-util.h" | |||
23 | #include "path-util.h" | |||
24 | #include "process-util.h" | |||
25 | #include "procfs-util.h" | |||
26 | #include "stdio-util.h" | |||
27 | #include "strv.h" | |||
28 | #include "terminal-util.h" | |||
29 | #include "unit-name.h" | |||
30 | #include "util.h" | |||
31 | #include "virt.h" | |||
32 | ||||
33 | typedef struct Group { | |||
34 | char *path; | |||
35 | ||||
36 | bool_Bool n_tasks_valid:1; | |||
37 | bool_Bool cpu_valid:1; | |||
38 | bool_Bool memory_valid:1; | |||
39 | bool_Bool io_valid:1; | |||
40 | ||||
41 | uint64_t n_tasks; | |||
42 | ||||
43 | unsigned cpu_iteration; | |||
44 | nsec_t cpu_usage; | |||
45 | nsec_t cpu_timestamp; | |||
46 | double cpu_fraction; | |||
47 | ||||
48 | uint64_t memory; | |||
49 | ||||
50 | unsigned io_iteration; | |||
51 | uint64_t io_input, io_output; | |||
52 | nsec_t io_timestamp; | |||
53 | uint64_t io_input_bps, io_output_bps; | |||
54 | } Group; | |||
55 | ||||
56 | static unsigned arg_depth = 3; | |||
57 | static unsigned arg_iterations = (unsigned) -1; | |||
58 | static bool_Bool arg_batch = false0; | |||
59 | static bool_Bool arg_raw = false0; | |||
60 | static usec_t arg_delay = 1*USEC_PER_SEC((usec_t) 1000000ULL); | |||
61 | static char* arg_machine = NULL((void*)0); | |||
62 | static char* arg_root = NULL((void*)0); | |||
63 | static bool_Bool arg_recursive = true1; | |||
64 | static bool_Bool arg_recursive_unset = false0; | |||
65 | ||||
66 | static enum { | |||
67 | COUNT_PIDS, | |||
68 | COUNT_USERSPACE_PROCESSES, | |||
69 | COUNT_ALL_PROCESSES, | |||
70 | } arg_count = COUNT_PIDS; | |||
71 | ||||
72 | static enum { | |||
73 | ORDER_PATH, | |||
74 | ORDER_TASKS, | |||
75 | ORDER_CPU, | |||
76 | ORDER_MEMORY, | |||
77 | ORDER_IO, | |||
78 | } arg_order = ORDER_CPU; | |||
79 | ||||
80 | static enum { | |||
81 | CPU_PERCENT, | |||
82 | CPU_TIME, | |||
83 | } arg_cpu_type = CPU_PERCENT; | |||
84 | ||||
85 | static void group_free(Group *g) { | |||
86 | assert(g)do { if ((__builtin_expect(!!(!(g)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("g"), "../src/cgtop/cgtop.c", 86, __PRETTY_FUNCTION__ ); } while (0); | |||
87 | ||||
88 | free(g->path); | |||
89 | free(g); | |||
90 | } | |||
91 | ||||
92 | static void group_hashmap_clear(Hashmap *h) { | |||
93 | hashmap_clear_with_destructor(h, group_free)({ void *_item; while ((_item = hashmap_steal_first(h))) group_free (_item); }); | |||
94 | } | |||
95 | ||||
96 | static void group_hashmap_free(Hashmap *h) { | |||
97 | group_hashmap_clear(h); | |||
98 | hashmap_free(h); | |||
99 | } | |||
100 | ||||
101 | static const char *maybe_format_bytes(char *buf, size_t l, bool_Bool is_valid, uint64_t t) { | |||
102 | if (!is_valid) | |||
103 | return "-"; | |||
104 | if (arg_raw) { | |||
105 | snprintf(buf, l, "%" PRIu64"l" "u", t); | |||
106 | return buf; | |||
107 | } | |||
108 | return format_bytes(buf, l, t); | |||
109 | } | |||
110 | ||||
111 | static bool_Bool is_root_cgroup(const char *path) { | |||
112 | ||||
113 | /* Returns true if the specified path belongs to the root cgroup. The root cgroup is special on cgroupsv2 as it | |||
114 | * carries only very few attributes in order not to export multiple truth about system state as most | |||
115 | * information is available elsewhere in /proc anyway. We need to be able to deal with that, and need to get | |||
116 | * our data from different sources in that case. | |||
117 | * | |||
118 | * There's one extra complication in all of this, though 😣: if the path to the cgroup indicates we are in the | |||
119 | * root cgroup this might actually not be the case, because cgroup namespacing might be in effect | |||
120 | * (CLONE_NEWCGROUP). Since there's no nice way to distuingish a real cgroup root from a fake namespaced one we | |||
121 | * do an explicit container check here, under the assumption that CLONE_NEWCGROUP is generally used when | |||
122 | * container managers are used too. | |||
123 | * | |||
124 | * Note that checking for a container environment is kinda ugly, since in theory people could use cgtop from | |||
125 | * inside a container where cgroup namespacing is turned off to watch the host system. However, that's mostly a | |||
126 | * theoretic usecase, and if people actually try all they'll lose is accounting for the top-level cgroup. Which | |||
127 | * isn't too bad. */ | |||
128 | ||||
129 | if (detect_container() > 0) | |||
130 | return false0; | |||
131 | ||||
132 | return empty_or_root(path); | |||
133 | } | |||
134 | ||||
135 | static int process( | |||
136 | const char *controller, | |||
137 | const char *path, | |||
138 | Hashmap *a, | |||
139 | Hashmap *b, | |||
140 | unsigned iteration, | |||
141 | Group **ret) { | |||
142 | ||||
143 | Group *g; | |||
144 | int r, all_unified; | |||
145 | ||||
146 | assert(controller)do { if ((__builtin_expect(!!(!(controller)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("controller"), "../src/cgtop/cgtop.c", 146 , __PRETTY_FUNCTION__); } while (0); | |||
147 | assert(path)do { if ((__builtin_expect(!!(!(path)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("path"), "../src/cgtop/cgtop.c", 147, __PRETTY_FUNCTION__ ); } while (0); | |||
148 | assert(a)do { if ((__builtin_expect(!!(!(a)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("a"), "../src/cgtop/cgtop.c", 148, __PRETTY_FUNCTION__ ); } while (0); | |||
149 | ||||
150 | all_unified = cg_all_unified(); | |||
151 | if (all_unified < 0) | |||
152 | return all_unified; | |||
153 | ||||
154 | g = hashmap_get(a, path); | |||
155 | if (!g) { | |||
156 | g = hashmap_get(b, path); | |||
157 | if (!g) { | |||
158 | g = new0(Group, 1)((Group*) calloc((1), sizeof(Group))); | |||
159 | if (!g) | |||
160 | return -ENOMEM12; | |||
161 | ||||
162 | g->path = strdup(path); | |||
163 | if (!g->path) { | |||
164 | group_free(g); | |||
165 | return -ENOMEM12; | |||
166 | } | |||
167 | ||||
168 | r = hashmap_put(a, g->path, g); | |||
169 | if (r < 0) { | |||
170 | group_free(g); | |||
171 | return r; | |||
172 | } | |||
173 | } else { | |||
174 | r = hashmap_move_one(a, b, path); | |||
175 | if (r < 0) | |||
176 | return r; | |||
177 | ||||
178 | g->cpu_valid = g->memory_valid = g->io_valid = g->n_tasks_valid = false0; | |||
179 | } | |||
180 | } | |||
181 | ||||
182 | if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)(strcmp((controller),("_systemd")) == 0) && | |||
183 | IN_SET(arg_count, COUNT_ALL_PROCESSES, COUNT_USERSPACE_PROCESSES)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){COUNT_ALL_PROCESSES, COUNT_USERSPACE_PROCESSES })/sizeof(int)]; switch(arg_count) { case COUNT_ALL_PROCESSES : case COUNT_USERSPACE_PROCESSES: _found = 1; break; default: break; } _found; })) { | |||
184 | _cleanup_fclose___attribute__((cleanup(fclosep))) FILE *f = NULL((void*)0); | |||
185 | pid_t pid; | |||
186 | ||||
187 | r = cg_enumerate_processes(controller, path, &f); | |||
188 | if (r == -ENOENT2) | |||
189 | return 0; | |||
190 | if (r < 0) | |||
191 | return r; | |||
192 | ||||
193 | g->n_tasks = 0; | |||
194 | while (cg_read_pid(f, &pid) > 0) { | |||
195 | ||||
196 | if (arg_count == COUNT_USERSPACE_PROCESSES && is_kernel_thread(pid) > 0) | |||
197 | continue; | |||
198 | ||||
199 | g->n_tasks++; | |||
200 | } | |||
201 | ||||
202 | if (g->n_tasks > 0) | |||
203 | g->n_tasks_valid = true1; | |||
204 | ||||
205 | } else if (streq(controller, "pids")(strcmp((controller),("pids")) == 0) && arg_count == COUNT_PIDS) { | |||
206 | ||||
207 | if (is_root_cgroup(path)) { | |||
208 | r = procfs_tasks_get_current(&g->n_tasks); | |||
209 | if (r < 0) | |||
210 | return r; | |||
211 | } else { | |||
212 | _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0), *v = NULL((void*)0); | |||
213 | ||||
214 | r = cg_get_path(controller, path, "pids.current", &p); | |||
215 | if (r < 0) | |||
216 | return r; | |||
217 | ||||
218 | r = read_one_line_file(p, &v); | |||
219 | if (r == -ENOENT2) | |||
220 | return 0; | |||
221 | if (r < 0) | |||
222 | return r; | |||
223 | ||||
224 | r = safe_atou64(v, &g->n_tasks); | |||
225 | if (r < 0) | |||
226 | return r; | |||
227 | } | |||
228 | ||||
229 | if (g->n_tasks > 0) | |||
230 | g->n_tasks_valid = true1; | |||
231 | ||||
232 | } else if (STR_IN_SET(controller, "cpu", "cpuacct")(!!strv_find((((char**) ((const char*[]) { "cpu", "cpuacct", ( (void*)0) }))), (controller)))) { | |||
233 | _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0), *v = NULL((void*)0); | |||
234 | uint64_t new_usage; | |||
235 | nsec_t timestamp; | |||
236 | ||||
237 | if (is_root_cgroup(path)) { | |||
238 | r = procfs_cpu_get_usage(&new_usage); | |||
239 | if (r < 0) | |||
240 | return r; | |||
241 | } else if (all_unified) { | |||
242 | _cleanup_free___attribute__((cleanup(freep))) char *val = NULL((void*)0); | |||
243 | ||||
244 | if (!streq(controller, "cpu")(strcmp((controller),("cpu")) == 0)) | |||
245 | return 0; | |||
246 | ||||
247 | r = cg_get_keyed_attribute("cpu", path, "cpu.stat", STRV_MAKE("usage_usec")((char**) ((const char*[]) { "usage_usec", ((void*)0) })), &val); | |||
248 | if (IN_SET(r, -ENOENT, -ENXIO)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){-2, -6})/sizeof(int)]; switch(r) { case - 2: case -6: _found = 1; break; default: break; } _found; })) | |||
249 | return 0; | |||
250 | if (r < 0) | |||
251 | return r; | |||
252 | ||||
253 | r = safe_atou64(val, &new_usage); | |||
254 | if (r < 0) | |||
255 | return r; | |||
256 | ||||
257 | new_usage *= NSEC_PER_USEC((nsec_t) 1000ULL); | |||
258 | } else { | |||
259 | if (!streq(controller, "cpuacct")(strcmp((controller),("cpuacct")) == 0)) | |||
260 | return 0; | |||
261 | ||||
262 | r = cg_get_path(controller, path, "cpuacct.usage", &p); | |||
263 | if (r < 0) | |||
264 | return r; | |||
265 | ||||
266 | r = read_one_line_file(p, &v); | |||
267 | if (r == -ENOENT2) | |||
268 | return 0; | |||
269 | if (r < 0) | |||
270 | return r; | |||
271 | ||||
272 | r = safe_atou64(v, &new_usage); | |||
273 | if (r < 0) | |||
274 | return r; | |||
275 | } | |||
276 | ||||
277 | timestamp = now_nsec(CLOCK_MONOTONIC1); | |||
278 | ||||
279 | if (g->cpu_iteration == iteration - 1 && | |||
280 | (nsec_t) new_usage > g->cpu_usage) { | |||
281 | ||||
282 | nsec_t x, y; | |||
283 | ||||
284 | x = timestamp - g->cpu_timestamp; | |||
285 | if (x < 1) | |||
286 | x = 1; | |||
287 | ||||
288 | y = (nsec_t) new_usage - g->cpu_usage; | |||
289 | g->cpu_fraction = (double) y / (double) x; | |||
290 | g->cpu_valid = true1; | |||
291 | } | |||
292 | ||||
293 | g->cpu_usage = (nsec_t) new_usage; | |||
294 | g->cpu_timestamp = timestamp; | |||
295 | g->cpu_iteration = iteration; | |||
296 | ||||
297 | } else if (streq(controller, "memory")(strcmp((controller),("memory")) == 0)) { | |||
298 | ||||
299 | if (is_root_cgroup(path)) { | |||
300 | r = procfs_memory_get_used(&g->memory); | |||
301 | if (r < 0) | |||
302 | return r; | |||
303 | } else { | |||
304 | _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0), *v = NULL((void*)0); | |||
305 | ||||
306 | if (all_unified) | |||
307 | r = cg_get_path(controller, path, "memory.current", &p); | |||
308 | else | |||
309 | r = cg_get_path(controller, path, "memory.usage_in_bytes", &p); | |||
310 | if (r < 0) | |||
311 | return r; | |||
312 | ||||
313 | r = read_one_line_file(p, &v); | |||
314 | if (r == -ENOENT2) | |||
315 | return 0; | |||
316 | if (r < 0) | |||
317 | return r; | |||
318 | ||||
319 | r = safe_atou64(v, &g->memory); | |||
320 | if (r < 0) | |||
321 | return r; | |||
322 | } | |||
323 | ||||
324 | if (g->memory > 0) | |||
325 | g->memory_valid = true1; | |||
326 | ||||
327 | } else if ((streq(controller, "io")(strcmp((controller),("io")) == 0) && all_unified) || | |||
328 | (streq(controller, "blkio")(strcmp((controller),("blkio")) == 0) && !all_unified)) { | |||
329 | _cleanup_fclose___attribute__((cleanup(fclosep))) FILE *f = NULL((void*)0); | |||
330 | _cleanup_free___attribute__((cleanup(freep))) char *p = NULL((void*)0); | |||
331 | uint64_t wr = 0, rd = 0; | |||
332 | nsec_t timestamp; | |||
333 | ||||
334 | r = cg_get_path(controller, path, all_unified ? "io.stat" : "blkio.io_service_bytes", &p); | |||
335 | if (r < 0) | |||
336 | return r; | |||
337 | ||||
338 | f = fopen(p, "re"); | |||
339 | if (!f) { | |||
340 | if (errno(*__errno_location ()) == ENOENT2) | |||
341 | return 0; | |||
342 | return -errno(*__errno_location ()); | |||
343 | } | |||
344 | ||||
345 | for (;;) { | |||
346 | char line[LINE_MAX2048], *l; | |||
347 | uint64_t k, *q; | |||
348 | ||||
349 | if (!fgets(line, sizeof(line), f)) | |||
350 | break; | |||
351 | ||||
352 | /* Trim and skip the device */ | |||
353 | l = strstrip(line); | |||
354 | l += strcspn(l, WHITESPACE" \t\n\r"); | |||
355 | l += strspn(l, WHITESPACE" \t\n\r"); | |||
356 | ||||
357 | if (all_unified) { | |||
358 | while (!isempty(l)) { | |||
359 | if (sscanf(l, "rbytes=%" SCNu64"l" "u", &k)) | |||
360 | rd += k; | |||
361 | else if (sscanf(l, "wbytes=%" SCNu64"l" "u", &k)) | |||
362 | wr += k; | |||
363 | ||||
364 | l += strcspn(l, WHITESPACE" \t\n\r"); | |||
365 | l += strspn(l, WHITESPACE" \t\n\r"); | |||
366 | } | |||
367 | } else { | |||
368 | if (first_word(l, "Read")) { | |||
369 | l += 4; | |||
370 | q = &rd; | |||
371 | } else if (first_word(l, "Write")) { | |||
372 | l += 5; | |||
373 | q = ≀ | |||
374 | } else | |||
375 | continue; | |||
376 | ||||
377 | l += strspn(l, WHITESPACE" \t\n\r"); | |||
378 | r = safe_atou64(l, &k); | |||
379 | if (r < 0) | |||
380 | continue; | |||
381 | ||||
382 | *q += k; | |||
383 | } | |||
384 | } | |||
385 | ||||
386 | timestamp = now_nsec(CLOCK_MONOTONIC1); | |||
387 | ||||
388 | if (g->io_iteration == iteration - 1) { | |||
389 | uint64_t x, yr, yw; | |||
390 | ||||
391 | x = (uint64_t) (timestamp - g->io_timestamp); | |||
392 | if (x < 1) | |||
393 | x = 1; | |||
394 | ||||
395 | if (rd > g->io_input) | |||
396 | yr = rd - g->io_input; | |||
397 | else | |||
398 | yr = 0; | |||
399 | ||||
400 | if (wr > g->io_output) | |||
401 | yw = wr - g->io_output; | |||
402 | else | |||
403 | yw = 0; | |||
404 | ||||
405 | if (yr > 0 || yw > 0) { | |||
406 | g->io_input_bps = (yr * 1000000000ULL) / x; | |||
407 | g->io_output_bps = (yw * 1000000000ULL) / x; | |||
408 | g->io_valid = true1; | |||
409 | } | |||
410 | } | |||
411 | ||||
412 | g->io_input = rd; | |||
413 | g->io_output = wr; | |||
414 | g->io_timestamp = timestamp; | |||
415 | g->io_iteration = iteration; | |||
416 | } | |||
417 | ||||
418 | if (ret) | |||
419 | *ret = g; | |||
420 | ||||
421 | return 0; | |||
422 | } | |||
423 | ||||
424 | static int refresh_one( | |||
425 | const char *controller, | |||
426 | const char *path, | |||
427 | Hashmap *a, | |||
428 | Hashmap *b, | |||
429 | unsigned iteration, | |||
430 | unsigned depth, | |||
431 | Group **ret) { | |||
432 | ||||
433 | _cleanup_closedir___attribute__((cleanup(closedirp))) DIR *d = NULL((void*)0); | |||
434 | Group *ours = NULL((void*)0); | |||
435 | int r; | |||
436 | ||||
437 | assert(controller)do { if ((__builtin_expect(!!(!(controller)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("controller"), "../src/cgtop/cgtop.c", 437 , __PRETTY_FUNCTION__); } while (0); | |||
438 | assert(path)do { if ((__builtin_expect(!!(!(path)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("path"), "../src/cgtop/cgtop.c", 438, __PRETTY_FUNCTION__ ); } while (0); | |||
439 | assert(a)do { if ((__builtin_expect(!!(!(a)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("a"), "../src/cgtop/cgtop.c", 439, __PRETTY_FUNCTION__ ); } while (0); | |||
440 | ||||
441 | if (depth > arg_depth) | |||
442 | return 0; | |||
443 | ||||
444 | r = process(controller, path, a, b, iteration, &ours); | |||
445 | if (r < 0) | |||
446 | return r; | |||
447 | ||||
448 | r = cg_enumerate_subgroups(controller, path, &d); | |||
449 | if (r == -ENOENT2) | |||
450 | return 0; | |||
451 | if (r < 0) | |||
452 | return r; | |||
453 | ||||
454 | for (;;) { | |||
455 | _cleanup_free___attribute__((cleanup(freep))) char *fn = NULL((void*)0), *p = NULL((void*)0); | |||
456 | Group *child = NULL((void*)0); | |||
457 | ||||
458 | r = cg_read_subgroup(d, &fn); | |||
459 | if (r < 0) | |||
460 | return r; | |||
461 | if (r == 0) | |||
462 | break; | |||
463 | ||||
464 | p = strjoin(path, "/", fn)strjoin_real((path), "/", fn, ((void*)0)); | |||
465 | if (!p) | |||
466 | return -ENOMEM12; | |||
467 | ||||
468 | path_simplify(p, false0); | |||
469 | ||||
470 | r = refresh_one(controller, p, a, b, iteration, depth + 1, &child); | |||
471 | if (r < 0) | |||
472 | return r; | |||
473 | ||||
474 | if (arg_recursive && | |||
475 | IN_SET(arg_count, COUNT_ALL_PROCESSES, COUNT_USERSPACE_PROCESSES)({ _Bool _found = 0; static __attribute__ ((unused)) char _static_assert__macros_need_to_be_extended [20 - sizeof((int[]){COUNT_ALL_PROCESSES, COUNT_USERSPACE_PROCESSES })/sizeof(int)]; switch(arg_count) { case COUNT_ALL_PROCESSES : case COUNT_USERSPACE_PROCESSES: _found = 1; break; default: break; } _found; }) && | |||
476 | child && | |||
477 | child->n_tasks_valid && | |||
478 | streq(controller, SYSTEMD_CGROUP_CONTROLLER)(strcmp((controller),("_systemd")) == 0)) { | |||
479 | ||||
480 | /* Recursively sum up processes */ | |||
481 | ||||
482 | if (ours->n_tasks_valid) | |||
483 | ours->n_tasks += child->n_tasks; | |||
484 | else { | |||
485 | ours->n_tasks = child->n_tasks; | |||
486 | ours->n_tasks_valid = true1; | |||
487 | } | |||
488 | } | |||
489 | } | |||
490 | ||||
491 | if (ret) | |||
492 | *ret = ours; | |||
493 | ||||
494 | return 1; | |||
495 | } | |||
496 | ||||
497 | static int refresh(const char *root, Hashmap *a, Hashmap *b, unsigned iteration) { | |||
498 | int r; | |||
499 | ||||
500 | assert(a)do { if ((__builtin_expect(!!(!(a)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("a"), "../src/cgtop/cgtop.c", 500, __PRETTY_FUNCTION__ ); } while (0); | |||
501 | ||||
502 | r = refresh_one(SYSTEMD_CGROUP_CONTROLLER"_systemd", root, a, b, iteration, 0, NULL((void*)0)); | |||
503 | if (r < 0) | |||
504 | return r; | |||
505 | r = refresh_one("cpu", root, a, b, iteration, 0, NULL((void*)0)); | |||
506 | if (r < 0) | |||
507 | return r; | |||
508 | r = refresh_one("cpuacct", root, a, b, iteration, 0, NULL((void*)0)); | |||
509 | if (r < 0) | |||
510 | return r; | |||
511 | r = refresh_one("memory", root, a, b, iteration, 0, NULL((void*)0)); | |||
512 | if (r < 0) | |||
513 | return r; | |||
514 | r = refresh_one("io", root, a, b, iteration, 0, NULL((void*)0)); | |||
515 | if (r < 0) | |||
516 | return r; | |||
517 | r = refresh_one("blkio", root, a, b, iteration, 0, NULL((void*)0)); | |||
518 | if (r < 0) | |||
519 | return r; | |||
520 | r = refresh_one("pids", root, a, b, iteration, 0, NULL((void*)0)); | |||
521 | if (r < 0) | |||
522 | return r; | |||
523 | ||||
524 | return 0; | |||
525 | } | |||
526 | ||||
527 | static int group_compare(const void*a, const void *b) { | |||
528 | const Group *x = *(Group**)a, *y = *(Group**)b; | |||
529 | ||||
530 | if (arg_order != ORDER_TASKS || arg_recursive) { | |||
531 | /* Let's make sure that the parent is always before | |||
532 | * the child. Except when ordering by tasks and | |||
533 | * recursive summing is off, since that is actually | |||
534 | * not accumulative for all children. */ | |||
535 | ||||
536 | if (path_startswith(empty_to_root(y->path), empty_to_root(x->path))) | |||
537 | return -1; | |||
538 | if (path_startswith(empty_to_root(x->path), empty_to_root(y->path))) | |||
539 | return 1; | |||
540 | } | |||
541 | ||||
542 | switch (arg_order) { | |||
543 | ||||
544 | case ORDER_PATH: | |||
545 | break; | |||
546 | ||||
547 | case ORDER_CPU: | |||
548 | if (arg_cpu_type == CPU_PERCENT) { | |||
549 | if (x->cpu_valid && y->cpu_valid) { | |||
550 | if (x->cpu_fraction > y->cpu_fraction) | |||
551 | return -1; | |||
552 | else if (x->cpu_fraction < y->cpu_fraction) | |||
553 | return 1; | |||
554 | } else if (x->cpu_valid) | |||
555 | return -1; | |||
556 | else if (y->cpu_valid) | |||
557 | return 1; | |||
558 | } else { | |||
559 | if (x->cpu_usage > y->cpu_usage) | |||
560 | return -1; | |||
561 | else if (x->cpu_usage < y->cpu_usage) | |||
562 | return 1; | |||
563 | } | |||
564 | ||||
565 | break; | |||
566 | ||||
567 | case ORDER_TASKS: | |||
568 | if (x->n_tasks_valid && y->n_tasks_valid) { | |||
569 | if (x->n_tasks > y->n_tasks) | |||
570 | return -1; | |||
571 | else if (x->n_tasks < y->n_tasks) | |||
572 | return 1; | |||
573 | } else if (x->n_tasks_valid) | |||
574 | return -1; | |||
575 | else if (y->n_tasks_valid) | |||
576 | return 1; | |||
577 | ||||
578 | break; | |||
579 | ||||
580 | case ORDER_MEMORY: | |||
581 | if (x->memory_valid && y->memory_valid) { | |||
582 | if (x->memory > y->memory) | |||
583 | return -1; | |||
584 | else if (x->memory < y->memory) | |||
585 | return 1; | |||
586 | } else if (x->memory_valid) | |||
587 | return -1; | |||
588 | else if (y->memory_valid) | |||
589 | return 1; | |||
590 | ||||
591 | break; | |||
592 | ||||
593 | case ORDER_IO: | |||
594 | if (x->io_valid && y->io_valid) { | |||
595 | if (x->io_input_bps + x->io_output_bps > y->io_input_bps + y->io_output_bps) | |||
596 | return -1; | |||
597 | else if (x->io_input_bps + x->io_output_bps < y->io_input_bps + y->io_output_bps) | |||
598 | return 1; | |||
599 | } else if (x->io_valid) | |||
600 | return -1; | |||
601 | else if (y->io_valid) | |||
602 | return 1; | |||
603 | } | |||
604 | ||||
605 | return path_compare(x->path, y->path); | |||
606 | } | |||
607 | ||||
608 | static void display(Hashmap *a) { | |||
609 | Iterator i; | |||
610 | Group *g; | |||
611 | Group **array; | |||
612 | signed path_columns; | |||
613 | unsigned rows, n = 0, j, maxtcpu = 0, maxtpath = 3; /* 3 for ellipsize() to work properly */ | |||
614 | char buffer[MAX3(21, FORMAT_BYTES_MAX, FORMAT_TIMESPAN_MAX)__extension__ ({ const typeof(21) _c = __extension__ ({ const typeof((21)) __unique_prefix_A24 = ((21)); const typeof((8)) __unique_prefix_B25 = ((8)); __unique_prefix_A24 > __unique_prefix_B25 ? __unique_prefix_A24 : __unique_prefix_B25; }); __extension__ ({ const typeof((_c)) __unique_prefix_A26 = ((_c)); const typeof ((64)) __unique_prefix_B27 = ((64)); __unique_prefix_A26 > __unique_prefix_B27 ? __unique_prefix_A26 : __unique_prefix_B27 ; }); })]; | |||
615 | ||||
616 | assert(a)do { if ((__builtin_expect(!!(!(a)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("a"), "../src/cgtop/cgtop.c", 616, __PRETTY_FUNCTION__ ); } while (0); | |||
617 | ||||
618 | if (!terminal_is_dumb()) | |||
619 | fputs(ANSI_HOME_CLEAR"\x1B[H\x1B[2J", stdoutstdout); | |||
620 | ||||
621 | array = newa(Group*, hashmap_size(a))({ do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof (Group*), hashmap_size(a)))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("!size_multiply_overflow(sizeof(Group*), hashmap_size(a))" ), "../src/cgtop/cgtop.c", 621, __PRETTY_FUNCTION__); } while (0); (Group**) __builtin_alloca (sizeof(Group*)*(hashmap_size (a))); }); | |||
622 | ||||
623 | HASHMAP_FOREACH(g, a, i)for ((i) = ((Iterator) { .idx = ((2147483647 *2U +1U) - 1), . next_key = ((void*)0) }); hashmap_iterate((a), &(i), (void **)&(g), ((void*)0)); ) | |||
624 | if (g->n_tasks_valid || g->cpu_valid || g->memory_valid || g->io_valid) | |||
625 | array[n++] = g; | |||
626 | ||||
627 | qsort_safe(array, n, sizeof(Group*), group_compare); | |||
628 | ||||
629 | /* Find the longest names in one run */ | |||
630 | for (j = 0; j < n; j++) { | |||
631 | unsigned cputlen, pathtlen; | |||
632 | ||||
633 | format_timespan(buffer, sizeof(buffer), (usec_t) (array[j]->cpu_usage / NSEC_PER_USEC((nsec_t) 1000ULL)), 0); | |||
634 | cputlen = strlen(buffer); | |||
635 | maxtcpu = MAX(maxtcpu, cputlen)__extension__ ({ const typeof((maxtcpu)) __unique_prefix_A28 = ((maxtcpu)); const typeof((cputlen)) __unique_prefix_B29 = ( (cputlen)); __unique_prefix_A28 > __unique_prefix_B29 ? __unique_prefix_A28 : __unique_prefix_B29; }); | |||
636 | ||||
637 | pathtlen = strlen(array[j]->path); | |||
638 | maxtpath = MAX(maxtpath, pathtlen)__extension__ ({ const typeof((maxtpath)) __unique_prefix_A30 = ((maxtpath)); const typeof((pathtlen)) __unique_prefix_B31 = ((pathtlen)); __unique_prefix_A30 > __unique_prefix_B31 ? __unique_prefix_A30 : __unique_prefix_B31; }); | |||
639 | } | |||
640 | ||||
641 | if (arg_cpu_type == CPU_PERCENT) | |||
642 | xsprintf(buffer, "%6s", "%CPU")do { if ((__builtin_expect(!!(!(((size_t) snprintf(buffer, __extension__ (__builtin_choose_expr( !__builtin_types_compatible_p(typeof (buffer), typeof(&*(buffer))), sizeof(buffer)/sizeof((buffer )[0]), ((void)0))), "%6s", "%CPU") < (__extension__ (__builtin_choose_expr ( !__builtin_types_compatible_p(typeof(buffer), typeof(&* (buffer))), sizeof(buffer)/sizeof((buffer)[0]), ((void)0))))) )),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("xsprintf: " "buffer" "[] must be big enough"), "../src/cgtop/cgtop.c", 642 , __PRETTY_FUNCTION__); } while (0); | |||
643 | else | |||
644 | xsprintf(buffer, "%*s", maxtcpu, "CPU Time")do { if ((__builtin_expect(!!(!(((size_t) snprintf(buffer, __extension__ (__builtin_choose_expr( !__builtin_types_compatible_p(typeof (buffer), typeof(&*(buffer))), sizeof(buffer)/sizeof((buffer )[0]), ((void)0))), "%*s", maxtcpu, "CPU Time") < (__extension__ (__builtin_choose_expr( !__builtin_types_compatible_p(typeof (buffer), typeof(&*(buffer))), sizeof(buffer)/sizeof((buffer )[0]), ((void)0))))))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD , ("xsprintf: " "buffer" "[] must be big enough"), "../src/cgtop/cgtop.c" , 644, __PRETTY_FUNCTION__); } while (0); | |||
645 | ||||
646 | rows = lines(); | |||
647 | if (rows <= 10) | |||
648 | rows = 10; | |||
649 | ||||
650 | if (on_tty()) { | |||
651 | const char *on, *off; | |||
652 | ||||
653 | path_columns = columns() - 36 - strlen(buffer); | |||
654 | if (path_columns < 10) | |||
655 | path_columns = 10; | |||
656 | ||||
657 | on = ansi_highlight_underline(); | |||
658 | off = ansi_underline(); | |||
659 | ||||
660 | printf("%s%s%-*s%s %s%7s%s %s%s%s %s%8s%s %s%8s%s %s%8s%s%s\n", | |||
661 | ansi_underline(), | |||
662 | arg_order == ORDER_PATH ? on : "", path_columns, "Control Group", | |||
663 | arg_order == ORDER_PATH ? off : "", | |||
664 | arg_order == ORDER_TASKS ? on : "", arg_count == COUNT_PIDS ? "Tasks" : arg_count == COUNT_USERSPACE_PROCESSES ? "Procs" : "Proc+", | |||
665 | arg_order == ORDER_TASKS ? off : "", | |||
666 | arg_order == ORDER_CPU ? on : "", buffer, | |||
667 | arg_order == ORDER_CPU ? off : "", | |||
668 | arg_order == ORDER_MEMORY ? on : "", "Memory", | |||
669 | arg_order == ORDER_MEMORY ? off : "", | |||
670 | arg_order == ORDER_IO ? on : "", "Input/s", | |||
671 | arg_order == ORDER_IO ? off : "", | |||
672 | arg_order == ORDER_IO ? on : "", "Output/s", | |||
673 | arg_order == ORDER_IO ? off : "", | |||
674 | ansi_normal()); | |||
675 | } else | |||
676 | path_columns = maxtpath; | |||
677 | ||||
678 | for (j = 0; j < n; j++) { | |||
679 | _cleanup_free___attribute__((cleanup(freep))) char *ellipsized = NULL((void*)0); | |||
680 | const char *path; | |||
681 | ||||
682 | if (on_tty() && j + 6 > rows) | |||
683 | break; | |||
684 | ||||
685 | g = array[j]; | |||
686 | ||||
687 | path = empty_to_root(g->path); | |||
688 | ellipsized = ellipsize(path, path_columns, 33); | |||
689 | printf("%-*s", path_columns, ellipsized ?: path); | |||
690 | ||||
691 | if (g->n_tasks_valid) | |||
692 | printf(" %7" PRIu64"l" "u", g->n_tasks); | |||
693 | else | |||
694 | fputs(" -", stdoutstdout); | |||
695 | ||||
696 | if (arg_cpu_type == CPU_PERCENT) { | |||
697 | if (g->cpu_valid) | |||
698 | printf(" %6.1f", g->cpu_fraction*100); | |||
699 | else | |||
700 | fputs(" -", stdoutstdout); | |||
701 | } else | |||
702 | printf(" %*s", maxtcpu, format_timespan(buffer, sizeof(buffer), (usec_t) (g->cpu_usage / NSEC_PER_USEC((nsec_t) 1000ULL)), 0)); | |||
703 | ||||
704 | printf(" %8s", maybe_format_bytes(buffer, sizeof(buffer), g->memory_valid, g->memory)); | |||
705 | printf(" %8s", maybe_format_bytes(buffer, sizeof(buffer), g->io_valid, g->io_input_bps)); | |||
706 | printf(" %8s", maybe_format_bytes(buffer, sizeof(buffer), g->io_valid, g->io_output_bps)); | |||
707 | ||||
708 | putchar('\n'); | |||
709 | } | |||
710 | } | |||
711 | ||||
712 | static void help(void) { | |||
713 | printf("%s [OPTIONS...] [CGROUP]\n\n" | |||
714 | "Show top control groups by their resource usage.\n\n" | |||
715 | " -h --help Show this help\n" | |||
716 | " --version Show package version\n" | |||
717 | " -p --order=path Order by path\n" | |||
718 | " -t --order=tasks Order by number of tasks/processes\n" | |||
719 | " -c --order=cpu Order by CPU load (default)\n" | |||
720 | " -m --order=memory Order by memory load\n" | |||
721 | " -i --order=io Order by IO load\n" | |||
722 | " -r --raw Provide raw (not human-readable) numbers\n" | |||
723 | " --cpu=percentage Show CPU usage as percentage (default)\n" | |||
724 | " --cpu=time Show CPU usage as time\n" | |||
725 | " -P Count userspace processes instead of tasks (excl. kernel)\n" | |||
726 | " -k Count all processes instead of tasks (incl. kernel)\n" | |||
727 | " --recursive=BOOL Sum up process count recursively\n" | |||
728 | " -d --delay=DELAY Delay between updates\n" | |||
729 | " -n --iterations=N Run for N iterations before exiting\n" | |||
730 | " -1 Shortcut for --iterations=1\n" | |||
731 | " -b --batch Run in batch mode, accepting no input\n" | |||
732 | " --depth=DEPTH Maximum traversal depth (default: %u)\n" | |||
733 | " -M --machine= Show container\n" | |||
734 | , program_invocation_short_name, arg_depth); | |||
735 | } | |||
736 | ||||
737 | static int parse_argv(int argc, char *argv[]) { | |||
738 | ||||
739 | enum { | |||
740 | ARG_VERSION = 0x100, | |||
741 | ARG_DEPTH, | |||
742 | ARG_CPU_TYPE, | |||
743 | ARG_ORDER, | |||
744 | ARG_RECURSIVE, | |||
745 | }; | |||
746 | ||||
747 | static const struct option options[] = { | |||
748 | { "help", no_argument0, NULL((void*)0), 'h' }, | |||
749 | { "version", no_argument0, NULL((void*)0), ARG_VERSION }, | |||
750 | { "delay", required_argument1, NULL((void*)0), 'd' }, | |||
751 | { "iterations", required_argument1, NULL((void*)0), 'n' }, | |||
752 | { "batch", no_argument0, NULL((void*)0), 'b' }, | |||
753 | { "raw", no_argument0, NULL((void*)0), 'r' }, | |||
754 | { "depth", required_argument1, NULL((void*)0), ARG_DEPTH }, | |||
755 | { "cpu", optional_argument2, NULL((void*)0), ARG_CPU_TYPE }, | |||
756 | { "order", required_argument1, NULL((void*)0), ARG_ORDER }, | |||
757 | { "recursive", required_argument1, NULL((void*)0), ARG_RECURSIVE }, | |||
758 | { "machine", required_argument1, NULL((void*)0), 'M' }, | |||
759 | {} | |||
760 | }; | |||
761 | ||||
762 | int c, r; | |||
763 | ||||
764 | assert(argc >= 1)do { if ((__builtin_expect(!!(!(argc >= 1)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("argc >= 1"), "../src/cgtop/cgtop.c", 764, __PRETTY_FUNCTION__); } while (0); | |||
765 | assert(argv)do { if ((__builtin_expect(!!(!(argv)),0))) log_assert_failed_realm (LOG_REALM_SYSTEMD, ("argv"), "../src/cgtop/cgtop.c", 765, __PRETTY_FUNCTION__ ); } while (0); | |||
766 | ||||
767 | while ((c = getopt_long(argc, argv, "hptcmin:brd:kPM:1", options, NULL((void*)0))) >= 0) | |||
768 | ||||
769 | switch (c) { | |||
770 | ||||
771 | case 'h': | |||
772 | help(); | |||
773 | return 0; | |||
774 | ||||
775 | case ARG_VERSION: | |||
776 | return version(); | |||
777 | ||||
778 | case ARG_CPU_TYPE: | |||
779 | if (optarg) { | |||
780 | if (streq(optarg, "time")(strcmp((optarg),("time")) == 0)) | |||
781 | arg_cpu_type = CPU_TIME; | |||
782 | else if (streq(optarg, "percentage")(strcmp((optarg),("percentage")) == 0)) | |||
783 | arg_cpu_type = CPU_PERCENT; | |||
784 | else { | |||
785 | log_error("Unknown argument to --cpu=: %s", optarg)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/cgtop/cgtop.c", 785, __func__, "Unknown argument to --cpu=: %s" , optarg) : -abs(_e); }); | |||
786 | return -EINVAL22; | |||
787 | } | |||
788 | } else | |||
789 | arg_cpu_type = CPU_TIME; | |||
790 | ||||
791 | break; | |||
792 | ||||
793 | case ARG_DEPTH: | |||
794 | r = safe_atou(optarg, &arg_depth); | |||
795 | if (r < 0) | |||
796 | return log_error_errno(r, "Failed to parse depth parameter: %s", optarg)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/cgtop/cgtop.c", 796, __func__, "Failed to parse depth parameter: %s" , optarg) : -abs(_e); }); | |||
797 | ||||
798 | break; | |||
799 | ||||
800 | case 'd': | |||
801 | r = parse_sec(optarg, &arg_delay); | |||
802 | if (r < 0 || arg_delay <= 0) { | |||
803 | log_error("Failed to parse delay parameter: %s", optarg)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/cgtop/cgtop.c", 803, __func__, "Failed to parse delay parameter: %s" , optarg) : -abs(_e); }); | |||
804 | return -EINVAL22; | |||
805 | } | |||
806 | ||||
807 | break; | |||
808 | ||||
809 | case 'n': | |||
810 | r = safe_atou(optarg, &arg_iterations); | |||
811 | if (r < 0) | |||
812 | return log_error_errno(r, "Failed to parse iterations parameter: %s", optarg)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/cgtop/cgtop.c", 812, __func__, "Failed to parse iterations parameter: %s" , optarg) : -abs(_e); }); | |||
813 | ||||
814 | break; | |||
815 | ||||
816 | case '1': | |||
817 | arg_iterations = 1; | |||
818 | break; | |||
819 | ||||
820 | case 'b': | |||
821 | arg_batch = true1; | |||
822 | break; | |||
823 | ||||
824 | case 'r': | |||
825 | arg_raw = true1; | |||
826 | break; | |||
827 | ||||
828 | case 'p': | |||
829 | arg_order = ORDER_PATH; | |||
830 | break; | |||
831 | ||||
832 | case 't': | |||
833 | arg_order = ORDER_TASKS; | |||
834 | break; | |||
835 | ||||
836 | case 'c': | |||
837 | arg_order = ORDER_CPU; | |||
838 | break; | |||
839 | ||||
840 | case 'm': | |||
841 | arg_order = ORDER_MEMORY; | |||
842 | break; | |||
843 | ||||
844 | case 'i': | |||
845 | arg_order = ORDER_IO; | |||
846 | break; | |||
847 | ||||
848 | case ARG_ORDER: | |||
849 | if (streq(optarg, "path")(strcmp((optarg),("path")) == 0)) | |||
| ||||
850 | arg_order = ORDER_PATH; | |||
851 | else if (streq(optarg, "tasks")(strcmp((optarg),("tasks")) == 0)) | |||
852 | arg_order = ORDER_TASKS; | |||
853 | else if (streq(optarg, "cpu")(strcmp((optarg),("cpu")) == 0)) | |||
854 | arg_order = ORDER_CPU; | |||
855 | else if (streq(optarg, "memory")(strcmp((optarg),("memory")) == 0)) | |||
856 | arg_order = ORDER_MEMORY; | |||
857 | else if (streq(optarg, "io")(strcmp((optarg),("io")) == 0)) | |||
858 | arg_order = ORDER_IO; | |||
859 | else { | |||
860 | log_error("Invalid argument to --order=: %s", optarg)({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/cgtop/cgtop.c", 860, __func__, "Invalid argument to --order=: %s" , optarg) : -abs(_e); }); | |||
861 | return -EINVAL22; | |||
862 | } | |||
863 | break; | |||
864 | ||||
865 | case 'k': | |||
866 | arg_count = COUNT_ALL_PROCESSES; | |||
867 | break; | |||
868 | ||||
869 | case 'P': | |||
870 | arg_count = COUNT_USERSPACE_PROCESSES; | |||
871 | break; | |||
872 | ||||
873 | case ARG_RECURSIVE: | |||
874 | r = parse_boolean(optarg); | |||
875 | if (r < 0) | |||
876 | return log_error_errno(r, "Failed to parse --recursive= argument: %s", optarg)({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/cgtop/cgtop.c", 876, __func__, "Failed to parse --recursive= argument: %s" , optarg) : -abs(_e); }); | |||
877 | ||||
878 | arg_recursive = r; | |||
879 | arg_recursive_unset = r == 0; | |||
880 | break; | |||
881 | ||||
882 | case 'M': | |||
883 | arg_machine = optarg; | |||
884 | break; | |||
885 | ||||
886 | case '?': | |||
887 | return -EINVAL22; | |||
888 | ||||
889 | default: | |||
890 | assert_not_reached("Unhandled option")do { log_assert_failed_unreachable_realm(LOG_REALM_SYSTEMD, ( "Unhandled option"), "../src/cgtop/cgtop.c", 890, __PRETTY_FUNCTION__ ); } while (0); | |||
891 | } | |||
892 | ||||
893 | if (optind == argc - 1) | |||
894 | arg_root = argv[optind]; | |||
895 | else if (optind < argc) { | |||
896 | log_error("Too many arguments.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/cgtop/cgtop.c", 896, __func__, "Too many arguments." ) : -abs(_e); }); | |||
897 | return -EINVAL22; | |||
898 | } | |||
899 | ||||
900 | return 1; | |||
901 | } | |||
902 | ||||
903 | static const char* counting_what(void) { | |||
904 | if (arg_count == COUNT_PIDS) | |||
905 | return "tasks"; | |||
906 | else if (arg_count == COUNT_ALL_PROCESSES) | |||
907 | return "all processes (incl. kernel)"; | |||
908 | else | |||
909 | return "userspace processes (excl. kernel)"; | |||
910 | } | |||
911 | ||||
912 | int main(int argc, char *argv[]) { | |||
913 | int r; | |||
914 | Hashmap *a = NULL((void*)0), *b = NULL((void*)0); | |||
915 | unsigned iteration = 0; | |||
916 | usec_t last_refresh = 0; | |||
917 | bool_Bool quit = false0, immediate_refresh = false0; | |||
918 | _cleanup_free___attribute__((cleanup(freep))) char *root = NULL((void*)0); | |||
919 | CGroupMask mask; | |||
920 | ||||
921 | log_parse_environment()log_parse_environment_realm(LOG_REALM_SYSTEMD); | |||
922 | log_open(); | |||
923 | ||||
924 | r = parse_argv(argc, argv); | |||
| ||||
925 | if (r <= 0) | |||
926 | goto finish; | |||
927 | ||||
928 | r = cg_mask_supported(&mask); | |||
929 | if (r < 0) { | |||
930 | log_error_errno(r, "Failed to determine supported controllers: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/cgtop/cgtop.c", 930, __func__, "Failed to determine supported controllers: %m" ) : -abs(_e); }); | |||
931 | goto finish; | |||
932 | } | |||
933 | ||||
934 | arg_count = (mask & CGROUP_MASK_PIDS) ? COUNT_PIDS : COUNT_USERSPACE_PROCESSES; | |||
935 | ||||
936 | if (arg_recursive_unset && arg_count == COUNT_PIDS) { | |||
937 | log_error("Non-recursive counting is only supported when counting processes, not tasks. Use -P or -k.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/cgtop/cgtop.c", 937, __func__, "Non-recursive counting is only supported when counting processes, not tasks. Use -P or -k." ) : -abs(_e); }); | |||
938 | return -EINVAL22; | |||
939 | } | |||
940 | ||||
941 | r = show_cgroup_get_path_and_warn(arg_machine, arg_root, &root); | |||
942 | if (r < 0) { | |||
943 | log_error_errno(r, "Failed to get root control group path: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/cgtop/cgtop.c", 943, __func__, "Failed to get root control group path: %m" ) : -abs(_e); }); | |||
944 | goto finish; | |||
945 | } else | |||
946 | log_debug("Cgroup path: %s", root)({ int _level = (((7))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/cgtop/cgtop.c", 946, __func__, "Cgroup path: %s", root ) : -abs(_e); }); | |||
947 | ||||
948 | a = hashmap_new(&path_hash_ops)internal_hashmap_new(&path_hash_ops ); | |||
949 | b = hashmap_new(&path_hash_ops)internal_hashmap_new(&path_hash_ops ); | |||
950 | if (!a || !b) { | |||
951 | r = log_oom()log_oom_internal(LOG_REALM_SYSTEMD, "../src/cgtop/cgtop.c", 951 , __func__); | |||
952 | goto finish; | |||
953 | } | |||
954 | ||||
955 | signal(SIGWINCH28, columns_lines_cache_reset); | |||
956 | ||||
957 | if (arg_iterations == (unsigned) -1) | |||
958 | arg_iterations = on_tty() ? 0 : 1; | |||
959 | ||||
960 | while (!quit) { | |||
961 | Hashmap *c; | |||
962 | usec_t t; | |||
963 | char key; | |||
964 | char h[FORMAT_TIMESPAN_MAX64]; | |||
965 | ||||
966 | t = now(CLOCK_MONOTONIC1); | |||
967 | ||||
968 | if (t >= last_refresh + arg_delay || immediate_refresh) { | |||
969 | ||||
970 | r = refresh(root, a, b, iteration++); | |||
971 | if (r < 0) { | |||
972 | log_error_errno(r, "Failed to refresh: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/cgtop/cgtop.c", 972, __func__, "Failed to refresh: %m" ) : -abs(_e); }); | |||
973 | goto finish; | |||
974 | } | |||
975 | ||||
976 | group_hashmap_clear(b); | |||
977 | ||||
978 | c = a; | |||
979 | a = b; | |||
980 | b = c; | |||
981 | ||||
982 | last_refresh = t; | |||
983 | immediate_refresh = false0; | |||
984 | } | |||
985 | ||||
986 | display(b); | |||
987 | ||||
988 | if (arg_iterations && iteration >= arg_iterations) | |||
989 | break; | |||
990 | ||||
991 | if (!on_tty()) /* non-TTY: Empty newline as delimiter between polls */ | |||
992 | fputs("\n", stdoutstdout); | |||
993 | fflush(stdoutstdout); | |||
994 | ||||
995 | if (arg_batch) | |||
996 | (void) usleep(last_refresh + arg_delay - t); | |||
997 | else { | |||
998 | r = read_one_char(stdinstdin, &key, last_refresh + arg_delay - t, NULL((void*)0)); | |||
999 | if (r == -ETIMEDOUT110) | |||
1000 | continue; | |||
1001 | if (r < 0) { | |||
1002 | log_error_errno(r, "Couldn't read key: %m")({ int _level = ((3)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD ); (log_get_max_level_realm(_realm) >= ((_level) & 0x07 )) ? log_internal_realm(((_realm) << 10 | (_level)), _e , "../src/cgtop/cgtop.c", 1002, __func__, "Couldn't read key: %m" ) : -abs(_e); }); | |||
1003 | goto finish; | |||
1004 | } | |||
1005 | } | |||
1006 | ||||
1007 | if (on_tty()) { /* TTY: Clear any user keystroke */ | |||
1008 | fputs("\r \r", stdoutstdout); | |||
1009 | fflush(stdoutstdout); | |||
1010 | } | |||
1011 | ||||
1012 | if (arg_batch) | |||
1013 | continue; | |||
1014 | ||||
1015 | switch (key) { | |||
1016 | ||||
1017 | case ' ': | |||
1018 | immediate_refresh = true1; | |||
1019 | break; | |||
1020 | ||||
1021 | case 'q': | |||
1022 | quit = true1; | |||
1023 | break; | |||
1024 | ||||
1025 | case 'p': | |||
1026 | arg_order = ORDER_PATH; | |||
1027 | break; | |||
1028 | ||||
1029 | case 't': | |||
1030 | arg_order = ORDER_TASKS; | |||
1031 | break; | |||
1032 | ||||
1033 | case 'c': | |||
1034 | arg_order = ORDER_CPU; | |||
1035 | break; | |||
1036 | ||||
1037 | case 'm': | |||
1038 | arg_order = ORDER_MEMORY; | |||
1039 | break; | |||
1040 | ||||
1041 | case 'i': | |||
1042 | arg_order = ORDER_IO; | |||
1043 | break; | |||
1044 | ||||
1045 | case '%': | |||
1046 | arg_cpu_type = arg_cpu_type == CPU_TIME ? CPU_PERCENT : CPU_TIME; | |||
1047 | break; | |||
1048 | ||||
1049 | case 'k': | |||
1050 | arg_count = arg_count != COUNT_ALL_PROCESSES ? COUNT_ALL_PROCESSES : COUNT_PIDS; | |||
1051 | fprintf(stdoutstdout, "\nCounting: %s.", counting_what()); | |||
1052 | fflush(stdoutstdout); | |||
1053 | sleep(1); | |||
1054 | break; | |||
1055 | ||||
1056 | case 'P': | |||
1057 | arg_count = arg_count != COUNT_USERSPACE_PROCESSES ? COUNT_USERSPACE_PROCESSES : COUNT_PIDS; | |||
1058 | fprintf(stdoutstdout, "\nCounting: %s.", counting_what()); | |||
1059 | fflush(stdoutstdout); | |||
1060 | sleep(1); | |||
1061 | break; | |||
1062 | ||||
1063 | case 'r': | |||
1064 | if (arg_count == COUNT_PIDS) | |||
1065 | fprintf(stdoutstdout, "\n\aCannot toggle recursive counting, not available in task counting mode."); | |||
1066 | else { | |||
1067 | arg_recursive = !arg_recursive; | |||
1068 | fprintf(stdoutstdout, "\nRecursive process counting: %s", yes_no(arg_recursive)); | |||
1069 | } | |||
1070 | fflush(stdoutstdout); | |||
1071 | sleep(1); | |||
1072 | break; | |||
1073 | ||||
1074 | case '+': | |||
1075 | if (arg_delay < USEC_PER_SEC((usec_t) 1000000ULL)) | |||
1076 | arg_delay += USEC_PER_MSEC((usec_t) 1000ULL)*250; | |||
1077 | else | |||
1078 | arg_delay += USEC_PER_SEC((usec_t) 1000000ULL); | |||
1079 | ||||
1080 | fprintf(stdoutstdout, "\nIncreased delay to %s.", format_timespan(h, sizeof(h), arg_delay, 0)); | |||
1081 | fflush(stdoutstdout); | |||
1082 | sleep(1); | |||
1083 | break; | |||
1084 | ||||
1085 | case '-': | |||
1086 | if (arg_delay <= USEC_PER_MSEC((usec_t) 1000ULL)*500) | |||
1087 | arg_delay = USEC_PER_MSEC((usec_t) 1000ULL)*250; | |||
1088 | else if (arg_delay < USEC_PER_MSEC((usec_t) 1000ULL)*1250) | |||
1089 | arg_delay -= USEC_PER_MSEC((usec_t) 1000ULL)*250; | |||
1090 | else | |||
1091 | arg_delay -= USEC_PER_SEC((usec_t) 1000000ULL); | |||
1092 | ||||
1093 | fprintf(stdoutstdout, "\nDecreased delay to %s.", format_timespan(h, sizeof(h), arg_delay, 0)); | |||
1094 | fflush(stdoutstdout); | |||
1095 | sleep(1); | |||
1096 | break; | |||
1097 | ||||
1098 | case '?': | |||
1099 | case 'h': | |||
1100 | ||||
1101 | #define ON"\x1B[0;1;39m" ANSI_HIGHLIGHT"\x1B[0;1;39m" | |||
1102 | #define OFF"\x1B[0m" ANSI_NORMAL"\x1B[0m" | |||
1103 | ||||
1104 | fprintf(stdoutstdout, | |||
1105 | "\t<" ON"\x1B[0;1;39m" "p" OFF"\x1B[0m" "> By path; <" ON"\x1B[0;1;39m" "t" OFF"\x1B[0m" "> By tasks/procs; <" ON"\x1B[0;1;39m" "c" OFF"\x1B[0m" "> By CPU; <" ON"\x1B[0;1;39m" "m" OFF"\x1B[0m" "> By memory; <" ON"\x1B[0;1;39m" "i" OFF"\x1B[0m" "> By I/O\n" | |||
1106 | "\t<" ON"\x1B[0;1;39m" "+" OFF"\x1B[0m" "> Inc. delay; <" ON"\x1B[0;1;39m" "-" OFF"\x1B[0m" "> Dec. delay; <" ON"\x1B[0;1;39m" "%%" OFF"\x1B[0m" "> Toggle time; <" ON"\x1B[0;1;39m" "SPACE" OFF"\x1B[0m" "> Refresh\n" | |||
1107 | "\t<" ON"\x1B[0;1;39m" "P" OFF"\x1B[0m" "> Toggle count userspace processes; <" ON"\x1B[0;1;39m" "k" OFF"\x1B[0m" "> Toggle count all processes\n" | |||
1108 | "\t<" ON"\x1B[0;1;39m" "r" OFF"\x1B[0m" "> Count processes recursively; <" ON"\x1B[0;1;39m" "q" OFF"\x1B[0m" "> Quit"); | |||
1109 | fflush(stdoutstdout); | |||
1110 | sleep(3); | |||
1111 | break; | |||
1112 | ||||
1113 | default: | |||
1114 | if (key < ' ') | |||
1115 | fprintf(stdoutstdout, "\nUnknown key '\\x%x'. Ignoring.", key); | |||
1116 | else | |||
1117 | fprintf(stdoutstdout, "\nUnknown key '%c'. Ignoring.", key); | |||
1118 | fflush(stdoutstdout); | |||
1119 | sleep(1); | |||
1120 | break; | |||
1121 | } | |||
1122 | } | |||
1123 | ||||
1124 | r = 0; | |||
1125 | ||||
1126 | finish: | |||
1127 | group_hashmap_free(a); | |||
1128 | group_hashmap_free(b); | |||
1129 | ||||
1130 | return r < 0 ? EXIT_FAILURE1 : EXIT_SUCCESS0; | |||
1131 | } |