1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "analyze.h"
4 #include "analyze-plot.h"
5 #include "analyze-time-data.h"
6 #include "bus-error.h"
7 #include "bus-map-properties.h"
8 #include "sort-util.h"
9 #include "version.h"
10
11 #define SCALE_X (0.1 / 1000.0) /* pixels per us */
12 #define SCALE_Y (20.0)
13
14 #define svg(...) printf(__VA_ARGS__)
15
16 #define svg_bar(class, x1, x2, y) \
17 svg(" <rect class=\"%s\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n", \
18 (class), \
19 SCALE_X * (x1), SCALE_Y * (y), \
20 SCALE_X * ((x2) - (x1)), SCALE_Y - 1.0)
21
22 #define svg_text(b, x, y, format, ...) \
23 do { \
24 svg(" <text class=\"%s\" x=\"%.03f\" y=\"%.03f\">", (b) ? "left" : "right", SCALE_X * (x) + (b ? 5.0 : -5.0), SCALE_Y * (y) + 14.0); \
25 svg(format, ## __VA_ARGS__); \
26 svg("</text>\n"); \
27 } while (false)
28
29
30 typedef struct HostInfo {
31 char *hostname;
32 char *kernel_name;
33 char *kernel_release;
34 char *kernel_version;
35 char *os_pretty_name;
36 char *virtualization;
37 char *architecture;
38 } HostInfo;
39
free_host_info(HostInfo * hi)40 static HostInfo* free_host_info(HostInfo *hi) {
41 if (!hi)
42 return NULL;
43
44 free(hi->hostname);
45 free(hi->kernel_name);
46 free(hi->kernel_release);
47 free(hi->kernel_version);
48 free(hi->os_pretty_name);
49 free(hi->virtualization);
50 free(hi->architecture);
51 return mfree(hi);
52 }
53
54 DEFINE_TRIVIAL_CLEANUP_FUNC(HostInfo *, free_host_info);
55
acquire_host_info(sd_bus * bus,HostInfo ** hi)56 static int acquire_host_info(sd_bus *bus, HostInfo **hi) {
57 static const struct bus_properties_map hostname_map[] = {
58 { "Hostname", "s", NULL, offsetof(HostInfo, hostname) },
59 { "KernelName", "s", NULL, offsetof(HostInfo, kernel_name) },
60 { "KernelRelease", "s", NULL, offsetof(HostInfo, kernel_release) },
61 { "KernelVersion", "s", NULL, offsetof(HostInfo, kernel_version) },
62 { "OperatingSystemPrettyName", "s", NULL, offsetof(HostInfo, os_pretty_name) },
63 {}
64 };
65
66 static const struct bus_properties_map manager_map[] = {
67 { "Virtualization", "s", NULL, offsetof(HostInfo, virtualization) },
68 { "Architecture", "s", NULL, offsetof(HostInfo, architecture) },
69 {}
70 };
71
72 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
73 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *system_bus = NULL;
74 _cleanup_(free_host_infop) HostInfo *host = NULL;
75 int r;
76
77 host = new0(HostInfo, 1);
78 if (!host)
79 return log_oom();
80
81 if (arg_scope != LOOKUP_SCOPE_SYSTEM) {
82 r = bus_connect_transport(arg_transport, arg_host, false, &system_bus);
83 if (r < 0) {
84 log_debug_errno(r, "Failed to connect to system bus, ignoring: %m");
85 goto manager;
86 }
87 }
88
89 r = bus_map_all_properties(
90 system_bus ?: bus,
91 "org.freedesktop.hostname1",
92 "/org/freedesktop/hostname1",
93 hostname_map,
94 BUS_MAP_STRDUP,
95 &error,
96 NULL,
97 host);
98 if (r < 0) {
99 log_debug_errno(r, "Failed to get host information from systemd-hostnamed, ignoring: %s",
100 bus_error_message(&error, r));
101 sd_bus_error_free(&error);
102 }
103
104 manager:
105 r = bus_map_all_properties(
106 bus,
107 "org.freedesktop.systemd1",
108 "/org/freedesktop/systemd1",
109 manager_map,
110 BUS_MAP_STRDUP,
111 &error,
112 NULL,
113 host);
114 if (r < 0)
115 return log_error_errno(r, "Failed to get host information from systemd: %s",
116 bus_error_message(&error, r));
117
118 *hi = TAKE_PTR(host);
119 return 0;
120 }
121
compare_unit_start(const UnitTimes * a,const UnitTimes * b)122 static int compare_unit_start(const UnitTimes *a, const UnitTimes *b) {
123 return CMP(a->activating, b->activating);
124 }
125
svg_graph_box(double height,double begin,double end)126 static void svg_graph_box(double height, double begin, double end) {
127 /* outside box, fill */
128 svg("<rect class=\"box\" x=\"0\" y=\"0\" width=\"%.03f\" height=\"%.03f\" />\n",
129 SCALE_X * (end - begin),
130 SCALE_Y * height);
131
132 for (long long i = ((long long) (begin / 100000)) * 100000; i <= end; i += 100000) {
133 /* lines for each second */
134 if (i % 5000000 == 0)
135 svg(" <line class=\"sec5\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n"
136 " <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
137 SCALE_X * i,
138 SCALE_X * i,
139 SCALE_Y * height,
140 SCALE_X * i,
141 -5.0,
142 0.000001 * i);
143 else if (i % 1000000 == 0)
144 svg(" <line class=\"sec1\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n"
145 " <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
146 SCALE_X * i,
147 SCALE_X * i,
148 SCALE_Y * height,
149 SCALE_X * i,
150 -5.0,
151 0.000001 * i);
152 else
153 svg(" <line class=\"sec01\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n",
154 SCALE_X * i,
155 SCALE_X * i,
156 SCALE_Y * height);
157 }
158 }
159
plot_unit_times(UnitTimes * u,double width,int y)160 static int plot_unit_times(UnitTimes *u, double width, int y) {
161 bool b;
162
163 if (!u->name)
164 return 0;
165
166 svg_bar("activating", u->activating, u->activated, y);
167 svg_bar("active", u->activated, u->deactivating, y);
168 svg_bar("deactivating", u->deactivating, u->deactivated, y);
169
170 /* place the text on the left if we have passed the half of the svg width */
171 b = u->activating * SCALE_X < width / 2;
172 if (u->time)
173 svg_text(b, u->activating, y, "%s (%s)",
174 u->name, FORMAT_TIMESPAN(u->time, USEC_PER_MSEC));
175 else
176 svg_text(b, u->activating, y, "%s", u->name);
177
178 return 1;
179 }
180
verb_plot(int argc,char * argv[],void * userdata)181 int verb_plot(int argc, char *argv[], void *userdata) {
182 _cleanup_(free_host_infop) HostInfo *host = NULL;
183 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
184 _cleanup_(unit_times_free_arrayp) UnitTimes *times = NULL;
185 _cleanup_free_ char *pretty_times = NULL;
186 bool use_full_bus = arg_scope == LOOKUP_SCOPE_SYSTEM;
187 BootTimes *boot;
188 UnitTimes *u;
189 int n, m = 1, y = 0, r;
190 double width;
191
192 r = acquire_bus(&bus, &use_full_bus);
193 if (r < 0)
194 return bus_log_connect_error(r, arg_transport);
195
196 n = acquire_boot_times(bus, &boot);
197 if (n < 0)
198 return n;
199
200 n = pretty_boot_time(bus, &pretty_times);
201 if (n < 0)
202 return n;
203
204 if (use_full_bus || arg_scope != LOOKUP_SCOPE_SYSTEM) {
205 n = acquire_host_info(bus, &host);
206 if (n < 0)
207 return n;
208 }
209
210 n = acquire_time_data(bus, ×);
211 if (n <= 0)
212 return n;
213
214 typesafe_qsort(times, n, compare_unit_start);
215
216 width = SCALE_X * (boot->firmware_time + boot->finish_time);
217 if (width < 800.0)
218 width = 800.0;
219
220 if (boot->firmware_time > boot->loader_time)
221 m++;
222 if (boot->loader_time > 0) {
223 m++;
224 if (width < 1000.0)
225 width = 1000.0;
226 }
227 if (boot->initrd_time > 0)
228 m++;
229 if (boot->kernel_done_time > 0)
230 m++;
231
232 for (u = times; u->has_data; u++) {
233 double text_start, text_width;
234
235 if (u->activating > boot->finish_time) {
236 u->name = mfree(u->name);
237 continue;
238 }
239
240 /* If the text cannot fit on the left side then
241 * increase the svg width so it fits on the right.
242 * TODO: calculate the text width more accurately */
243 text_width = 8.0 * strlen(u->name);
244 text_start = (boot->firmware_time + u->activating) * SCALE_X;
245 if (text_width > text_start && text_width + text_start > width)
246 width = text_width + text_start;
247
248 if (u->deactivated > u->activating &&
249 u->deactivated <= boot->finish_time &&
250 u->activated == 0 && u->deactivating == 0)
251 u->activated = u->deactivating = u->deactivated;
252 if (u->activated < u->activating || u->activated > boot->finish_time)
253 u->activated = boot->finish_time;
254 if (u->deactivating < u->activated || u->deactivating > boot->finish_time)
255 u->deactivating = boot->finish_time;
256 if (u->deactivated < u->deactivating || u->deactivated > boot->finish_time)
257 u->deactivated = boot->finish_time;
258 m++;
259 }
260
261 svg("<?xml version=\"1.0\" standalone=\"no\"?>\n"
262 "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
263 "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
264
265 svg("<svg width=\"%.0fpx\" height=\"%.0fpx\" version=\"1.1\" "
266 "xmlns=\"http://www.w3.org/2000/svg\">\n\n",
267 80.0 + width, 150.0 + (m * SCALE_Y) +
268 5 * SCALE_Y /* legend */);
269
270 /* write some basic info as a comment, including some help */
271 svg("<!-- This file is a systemd-analyze SVG file. It is best rendered in a -->\n"
272 "<!-- browser such as Chrome, Chromium or Firefox. Other applications -->\n"
273 "<!-- that render these files properly but much slower are ImageMagick, -->\n"
274 "<!-- gimp, inkscape, etc. To display the files on your system, just -->\n"
275 "<!-- point your browser to this file. -->\n\n"
276 "<!-- This plot was generated by systemd-analyze version %-16.16s -->\n\n", GIT_VERSION);
277
278 /* style sheet */
279 svg("<defs>\n <style type=\"text/css\">\n <![CDATA[\n"
280 " rect { stroke-width: 1; stroke-opacity: 0; }\n"
281 " rect.background { fill: rgb(255,255,255); }\n"
282 " rect.activating { fill: rgb(255,0,0); fill-opacity: 0.7; }\n"
283 " rect.active { fill: rgb(200,150,150); fill-opacity: 0.7; }\n"
284 " rect.deactivating { fill: rgb(150,100,100); fill-opacity: 0.7; }\n"
285 " rect.kernel { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
286 " rect.initrd { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
287 " rect.firmware { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
288 " rect.loader { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
289 " rect.userspace { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
290 " rect.security { fill: rgb(144,238,144); fill-opacity: 0.7; }\n"
291 " rect.generators { fill: rgb(102,204,255); fill-opacity: 0.7; }\n"
292 " rect.unitsload { fill: rgb( 82,184,255); fill-opacity: 0.7; }\n"
293 " rect.box { fill: rgb(240,240,240); stroke: rgb(192,192,192); }\n"
294 " line { stroke: rgb(64,64,64); stroke-width: 1; }\n"
295 "// line.sec1 { }\n"
296 " line.sec5 { stroke-width: 2; }\n"
297 " line.sec01 { stroke: rgb(224,224,224); stroke-width: 1; }\n"
298 " text { font-family: Verdana, Helvetica; font-size: 14px; }\n"
299 " text.left { font-family: Verdana, Helvetica; font-size: 14px; text-anchor: start; }\n"
300 " text.right { font-family: Verdana, Helvetica; font-size: 14px; text-anchor: end; }\n"
301 " text.sec { font-size: 10px; }\n"
302 " ]]>\n </style>\n</defs>\n\n");
303
304 svg("<rect class=\"background\" width=\"100%%\" height=\"100%%\" />\n");
305 svg("<text x=\"20\" y=\"50\">%s</text>", pretty_times);
306 if (host)
307 svg("<text x=\"20\" y=\"30\">%s %s (%s %s %s) %s %s</text>",
308 isempty(host->os_pretty_name) ? "Linux" : host->os_pretty_name,
309 strempty(host->hostname),
310 strempty(host->kernel_name),
311 strempty(host->kernel_release),
312 strempty(host->kernel_version),
313 strempty(host->architecture),
314 strempty(host->virtualization));
315
316 svg("<g transform=\"translate(%.3f,100)\">\n", 20.0 + (SCALE_X * boot->firmware_time));
317 svg_graph_box(m, -(double) boot->firmware_time, boot->finish_time);
318
319 if (boot->firmware_time > 0) {
320 svg_bar("firmware", -(double) boot->firmware_time, -(double) boot->loader_time, y);
321 svg_text(true, -(double) boot->firmware_time, y, "firmware");
322 y++;
323 }
324 if (boot->loader_time > 0) {
325 svg_bar("loader", -(double) boot->loader_time, 0, y);
326 svg_text(true, -(double) boot->loader_time, y, "loader");
327 y++;
328 }
329 if (boot->kernel_done_time > 0) {
330 svg_bar("kernel", 0, boot->kernel_done_time, y);
331 svg_text(true, 0, y, "kernel");
332 y++;
333 }
334 if (boot->initrd_time > 0) {
335 svg_bar("initrd", boot->initrd_time, boot->userspace_time, y);
336 if (boot->initrd_security_start_time < boot->initrd_security_finish_time)
337 svg_bar("security", boot->initrd_security_start_time, boot->initrd_security_finish_time, y);
338 if (boot->initrd_generators_start_time < boot->initrd_generators_finish_time)
339 svg_bar("generators", boot->initrd_generators_start_time, boot->initrd_generators_finish_time, y);
340 if (boot->initrd_unitsload_start_time < boot->initrd_unitsload_finish_time)
341 svg_bar("unitsload", boot->initrd_unitsload_start_time, boot->initrd_unitsload_finish_time, y);
342 svg_text(true, boot->initrd_time, y, "initrd");
343 y++;
344 }
345
346 for (u = times; u->has_data; u++) {
347 if (u->activating >= boot->userspace_time)
348 break;
349
350 y += plot_unit_times(u, width, y);
351 }
352
353 svg_bar("active", boot->userspace_time, boot->finish_time, y);
354 if (boot->security_start_time > 0)
355 svg_bar("security", boot->security_start_time, boot->security_finish_time, y);
356 svg_bar("generators", boot->generators_start_time, boot->generators_finish_time, y);
357 svg_bar("unitsload", boot->unitsload_start_time, boot->unitsload_finish_time, y);
358 svg_text(true, boot->userspace_time, y, "systemd");
359 y++;
360
361 for (; u->has_data; u++)
362 y += plot_unit_times(u, width, y);
363
364 svg("</g>\n");
365
366 /* Legend */
367 svg("<g transform=\"translate(20,100)\">\n");
368 y++;
369 svg_bar("activating", 0, 300000, y);
370 svg_text(true, 400000, y, "Activating");
371 y++;
372 svg_bar("active", 0, 300000, y);
373 svg_text(true, 400000, y, "Active");
374 y++;
375 svg_bar("deactivating", 0, 300000, y);
376 svg_text(true, 400000, y, "Deactivating");
377 y++;
378 if (boot->security_start_time > 0) {
379 svg_bar("security", 0, 300000, y);
380 svg_text(true, 400000, y, "Setting up security module");
381 y++;
382 }
383 svg_bar("generators", 0, 300000, y);
384 svg_text(true, 400000, y, "Generators");
385 y++;
386 svg_bar("unitsload", 0, 300000, y);
387 svg_text(true, 400000, y, "Loading unit files");
388 y++;
389
390 svg("</g>\n\n");
391
392 svg("</svg>\n");
393
394 return 0;
395 }
396