Skip to content

Commit 4294951

Browse files
arianvppoettering
authored andcommitted
cgtop: Display cpu time in microseonds with --raw
this makes the CPU time easily parseable; which was the goal of --raw in the first place. This only triggers if --raw is combined with --cpu=time
1 parent 9d7b11f commit 4294951

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

man/systemd-cgtop.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
<term><option>-r</option></term>
131131
<term><option>--raw</option></term>
132132

133-
<listitem><para>Format byte counts (as in memory usage and I/O metrics)
133+
<listitem><para>Format byte counts (as in memory usage and I/O metrics) and CPU time
134134
with raw numeric values rather than human-readable
135135
numbers.</para></listitem>
136136
</varlistentry>

src/basic/macro.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ static inline size_t GREEDY_ALLOC_ROUND_UP(size_t l) {
281281
MAX(_c, z); \
282282
})
283283

284+
#define MAX4(x, y, z, a) \
285+
({ \
286+
const typeof(x) _d = MAX3(x, y, z); \
287+
MAX(_d, a); \
288+
})
289+
284290
#undef MIN
285291
#define MIN(a, b) __MIN(UNIQ, (a), UNIQ, (b))
286292
#define __MIN(aq, a, bq, b) \

src/cgtop/cgtop.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ static Group *group_free(Group *g) {
9191
return mfree(g);
9292
}
9393

94+
95+
static const char *maybe_format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) {
96+
if (arg_raw) {
97+
snprintf(buf, l, USEC_FMT, t);
98+
return buf;
99+
}
100+
return format_timespan(buf, l, t, accuracy);
101+
}
102+
94103
static const char *maybe_format_bytes(char *buf, size_t l, bool is_valid, uint64_t t) {
95104
if (!is_valid)
96105
return "-";
@@ -586,7 +595,7 @@ static void display(Hashmap *a) {
586595
Group **array;
587596
signed path_columns;
588597
unsigned rows, n = 0, j, maxtcpu = 0, maxtpath = 3; /* 3 for ellipsize() to work properly */
589-
char buffer[MAX3(21U, FORMAT_BYTES_MAX, FORMAT_TIMESPAN_MAX)];
598+
char buffer[MAX4(21U, FORMAT_BYTES_MAX, FORMAT_TIMESPAN_MAX, DECIMAL_STR_MAX(usec_t))];
590599

591600
assert(a);
592601

@@ -605,7 +614,7 @@ static void display(Hashmap *a) {
605614
for (j = 0; j < n; j++) {
606615
unsigned cputlen, pathtlen;
607616

608-
format_timespan(buffer, sizeof(buffer), (usec_t) (array[j]->cpu_usage / NSEC_PER_USEC), 0);
617+
maybe_format_timespan(buffer, sizeof(buffer), (usec_t) (array[j]->cpu_usage / NSEC_PER_USEC), 0);
609618
cputlen = strlen(buffer);
610619
maxtcpu = MAX(maxtcpu, cputlen);
611620

@@ -674,7 +683,7 @@ static void display(Hashmap *a) {
674683
else
675684
fputs(" -", stdout);
676685
} else
677-
printf(" %*s", maxtcpu, format_timespan(buffer, sizeof(buffer), (usec_t) (g->cpu_usage / NSEC_PER_USEC), 0));
686+
printf(" %*s", maxtcpu, maybe_format_timespan(buffer, sizeof(buffer), (usec_t) (g->cpu_usage / NSEC_PER_USEC), 0));
678687

679688
printf(" %8s", maybe_format_bytes(buffer, sizeof(buffer), g->memory_valid, g->memory));
680689
printf(" %8s", maybe_format_bytes(buffer, sizeof(buffer), g->io_valid, g->io_input_bps));

0 commit comments

Comments
 (0)