]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
analyze: format output of 'systemd-analyze blame' by using Table
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 15 Jul 2019 15:19:59 +0000 (00:19 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 15 Jul 2019 15:19:59 +0000 (00:19 +0900)
src/analyze/analyze.c

index 97642660e49f4a2da6dafbbf7a4eba9ee8115c8c..aa89b11442afced27c555865a82e14b22bb88b8d 100644 (file)
@@ -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) {