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
This commit is contained in:
+12
-4
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user