1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "analyze.h"
4 #include "analyze-blame.h"
5 #include "analyze-time-data.h"
6 #include "format-table.h"
7 
verb_blame(int argc,char * argv[],void * userdata)8 int verb_blame(int argc, char *argv[], void *userdata) {
9         _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
10         _cleanup_(unit_times_free_arrayp) UnitTimes *times = NULL;
11         _cleanup_(table_unrefp) Table *table = NULL;
12         TableCell *cell;
13         int n, r;
14 
15         r = acquire_bus(&bus, NULL);
16         if (r < 0)
17                 return bus_log_connect_error(r, arg_transport);
18 
19         n = acquire_time_data(bus, &times);
20         if (n <= 0)
21                 return n;
22 
23         table = table_new("time", "unit");
24         if (!table)
25                 return log_oom();
26 
27         table_set_header(table, false);
28 
29         assert_se(cell = table_get_cell(table, 0, 0));
30         r = table_set_ellipsize_percent(table, cell, 100);
31         if (r < 0)
32                 return r;
33 
34         r = table_set_align_percent(table, cell, 100);
35         if (r < 0)
36                 return r;
37 
38         assert_se(cell = table_get_cell(table, 0, 1));
39         r = table_set_ellipsize_percent(table, cell, 100);
40         if (r < 0)
41                 return r;
42 
43         r = table_set_sort(table, (size_t) 0);
44         if (r < 0)
45                 return r;
46 
47         r = table_set_reverse(table, 0, true);
48         if (r < 0)
49                 return r;
50 
51         for (UnitTimes *u = times; u->has_data; u++) {
52                 if (u->time <= 0)
53                         continue;
54 
55                 r = table_add_many(table,
56                                    TABLE_TIMESPAN_MSEC, u->time,
57                                    TABLE_STRING, u->name);
58                 if (r < 0)
59                         return table_log_add_error(r);
60         }
61 
62         pager_open(arg_pager_flags);
63 
64         return table_print(table, NULL);
65 }
66