From 9b75d9bc66dc4f64e4fdd33603d199d374c0873b Mon Sep 17 00:00:00 2001 From: Kakueeen Date: Mon, 10 Aug 2026 23:38:17 +0800 Subject: [PATCH] cgtop: use dynamic width for CPU% column (#43286) On many-CPU hosts, the CPU% column in systemd-cgtop can exceed the hardcoded %6.1f format width (e.g. 12769.4% on a 128-CPU host), causing column overflow and table misalignment. Follow the existing maxtcpu pattern for CPU Time: dynamically compute the maximum width of CPU percentage values and use it for both the header and data rows. Fixes #39819 --- src/cgtop/cgtop.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c index 0c96ad7891a..974a946142a 100644 --- a/src/cgtop/cgtop.c +++ b/src/cgtop/cgtop.c @@ -7,6 +7,7 @@ #include "build.h" #include "cgroup-show.h" #include "cgroup-util.h" +#include "cpu-set-util.h" #include "fd-util.h" #include "fileio.h" #include "format-table.h" @@ -581,7 +582,8 @@ static void display(Hashmap *a) { Group *g; Group **array; signed path_columns; - unsigned rows, n = 0, maxtcpu = 0, maxtpath = 3; /* 3 for ellipsize() to work properly */ + unsigned rows, n = 0, maxtcpu = 0, maxtpath = 3, /* 3 for ellipsize() to work properly */ + maxpcpu = 6; /* minimum width for "%CPU" header */ assert(a); @@ -604,13 +606,19 @@ static void display(Hashmap *a) { strlen(array[j]->path)); } + if (arg_cpu_type == CPU_PERCENTAGE) { + unsigned n_cpus; + if (cpus_online(&n_cpus) >= 0) + maxpcpu = MAX(maxpcpu, DECIMAL_STR_WIDTH(n_cpus) + STRLEN("00.0")); + } + rows = lines(); if (rows <= 10) rows = 10; if (on_tty()) { const char *on, *off; - int cpu_len = arg_cpu_type == CPU_PERCENTAGE ? 6 : maxtcpu; + int cpu_len = arg_cpu_type == CPU_PERCENTAGE ? (int) maxpcpu : (int) maxtcpu; path_columns = columns() - 36 - cpu_len; if (path_columns < 10) @@ -660,9 +668,9 @@ static void display(Hashmap *a) { if (arg_cpu_type == CPU_PERCENTAGE) { if (g->cpu_valid) - printf(" %6.1f", g->cpu_fraction*100); + printf(" %*.1f", (int) maxpcpu, g->cpu_fraction*100); else - fputs(" -", stdout); + printf(" %*s", (int) maxpcpu, "-"); } else printf(" %*s", (int) maxtcpu, -- 2.47.3