Debian Codegraph @

src/analyze/analyze-time-data.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-time-data.h"
8 #include "bus-error.h"
9 #include "bus-locator.h"
10 #include "bus-map-properties.h"
11 #include "bus-unit-util.h"
12 #include "bus-util.h"
13 #include "log.h"
14 #include "runtime-scope.h"
15 #include "special.h"
16 #include "string-util.h"
17 #include "strv.h"
18 #include "time-util.h"
19
20 static void subtract_timestamp(usec_t *a, usec_t b) {
21 assert(a);
22
23 if (*a > 0) {
24 assert(*a >= b);
25 *a -= b;
26 }
27 }
28
29 static int log_not_finished(usec_t finish_time) {
30 return log_error_errno(SYNTHETIC_ERRNO(EINPROGRESS),
31 "Bootup is not yet finished (org.freedesktop.systemd1.Manager.FinishTimestampMonotonic=%"PRIu64").\n"
32 "Please try again later.\n"
33 "Hint: Use 'systemctl%s list-jobs' to see active jobs",
34 finish_time,
35 arg_runtime_scope == RUNTIME_SCOPE_SYSTEM ? "" : " --user");
36 }
37
38 int acquire_boot_times(sd_bus *bus, bool require_finished, BootTimes **ret) {
39 static const struct bus_properties_map property_map[] = {
40 { "FirmwareTimestampMonotonic", "t", NULL, offsetof(BootTimes, firmware_time) },
41 { "LoaderTimestampMonotonic", "t", NULL, offsetof(BootTimes, loader_time) },
42 { "KernelTimestamp", "t", NULL, offsetof(BootTimes, kernel_time) },
43 { "InitRDTimestampMonotonic", "t", NULL, offsetof(BootTimes, initrd_time) },
44 { "UserspaceTimestampMonotonic", "t", NULL, offsetof(BootTimes, userspace_time) },
45 { "FinishTimestampMonotonic", "t", NULL, offsetof(BootTimes, finish_time) },
46 { "SecurityStartTimestampMonotonic", "t", NULL, offsetof(BootTimes, security_start_time) },
47 { "SecurityFinishTimestampMonotonic", "t", NULL, offsetof(BootTimes, security_finish_time) },
48 { "ShutdownStartTimestampMonotonic", "t", NULL, offsetof(BootTimes, shutdown_start_time) },
49 { "GeneratorsStartTimestampMonotonic", "t", NULL, offsetof(BootTimes, generators_start_time) },
50 { "GeneratorsFinishTimestampMonotonic", "t", NULL, offsetof(BootTimes, generators_finish_time) },
51 { "UnitsLoadStartTimestampMonotonic", "t", NULL, offsetof(BootTimes, unitsload_start_time) },
52 { "UnitsLoadFinishTimestampMonotonic", "t", NULL, offsetof(BootTimes, unitsload_finish_time) },
53 { "InitRDSecurityStartTimestampMonotonic", "t", NULL, offsetof(BootTimes, initrd_security_start_time) },
54 { "InitRDSecurityFinishTimestampMonotonic", "t", NULL, offsetof(BootTimes, initrd_security_finish_time) },
55 { "InitRDGeneratorsStartTimestampMonotonic", "t", NULL, offsetof(BootTimes, initrd_generators_start_time) },
56 { "InitRDGeneratorsFinishTimestampMonotonic", "t", NULL, offsetof(BootTimes, initrd_generators_finish_time) },
57 { "InitRDUnitsLoadStartTimestampMonotonic", "t", NULL, offsetof(BootTimes, initrd_unitsload_start_time) },
58 { "InitRDUnitsLoadFinishTimestampMonotonic", "t", NULL, offsetof(BootTimes, initrd_unitsload_finish_time) },
59 { "SoftRebootsCount", "u", NULL, offsetof(BootTimes, soft_reboots_count) },
60 {},
61 };
62 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
63 static BootTimes times;
64 static bool cached = false;
65 int r;
66
67 if (cached) {
68 if (require_finished && times.finish_time <= 0)
69 return log_not_finished(times.finish_time);
70
71 if (ret)
72 *ret = &times;
73 return 0;
74 }
75
76 assert_cc(sizeof(usec_t) == sizeof(uint64_t));
77
78 r = bus_map_all_properties(
79 bus,
80 "org.freedesktop.systemd1",
81 "/org/freedesktop/systemd1",
82 property_map,
83 BUS_MAP_STRDUP,
84 &error,
85 NULL,
86 &times);
87 if (r < 0)
88 return log_error_errno(r, "Failed to get timestamp properties: %s", bus_error_message(&error, r));
89
90 if (require_finished && times.finish_time <= 0)
91 return log_not_finished(times.finish_time);
92
93 if (arg_runtime_scope == RUNTIME_SCOPE_SYSTEM && times.soft_reboots_count > 0) {
94 /* On soft-reboot ignore kernel/firmware/initrd times as they are from the previous boot */
95 times.firmware_time = times.loader_time = times.kernel_time = times.initrd_time =
96 times.initrd_security_start_time = times.initrd_security_finish_time =
97 times.initrd_generators_start_time = times.initrd_generators_finish_time =
98 times.initrd_unitsload_start_time = times.initrd_unitsload_finish_time = 0;
99 times.reverse_offset = times.shutdown_start_time;
100
101 /* Clamp all timestamps to avoid showing huge graphs */
102 if (timestamp_is_set(times.finish_time))
103 subtract_timestamp(&times.finish_time, times.reverse_offset);
104 subtract_timestamp(&times.userspace_time, times.reverse_offset);
105
106 subtract_timestamp(&times.generators_start_time, times.reverse_offset);
107 subtract_timestamp(&times.generators_finish_time, times.reverse_offset);
108
109 subtract_timestamp(&times.unitsload_start_time, times.reverse_offset);
110 subtract_timestamp(&times.unitsload_finish_time, times.reverse_offset);
111 } else if (arg_runtime_scope == RUNTIME_SCOPE_SYSTEM && timestamp_is_set(times.security_start_time)) {
112 /* security_start_time is set when systemd is not running under container environment. */
113 if (times.initrd_time > 0)
114 times.kernel_done_time = times.initrd_time;
115 else
116 times.kernel_done_time = times.userspace_time;
117 } else {
118 /*
119 * User-instance-specific or container-system-specific timestamps processing
120 * (see comment to reverse_offset in BootTimes).
121 */
122 times.reverse_offset = times.userspace_time;
123
124 times.firmware_time = times.loader_time = times.kernel_time = times.initrd_time =
125 times.userspace_time = times.security_start_time = times.security_finish_time = 0;
126
127 if (times.finish_time > 0)
128 subtract_timestamp(&times.finish_time, times.reverse_offset);
129
130 subtract_timestamp(&times.generators_start_time, times.reverse_offset);
131 subtract_timestamp(&times.generators_finish_time, times.reverse_offset);
132
133 subtract_timestamp(&times.unitsload_start_time, times.reverse_offset);
134 subtract_timestamp(&times.unitsload_finish_time, times.reverse_offset);
135 }
136
137 cached = true;
138
139 if (ret)
140 *ret = &times;
141 return 0;
142 }
143
144 static int bus_get_uint64_property(sd_bus *bus, const char *path, const char *interface, const char *property, uint64_t *val) {
145 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
146 int r;
147
148 assert(bus);
149 assert(path);
150 assert(interface);
151 assert(property);
152 assert(val);
153
154 r = sd_bus_get_property_trivial(
155 bus,
156 "org.freedesktop.systemd1",
157 path,
158 interface,
159 property,
160 &error,
161 't', val);
162 if (r < 0)
163 return log_error_errno(r, "Failed to parse reply: %s", bus_error_message(&error, r));
164
165 return 0;
166 }
167
168 int pretty_boot_time(sd_bus *bus, char **ret) {
169 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
170 _cleanup_free_ char *path = NULL, *unit_id = NULL, *text = NULL;
171 usec_t activated_time = USEC_INFINITY;
172 BootTimes *t;
173 int r;
174
175 assert(ret);
176
177 r = acquire_boot_times(bus, /* require_finished= */ true, &t);
178 if (r < 0)
179 return r;
180
181 path = unit_dbus_path_from_name(SPECIAL_DEFAULT_TARGET);
182 if (!path)
183 return log_oom();
184
185 r = sd_bus_get_property_string(
186 bus,
187 "org.freedesktop.systemd1",
188 path,
189 "org.freedesktop.systemd1.Unit",
190 "Id",
191 &error,
192 &unit_id);
193 if (r < 0)
194 log_warning_errno(r, "default.target doesn't seem to exist, ignoring: %s", bus_error_message(&error, r));
195
196 r = bus_get_uint64_property(bus, path,
197 "org.freedesktop.systemd1.Unit",
198 "ActiveEnterTimestampMonotonic",
199 &activated_time);
200 if (r < 0)
201 log_warning_errno(r, "Could not get time to reach default.target, ignoring: %m");
202
203 text = strdup("Startup finished in ");
204 if (!text)
205 return log_oom();
206
207 if (timestamp_is_set(t->firmware_time) && !strextend(&text, FORMAT_TIMESPAN(t->firmware_time - t->loader_time, USEC_PER_MSEC), " (firmware) + "))
208 return log_oom();
209 if (timestamp_is_set(t->loader_time) && !strextend(&text, FORMAT_TIMESPAN(t->loader_time, USEC_PER_MSEC), " (loader) + "))
210 return log_oom();
211 if (timestamp_is_set(t->kernel_done_time) && !strextend(&text, FORMAT_TIMESPAN(t->kernel_done_time, USEC_PER_MSEC), " (kernel) + "))
212 return log_oom();
213 if (timestamp_is_set(t->initrd_time) && !strextend(&text, FORMAT_TIMESPAN(t->userspace_time - t->initrd_time, USEC_PER_MSEC), " (initrd) + "))
214 return log_oom();
215 if (t->soft_reboots_count > 0 && strextendf(&text, "%s (soft reboot #%" PRIu32 ") + ", FORMAT_TIMESPAN(t->userspace_time, USEC_PER_MSEC), t->soft_reboots_count) < 0)
216 return log_oom();
217
218 if (!strextend(&text, FORMAT_TIMESPAN(t->finish_time - t->userspace_time, USEC_PER_MSEC), " (userspace) "))
219 return log_oom();
220
221 if (timestamp_is_set(t->kernel_done_time))
222 if (!strextend(&text, "= ", FORMAT_TIMESPAN(t->firmware_time + t->finish_time, USEC_PER_MSEC), " "))
223 return log_oom();
224
225 if (unit_id && timestamp_is_set(activated_time)) {
226 usec_t base;
227
228 /* On soft-reboot times are clamped to avoid showing huge graphs */
229 if (t->soft_reboots_count > 0 && timestamp_is_set(t->userspace_time))
230 base = t->userspace_time + t->reverse_offset;
231 else
232 base = timestamp_is_set(t->userspace_time) ? t->userspace_time : t->reverse_offset;
233
234 if (!strextend(&text, "\n", unit_id, " reached after ", FORMAT_TIMESPAN(activated_time - base, USEC_PER_MSEC), " in userspace."))
235 return log_oom();
236
237 } else if (unit_id && activated_time == 0) {
238
239 if (!strextend(&text, "\n", unit_id, " was never reached."))
240 return log_oom();
241
242 } else if (unit_id && activated_time == USEC_INFINITY) {
243
244 if (!strextend(&text, "\nCould not get time to reach ", unit_id, "."))
245 return log_oom();
246
247 } else if (!unit_id) {
248
249 if (!strextend(&text, "\ncould not find default.target."))
250 return log_oom();
251 }
252
253 *ret = TAKE_PTR(text);
254 return 0;
255 }
256
257 void unit_times_clear(UnitTimes *t) {
258 if (!t)
259 return;
260
261 FOREACH_ELEMENT(d, t->deps)
262 *d = strv_free(*d);
263
264 t->name = mfree(t->name);
265 }
266
267 UnitTimes* unit_times_free_array(UnitTimes *t) {
268 if (!t)
269 return NULL;
270
271 for (UnitTimes *p = t; p->has_data; p++)
272 unit_times_clear(p);
273
274 return mfree(t);
275 }
276
277 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(UnitTimes*, unit_times_clear, NULL);
278
279 int acquire_time_data(sd_bus *bus, bool require_finished, UnitTimes **out) {
280 static const struct bus_properties_map property_map[] = {
281 { "InactiveExitTimestampMonotonic", "t", NULL, offsetof(UnitTimes, activating) },
282 { "ActiveEnterTimestampMonotonic", "t", NULL, offsetof(UnitTimes, activated) },
283 { "ActiveExitTimestampMonotonic", "t", NULL, offsetof(UnitTimes, deactivating) },
284 { "InactiveEnterTimestampMonotonic", "t", NULL, offsetof(UnitTimes, deactivated) },
285 { "After", "as", NULL, offsetof(UnitTimes, deps[UNIT_AFTER]) },
286 { "Before", "as", NULL, offsetof(UnitTimes, deps[UNIT_BEFORE]) },
287 { "Requires", "as", NULL, offsetof(UnitTimes, deps[UNIT_REQUIRES]) },
288 { "Requisite", "as", NULL, offsetof(UnitTimes, deps[UNIT_REQUISITE]) },
289 { "Wants", "as", NULL, offsetof(UnitTimes, deps[UNIT_WANTS]) },
290 { "Conflicts", "as", NULL, offsetof(UnitTimes, deps[UNIT_CONFLICTS]) },
291 { "Upholds", "as", NULL, offsetof(UnitTimes, deps[UNIT_UPHOLDS]) },
292 {},
293 };
294 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
295 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
296 _cleanup_(unit_times_free_arrayp) UnitTimes *unit_times = NULL;
297 BootTimes *boot_times;
298 size_t c = 0;
299 UnitInfo u;
300 int r;
301
302 assert(out);
303
304 r = acquire_boot_times(bus, require_finished, &boot_times);
305 if (r < 0)
306 return r;
307
308 r = bus_call_method(bus, bus_systemd_mgr, "ListUnits", &error, &reply, NULL);
309 if (r < 0)
310 return log_error_errno(r, "Failed to list units: %s", bus_error_message(&error, r));
311
312 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
313 if (r < 0)
314 return bus_log_parse_error(r);
315
316 while ((r = bus_parse_unit_info(reply, &u)) > 0) {
317 _cleanup_(unit_times_clearp) UnitTimes *t = NULL;
318
319 if (!GREEDY_REALLOC0(unit_times, c + 2))
320 return log_oom();
321
322 /* t initially has pointers zeroed by the allocation, and unit_times_clearp will have zeroed
323 * them if the entry is being reused. */
324 t = &unit_times[c];
325
326 assert_cc(sizeof(usec_t) == sizeof(uint64_t));
327
328 r = bus_map_all_properties(
329 bus,
330 "org.freedesktop.systemd1",
331 u.unit_path,
332 property_map,
333 BUS_MAP_STRDUP,
334 &error,
335 NULL,
336 t);
337 if (r < 0)
338 return log_error_errno(r, "Failed to get timestamp properties of unit %s: %s",
339 u.id, bus_error_message(&error, r));
340
341 /* Activated in the previous soft-reboot iteration? Ignore it, we want new activations */
342 if ((t->activated > 0 && t->activated < boot_times->shutdown_start_time) ||
343 (t->activating > 0 && t->activating < boot_times->shutdown_start_time))
344 continue;
345
346 subtract_timestamp(&t->activating, boot_times->reverse_offset);
347 subtract_timestamp(&t->activated, boot_times->reverse_offset);
348
349 /* If the last deactivation was in the previous soft-reboot, ignore it */
350 if (boot_times->soft_reboots_count > 0) {
351 if (t->deactivating < boot_times->reverse_offset)
352 t->deactivating = 0;
353 else
354 subtract_timestamp(&t->deactivating, boot_times->reverse_offset);
355 if (t->deactivated < boot_times->reverse_offset)
356 t->deactivated = 0;
357 else
358 subtract_timestamp(&t->deactivated, boot_times->reverse_offset);
359 } else {
360 subtract_timestamp(&t->deactivating, boot_times->reverse_offset);
361 subtract_timestamp(&t->deactivated, boot_times->reverse_offset);
362 }
363
364 if (t->activated >= t->activating)
365 t->time = t->activated - t->activating;
366 else if (t->deactivated >= t->activating)
367 t->time = t->deactivated - t->activating;
368 else
369 t->time = 0;
370
371 if (t->activating == 0)
372 continue;
373
374 t->name = strdup(u.id);
375 if (!t->name)
376 return log_oom();
377
378 t->has_data = true;
379 /* Prevent destructor from running on t reference. */
380 TAKE_PTR(t);
381 c++;
382 }
383 if (r < 0)
384 return bus_log_parse_error(r);
385
386 *out = TAKE_PTR(unit_times);
387 return c;
388 }