From: Yu Watanabe Date: Mon, 15 Jul 2019 15:19:59 +0000 (+0900) Subject: analyze: format output of 'systemd-analyze blame' by using Table X-Git-Tag: v243-rc1~119^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d8bfdbe1fa8e5ddd1d9e4562b8d25f240136fc2b;p=thirdparty%2Fsystemd.git analyze: format output of 'systemd-analyze blame' by using Table --- diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c index 97642660e49..aa89b11442a 100644 --- a/src/analyze/analyze.c +++ b/src/analyze/analyze.c @@ -26,6 +26,7 @@ #include "def.h" #include "fd-util.h" #include "fileio.h" +#include "format-table.h" #include "glob-util.h" #include "hashmap.h" #include "locale-util.h" @@ -204,10 +205,6 @@ static int bus_get_unit_property_strv(sd_bus *bus, const char *path, const char return 0; } -static int compare_unit_time(const struct unit_times *a, const struct unit_times *b) { - return CMP(b->time, a->time); -} - static int compare_unit_start(const struct unit_times *a, const struct unit_times *b) { return CMP(a->activating, b->activating); } @@ -1070,7 +1067,9 @@ static int analyze_critical_chain(int argc, char *argv[], void *userdata) { static int analyze_blame(int argc, char *argv[], void *userdata) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; _cleanup_(unit_times_freep) struct unit_times *times = NULL; + _cleanup_(table_unrefp) Table *table = NULL; struct unit_times *u; + TableCell *cell; int n, r; r = acquire_bus(&bus, NULL); @@ -1081,18 +1080,50 @@ static int analyze_blame(int argc, char *argv[], void *userdata) { if (n <= 0) return n; - typesafe_qsort(times, n, compare_unit_time); + table = table_new("TIME", "UNIT"); + if (!table) + return log_oom(); - (void) pager_open(arg_pager_flags); + table_set_header(table, false); + + assert_se(cell = table_get_cell(table, 0, 0)); + r = table_set_ellipsize_percent(table, cell, 100); + if (r < 0) + return r; + + r = table_set_align_percent(table, cell, 100); + if (r < 0) + return r; + + assert_se(cell = table_get_cell(table, 0, 1)); + r = table_set_ellipsize_percent(table, cell, 100); + if (r < 0) + return r; + + r = table_set_sort(table, 0, SIZE_MAX); + if (r < 0) + return r; + + r = table_set_reverse(table, 0, true); + if (r < 0) + return r; for (u = times; u->has_data; u++) { - char ts[FORMAT_TIMESPAN_MAX]; + if (u->time <= 0) + continue; + + r = table_add_cell(table, NULL, TABLE_TIMESPAN_MSEC, &u->time); + if (r < 0) + return r; - if (u->time > 0) - printf("%16s %s\n", format_timespan(ts, sizeof(ts), u->time, USEC_PER_MSEC), u->name); + r = table_add_cell(table, NULL, TABLE_STRING, u->name); + if (r < 0) + return r; } - return 0; + (void) pager_open(arg_pager_flags); + + return table_print(table, NULL); } static int analyze_time(int argc, char *argv[], void *userdata) {