Skip to content

Commit 5fe74e8

Browse files
szab100poettering
authored andcommitted
cgtop: Fix processing of controllers other than CPU
After debugging the issue with gdb, I found that the following change 94ddb08 "cgtop: Still try to get CPU statistics if controller-free" has introduced a bug, which prevents process(..) method processing memory and io controllers when cpu_accounting_is_cheap() is true. The obvious fix is to move this branch to be the last one, keeping the intended behavior of the above change, without having a negative effect on the other controllers. Fixes systemd#11773 [systemd-cgtop no longer shows memory (and io) usage]
1 parent 804f8e1 commit 5fe74e8

File tree

1 file changed

+65
-65
lines changed

1 file changed

+65
-65
lines changed

src/cgtop/cgtop.c

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -223,71 +223,6 @@ static int process(
223223
if (g->n_tasks > 0)
224224
g->n_tasks_valid = true;
225225

226-
} else if (STR_IN_SET(controller, "cpu", "cpuacct") || cpu_accounting_is_cheap()) {
227-
_cleanup_free_ char *p = NULL, *v = NULL;
228-
uint64_t new_usage;
229-
nsec_t timestamp;
230-
231-
if (is_root_cgroup(path)) {
232-
r = procfs_cpu_get_usage(&new_usage);
233-
if (r < 0)
234-
return r;
235-
} else if (all_unified) {
236-
_cleanup_free_ char *val = NULL;
237-
238-
if (!streq(controller, "cpu"))
239-
return 0;
240-
241-
r = cg_get_keyed_attribute("cpu", path, "cpu.stat", STRV_MAKE("usage_usec"), &val);
242-
if (IN_SET(r, -ENOENT, -ENXIO))
243-
return 0;
244-
if (r < 0)
245-
return r;
246-
247-
r = safe_atou64(val, &new_usage);
248-
if (r < 0)
249-
return r;
250-
251-
new_usage *= NSEC_PER_USEC;
252-
} else {
253-
if (!streq(controller, "cpuacct"))
254-
return 0;
255-
256-
r = cg_get_path(controller, path, "cpuacct.usage", &p);
257-
if (r < 0)
258-
return r;
259-
260-
r = read_one_line_file(p, &v);
261-
if (r == -ENOENT)
262-
return 0;
263-
if (r < 0)
264-
return r;
265-
266-
r = safe_atou64(v, &new_usage);
267-
if (r < 0)
268-
return r;
269-
}
270-
271-
timestamp = now_nsec(CLOCK_MONOTONIC);
272-
273-
if (g->cpu_iteration == iteration - 1 &&
274-
(nsec_t) new_usage > g->cpu_usage) {
275-
276-
nsec_t x, y;
277-
278-
x = timestamp - g->cpu_timestamp;
279-
if (x < 1)
280-
x = 1;
281-
282-
y = (nsec_t) new_usage - g->cpu_usage;
283-
g->cpu_fraction = (double) y / (double) x;
284-
g->cpu_valid = true;
285-
}
286-
287-
g->cpu_usage = (nsec_t) new_usage;
288-
g->cpu_timestamp = timestamp;
289-
g->cpu_iteration = iteration;
290-
291226
} else if (streq(controller, "memory")) {
292227

293228
if (is_root_cgroup(path)) {
@@ -411,6 +346,71 @@ static int process(
411346
g->io_output = wr;
412347
g->io_timestamp = timestamp;
413348
g->io_iteration = iteration;
349+
} else if (STR_IN_SET(controller, "cpu", "cpuacct") || cpu_accounting_is_cheap()) {
350+
_cleanup_free_ char *p = NULL, *v = NULL;
351+
uint64_t new_usage;
352+
nsec_t timestamp;
353+
354+
if (is_root_cgroup(path)) {
355+
r = procfs_cpu_get_usage(&new_usage);
356+
if (r < 0)
357+
return r;
358+
} else if (all_unified) {
359+
_cleanup_free_ char *val = NULL;
360+
361+
if (!streq(controller, "cpu"))
362+
return 0;
363+
364+
r = cg_get_keyed_attribute("cpu", path, "cpu.stat", STRV_MAKE("usage_usec"), &val);
365+
if (IN_SET(r, -ENOENT, -ENXIO))
366+
return 0;
367+
if (r < 0)
368+
return r;
369+
370+
r = safe_atou64(val, &new_usage);
371+
if (r < 0)
372+
return r;
373+
374+
new_usage *= NSEC_PER_USEC;
375+
} else {
376+
if (!streq(controller, "cpuacct"))
377+
return 0;
378+
379+
r = cg_get_path(controller, path, "cpuacct.usage", &p);
380+
if (r < 0)
381+
return r;
382+
383+
r = read_one_line_file(p, &v);
384+
if (r == -ENOENT)
385+
return 0;
386+
if (r < 0)
387+
return r;
388+
389+
r = safe_atou64(v, &new_usage);
390+
if (r < 0)
391+
return r;
392+
}
393+
394+
timestamp = now_nsec(CLOCK_MONOTONIC);
395+
396+
if (g->cpu_iteration == iteration - 1 &&
397+
(nsec_t) new_usage > g->cpu_usage) {
398+
399+
nsec_t x, y;
400+
401+
x = timestamp - g->cpu_timestamp;
402+
if (x < 1)
403+
x = 1;
404+
405+
y = (nsec_t) new_usage - g->cpu_usage;
406+
g->cpu_fraction = (double) y / (double) x;
407+
g->cpu_valid = true;
408+
}
409+
410+
g->cpu_usage = (nsec_t) new_usage;
411+
g->cpu_timestamp = timestamp;
412+
g->cpu_iteration = iteration;
413+
414414
}
415415

416416
if (ret)

0 commit comments

Comments
 (0)