cgtop: use anonymous buffers for formatting of bytes and timespans
This commit is contained in:
+27
-25
@@ -101,6 +101,10 @@ static const char *maybe_format_timespan(char *buf, size_t l, usec_t t, usec_t a
|
||||
return format_timespan(buf, l, t, accuracy);
|
||||
}
|
||||
|
||||
#define BUFSIZE1 CONST_MAX(FORMAT_TIMESPAN_MAX, DECIMAL_STR_MAX(usec_t))
|
||||
#define MAYBE_FORMAT_TIMESPAN(t, accuracy) \
|
||||
maybe_format_timespan((char[BUFSIZE1]){}, BUFSIZE1, t, accuracy)
|
||||
|
||||
static const char *maybe_format_bytes(char *buf, size_t l, bool is_valid, uint64_t t) {
|
||||
if (!is_valid)
|
||||
return "-";
|
||||
@@ -111,6 +115,10 @@ static const char *maybe_format_bytes(char *buf, size_t l, bool is_valid, uint64
|
||||
return format_bytes(buf, l, t);
|
||||
}
|
||||
|
||||
#define BUFSIZE2 CONST_MAX(FORMAT_BYTES_MAX, DECIMAL_STR_MAX(uint64_t))
|
||||
#define MAYBE_FORMAT_BYTES(is_valid, t) \
|
||||
maybe_format_bytes((char[BUFSIZE2]){}, BUFSIZE2, is_valid, t)
|
||||
|
||||
static bool is_root_cgroup(const char *path) {
|
||||
|
||||
/* Returns true if the specified path belongs to the root cgroup. The root cgroup is special on cgroup v2 as it
|
||||
@@ -595,8 +603,7 @@ static void display(Hashmap *a) {
|
||||
Group *g;
|
||||
Group **array;
|
||||
signed path_columns;
|
||||
unsigned rows, n = 0, j, maxtcpu = 0, maxtpath = 3; /* 3 for ellipsize() to work properly */
|
||||
char buffer[MAX4(21U, FORMAT_BYTES_MAX, FORMAT_TIMESPAN_MAX, DECIMAL_STR_MAX(usec_t))];
|
||||
unsigned rows, n = 0, maxtcpu = 0, maxtpath = 3; /* 3 for ellipsize() to work properly */
|
||||
|
||||
assert(a);
|
||||
|
||||
@@ -612,43 +619,38 @@ static void display(Hashmap *a) {
|
||||
typesafe_qsort(array, n, group_compare);
|
||||
|
||||
/* Find the longest names in one run */
|
||||
for (j = 0; j < n; j++) {
|
||||
unsigned cputlen, pathtlen;
|
||||
|
||||
maybe_format_timespan(buffer, sizeof(buffer), (usec_t) (array[j]->cpu_usage / NSEC_PER_USEC), 0);
|
||||
cputlen = strlen(buffer);
|
||||
maxtcpu = MAX(maxtcpu, cputlen);
|
||||
|
||||
pathtlen = strlen(array[j]->path);
|
||||
maxtpath = MAX(maxtpath, pathtlen);
|
||||
for (unsigned j = 0; j < n; j++) {
|
||||
maxtcpu = MAX(maxtcpu,
|
||||
strlen(MAYBE_FORMAT_TIMESPAN((usec_t) (array[j]->cpu_usage / NSEC_PER_USEC), 0)));
|
||||
maxtpath = MAX(maxtpath,
|
||||
strlen(array[j]->path));
|
||||
}
|
||||
|
||||
if (arg_cpu_type == CPU_PERCENT)
|
||||
xsprintf(buffer, "%6s", "%CPU");
|
||||
else
|
||||
xsprintf(buffer, "%*s", maxtcpu, "CPU Time");
|
||||
|
||||
rows = lines();
|
||||
if (rows <= 10)
|
||||
rows = 10;
|
||||
|
||||
if (on_tty()) {
|
||||
const char *on, *off;
|
||||
unsigned cpu_len = arg_cpu_type == CPU_PERCENT ? 6 : maxtcpu;
|
||||
|
||||
path_columns = columns() - 36 - strlen(buffer);
|
||||
path_columns = columns() - 36 - cpu_len;
|
||||
if (path_columns < 10)
|
||||
path_columns = 10;
|
||||
|
||||
on = ansi_highlight_underline();
|
||||
off = ansi_underline();
|
||||
|
||||
printf("%s%s%-*s%s %s%7s%s %s%s%s %s%8s%s %s%8s%s %s%8s%s%s\n",
|
||||
printf("%s%s%-*s%s %s%7s%s %s%*s%s %s%8s%s %s%8s%s %s%8s%s%s\n",
|
||||
ansi_underline(),
|
||||
arg_order == ORDER_PATH ? on : "", path_columns, "Control Group",
|
||||
arg_order == ORDER_PATH ? off : "",
|
||||
arg_order == ORDER_TASKS ? on : "", arg_count == COUNT_PIDS ? "Tasks" : arg_count == COUNT_USERSPACE_PROCESSES ? "Procs" : "Proc+",
|
||||
arg_order == ORDER_TASKS ? on : "",
|
||||
arg_count == COUNT_PIDS ? "Tasks" : arg_count == COUNT_USERSPACE_PROCESSES ? "Procs" : "Proc+",
|
||||
arg_order == ORDER_TASKS ? off : "",
|
||||
arg_order == ORDER_CPU ? on : "", buffer,
|
||||
arg_order == ORDER_CPU ? on : "",
|
||||
cpu_len,
|
||||
arg_cpu_type == CPU_PERCENT ? "%CPU" : "CPU Time",
|
||||
arg_order == ORDER_CPU ? off : "",
|
||||
arg_order == ORDER_MEMORY ? on : "", "Memory",
|
||||
arg_order == ORDER_MEMORY ? off : "",
|
||||
@@ -660,7 +662,7 @@ static void display(Hashmap *a) {
|
||||
} else
|
||||
path_columns = maxtpath;
|
||||
|
||||
for (j = 0; j < n; j++) {
|
||||
for (unsigned j = 0; j < n; j++) {
|
||||
_cleanup_free_ char *ellipsized = NULL;
|
||||
const char *path;
|
||||
|
||||
@@ -684,11 +686,11 @@ static void display(Hashmap *a) {
|
||||
else
|
||||
fputs(" -", stdout);
|
||||
} else
|
||||
printf(" %*s", maxtcpu, maybe_format_timespan(buffer, sizeof(buffer), (usec_t) (g->cpu_usage / NSEC_PER_USEC), 0));
|
||||
printf(" %*s", maxtcpu, MAYBE_FORMAT_TIMESPAN((usec_t) (g->cpu_usage / NSEC_PER_USEC), 0));
|
||||
|
||||
printf(" %8s", maybe_format_bytes(buffer, sizeof(buffer), g->memory_valid, g->memory));
|
||||
printf(" %8s", maybe_format_bytes(buffer, sizeof(buffer), g->io_valid, g->io_input_bps));
|
||||
printf(" %8s", maybe_format_bytes(buffer, sizeof(buffer), g->io_valid, g->io_output_bps));
|
||||
printf(" %8s", MAYBE_FORMAT_BYTES(g->memory_valid, g->memory));
|
||||
printf(" %8s", MAYBE_FORMAT_BYTES(g->io_valid, g->io_input_bps));
|
||||
printf(" %8s", MAYBE_FORMAT_BYTES(g->io_valid, g->io_output_bps));
|
||||
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user