]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
analyze: split out "plot" verb
authorLennart Poettering <lennart@poettering.net>
Mon, 21 Feb 2022 12:36:40 +0000 (13:36 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 21 Feb 2022 16:22:23 +0000 (17:22 +0100)
src/analyze/analyze-plot.c [new file with mode: 0644]
src/analyze/analyze-plot.h [new file with mode: 0644]
src/analyze/analyze-time-data.c
src/analyze/analyze-time-data.h
src/analyze/analyze.c
src/analyze/analyze.h
src/analyze/meson.build

diff --git a/src/analyze/analyze-plot.c b/src/analyze/analyze-plot.c
new file mode 100644 (file)
index 0000000..d2acf36
--- /dev/null
@@ -0,0 +1,395 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "analyze.h"
+#include "analyze-plot.h"
+#include "analyze-time-data.h"
+#include "bus-error.h"
+#include "bus-map-properties.h"
+#include "sort-util.h"
+#include "version.h"
+
+#define SCALE_X (0.1 / 1000.0) /* pixels per us */
+#define SCALE_Y (20.0)
+
+#define svg(...) printf(__VA_ARGS__)
+
+#define svg_bar(class, x1, x2, y)                                       \
+        svg("  <rect class=\"%s\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n", \
+            (class),                                                    \
+            SCALE_X * (x1), SCALE_Y * (y),                              \
+            SCALE_X * ((x2) - (x1)), SCALE_Y - 1.0)
+
+#define svg_text(b, x, y, format, ...)                                  \
+        do {                                                            \
+                svg("  <text class=\"%s\" x=\"%.03f\" y=\"%.03f\">", (b) ? "left" : "right", SCALE_X * (x) + (b ? 5.0 : -5.0), SCALE_Y * (y) + 14.0); \
+                svg(format, ## __VA_ARGS__);                            \
+                svg("</text>\n");                                       \
+        } while (false)
+
+
+typedef struct HostInfo {
+        char *hostname;
+        char *kernel_name;
+        char *kernel_release;
+        char *kernel_version;
+        char *os_pretty_name;
+        char *virtualization;
+        char *architecture;
+} HostInfo;
+
+static HostInfo* free_host_info(HostInfo *hi) {
+        if (!hi)
+                return NULL;
+
+        free(hi->hostname);
+        free(hi->kernel_name);
+        free(hi->kernel_release);
+        free(hi->kernel_version);
+        free(hi->os_pretty_name);
+        free(hi->virtualization);
+        free(hi->architecture);
+        return mfree(hi);
+}
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(HostInfo *, free_host_info);
+
+static int acquire_host_info(sd_bus *bus, HostInfo **hi) {
+        static const struct bus_properties_map hostname_map[] = {
+                { "Hostname",                  "s", NULL, offsetof(HostInfo, hostname)       },
+                { "KernelName",                "s", NULL, offsetof(HostInfo, kernel_name)    },
+                { "KernelRelease",             "s", NULL, offsetof(HostInfo, kernel_release) },
+                { "KernelVersion",             "s", NULL, offsetof(HostInfo, kernel_version) },
+                { "OperatingSystemPrettyName", "s", NULL, offsetof(HostInfo, os_pretty_name) },
+                {}
+        };
+
+        static const struct bus_properties_map manager_map[] = {
+                { "Virtualization", "s", NULL, offsetof(HostInfo, virtualization) },
+                { "Architecture",   "s", NULL, offsetof(HostInfo, architecture)   },
+                {}
+        };
+
+        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
+        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *system_bus = NULL;
+        _cleanup_(free_host_infop) HostInfo *host = NULL;
+        int r;
+
+        host = new0(HostInfo, 1);
+        if (!host)
+                return log_oom();
+
+        if (arg_scope != UNIT_FILE_SYSTEM) {
+                r = bus_connect_transport(arg_transport, arg_host, false, &system_bus);
+                if (r < 0) {
+                        log_debug_errno(r, "Failed to connect to system bus, ignoring: %m");
+                        goto manager;
+                }
+        }
+
+        r = bus_map_all_properties(
+                        system_bus ?: bus,
+                        "org.freedesktop.hostname1",
+                        "/org/freedesktop/hostname1",
+                        hostname_map,
+                        BUS_MAP_STRDUP,
+                        &error,
+                        NULL,
+                        host);
+        if (r < 0) {
+                log_debug_errno(r, "Failed to get host information from systemd-hostnamed, ignoring: %s",
+                                bus_error_message(&error, r));
+                sd_bus_error_free(&error);
+        }
+
+manager:
+        r = bus_map_all_properties(
+                        bus,
+                        "org.freedesktop.systemd1",
+                        "/org/freedesktop/systemd1",
+                        manager_map,
+                        BUS_MAP_STRDUP,
+                        &error,
+                        NULL,
+                        host);
+        if (r < 0)
+                return log_error_errno(r, "Failed to get host information from systemd: %s",
+                                       bus_error_message(&error, r));
+
+        *hi = TAKE_PTR(host);
+        return 0;
+}
+
+static int compare_unit_start(const UnitTimes *a, const UnitTimes *b) {
+        return CMP(a->activating, b->activating);
+}
+
+static void svg_graph_box(double height, double begin, double end) {
+        /* outside box, fill */
+        svg("<rect class=\"box\" x=\"0\" y=\"0\" width=\"%.03f\" height=\"%.03f\" />\n",
+            SCALE_X * (end - begin),
+            SCALE_Y * height);
+
+        for (long long i = ((long long) (begin / 100000)) * 100000; i <= end; i += 100000) {
+                /* lines for each second */
+                if (i % 5000000 == 0)
+                        svg("  <line class=\"sec5\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n"
+                            "  <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
+                            SCALE_X * i,
+                            SCALE_X * i,
+                            SCALE_Y * height,
+                            SCALE_X * i,
+                            -5.0,
+                            0.000001 * i);
+                else if (i % 1000000 == 0)
+                        svg("  <line class=\"sec1\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n"
+                            "  <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
+                            SCALE_X * i,
+                            SCALE_X * i,
+                            SCALE_Y * height,
+                            SCALE_X * i,
+                            -5.0,
+                            0.000001 * i);
+                else
+                        svg("  <line class=\"sec01\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n",
+                            SCALE_X * i,
+                            SCALE_X * i,
+                            SCALE_Y * height);
+        }
+}
+
+static int plot_unit_times(UnitTimes *u, double width, int y) {
+        bool b;
+
+        if (!u->name)
+                return 0;
+
+        svg_bar("activating",   u->activating, u->activated, y);
+        svg_bar("active",       u->activated, u->deactivating, y);
+        svg_bar("deactivating", u->deactivating, u->deactivated, y);
+
+        /* place the text on the left if we have passed the half of the svg width */
+        b = u->activating * SCALE_X < width / 2;
+        if (u->time)
+                svg_text(b, u->activating, y, "%s (%s)",
+                         u->name, FORMAT_TIMESPAN(u->time, USEC_PER_MSEC));
+        else
+                svg_text(b, u->activating, y, "%s", u->name);
+
+        return 1;
+}
+
+int analyze_plot(int argc, char *argv[], void *userdata) {
+        _cleanup_(free_host_infop) HostInfo *host = NULL;
+        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
+        _cleanup_(unit_times_free_arrayp) UnitTimes *times = NULL;
+        _cleanup_free_ char *pretty_times = NULL;
+        bool use_full_bus = arg_scope == UNIT_FILE_SYSTEM;
+        BootTimes *boot;
+        UnitTimes *u;
+        int n, m = 1, y = 0, r;
+        double width;
+
+        r = acquire_bus(&bus, &use_full_bus);
+        if (r < 0)
+                return bus_log_connect_error(r, arg_transport);
+
+        n = acquire_boot_times(bus, &boot);
+        if (n < 0)
+                return n;
+
+        n = pretty_boot_time(bus, &pretty_times);
+        if (n < 0)
+                return n;
+
+        if (use_full_bus || arg_scope != UNIT_FILE_SYSTEM) {
+                n = acquire_host_info(bus, &host);
+                if (n < 0)
+                        return n;
+        }
+
+        n = acquire_time_data(bus, &times);
+        if (n <= 0)
+                return n;
+
+        typesafe_qsort(times, n, compare_unit_start);
+
+        width = SCALE_X * (boot->firmware_time + boot->finish_time);
+        if (width < 800.0)
+                width = 800.0;
+
+        if (boot->firmware_time > boot->loader_time)
+                m++;
+        if (boot->loader_time > 0) {
+                m++;
+                if (width < 1000.0)
+                        width = 1000.0;
+        }
+        if (boot->initrd_time > 0)
+                m++;
+        if (boot->kernel_done_time > 0)
+                m++;
+
+        for (u = times; u->has_data; u++) {
+                double text_start, text_width;
+
+                if (u->activating > boot->finish_time) {
+                        u->name = mfree(u->name);
+                        continue;
+                }
+
+                /* If the text cannot fit on the left side then
+                 * increase the svg width so it fits on the right.
+                 * TODO: calculate the text width more accurately */
+                text_width = 8.0 * strlen(u->name);
+                text_start = (boot->firmware_time + u->activating) * SCALE_X;
+                if (text_width > text_start && text_width + text_start > width)
+                        width = text_width + text_start;
+
+                if (u->deactivated > u->activating &&
+                    u->deactivated <= boot->finish_time &&
+                    u->activated == 0 && u->deactivating == 0)
+                        u->activated = u->deactivating = u->deactivated;
+                if (u->activated < u->activating || u->activated > boot->finish_time)
+                        u->activated = boot->finish_time;
+                if (u->deactivating < u->activated || u->deactivating > boot->finish_time)
+                        u->deactivating = boot->finish_time;
+                if (u->deactivated < u->deactivating || u->deactivated > boot->finish_time)
+                        u->deactivated = boot->finish_time;
+                m++;
+        }
+
+        svg("<?xml version=\"1.0\" standalone=\"no\"?>\n"
+            "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
+            "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
+
+        svg("<svg width=\"%.0fpx\" height=\"%.0fpx\" version=\"1.1\" "
+            "xmlns=\"http://www.w3.org/2000/svg\">\n\n",
+                        80.0 + width, 150.0 + (m * SCALE_Y) +
+                        5 * SCALE_Y /* legend */);
+
+        /* write some basic info as a comment, including some help */
+        svg("<!-- This file is a systemd-analyze SVG file. It is best rendered in a   -->\n"
+            "<!-- browser such as Chrome, Chromium or Firefox. Other applications     -->\n"
+            "<!-- that render these files properly but much slower are ImageMagick,   -->\n"
+            "<!-- gimp, inkscape, etc. To display the files on your system, just      -->\n"
+            "<!-- point your browser to this file.                                    -->\n\n"
+            "<!-- This plot was generated by systemd-analyze version %-16.16s -->\n\n", GIT_VERSION);
+
+        /* style sheet */
+        svg("<defs>\n  <style type=\"text/css\">\n    <![CDATA[\n"
+            "      rect       { stroke-width: 1; stroke-opacity: 0; }\n"
+            "      rect.background   { fill: rgb(255,255,255); }\n"
+            "      rect.activating   { fill: rgb(255,0,0); fill-opacity: 0.7; }\n"
+            "      rect.active       { fill: rgb(200,150,150); fill-opacity: 0.7; }\n"
+            "      rect.deactivating { fill: rgb(150,100,100); fill-opacity: 0.7; }\n"
+            "      rect.kernel       { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
+            "      rect.initrd       { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
+            "      rect.firmware     { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
+            "      rect.loader       { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
+            "      rect.userspace    { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
+            "      rect.security     { fill: rgb(144,238,144); fill-opacity: 0.7; }\n"
+            "      rect.generators   { fill: rgb(102,204,255); fill-opacity: 0.7; }\n"
+            "      rect.unitsload    { fill: rgb( 82,184,255); fill-opacity: 0.7; }\n"
+            "      rect.box   { fill: rgb(240,240,240); stroke: rgb(192,192,192); }\n"
+            "      line       { stroke: rgb(64,64,64); stroke-width: 1; }\n"
+            "//    line.sec1  { }\n"
+            "      line.sec5  { stroke-width: 2; }\n"
+            "      line.sec01 { stroke: rgb(224,224,224); stroke-width: 1; }\n"
+            "      text       { font-family: Verdana, Helvetica; font-size: 14px; }\n"
+            "      text.left  { font-family: Verdana, Helvetica; font-size: 14px; text-anchor: start; }\n"
+            "      text.right { font-family: Verdana, Helvetica; font-size: 14px; text-anchor: end; }\n"
+            "      text.sec   { font-size: 10px; }\n"
+            "    ]]>\n   </style>\n</defs>\n\n");
+
+        svg("<rect class=\"background\" width=\"100%%\" height=\"100%%\" />\n");
+        svg("<text x=\"20\" y=\"50\">%s</text>", pretty_times);
+        if (host)
+                svg("<text x=\"20\" y=\"30\">%s %s (%s %s %s) %s %s</text>",
+                    isempty(host->os_pretty_name) ? "Linux" : host->os_pretty_name,
+                    strempty(host->hostname),
+                    strempty(host->kernel_name),
+                    strempty(host->kernel_release),
+                    strempty(host->kernel_version),
+                    strempty(host->architecture),
+                    strempty(host->virtualization));
+
+        svg("<g transform=\"translate(%.3f,100)\">\n", 20.0 + (SCALE_X * boot->firmware_time));
+        svg_graph_box(m, -(double) boot->firmware_time, boot->finish_time);
+
+        if (boot->firmware_time > 0) {
+                svg_bar("firmware", -(double) boot->firmware_time, -(double) boot->loader_time, y);
+                svg_text(true, -(double) boot->firmware_time, y, "firmware");
+                y++;
+        }
+        if (boot->loader_time > 0) {
+                svg_bar("loader", -(double) boot->loader_time, 0, y);
+                svg_text(true, -(double) boot->loader_time, y, "loader");
+                y++;
+        }
+        if (boot->kernel_done_time > 0) {
+                svg_bar("kernel", 0, boot->kernel_done_time, y);
+                svg_text(true, 0, y, "kernel");
+                y++;
+        }
+        if (boot->initrd_time > 0) {
+                svg_bar("initrd", boot->initrd_time, boot->userspace_time, y);
+                if (boot->initrd_security_start_time < boot->initrd_security_finish_time)
+                        svg_bar("security", boot->initrd_security_start_time, boot->initrd_security_finish_time, y);
+                if (boot->initrd_generators_start_time < boot->initrd_generators_finish_time)
+                        svg_bar("generators", boot->initrd_generators_start_time, boot->initrd_generators_finish_time, y);
+                if (boot->initrd_unitsload_start_time < boot->initrd_unitsload_finish_time)
+                        svg_bar("unitsload", boot->initrd_unitsload_start_time, boot->initrd_unitsload_finish_time, y);
+                svg_text(true, boot->initrd_time, y, "initrd");
+                y++;
+        }
+
+        for (u = times; u->has_data; u++) {
+                if (u->activating >= boot->userspace_time)
+                        break;
+
+                y += plot_unit_times(u, width, y);
+        }
+
+        svg_bar("active", boot->userspace_time, boot->finish_time, y);
+        if (boot->security_start_time > 0)
+                svg_bar("security", boot->security_start_time, boot->security_finish_time, y);
+        svg_bar("generators", boot->generators_start_time, boot->generators_finish_time, y);
+        svg_bar("unitsload", boot->unitsload_start_time, boot->unitsload_finish_time, y);
+        svg_text(true, boot->userspace_time, y, "systemd");
+        y++;
+
+        for (; u->has_data; u++)
+                y += plot_unit_times(u, width, y);
+
+        svg("</g>\n");
+
+        /* Legend */
+        svg("<g transform=\"translate(20,100)\">\n");
+        y++;
+        svg_bar("activating", 0, 300000, y);
+        svg_text(true, 400000, y, "Activating");
+        y++;
+        svg_bar("active", 0, 300000, y);
+        svg_text(true, 400000, y, "Active");
+        y++;
+        svg_bar("deactivating", 0, 300000, y);
+        svg_text(true, 400000, y, "Deactivating");
+        y++;
+        if (boot->security_start_time > 0) {
+                svg_bar("security", 0, 300000, y);
+                svg_text(true, 400000, y, "Setting up security module");
+                y++;
+        }
+        svg_bar("generators", 0, 300000, y);
+        svg_text(true, 400000, y, "Generators");
+        y++;
+        svg_bar("unitsload", 0, 300000, y);
+        svg_text(true, 400000, y, "Loading unit files");
+        y++;
+
+        svg("</g>\n\n");
+
+        svg("</svg>\n");
+
+        return 0;
+}
diff --git a/src/analyze/analyze-plot.h b/src/analyze/analyze-plot.h
new file mode 100644 (file)
index 0000000..65414b6
--- /dev/null
@@ -0,0 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+int analyze_plot(int argc, char *argv[], void *userdata);
index 00aac100e8aaa998efd121ae0047104c29c8a47b..8eed01dd87d30a54f45e1c065583fe7ea41bb671 100644 (file)
@@ -6,6 +6,8 @@
 #include "bus-locator.h"
 #include "bus-map-properties.h"
 #include "bus-unit-util.h"
+#include "special.h"
+#include "strxcpyx.h"
 
 static void subtract_timestamp(usec_t *a, usec_t b) {
         assert(a);
@@ -100,6 +102,107 @@ finish:
         return 0;
 }
 
+static int bus_get_uint64_property(sd_bus *bus, const char *path, const char *interface, const char *property, uint64_t *val) {
+        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
+        int r;
+
+        assert(bus);
+        assert(path);
+        assert(interface);
+        assert(property);
+        assert(val);
+
+        r = sd_bus_get_property_trivial(
+                        bus,
+                        "org.freedesktop.systemd1",
+                        path,
+                        interface,
+                        property,
+                        &error,
+                        't', val);
+        if (r < 0)
+                return log_error_errno(r, "Failed to parse reply: %s", bus_error_message(&error, r));
+
+        return 0;
+}
+
+int pretty_boot_time(sd_bus *bus, char **ret) {
+        BootTimes *t;
+        static char buf[4096];
+        size_t size;
+        char *ptr;
+        int r;
+        usec_t activated_time = USEC_INFINITY;
+        _cleanup_free_ char *path = NULL, *unit_id = NULL;
+        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
+
+        r = acquire_boot_times(bus, &t);
+        if (r < 0)
+                return r;
+
+        path = unit_dbus_path_from_name(SPECIAL_DEFAULT_TARGET);
+        if (!path)
+                return log_oom();
+
+        r = sd_bus_get_property_string(
+                        bus,
+                        "org.freedesktop.systemd1",
+                        path,
+                        "org.freedesktop.systemd1.Unit",
+                        "Id",
+                        &error,
+                        &unit_id);
+        if (r < 0) {
+                log_error_errno(r, "default.target doesn't seem to exist: %s", bus_error_message(&error, r));
+                unit_id = NULL;
+        }
+
+        r = bus_get_uint64_property(bus, path,
+                        "org.freedesktop.systemd1.Unit",
+                        "ActiveEnterTimestampMonotonic",
+                        &activated_time);
+        if (r < 0) {
+                log_info_errno(r, "Could not get time to reach default.target, ignoring: %m");
+                activated_time = USEC_INFINITY;
+        }
+
+        ptr = buf;
+        size = sizeof(buf);
+
+        size = strpcpyf(&ptr, size, "Startup finished in ");
+        if (t->firmware_time > 0)
+                size = strpcpyf(&ptr, size, "%s (firmware) + ", FORMAT_TIMESPAN(t->firmware_time - t->loader_time, USEC_PER_MSEC));
+        if (t->loader_time > 0)
+                size = strpcpyf(&ptr, size, "%s (loader) + ", FORMAT_TIMESPAN(t->loader_time, USEC_PER_MSEC));
+        if (t->kernel_done_time > 0)
+                size = strpcpyf(&ptr, size, "%s (kernel) + ", FORMAT_TIMESPAN(t->kernel_done_time, USEC_PER_MSEC));
+        if (t->initrd_time > 0)
+                size = strpcpyf(&ptr, size, "%s (initrd) + ", FORMAT_TIMESPAN(t->userspace_time - t->initrd_time, USEC_PER_MSEC));
+
+        size = strpcpyf(&ptr, size, "%s (userspace) ", FORMAT_TIMESPAN(t->finish_time - t->userspace_time, USEC_PER_MSEC));
+        if (t->kernel_done_time > 0)
+                strpcpyf(&ptr, size, "= %s ", FORMAT_TIMESPAN(t->firmware_time + t->finish_time, USEC_PER_MSEC));
+
+        if (unit_id && timestamp_is_set(activated_time)) {
+                usec_t base = t->userspace_time > 0 ? t->userspace_time : t->reverse_offset;
+
+                size = strpcpyf(&ptr, size, "\n%s reached after %s in userspace", unit_id,
+                                FORMAT_TIMESPAN(activated_time - base, USEC_PER_MSEC));
+        } else if (unit_id && activated_time == 0)
+                size = strpcpyf(&ptr, size, "\n%s was never reached", unit_id);
+        else if (unit_id && activated_time == USEC_INFINITY)
+                size = strpcpyf(&ptr, size, "\nCould not get time to reach %s.", unit_id);
+        else if (!unit_id)
+                size = strpcpyf(&ptr, size, "\ncould not find default.target");
+
+        ptr = strdup(buf);
+        if (!ptr)
+                return log_oom();
+
+        *ret = ptr;
+        return 0;
+}
+
 UnitTimes* unit_times_free_array(UnitTimes *t) {
         if (!t)
                 return NULL;
index 18e828857c6c09b0049d03c2ecdefcca8563b76e..0082de213bbc880a2fa3457dc77221a896d546f8 100644 (file)
@@ -46,6 +46,7 @@ typedef struct UnitTimes {
 } UnitTimes;
 
 int acquire_boot_times(sd_bus *bus, BootTimes **ret);
+int pretty_boot_time(sd_bus *bus, char **ret);
 
 UnitTimes* unit_times_free_array(UnitTimes *t);
 DEFINE_TRIVIAL_CLEANUP_FUNC(UnitTimes*, unit_times_free_array);
index 32276c78fb3007fad24d27b20998c71c2e66dd5a..90d1eb13495b060f1801b41ca988a73f4d0f7031 100644 (file)
@@ -22,6 +22,7 @@
 #include "analyze-elf.h"
 #include "analyze-exit-status.h"
 #include "analyze-filesystems.h"
+#include "analyze-plot.h"
 #include "analyze-security.h"
 #include "analyze-service-watchdogs.h"
 #include "analyze-syscall-filter.h"
 #include "verbs.h"
 #include "version.h"
 
-#define SCALE_X (0.1 / 1000.0) /* pixels per us */
-#define SCALE_Y (20.0)
-
-#define svg(...) printf(__VA_ARGS__)
-
-#define svg_bar(class, x1, x2, y)                                       \
-        svg("  <rect class=\"%s\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n", \
-            (class),                                                    \
-            SCALE_X * (x1), SCALE_Y * (y),                              \
-            SCALE_X * ((x2) - (x1)), SCALE_Y - 1.0)
-
-#define svg_text(b, x, y, format, ...)                                  \
-        do {                                                            \
-                svg("  <text class=\"%s\" x=\"%.03f\" y=\"%.03f\">", (b) ? "left" : "right", SCALE_X * (x) + (b ? 5.0 : -5.0), SCALE_Y * (y) + 14.0); \
-                svg(format, ## __VA_ARGS__);                            \
-                svg("</text>\n");                                       \
-        } while (false)
-
 DotMode arg_dot = DEP_ALL;
 char **arg_dot_from_patterns = NULL, **arg_dot_to_patterns = NULL;
 static usec_t arg_fuzz = 0;
 PagerFlags arg_pager_flags = 0;
 BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
-static const char *arg_host = NULL;
+const char *arg_host = NULL;
 UnitFileScope arg_scope = UNIT_FILE_SYSTEM;
 static RecursiveErrors arg_recursive_errors = RECURSIVE_ERRORS_YES;
 static bool arg_man = true;
@@ -124,16 +107,6 @@ STATIC_DESTRUCTOR_REGISTER(arg_security_policy, freep);
 STATIC_DESTRUCTOR_REGISTER(arg_unit, freep);
 STATIC_DESTRUCTOR_REGISTER(arg_profile, freep);
 
-typedef struct HostInfo {
-        char *hostname;
-        char *kernel_name;
-        char *kernel_release;
-        char *kernel_version;
-        char *os_pretty_name;
-        char *virtualization;
-        char *architecture;
-} HostInfo;
-
 int acquire_bus(sd_bus **bus, bool *use_full_bus) {
         bool user = arg_scope != UNIT_FILE_SYSTEM;
         int r;
@@ -149,30 +122,6 @@ int acquire_bus(sd_bus **bus, bool *use_full_bus) {
         return bus_connect_transport_systemd(arg_transport, arg_host, user, bus);
 }
 
-static int bus_get_uint64_property(sd_bus *bus, const char *path, const char *interface, const char *property, uint64_t *val) {
-        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
-        int r;
-
-        assert(bus);
-        assert(path);
-        assert(interface);
-        assert(property);
-        assert(val);
-
-        r = sd_bus_get_property_trivial(
-                        bus,
-                        "org.freedesktop.systemd1",
-                        path,
-                        interface,
-                        property,
-                        &error,
-                        't', val);
-        if (r < 0)
-                return log_error_errno(r, "Failed to parse reply: %s", bus_error_message(&error, r));
-
-        return 0;
-}
-
 int bus_get_unit_property_strv(sd_bus *bus, const char *path, const char *property, char ***strv) {
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
         int r;
@@ -196,10 +145,6 @@ int bus_get_unit_property_strv(sd_bus *bus, const char *path, const char *proper
         return 0;
 }
 
-static int compare_unit_start(const UnitTimes *a, const UnitTimes *b) {
-        return CMP(a->activating, b->activating);
-}
-
 static int process_aliases(char *argv[], char *tempdir, char ***ret) {
         _cleanup_strv_free_ char **filenames = NULL;
         char **filename;
@@ -247,436 +192,6 @@ static int process_aliases(char *argv[], char *tempdir, char ***ret) {
         return 0;
 }
 
-static HostInfo* free_host_info(HostInfo *hi) {
-        if (!hi)
-                return NULL;
-
-        free(hi->hostname);
-        free(hi->kernel_name);
-        free(hi->kernel_release);
-        free(hi->kernel_version);
-        free(hi->os_pretty_name);
-        free(hi->virtualization);
-        free(hi->architecture);
-        return mfree(hi);
-}
-
-DEFINE_TRIVIAL_CLEANUP_FUNC(HostInfo *, free_host_info);
-
-static int acquire_host_info(sd_bus *bus, HostInfo **hi) {
-        static const struct bus_properties_map hostname_map[] = {
-                { "Hostname",                  "s", NULL, offsetof(HostInfo, hostname)       },
-                { "KernelName",                "s", NULL, offsetof(HostInfo, kernel_name)    },
-                { "KernelRelease",             "s", NULL, offsetof(HostInfo, kernel_release) },
-                { "KernelVersion",             "s", NULL, offsetof(HostInfo, kernel_version) },
-                { "OperatingSystemPrettyName", "s", NULL, offsetof(HostInfo, os_pretty_name) },
-                {}
-        };
-
-        static const struct bus_properties_map manager_map[] = {
-                { "Virtualization", "s", NULL, offsetof(HostInfo, virtualization) },
-                { "Architecture",   "s", NULL, offsetof(HostInfo, architecture)   },
-                {}
-        };
-
-        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
-        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *system_bus = NULL;
-        _cleanup_(free_host_infop) HostInfo *host = NULL;
-        int r;
-
-        host = new0(HostInfo, 1);
-        if (!host)
-                return log_oom();
-
-        if (arg_scope != UNIT_FILE_SYSTEM) {
-                r = bus_connect_transport(arg_transport, arg_host, false, &system_bus);
-                if (r < 0) {
-                        log_debug_errno(r, "Failed to connect to system bus, ignoring: %m");
-                        goto manager;
-                }
-        }
-
-        r = bus_map_all_properties(
-                        system_bus ?: bus,
-                        "org.freedesktop.hostname1",
-                        "/org/freedesktop/hostname1",
-                        hostname_map,
-                        BUS_MAP_STRDUP,
-                        &error,
-                        NULL,
-                        host);
-        if (r < 0) {
-                log_debug_errno(r, "Failed to get host information from systemd-hostnamed, ignoring: %s",
-                                bus_error_message(&error, r));
-                sd_bus_error_free(&error);
-        }
-
-manager:
-        r = bus_map_all_properties(
-                        bus,
-                        "org.freedesktop.systemd1",
-                        "/org/freedesktop/systemd1",
-                        manager_map,
-                        BUS_MAP_STRDUP,
-                        &error,
-                        NULL,
-                        host);
-        if (r < 0)
-                return log_error_errno(r, "Failed to get host information from systemd: %s",
-                                       bus_error_message(&error, r));
-
-        *hi = TAKE_PTR(host);
-        return 0;
-}
-
-static int pretty_boot_time(sd_bus *bus, char **_buf) {
-        BootTimes *t;
-        static char buf[4096];
-        size_t size;
-        char *ptr;
-        int r;
-        usec_t activated_time = USEC_INFINITY;
-        _cleanup_free_ char *path = NULL, *unit_id = NULL;
-        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
-
-        r = acquire_boot_times(bus, &t);
-        if (r < 0)
-                return r;
-
-        path = unit_dbus_path_from_name(SPECIAL_DEFAULT_TARGET);
-        if (!path)
-                return log_oom();
-
-        r = sd_bus_get_property_string(
-                        bus,
-                        "org.freedesktop.systemd1",
-                        path,
-                        "org.freedesktop.systemd1.Unit",
-                        "Id",
-                        &error,
-                        &unit_id);
-        if (r < 0) {
-                log_error_errno(r, "default.target doesn't seem to exist: %s", bus_error_message(&error, r));
-                unit_id = NULL;
-        }
-
-        r = bus_get_uint64_property(bus, path,
-                        "org.freedesktop.systemd1.Unit",
-                        "ActiveEnterTimestampMonotonic",
-                        &activated_time);
-        if (r < 0) {
-                log_info_errno(r, "Could not get time to reach default.target, ignoring: %m");
-                activated_time = USEC_INFINITY;
-        }
-
-        ptr = buf;
-        size = sizeof(buf);
-
-        size = strpcpyf(&ptr, size, "Startup finished in ");
-        if (t->firmware_time > 0)
-                size = strpcpyf(&ptr, size, "%s (firmware) + ", FORMAT_TIMESPAN(t->firmware_time - t->loader_time, USEC_PER_MSEC));
-        if (t->loader_time > 0)
-                size = strpcpyf(&ptr, size, "%s (loader) + ", FORMAT_TIMESPAN(t->loader_time, USEC_PER_MSEC));
-        if (t->kernel_done_time > 0)
-                size = strpcpyf(&ptr, size, "%s (kernel) + ", FORMAT_TIMESPAN(t->kernel_done_time, USEC_PER_MSEC));
-        if (t->initrd_time > 0)
-                size = strpcpyf(&ptr, size, "%s (initrd) + ", FORMAT_TIMESPAN(t->userspace_time - t->initrd_time, USEC_PER_MSEC));
-
-        size = strpcpyf(&ptr, size, "%s (userspace) ", FORMAT_TIMESPAN(t->finish_time - t->userspace_time, USEC_PER_MSEC));
-        if (t->kernel_done_time > 0)
-                strpcpyf(&ptr, size, "= %s ", FORMAT_TIMESPAN(t->firmware_time + t->finish_time, USEC_PER_MSEC));
-
-        if (unit_id && timestamp_is_set(activated_time)) {
-                usec_t base = t->userspace_time > 0 ? t->userspace_time : t->reverse_offset;
-
-                size = strpcpyf(&ptr, size, "\n%s reached after %s in userspace", unit_id,
-                                FORMAT_TIMESPAN(activated_time - base, USEC_PER_MSEC));
-        } else if (unit_id && activated_time == 0)
-                size = strpcpyf(&ptr, size, "\n%s was never reached", unit_id);
-        else if (unit_id && activated_time == USEC_INFINITY)
-                size = strpcpyf(&ptr, size, "\nCould not get time to reach %s.", unit_id);
-        else if (!unit_id)
-                size = strpcpyf(&ptr, size, "\ncould not find default.target");
-
-        ptr = strdup(buf);
-        if (!ptr)
-                return log_oom();
-
-        *_buf = ptr;
-        return 0;
-}
-
-static void svg_graph_box(double height, double begin, double end) {
-        /* outside box, fill */
-        svg("<rect class=\"box\" x=\"0\" y=\"0\" width=\"%.03f\" height=\"%.03f\" />\n",
-            SCALE_X * (end - begin),
-            SCALE_Y * height);
-
-        for (long long i = ((long long) (begin / 100000)) * 100000; i <= end; i += 100000) {
-                /* lines for each second */
-                if (i % 5000000 == 0)
-                        svg("  <line class=\"sec5\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n"
-                            "  <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
-                            SCALE_X * i,
-                            SCALE_X * i,
-                            SCALE_Y * height,
-                            SCALE_X * i,
-                            -5.0,
-                            0.000001 * i);
-                else if (i % 1000000 == 0)
-                        svg("  <line class=\"sec1\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n"
-                            "  <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
-                            SCALE_X * i,
-                            SCALE_X * i,
-                            SCALE_Y * height,
-                            SCALE_X * i,
-                            -5.0,
-                            0.000001 * i);
-                else
-                        svg("  <line class=\"sec01\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n",
-                            SCALE_X * i,
-                            SCALE_X * i,
-                            SCALE_Y * height);
-        }
-}
-
-static int plot_unit_times(UnitTimes *u, double width, int y) {
-        bool b;
-
-        if (!u->name)
-                return 0;
-
-        svg_bar("activating",   u->activating, u->activated, y);
-        svg_bar("active",       u->activated, u->deactivating, y);
-        svg_bar("deactivating", u->deactivating, u->deactivated, y);
-
-        /* place the text on the left if we have passed the half of the svg width */
-        b = u->activating * SCALE_X < width / 2;
-        if (u->time)
-                svg_text(b, u->activating, y, "%s (%s)",
-                         u->name, FORMAT_TIMESPAN(u->time, USEC_PER_MSEC));
-        else
-                svg_text(b, u->activating, y, "%s", u->name);
-
-        return 1;
-}
-
-static int analyze_plot(int argc, char *argv[], void *userdata) {
-        _cleanup_(free_host_infop) HostInfo *host = NULL;
-        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
-        _cleanup_(unit_times_free_arrayp) UnitTimes *times = NULL;
-        _cleanup_free_ char *pretty_times = NULL;
-        bool use_full_bus = arg_scope == UNIT_FILE_SYSTEM;
-        BootTimes *boot;
-        UnitTimes *u;
-        int n, m = 1, y = 0, r;
-        double width;
-
-        r = acquire_bus(&bus, &use_full_bus);
-        if (r < 0)
-                return bus_log_connect_error(r, arg_transport);
-
-        n = acquire_boot_times(bus, &boot);
-        if (n < 0)
-                return n;
-
-        n = pretty_boot_time(bus, &pretty_times);
-        if (n < 0)
-                return n;
-
-        if (use_full_bus || arg_scope != UNIT_FILE_SYSTEM) {
-                n = acquire_host_info(bus, &host);
-                if (n < 0)
-                        return n;
-        }
-
-        n = acquire_time_data(bus, &times);
-        if (n <= 0)
-                return n;
-
-        typesafe_qsort(times, n, compare_unit_start);
-
-        width = SCALE_X * (boot->firmware_time + boot->finish_time);
-        if (width < 800.0)
-                width = 800.0;
-
-        if (boot->firmware_time > boot->loader_time)
-                m++;
-        if (boot->loader_time > 0) {
-                m++;
-                if (width < 1000.0)
-                        width = 1000.0;
-        }
-        if (boot->initrd_time > 0)
-                m++;
-        if (boot->kernel_done_time > 0)
-                m++;
-
-        for (u = times; u->has_data; u++) {
-                double text_start, text_width;
-
-                if (u->activating > boot->finish_time) {
-                        u->name = mfree(u->name);
-                        continue;
-                }
-
-                /* If the text cannot fit on the left side then
-                 * increase the svg width so it fits on the right.
-                 * TODO: calculate the text width more accurately */
-                text_width = 8.0 * strlen(u->name);
-                text_start = (boot->firmware_time + u->activating) * SCALE_X;
-                if (text_width > text_start && text_width + text_start > width)
-                        width = text_width + text_start;
-
-                if (u->deactivated > u->activating &&
-                    u->deactivated <= boot->finish_time &&
-                    u->activated == 0 && u->deactivating == 0)
-                        u->activated = u->deactivating = u->deactivated;
-                if (u->activated < u->activating || u->activated > boot->finish_time)
-                        u->activated = boot->finish_time;
-                if (u->deactivating < u->activated || u->deactivating > boot->finish_time)
-                        u->deactivating = boot->finish_time;
-                if (u->deactivated < u->deactivating || u->deactivated > boot->finish_time)
-                        u->deactivated = boot->finish_time;
-                m++;
-        }
-
-        svg("<?xml version=\"1.0\" standalone=\"no\"?>\n"
-            "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
-            "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
-
-        svg("<svg width=\"%.0fpx\" height=\"%.0fpx\" version=\"1.1\" "
-            "xmlns=\"http://www.w3.org/2000/svg\">\n\n",
-                        80.0 + width, 150.0 + (m * SCALE_Y) +
-                        5 * SCALE_Y /* legend */);
-
-        /* write some basic info as a comment, including some help */
-        svg("<!-- This file is a systemd-analyze SVG file. It is best rendered in a   -->\n"
-            "<!-- browser such as Chrome, Chromium or Firefox. Other applications     -->\n"
-            "<!-- that render these files properly but much slower are ImageMagick,   -->\n"
-            "<!-- gimp, inkscape, etc. To display the files on your system, just      -->\n"
-            "<!-- point your browser to this file.                                    -->\n\n"
-            "<!-- This plot was generated by systemd-analyze version %-16.16s -->\n\n", GIT_VERSION);
-
-        /* style sheet */
-        svg("<defs>\n  <style type=\"text/css\">\n    <![CDATA[\n"
-            "      rect       { stroke-width: 1; stroke-opacity: 0; }\n"
-            "      rect.background   { fill: rgb(255,255,255); }\n"
-            "      rect.activating   { fill: rgb(255,0,0); fill-opacity: 0.7; }\n"
-            "      rect.active       { fill: rgb(200,150,150); fill-opacity: 0.7; }\n"
-            "      rect.deactivating { fill: rgb(150,100,100); fill-opacity: 0.7; }\n"
-            "      rect.kernel       { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
-            "      rect.initrd       { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
-            "      rect.firmware     { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
-            "      rect.loader       { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
-            "      rect.userspace    { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
-            "      rect.security     { fill: rgb(144,238,144); fill-opacity: 0.7; }\n"
-            "      rect.generators   { fill: rgb(102,204,255); fill-opacity: 0.7; }\n"
-            "      rect.unitsload    { fill: rgb( 82,184,255); fill-opacity: 0.7; }\n"
-            "      rect.box   { fill: rgb(240,240,240); stroke: rgb(192,192,192); }\n"
-            "      line       { stroke: rgb(64,64,64); stroke-width: 1; }\n"
-            "//    line.sec1  { }\n"
-            "      line.sec5  { stroke-width: 2; }\n"
-            "      line.sec01 { stroke: rgb(224,224,224); stroke-width: 1; }\n"
-            "      text       { font-family: Verdana, Helvetica; font-size: 14px; }\n"
-            "      text.left  { font-family: Verdana, Helvetica; font-size: 14px; text-anchor: start; }\n"
-            "      text.right { font-family: Verdana, Helvetica; font-size: 14px; text-anchor: end; }\n"
-            "      text.sec   { font-size: 10px; }\n"
-            "    ]]>\n   </style>\n</defs>\n\n");
-
-        svg("<rect class=\"background\" width=\"100%%\" height=\"100%%\" />\n");
-        svg("<text x=\"20\" y=\"50\">%s</text>", pretty_times);
-        if (host)
-                svg("<text x=\"20\" y=\"30\">%s %s (%s %s %s) %s %s</text>",
-                    isempty(host->os_pretty_name) ? "Linux" : host->os_pretty_name,
-                    strempty(host->hostname),
-                    strempty(host->kernel_name),
-                    strempty(host->kernel_release),
-                    strempty(host->kernel_version),
-                    strempty(host->architecture),
-                    strempty(host->virtualization));
-
-        svg("<g transform=\"translate(%.3f,100)\">\n", 20.0 + (SCALE_X * boot->firmware_time));
-        svg_graph_box(m, -(double) boot->firmware_time, boot->finish_time);
-
-        if (boot->firmware_time > 0) {
-                svg_bar("firmware", -(double) boot->firmware_time, -(double) boot->loader_time, y);
-                svg_text(true, -(double) boot->firmware_time, y, "firmware");
-                y++;
-        }
-        if (boot->loader_time > 0) {
-                svg_bar("loader", -(double) boot->loader_time, 0, y);
-                svg_text(true, -(double) boot->loader_time, y, "loader");
-                y++;
-        }
-        if (boot->kernel_done_time > 0) {
-                svg_bar("kernel", 0, boot->kernel_done_time, y);
-                svg_text(true, 0, y, "kernel");
-                y++;
-        }
-        if (boot->initrd_time > 0) {
-                svg_bar("initrd", boot->initrd_time, boot->userspace_time, y);
-                if (boot->initrd_security_start_time < boot->initrd_security_finish_time)
-                        svg_bar("security", boot->initrd_security_start_time, boot->initrd_security_finish_time, y);
-                if (boot->initrd_generators_start_time < boot->initrd_generators_finish_time)
-                        svg_bar("generators", boot->initrd_generators_start_time, boot->initrd_generators_finish_time, y);
-                if (boot->initrd_unitsload_start_time < boot->initrd_unitsload_finish_time)
-                        svg_bar("unitsload", boot->initrd_unitsload_start_time, boot->initrd_unitsload_finish_time, y);
-                svg_text(true, boot->initrd_time, y, "initrd");
-                y++;
-        }
-
-        for (u = times; u->has_data; u++) {
-                if (u->activating >= boot->userspace_time)
-                        break;
-
-                y += plot_unit_times(u, width, y);
-        }
-
-        svg_bar("active", boot->userspace_time, boot->finish_time, y);
-        if (boot->security_start_time > 0)
-                svg_bar("security", boot->security_start_time, boot->security_finish_time, y);
-        svg_bar("generators", boot->generators_start_time, boot->generators_finish_time, y);
-        svg_bar("unitsload", boot->unitsload_start_time, boot->unitsload_finish_time, y);
-        svg_text(true, boot->userspace_time, y, "systemd");
-        y++;
-
-        for (; u->has_data; u++)
-                y += plot_unit_times(u, width, y);
-
-        svg("</g>\n");
-
-        /* Legend */
-        svg("<g transform=\"translate(20,100)\">\n");
-        y++;
-        svg_bar("activating", 0, 300000, y);
-        svg_text(true, 400000, y, "Activating");
-        y++;
-        svg_bar("active", 0, 300000, y);
-        svg_text(true, 400000, y, "Active");
-        y++;
-        svg_bar("deactivating", 0, 300000, y);
-        svg_text(true, 400000, y, "Deactivating");
-        y++;
-        if (boot->security_start_time > 0) {
-                svg_bar("security", 0, 300000, y);
-                svg_text(true, 400000, y, "Setting up security module");
-                y++;
-        }
-        svg_bar("generators", 0, 300000, y);
-        svg_text(true, 400000, y, "Generators");
-        y++;
-        svg_bar("unitsload", 0, 300000, y);
-        svg_text(true, 400000, y, "Loading unit files");
-        y++;
-
-        svg("</g>\n\n");
-
-        svg("</svg>\n");
-
-        return 0;
-}
-
 static int list_dependencies_print(
                 const char *name,
                 unsigned level,
index cd978a532faef9657267831015e540e34028b021..97b43da0caf69dc2e85d2c03596b0c823325b583 100644 (file)
@@ -18,6 +18,7 @@ extern DotMode arg_dot;
 extern char **arg_dot_from_patterns, **arg_dot_to_patterns;
 extern PagerFlags arg_pager_flags;
 extern BusTransport arg_transport;
+extern const char *arg_host;
 extern UnitFileScope arg_scope;
 extern unsigned arg_iterations;
 extern usec_t arg_base_time;
index 30fa0487fb08f33104e000786585eb02e57af1be..7b4bf4eee7bdf6dbfdc609a111f2fd5ca0128352 100644 (file)
@@ -19,6 +19,8 @@ systemd_analyze_sources = files('''
         analyze-exit-status.h
         analyze-filesystems.c
         analyze-filesystems.h
+        analyze-plot.c
+        analyze-plot.h
         analyze-security.c
         analyze-security.h
         analyze-service-watchdogs.c