Debian Codegraph @

src/analyze/analyze-critical-chain.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-bus.h"
4
5 #include "alloc-util.h"
6 #include "analyze.h"
7 #include "analyze-critical-chain.h"
8 #include "analyze-time-data.h"
9 #include "ansi-color.h"
10 #include "bus-error.h"
11 #include "bus-util.h"
12 #include "glyph-util.h"
13 #include "hashmap.h"
14 #include "log.h"
15 #include "pager.h"
16 #include "runtime-scope.h"
17 #include "sort-util.h"
18 #include "special.h"
19 #include "static-destruct.h"
20 #include "strv.h"
21 #include "time-util.h"
22
23 static Hashmap *unit_times_hashmap = NULL;
24 STATIC_DESTRUCTOR_REGISTER(unit_times_hashmap, hashmap_freep);
25
26 static int list_dependencies_print(
27 const char *name,
28 unsigned level,
29 unsigned branches,
30 bool last,
31 UnitTimes *times,
32 BootTimes *boot) {
33
34 for (unsigned i = level; i > 0; i--)
35 printf("%s", glyph(branches & (1 << (i-1)) ? GLYPH_TREE_VERTICAL : GLYPH_TREE_SPACE));
36
37 printf("%s", glyph(last ? GLYPH_TREE_RIGHT : GLYPH_TREE_BRANCH));
38
39 if (times && times->activating >= boot->userspace_time) {
40 if (timestamp_is_set(times->time))
41 printf("%s%s @%s +%s%s\n", ansi_highlight_red(), name,
42 FORMAT_TIMESPAN(times->activating - boot->userspace_time, USEC_PER_MSEC),
43 FORMAT_TIMESPAN(times->time, USEC_PER_MSEC), ansi_normal());
44 else
45 printf("%s @%s\n", name, FORMAT_TIMESPAN(times->activated - boot->userspace_time, USEC_PER_MSEC));
46 } else
47 printf("%s\n", name);
48
49 return 0;
50 }
51
52 static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, char ***deps) {
53 _cleanup_free_ char *path = NULL;
54
55 assert(bus);
56 assert(name);
57 assert(deps);
58
59 path = unit_dbus_path_from_name(name);
60 if (!path)
61 return -ENOMEM;
62
63 return bus_get_unit_property_strv(bus, path, "After", deps);
64 }
65
66 static int list_dependencies_compare(char *const *a, char *const *b) {
67 usec_t usa = 0, usb = 0;
68 UnitTimes *times;
69
70 assert(a);
71 assert(b);
72
73 times = hashmap_get(unit_times_hashmap, *a);
74 if (times)
75 usa = times->activated;
76 times = hashmap_get(unit_times_hashmap, *b);
77 if (times)
78 usb = times->activated;
79
80 return CMP(usb, usa);
81 }
82
83 static bool times_in_range(const UnitTimes *times, const BootTimes *boot) {
84 return times && times->activated > 0 && times->activated <= boot->finish_time;
85 }
86
87 static int list_dependencies_one(sd_bus *bus, const char *name, unsigned level, char ***units, unsigned branches) {
88 _cleanup_strv_free_ char **deps = NULL;
89 int r;
90 usec_t service_longest = 0;
91 int to_print = 0;
92 UnitTimes *times;
93 BootTimes *boot;
94
95 if (strv_extend(units, name))
96 return log_oom();
97
98 r = list_dependencies_get_dependencies(bus, name, &deps);
99 if (r < 0)
100 return r;
101
102 typesafe_qsort(deps, strv_length(deps), list_dependencies_compare);
103
104 r = acquire_boot_times(bus, /* require_finished= */ true, &boot);
105 if (r < 0)
106 return r;
107
108 STRV_FOREACH(c, deps) {
109 times = hashmap_get(unit_times_hashmap, *c);
110 if (times_in_range(times, boot) && times->activated >= service_longest)
111 service_longest = times->activated;
112 }
113
114 if (service_longest == 0)
115 return r;
116
117 STRV_FOREACH(c, deps) {
118 times = hashmap_get(unit_times_hashmap, *c);
119 if (times_in_range(times, boot) && service_longest - times->activated <= arg_fuzz)
120 to_print++;
121 }
122
123 if (!to_print)
124 return r;
125
126 STRV_FOREACH(c, deps) {
127 times = hashmap_get(unit_times_hashmap, *c);
128 if (!times_in_range(times, boot) || service_longest - times->activated > arg_fuzz)
129 continue;
130
131 to_print--;
132
133 r = list_dependencies_print(*c, level, branches, to_print == 0, times, boot);
134 if (r < 0)
135 return r;
136
137 if (strv_contains(*units, *c)) {
138 r = list_dependencies_print("...", level + 1, (branches << 1) | (to_print ? 1 : 0),
139 true, NULL, boot);
140 if (r < 0)
141 return r;
142 continue;
143 }
144
145 r = list_dependencies_one(bus, *c, level + 1, units, (branches << 1) | (to_print ? 1 : 0));
146 if (r < 0)
147 return r;
148
149 if (to_print == 0)
150 break;
151 }
152 return 0;
153 }
154
155 static int list_dependencies(sd_bus *bus, const char *name) {
156 _cleanup_strv_free_ char **units = NULL;
157 UnitTimes *times;
158 int r;
159 const char *id;
160 _cleanup_free_ char *path = NULL;
161 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
162 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
163 BootTimes *boot;
164
165 assert(bus);
166
167 path = unit_dbus_path_from_name(name);
168 if (!path)
169 return -ENOMEM;
170
171 r = sd_bus_get_property(
172 bus,
173 "org.freedesktop.systemd1",
174 path,
175 "org.freedesktop.systemd1.Unit",
176 "Id",
177 &error,
178 &reply,
179 "s");
180 if (r < 0)
181 return log_error_errno(r, "Failed to get ID: %s", bus_error_message(&error, r));
182
183 r = sd_bus_message_read(reply, "s", &id);
184 if (r < 0)
185 return bus_log_parse_error(r);
186
187 times = hashmap_get(unit_times_hashmap, id);
188
189 r = acquire_boot_times(bus, /* require_finished= */ true, &boot);
190 if (r < 0)
191 return r;
192
193 if (times) {
194 if (times->time)
195 printf("%s%s +%s%s\n", ansi_highlight_red(), id,
196 FORMAT_TIMESPAN(times->time, USEC_PER_MSEC), ansi_normal());
197 else if (times->activated > boot->userspace_time)
198 printf("%s @%s\n", id,
199 FORMAT_TIMESPAN(times->activated - boot->userspace_time, USEC_PER_MSEC));
200 else
201 printf("%s\n", id);
202 }
203
204 return list_dependencies_one(bus, name, 0, &units, 0);
205 }
206
207 int verb_critical_chain(int argc, char *argv[], uintptr_t _data, void *userdata) {
208 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
209 _cleanup_(unit_times_free_arrayp) UnitTimes *times = NULL;
210 int n, r;
211
212 r = acquire_bus(&bus, NULL);
213 if (r < 0)
214 return bus_log_connect_error(r, arg_transport, arg_runtime_scope);
215
216 n = acquire_time_data(bus, /* require_finished= */ true, &times);
217 if (n <= 0)
218 return n;
219
220 for (UnitTimes *u = times; u->has_data; u++) {
221 r = hashmap_ensure_put(&unit_times_hashmap, &string_hash_ops, u->name, u);
222 if (r < 0)
223 return log_error_errno(r, "Failed to add entry to hashmap: %m");
224 }
225
226 pager_open(arg_pager_flags);
227
228 puts("The time when unit became active or started is printed after the \"@\" character.\n"
229 "The time the unit took to start is printed after the \"+\" character.\n");
230
231 if (argc > 1)
232 STRV_FOREACH(name, strv_skip(argv, 1))
233 list_dependencies(bus, *name);
234 else
235 list_dependencies(bus, SPECIAL_DEFAULT_TARGET);
236
237 return EXIT_SUCCESS;
238 }