Debian Codegraph @

src/analyze/analyze-plot.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-plot.h"
8 #include "analyze-time-data.h"
9 #include "bus-error.h"
10 #include "bus-map-properties.h"
11 #include "bus-util.h"
12 #include "format-table.h"
13 #include "os-util.h"
14 #include "runtime-scope.h"
15 #include "sort-util.h"
16 #include "string-util.h"
17 #include "strv.h"
18 #include "time-util.h"
19 #include "unit-def.h"
20 #include "version.h"
21
22 #define SCALE_X (0.1 * arg_svg_timescale / 1000.0) /* pixels per us */
23 #define SCALE_Y (20.0)
24
25 #define svg(...) printf(__VA_ARGS__)
26
27 #define svg_bar(class, x1, x2, y) \
28 svg(" <rect class=\"%s\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n", \
29 (class), \
30 SCALE_X * (x1), SCALE_Y * (y), \
31 SCALE_X * ((x2) - (x1)), SCALE_Y - 1.0)
32
33 #define svg_text(b, x, y, format, ...) \
34 do { \
35 svg(" <text class=\"%s\" x=\"%.03f\" y=\"%.03f\">", (b) ? "left" : "right", SCALE_X * (x) + (b ? 5.0 : -5.0), SCALE_Y * (y) + 14.0); \
36 svg(format, ## __VA_ARGS__); \
37 svg("</text>\n"); \
38 } while (false)
39
40 #define svg_timestamp(b, t, y) \
41 svg_text(b, t, y, "%u.%03us", (unsigned)((t) / USEC_PER_SEC), (unsigned)(((t) % USEC_PER_SEC) / USEC_PER_MSEC))
42
43 typedef struct HostInfo {
44 char *hostname;
45 char *kernel_name;
46 char *kernel_release;
47 char *kernel_version;
48 char *os_pretty_name;
49 char *virtualization;
50 char *architecture;
51 } HostInfo;
52
53 static HostInfo *free_host_info(HostInfo *hi) {
54 if (!hi)
55 return NULL;
56
57 free(hi->hostname);
58 free(hi->kernel_name);
59 free(hi->kernel_release);
60 free(hi->kernel_version);
61 free(hi->os_pretty_name);
62 free(hi->virtualization);
63 free(hi->architecture);
64 return mfree(hi);
65 }
66
67 DEFINE_TRIVIAL_CLEANUP_FUNC(HostInfo *, free_host_info);
68
69 static int acquire_host_info(sd_bus *bus, HostInfo **hi) {
70 static const struct bus_properties_map hostname_map[] = {
71 { "Hostname", "s", NULL, offsetof(HostInfo, hostname) },
72 { "KernelName", "s", NULL, offsetof(HostInfo, kernel_name) },
73 { "KernelRelease", "s", NULL, offsetof(HostInfo, kernel_release) },
74 { "KernelVersion", "s", NULL, offsetof(HostInfo, kernel_version) },
75 { "OperatingSystemPrettyName", "s", NULL, offsetof(HostInfo, os_pretty_name) },
76 {}
77 };
78
79 static const struct bus_properties_map manager_map[] = {
80 { "Virtualization", "s", NULL, offsetof(HostInfo, virtualization) },
81 { "Architecture", "s", NULL, offsetof(HostInfo, architecture) },
82 {}
83 };
84
85 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
86 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *system_bus = NULL;
87 _cleanup_(free_host_infop) HostInfo *host = NULL;
88 int r;
89
90 assert(hi);
91
92 host = new0(HostInfo, 1);
93 if (!host)
94 return log_oom();
95
96 if (arg_runtime_scope != RUNTIME_SCOPE_SYSTEM) {
97 r = bus_connect_transport(arg_transport, arg_host, RUNTIME_SCOPE_SYSTEM, &system_bus);
98 if (r < 0) {
99 log_debug_errno(r, "Failed to connect to system bus, ignoring: %m");
100 goto manager;
101 }
102 }
103
104 r = bus_map_all_properties(
105 system_bus ?: bus,
106 "org.freedesktop.hostname1",
107 "/org/freedesktop/hostname1",
108 hostname_map,
109 BUS_MAP_STRDUP,
110 &error,
111 NULL,
112 host);
113 if (r < 0) {
114 log_debug_errno(r, "Failed to get host information from systemd-hostnamed, ignoring: %s",
115 bus_error_message(&error, r));
116 sd_bus_error_free(&error);
117 }
118
119 manager:
120 r = bus_map_all_properties(
121 bus,
122 "org.freedesktop.systemd1",
123 "/org/freedesktop/systemd1",
124 manager_map,
125 BUS_MAP_STRDUP,
126 &error,
127 NULL,
128 host);
129 if (r < 0)
130 return log_error_errno(r, "Failed to get host information from systemd: %s",
131 bus_error_message(&error, r));
132
133 *hi = TAKE_PTR(host);
134 return 0;
135 }
136
137 static int compare_unit_start(const UnitTimes *a, const UnitTimes *b) {
138 return CMP(a->activating, b->activating);
139 }
140
141 static void svg_graph_box(double height, double begin, double end) {
142 /* outside box, fill */
143 svg("<rect class=\"box\" x=\"0\" y=\"0\" width=\"%.03f\" height=\"%.03f\" />\n",
144 SCALE_X * (end - begin),
145 SCALE_Y * height);
146
147 for (long long i = ((long long) (begin / 100000)) * 100000; i <= end; i += 100000) {
148 /* lines for each second */
149 if (i % 5000000 == 0)
150 svg(" <line class=\"sec5\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n"
151 " <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
152 SCALE_X * i,
153 SCALE_X * i,
154 SCALE_Y * height,
155 SCALE_X * i,
156 -5.0,
157 0.000001 * i);
158 else if (i % 1000000 == 0)
159 svg(" <line class=\"sec1\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n"
160 " <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
161 SCALE_X * i,
162 SCALE_X * i,
163 SCALE_Y * height,
164 SCALE_X * i,
165 -5.0,
166 0.000001 * i);
167 else
168 svg(" <line class=\"sec01\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n",
169 SCALE_X * i,
170 SCALE_X * i,
171 SCALE_Y * height);
172 }
173 }
174
175 static void plot_tooltip(const UnitTimes *ut) {
176 assert(ut);
177 assert(ut->name);
178
179 svg("%s:\n", ut->name);
180 svg("Activating: %"PRI_USEC".%.3"PRI_USEC"\n", ut->activating / USEC_PER_SEC, ut->activating % USEC_PER_SEC);
181 svg("Activated: %"PRI_USEC".%.3"PRI_USEC"\n", ut->activated / USEC_PER_SEC, ut->activated % USEC_PER_SEC);
182
183 UnitDependency i;
184 FOREACH_ARGUMENT(i, UNIT_AFTER, UNIT_BEFORE, UNIT_REQUIRES, UNIT_REQUISITE, UNIT_WANTS, UNIT_CONFLICTS, UNIT_UPHOLDS)
185 if (!strv_isempty(ut->deps[i])) {
186 svg("\n%s:\n", unit_dependency_to_string(i));
187 STRV_FOREACH(s, ut->deps[i])
188 svg(" %s\n", *s);
189 }
190 }
191
192 static int plot_unit_times(UnitTimes *u, double width, int y) {
193 bool b;
194
195 if (!u->name)
196 return 0;
197
198 svg("<g>\n");
199 svg("<title>");
200 plot_tooltip(u);
201 svg("</title>\n");
202 svg_bar("activating", u->activating, u->activated, y);
203 svg_bar("active", u->activated, u->deactivating, y);
204 svg_bar("deactivating", u->deactivating, u->deactivated, y);
205
206 /* place the text on the left if we have passed the half of the svg width */
207 b = u->activating * SCALE_X < width / 2;
208 if (u->time)
209 svg_text(b, u->activating, y, "%s (%s)",
210 u->name, FORMAT_TIMESPAN(u->time, USEC_PER_MSEC));
211 else
212 svg_text(b, u->activating, y, "%s", u->name);
213 svg("</g>\n");
214
215 return 1;
216 }
217
218 static void limit_times_to_boot(const BootTimes *boot, UnitTimes *u) {
219 if (u->deactivated > u->activating && u->deactivated <= boot->finish_time && u->activated == 0
220 && u->deactivating == 0)
221 u->activated = u->deactivating = u->deactivated;
222 if (u->activated < u->activating || u->activated > boot->finish_time)
223 u->activated = boot->finish_time;
224 if (u->deactivating < u->activated || u->deactivating > boot->finish_time)
225 u->deactivating = boot->finish_time;
226 if (u->deactivated < u->deactivating || u->deactivated > boot->finish_time)
227 u->deactivated = boot->finish_time;
228 }
229
230 static int produce_plot_as_svg(
231 UnitTimes *times,
232 const HostInfo *host,
233 const BootTimes *boot,
234 const char *pretty_times) {
235 int m = 1, y = 0;
236 UnitTimes *u;
237 double width;
238
239 width = SCALE_X * (boot->firmware_time + boot->finish_time);
240 if (width < 800.0)
241 width = 800.0;
242
243 if (boot->firmware_time > boot->loader_time)
244 m++;
245 if (timestamp_is_set(boot->loader_time)) {
246 m++;
247 if (width < 1000.0)
248 width = 1000.0;
249 }
250 if (timestamp_is_set(boot->initrd_time))
251 m++;
252 if (timestamp_is_set(boot->kernel_done_time))
253 m++;
254
255 for (u = times; u->has_data; u++) {
256 double text_start, text_width;
257
258 if (u->activating > boot->finish_time) {
259 unit_times_clear(u);
260 continue;
261 }
262
263 /* If the text cannot fit on the left side then
264 * increase the svg width so it fits on the right.
265 * TODO: calculate the text width more accurately */
266 text_width = 8.0 * strlen(u->name);
267 text_start = (boot->firmware_time + u->activating) * SCALE_X;
268 if (text_width > text_start && text_width + text_start > width)
269 width = text_width + text_start;
270
271 limit_times_to_boot(boot, u);
272
273 m++;
274 }
275
276 svg("<?xml version=\"1.0\" standalone=\"no\"?>\n"
277 "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
278 "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
279
280 svg("<svg width=\"%.0fpx\" height=\"%.0fpx\" version=\"1.1\" "
281 "xmlns=\"http://www.w3.org/2000/svg\">\n\n",
282 80.0 + width, 150.0 + (m * SCALE_Y) +
283 5 * SCALE_Y /* legend */);
284
285 /* write some basic info as a comment, including some help */
286 svg("<!-- This file is a systemd-analyze SVG file. It is best rendered in a -->\n"
287 "<!-- browser such as Chrome, Chromium or Firefox. Other applications -->\n"
288 "<!-- that render these files properly but much slower are ImageMagick, -->\n"
289 "<!-- gimp, inkscape, etc. To display the files on your system, just -->\n"
290 "<!-- point your browser to this file. -->\n\n"
291 "<!-- This plot was generated by systemd-analyze version %-16.16s -->\n\n", GIT_VERSION);
292
293 /* style sheet */
294 svg("<defs>\n <style type=\"text/css\">\n <![CDATA[\n"
295 " rect { stroke-width: 1; stroke-opacity: 0; }\n"
296 " rect.background { fill: rgb(255,255,255); }\n"
297 " rect.activating { fill: rgb(255,0,0); fill-opacity: 0.7; }\n"
298 " rect.active { fill: rgb(200,150,150); fill-opacity: 0.7; }\n"
299 " rect.deactivating { fill: rgb(150,100,100); fill-opacity: 0.7; }\n"
300 " rect.kernel { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
301 " rect.initrd { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
302 " rect.firmware { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
303 " rect.loader { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
304 " rect.userspace { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
305 " rect.security { fill: rgb(144,238,144); fill-opacity: 0.7; }\n"
306 " rect.generators { fill: rgb(102,204,255); fill-opacity: 0.7; }\n"
307 " rect.unitsload { fill: rgb( 82,184,255); fill-opacity: 0.7; }\n"
308 " rect.box { fill: rgb(240,240,240); stroke: rgb(192,192,192); }\n"
309 " line { stroke: rgb(64,64,64); stroke-width: 1; }\n"
310 "// line.sec1 { }\n"
311 " line.sec5 { stroke-width: 2; }\n"
312 " line.sec01 { stroke: rgb(224,224,224); stroke-width: 1; }\n"
313 " text { font-family: Verdana, Helvetica; font-size: 14px; }\n"
314 " text.left { font-family: Verdana, Helvetica; font-size: 14px; text-anchor: start; }\n"
315 " text.right { font-family: Verdana, Helvetica; font-size: 14px; text-anchor: end; }\n"
316 " text.sec { font-size: 10px; }\n"
317 " ]]>\n </style>\n</defs>\n\n");
318
319 svg("<rect class=\"background\" width=\"100%%\" height=\"100%%\" />\n");
320 svg("<text x=\"20\" y=\"50\">%s</text>", pretty_times);
321 if (host)
322 svg("<text x=\"20\" y=\"30\">%s %s (%s %s %s) %s %s</text>",
323 os_release_pretty_name(host->os_pretty_name, NULL),
324 strempty(host->hostname),
325 strempty(host->kernel_name),
326 strempty(host->kernel_release),
327 strempty(host->kernel_version),
328 strempty(host->architecture),
329 strempty(host->virtualization));
330
331 svg("<g transform=\"translate(%.3f,100)\">\n", 20.0 + (SCALE_X * boot->firmware_time));
332 if (boot->soft_reboots_count > 0)
333 svg_graph_box(m, 0, boot->finish_time);
334 else
335 svg_graph_box(m, -(double) boot->firmware_time, boot->finish_time);
336
337 if (timestamp_is_set(boot->firmware_time)) {
338 svg_bar("firmware", -(double) boot->firmware_time, -(double) boot->loader_time, y);
339 svg_text(true, -(double) boot->firmware_time, y, "firmware");
340 y++;
341 }
342 if (timestamp_is_set(boot->loader_time)) {
343 svg_bar("loader", -(double) boot->loader_time, 0, y);
344 svg_text(true, -(double) boot->loader_time, y, "loader");
345 y++;
346 }
347 if (timestamp_is_set(boot->kernel_done_time)) {
348 svg_bar("kernel", 0, boot->kernel_done_time, y);
349 svg_text(true, 0, y, "kernel");
350 y++;
351 }
352 if (timestamp_is_set(boot->initrd_time)) {
353 svg_bar("initrd", boot->initrd_time, boot->userspace_time, y);
354 if (boot->initrd_security_start_time < boot->initrd_security_finish_time)
355 svg_bar("security", boot->initrd_security_start_time, boot->initrd_security_finish_time, y);
356 if (boot->initrd_generators_start_time < boot->initrd_generators_finish_time)
357 svg_bar("generators", boot->initrd_generators_start_time, boot->initrd_generators_finish_time, y);
358 if (boot->initrd_unitsload_start_time < boot->initrd_unitsload_finish_time)
359 svg_bar("unitsload", boot->initrd_unitsload_start_time, boot->initrd_unitsload_finish_time, y);
360 svg_text(true, boot->initrd_time, y, "initrd");
361 y++;
362 }
363 if (boot->soft_reboots_count > 0) {
364 svg_bar("soft-reboot", 0, boot->userspace_time, y);
365 svg_text(true, 0, y, "soft-reboot");
366 y++;
367 }
368
369 for (u = times; u->has_data; u++) {
370 if (u->activating >= boot->userspace_time)
371 break;
372
373 y += plot_unit_times(u, width, y);
374 }
375
376 svg_bar("active", boot->userspace_time, boot->finish_time, y);
377 if (timestamp_is_set(boot->security_start_time))
378 svg_bar("security", boot->security_start_time, boot->security_finish_time, y);
379 svg_bar("generators", boot->generators_start_time, boot->generators_finish_time, y);
380 svg_bar("unitsload", boot->unitsload_start_time, boot->unitsload_finish_time, y);
381 svg_text(true, boot->userspace_time, y, "systemd");
382 if (arg_detailed_svg)
383 svg_timestamp(false, boot->userspace_time, y);
384 y++;
385
386 for (; u->has_data; u++)
387 y += plot_unit_times(u, width, y);
388
389 svg("</g>\n");
390
391 /* Legend */
392 svg("<g transform=\"translate(20,100)\">\n");
393 y++;
394 svg_bar("activating", 0, 300000, y);
395 svg_text(true, 400000, y, "Activating");
396 y++;
397 svg_bar("active", 0, 300000, y);
398 svg_text(true, 400000, y, "Active");
399 y++;
400 svg_bar("deactivating", 0, 300000, y);
401 svg_text(true, 400000, y, "Deactivating");
402 y++;
403 if (timestamp_is_set(boot->security_start_time)) {
404 svg_bar("security", 0, 300000, y);
405 svg_text(true, 400000, y, "Setting up security module");
406 y++;
407 }
408 svg_bar("generators", 0, 300000, y);
409 svg_text(true, 400000, y, "Generators");
410 y++;
411 svg_bar("unitsload", 0, 300000, y);
412 svg_text(true, 400000, y, "Loading unit files");
413 y++;
414
415 svg("</g>\n\n");
416
417 svg("</svg>\n");
418
419 return 0;
420 }
421
422 static int show_table(Table *table, const char *word) {
423 int r;
424
425 assert(table);
426 assert(word);
427
428 if (!table_isempty(table)) {
429 table_set_header(table, arg_legend);
430
431 if (sd_json_format_enabled(arg_json_format_flags))
432 r = table_print_json(table, NULL, arg_json_format_flags | SD_JSON_FORMAT_COLOR_AUTO);
433 else
434 r = table_print(table);
435 if (r < 0)
436 return table_log_print_error(r);
437 }
438
439 if (arg_legend) {
440 if (table_isempty(table))
441 printf("No %s.\n", word);
442 else
443 printf("\n%zu %s listed.\n", table_get_rows(table) - 1, word);
444 }
445
446 return 0;
447 }
448
449 static int produce_plot_as_text(UnitTimes *times, const BootTimes *boot) {
450 _cleanup_(table_unrefp) Table *table = NULL;
451 int r;
452
453 table = table_new("name", "activated", "activating", "time", "deactivated", "deactivating");
454 if (!table)
455 return log_oom();
456
457 for (; times->has_data; times++) {
458 limit_times_to_boot(boot, times);
459
460 r = table_add_many(
461 table,
462 TABLE_STRING, times->name,
463 TABLE_TIMESPAN_MSEC, times->activated,
464 TABLE_TIMESPAN_MSEC, times->activating,
465 TABLE_TIMESPAN_MSEC, times->time,
466 TABLE_TIMESPAN_MSEC, times->deactivated,
467 TABLE_TIMESPAN_MSEC, times->deactivating);
468 if (r < 0)
469 return table_log_add_error(r);
470 }
471
472 return show_table(table, "Units");
473 }
474
475 int verb_plot(int argc, char *argv[], uintptr_t _data, void *userdata) {
476 _cleanup_(free_host_infop) HostInfo *host = NULL;
477 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
478 _cleanup_(unit_times_free_arrayp) UnitTimes *times = NULL;
479 _cleanup_free_ char *pretty_times = NULL;
480 bool use_full_bus = arg_runtime_scope == RUNTIME_SCOPE_SYSTEM;
481 BootTimes *boot;
482 int n, r;
483
484 r = acquire_bus(&bus, &use_full_bus);
485 if (r < 0)
486 return bus_log_connect_error(r, arg_transport, arg_runtime_scope);
487
488 n = acquire_boot_times(bus, /* require_finished= */ true, &boot);
489 if (n < 0)
490 return n;
491
492 n = pretty_boot_time(bus, &pretty_times);
493 if (n < 0)
494 return n;
495
496 if (use_full_bus || arg_runtime_scope != RUNTIME_SCOPE_SYSTEM) {
497 n = acquire_host_info(bus, &host);
498 if (n < 0)
499 return n;
500 }
501
502 n = acquire_time_data(bus, /* require_finished= */ true, &times);
503 if (n <= 0)
504 return n;
505
506 typesafe_qsort(times, n, compare_unit_start);
507
508 if (sd_json_format_enabled(arg_json_format_flags) || arg_table)
509 r = produce_plot_as_text(times, boot);
510 else
511 r = produce_plot_as_svg(times, host, boot, pretty_times);
512 if (r < 0)
513 return r;
514
515 return EXIT_SUCCESS;
516 }