analyze.c revision 8901b40502670d1d153f92294ae93245f608adc8
383N/A/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
383N/A
383N/A/***
383N/A This file is part of systemd.
383N/A
383N/A Copyright 2010-2013 Lennart Poettering
383N/A Copyright 2013 Simon Peeters
383N/A
383N/A systemd is free software; you can redistribute it and/or modify it
383N/A under the terms of the GNU Lesser General Public License as published by
383N/A the Free Software Foundation; either version 2.1 of the License, or
383N/A (at your option) any later version.
383N/A
383N/A systemd is distributed in the hope that it will be useful, but
383N/A WITHOUT ANY WARRANTY; without even the implied warranty of
383N/A MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
383N/A Lesser General Public License for more details.
383N/A
383N/A You should have received a copy of the GNU Lesser General Public License
383N/A along with systemd; If not, see <http://www.gnu.org/licenses/>.
383N/A***/
383N/A
383N/A#include <getopt.h>
383N/A#include <locale.h>
383N/A#include <stdio.h>
383N/A#include <stdlib.h>
383N/A
383N/A#include "sd-bus.h"
383N/A
383N/A#include "alloc-util.h"
383N/A#include "analyze-verify.h"
383N/A#include "bus-error.h"
383N/A#include "bus-util.h"
383N/A#include "glob-util.h"
383N/A#include "hashmap.h"
383N/A#include "locale-util.h"
383N/A#include "log.h"
383N/A#include "pager.h"
383N/A#include "parse-util.h"
383N/A#include "special.h"
383N/A#include "strv.h"
383N/A#include "strxcpyx.h"
383N/A#include "terminal-util.h"
383N/A#include "unit-name.h"
383N/A#include "util.h"
383N/A
383N/A#define SCALE_X (0.1 / 1000.0) /* pixels per us */
383N/A#define SCALE_Y (20.0)
383N/A
383N/A#define compare(a, b) (((a) > (b))? 1 : (((b) > (a))? -1 : 0))
383N/A
383N/A#define svg(...) printf(__VA_ARGS__)
383N/A
383N/A#define svg_bar(class, x1, x2, y) \
383N/A svg(" <rect class=\"%s\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n", \
383N/A (class), \
383N/A SCALE_X * (x1), SCALE_Y * (y), \
383N/A SCALE_X * ((x2) - (x1)), SCALE_Y - 1.0)
383N/A
383N/A#define svg_text(b, x, y, format, ...) \
383N/A do { \
383N/A svg(" <text class=\"%s\" x=\"%.03f\" y=\"%.03f\">", (b) ? "left" : "right", SCALE_X * (x) + (b ? 5.0 : -5.0), SCALE_Y * (y) + 14.0); \
383N/A svg(format, ## __VA_ARGS__); \
383N/A svg("</text>\n"); \
383N/A } while(false)
383N/A
383N/Astatic enum dot {
383N/A DEP_ALL,
383N/A DEP_ORDER,
383N/A DEP_REQUIRE
383N/A} arg_dot = DEP_ALL;
383N/Astatic char** arg_dot_from_patterns = NULL;
383N/Astatic char** arg_dot_to_patterns = NULL;
383N/Astatic usec_t arg_fuzz = 0;
383N/Astatic bool arg_no_pager = false;
383N/Astatic BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
383N/Astatic char *arg_host = NULL;
383N/Astatic bool arg_user = false;
383N/Astatic bool arg_man = true;
383N/A
383N/Astruct boot_times {
383N/A usec_t firmware_time;
383N/A usec_t loader_time;
383N/A usec_t kernel_time;
383N/A usec_t kernel_done_time;
383N/A usec_t initrd_time;
383N/A usec_t userspace_time;
383N/A usec_t finish_time;
383N/A usec_t security_start_time;
383N/A usec_t security_finish_time;
383N/A usec_t generators_start_time;
383N/A usec_t generators_finish_time;
383N/A usec_t unitsload_start_time;
383N/A usec_t unitsload_finish_time;
383N/A
383N/A /*
383N/A * If we're analyzing the user instance, all timestamps will be offset
383N/A * by its own start-up timestamp, which may be arbitrarily big.
383N/A * With "plot", this causes arbitrarily wide output SVG files which almost
383N/A * completely consist of empty space. Thus we cancel out this offset.
383N/A *
383N/A * This offset is subtracted from times above by acquire_boot_times(),
383N/A * but it still needs to be subtracted from unit-specific timestamps
383N/A * (so it is stored here for reference).
383N/A */
383N/A usec_t reverse_offset;
383N/A};
383N/A
383N/Astruct unit_times {
383N/A char *name;
383N/A usec_t activating;
383N/A usec_t activated;
383N/A usec_t deactivated;
383N/A usec_t deactivating;
383N/A usec_t time;
383N/A};
383N/A
383N/Astruct host_info {
383N/A char *hostname;
383N/A char *kernel_name;
383N/A char *kernel_release;
383N/A char *kernel_version;
383N/A char *os_pretty_name;
383N/A char *virtualization;
383N/A char *architecture;
383N/A};
383N/A
383N/Astatic void pager_open_if_enabled(void) {
383N/A
383N/A if (arg_no_pager)
383N/A return;
383N/A
383N/A pager_open(false);
383N/A}
383N/A
383N/Astatic int bus_get_uint64_property(sd_bus *bus, const char *path, const char *interface, const char *property, uint64_t *val) {
383N/A _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
383N/A int r;
383N/A
383N/A assert(bus);
383N/A assert(path);
383N/A assert(interface);
383N/A assert(property);
383N/A assert(val);
383N/A
383N/A r = sd_bus_get_property_trivial(
383N/A bus,
383N/A "org.freedesktop.systemd1",
383N/A path,
383N/A interface,
383N/A property,
383N/A &error,
383N/A 't', val);
383N/A
383N/A if (r < 0) {
383N/A log_error("Failed to parse reply: %s", bus_error_message(&error, -r));
383N/A return r;
383N/A }
383N/A
383N/A return 0;
383N/A}
383N/A
383N/Astatic int bus_get_unit_property_strv(sd_bus *bus, const char *path, const char *property, char ***strv) {
383N/A _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
383N/A int r;
383N/A
383N/A assert(bus);
383N/A assert(path);
383N/A assert(property);
383N/A assert(strv);
383N/A
383N/A r = sd_bus_get_property_strv(
383N/A bus,
383N/A "org.freedesktop.systemd1",
383N/A path,
383N/A "org.freedesktop.systemd1.Unit",
383N/A property,
383N/A &error,
383N/A strv);
383N/A if (r < 0) {
383N/A log_error("Failed to get unit property %s: %s", property, bus_error_message(&error, -r));
383N/A return r;
383N/A }
383N/A
383N/A return 0;
383N/A}
383N/A
383N/Astatic int compare_unit_time(const void *a, const void *b) {
383N/A return compare(((struct unit_times *)b)->time,
383N/A ((struct unit_times *)a)->time);
383N/A}
383N/A
383N/Astatic int compare_unit_start(const void *a, const void *b) {
383N/A return compare(((struct unit_times *)a)->activating,
383N/A ((struct unit_times *)b)->activating);
383N/A}
383N/A
383N/Astatic void free_unit_times(struct unit_times *t, unsigned n) {
383N/A struct unit_times *p;
383N/A
383N/A for (p = t; p < t + n; p++)
383N/A free(p->name);
383N/A
383N/A free(t);
383N/A}
383N/A
383N/Astatic void subtract_timestamp(usec_t *a, usec_t b) {
383N/A assert(a);
383N/A
383N/A if (*a > 0) {
383N/A assert(*a >= b);
383N/A *a -= b;
383N/A }
383N/A}
383N/A
383N/Astatic int acquire_boot_times(sd_bus *bus, struct boot_times **bt) {
383N/A static struct boot_times times;
383N/A static bool cached = false;
383N/A
383N/A if (cached)
383N/A goto finish;
383N/A
383N/A assert_cc(sizeof(usec_t) == sizeof(uint64_t));
383N/A
383N/A if (bus_get_uint64_property(bus,
383N/A "/org/freedesktop/systemd1",
383N/A "org.freedesktop.systemd1.Manager",
383N/A "FirmwareTimestampMonotonic",
383N/A &times.firmware_time) < 0 ||
383N/A bus_get_uint64_property(bus,
383N/A "/org/freedesktop/systemd1",
383N/A "org.freedesktop.systemd1.Manager",
383N/A "LoaderTimestampMonotonic",
383N/A &times.loader_time) < 0 ||
383N/A bus_get_uint64_property(bus,
383N/A "/org/freedesktop/systemd1",
383N/A "org.freedesktop.systemd1.Manager",
383N/A "KernelTimestamp",
383N/A &times.kernel_time) < 0 ||
383N/A bus_get_uint64_property(bus,
383N/A "/org/freedesktop/systemd1",
383N/A "org.freedesktop.systemd1.Manager",
383N/A "InitRDTimestampMonotonic",
383N/A &times.initrd_time) < 0 ||
383N/A bus_get_uint64_property(bus,
383N/A "/org/freedesktop/systemd1",
383N/A "org.freedesktop.systemd1.Manager",
383N/A "UserspaceTimestampMonotonic",
383N/A &times.userspace_time) < 0 ||
383N/A bus_get_uint64_property(bus,
383N/A "/org/freedesktop/systemd1",
383N/A "org.freedesktop.systemd1.Manager",
383N/A "FinishTimestampMonotonic",
383N/A &times.finish_time) < 0 ||
383N/A bus_get_uint64_property(bus,
383N/A "/org/freedesktop/systemd1",
383N/A "org.freedesktop.systemd1.Manager",
383N/A "SecurityStartTimestampMonotonic",
383N/A &times.security_start_time) < 0 ||
383N/A bus_get_uint64_property(bus,
383N/A "/org/freedesktop/systemd1",
383N/A "org.freedesktop.systemd1.Manager",
383N/A "SecurityFinishTimestampMonotonic",
383N/A &times.security_finish_time) < 0 ||
383N/A bus_get_uint64_property(bus,
383N/A "/org/freedesktop/systemd1",
383N/A "org.freedesktop.systemd1.Manager",
383N/A "GeneratorsStartTimestampMonotonic",
383N/A &times.generators_start_time) < 0 ||
383N/A bus_get_uint64_property(bus,
383N/A "/org/freedesktop/systemd1",
383N/A "org.freedesktop.systemd1.Manager",
383N/A "GeneratorsFinishTimestampMonotonic",
383N/A &times.generators_finish_time) < 0 ||
383N/A bus_get_uint64_property(bus,
383N/A "/org/freedesktop/systemd1",
383N/A "org.freedesktop.systemd1.Manager",
383N/A "UnitsLoadStartTimestampMonotonic",
383N/A &times.unitsload_start_time) < 0 ||
383N/A bus_get_uint64_property(bus,
383N/A "/org/freedesktop/systemd1",
383N/A "org.freedesktop.systemd1.Manager",
383N/A "UnitsLoadFinishTimestampMonotonic",
383N/A &times.unitsload_finish_time) < 0)
383N/A return -EIO;
383N/A
383N/A if (times.finish_time <= 0) {
383N/A log_error("Bootup is not yet finished. Please try again later.");
383N/A return -EINPROGRESS;
383N/A }
383N/A
383N/A if (arg_user) {
383N/A /*
383N/A * User-instance-specific timestamps processing
383N/A * (see comment to reverse_offset in struct boot_times).
383N/A */
383N/A times.reverse_offset = times.userspace_time;
383N/A
383N/A times.firmware_time = times.loader_time = times.kernel_time = times.initrd_time = times.userspace_time = 0;
383N/A subtract_timestamp(&times.finish_time, times.reverse_offset);
383N/A
383N/A subtract_timestamp(&times.security_start_time, times.reverse_offset);
383N/A subtract_timestamp(&times.security_finish_time, times.reverse_offset);
383N/A
383N/A subtract_timestamp(&times.generators_start_time, times.reverse_offset);
383N/A subtract_timestamp(&times.generators_finish_time, times.reverse_offset);
383N/A
383N/A subtract_timestamp(&times.unitsload_start_time, times.reverse_offset);
383N/A subtract_timestamp(&times.unitsload_finish_time, times.reverse_offset);
383N/A } else {
383N/A if (times.initrd_time)
383N/A times.kernel_done_time = times.initrd_time;
383N/A else
383N/A times.kernel_done_time = times.userspace_time;
383N/A }
383N/A
383N/A cached = true;
383N/A
383N/Afinish:
383N/A *bt = &times;
383N/A return 0;
383N/A}
383N/A
383N/Astatic void free_host_info(struct host_info *hi) {
383N/A
383N/A if (!hi)
383N/A return;
383N/A
383N/A free(hi->hostname);
383N/A free(hi->kernel_name);
383N/A free(hi->kernel_release);
383N/A free(hi->kernel_version);
383N/A free(hi->os_pretty_name);
383N/A free(hi->virtualization);
383N/A free(hi->architecture);
383N/A free(hi);
383N/A}
383N/A
383N/ADEFINE_TRIVIAL_CLEANUP_FUNC(struct host_info*, free_host_info);
383N/A
383N/Astatic int acquire_time_data(sd_bus *bus, struct unit_times **out) {
383N/A _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
383N/A _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
383N/A int r, c = 0;
383N/A struct boot_times *boot_times = NULL;
383N/A struct unit_times *unit_times = NULL;
383N/A size_t size = 0;
383N/A UnitInfo u;
383N/A
383N/A r = acquire_boot_times(bus, &boot_times);
383N/A if (r < 0)
383N/A goto fail;
383N/A
383N/A r = sd_bus_call_method(
383N/A bus,
383N/A "org.freedesktop.systemd1",
383N/A "/org/freedesktop/systemd1",
383N/A "org.freedesktop.systemd1.Manager",
383N/A "ListUnits",
383N/A &error, &reply,
383N/A NULL);
383N/A if (r < 0) {
383N/A log_error("Failed to list units: %s", bus_error_message(&error, -r));
383N/A goto fail;
383N/A }
383N/A
383N/A r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
383N/A if (r < 0) {
383N/A bus_log_parse_error(r);
383N/A goto fail;
383N/A }
383N/A
383N/A while ((r = bus_parse_unit_info(reply, &u)) > 0) {
383N/A struct unit_times *t;
383N/A
383N/A if (!GREEDY_REALLOC(unit_times, size, c+1)) {
383N/A r = log_oom();
383N/A goto fail;
383N/A }
383N/A
383N/A t = unit_times+c;
383N/A t->name = NULL;
383N/A
383N/A assert_cc(sizeof(usec_t) == sizeof(uint64_t));
383N/A
383N/A if (bus_get_uint64_property(bus, u.unit_path,
383N/A "org.freedesktop.systemd1.Unit",
383N/A "InactiveExitTimestampMonotonic",
383N/A &t->activating) < 0 ||
383N/A bus_get_uint64_property(bus, u.unit_path,
383N/A "org.freedesktop.systemd1.Unit",
383N/A "ActiveEnterTimestampMonotonic",
383N/A &t->activated) < 0 ||
383N/A bus_get_uint64_property(bus, u.unit_path,
383N/A "org.freedesktop.systemd1.Unit",
383N/A "ActiveExitTimestampMonotonic",
383N/A &t->deactivating) < 0 ||
383N/A bus_get_uint64_property(bus, u.unit_path,
383N/A "org.freedesktop.systemd1.Unit",
383N/A "InactiveEnterTimestampMonotonic",
383N/A &t->deactivated) < 0) {
383N/A r = -EIO;
383N/A goto fail;
383N/A }
383N/A
383N/A subtract_timestamp(&t->activating, boot_times->reverse_offset);
383N/A subtract_timestamp(&t->activated, boot_times->reverse_offset);
383N/A subtract_timestamp(&t->deactivating, boot_times->reverse_offset);
383N/A subtract_timestamp(&t->deactivated, boot_times->reverse_offset);
383N/A
383N/A if (t->activated >= t->activating)
383N/A t->time = t->activated - t->activating;
383N/A else if (t->deactivated >= t->activating)
383N/A t->time = t->deactivated - t->activating;
383N/A else
383N/A t->time = 0;
383N/A
383N/A if (t->activating == 0)
383N/A continue;
383N/A
383N/A t->name = strdup(u.id);
383N/A if (t->name == NULL) {
383N/A r = log_oom();
383N/A goto fail;
383N/A }
383N/A c++;
383N/A }
383N/A if (r < 0) {
383N/A bus_log_parse_error(r);
383N/A goto fail;
383N/A }
383N/A
383N/A *out = unit_times;
383N/A return c;
383N/A
383N/Afail:
383N/A if (unit_times)
383N/A free_unit_times(unit_times, (unsigned) c);
383N/A return r;
383N/A}
383N/A
383N/Astatic int acquire_host_info(sd_bus *bus, struct host_info **hi) {
383N/A static const struct bus_properties_map hostname_map[] = {
383N/A { "Hostname", "s", NULL, offsetof(struct host_info, hostname) },
383N/A { "KernelName", "s", NULL, offsetof(struct host_info, kernel_name) },
383N/A { "KernelRelease", "s", NULL, offsetof(struct host_info, kernel_release) },
383N/A { "KernelVersion", "s", NULL, offsetof(struct host_info, kernel_version) },
383N/A { "OperatingSystemPrettyName", "s", NULL, offsetof(struct host_info, os_pretty_name) },
383N/A {}
383N/A };
383N/A
383N/A static const struct bus_properties_map manager_map[] = {
383N/A { "Virtualization", "s", NULL, offsetof(struct host_info, virtualization) },
383N/A { "Architecture", "s", NULL, offsetof(struct host_info, architecture) },
383N/A {}
383N/A };
383N/A
383N/A _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
383N/A _cleanup_(free_host_infop) struct host_info *host;
383N/A int r;
383N/A
383N/A host = new0(struct host_info, 1);
383N/A if (!host)
383N/A return log_oom();
383N/A
383N/A r = bus_map_all_properties(bus,
383N/A "org.freedesktop.hostname1",
383N/A "/org/freedesktop/hostname1",
383N/A hostname_map,
383N/A host);
383N/A if (r < 0)
383N/A log_debug_errno(r, "Failed to get host information from systemd-hostnamed: %s", bus_error_message(&error, r));
383N/A
383N/A r = bus_map_all_properties(bus,
383N/A "org.freedesktop.systemd1",
383N/A "/org/freedesktop/systemd1",
383N/A manager_map,
383N/A host);
383N/A if (r < 0)
383N/A return log_error_errno(r, "Failed to get host information from systemd: %s", bus_error_message(&error, r));
383N/A
383N/A *hi = host;
383N/A host = NULL;
383N/A
383N/A return 0;
383N/A}
383N/A
383N/Astatic int pretty_boot_time(sd_bus *bus, char **_buf) {
383N/A char ts[FORMAT_TIMESPAN_MAX];
383N/A struct boot_times *t;
383N/A static char buf[4096];
383N/A size_t size;
383N/A char *ptr;
383N/A int r;
383N/A
383N/A r = acquire_boot_times(bus, &t);
383N/A if (r < 0)
383N/A return r;
383N/A
383N/A ptr = buf;
383N/A size = sizeof(buf);
383N/A
383N/A size = strpcpyf(&ptr, size, "Startup finished in ");
383N/A if (t->firmware_time)
383N/A size = strpcpyf(&ptr, size, "%s (firmware) + ", format_timespan(ts, sizeof(ts), t->firmware_time - t->loader_time, USEC_PER_MSEC));
383N/A if (t->loader_time)
383N/A size = strpcpyf(&ptr, size, "%s (loader) + ", format_timespan(ts, sizeof(ts), t->loader_time, USEC_PER_MSEC));
383N/A if (t->kernel_time)
383N/A size = strpcpyf(&ptr, size, "%s (kernel) + ", format_timespan(ts, sizeof(ts), t->kernel_done_time, USEC_PER_MSEC));
383N/A if (t->initrd_time > 0)
383N/A size = strpcpyf(&ptr, size, "%s (initrd) + ", format_timespan(ts, sizeof(ts), t->userspace_time - t->initrd_time, USEC_PER_MSEC));
383N/A
383N/A size = strpcpyf(&ptr, size, "%s (userspace) ", format_timespan(ts, sizeof(ts), t->finish_time - t->userspace_time, USEC_PER_MSEC));
383N/A strpcpyf(&ptr, size, "= %s", format_timespan(ts, sizeof(ts), t->firmware_time + t->finish_time, USEC_PER_MSEC));
383N/A
383N/A ptr = strdup(buf);
383N/A if (!ptr)
383N/A return log_oom();
383N/A
383N/A *_buf = ptr;
383N/A return 0;
383N/A}
383N/A
383N/Astatic void svg_graph_box(double height, double begin, double end) {
383N/A long long i;
383N/A
383N/A /* outside box, fill */
383N/A svg("<rect class=\"box\" x=\"0\" y=\"0\" width=\"%.03f\" height=\"%.03f\" />\n",
383N/A SCALE_X * (end - begin), SCALE_Y * height);
383N/A
383N/A for (i = ((long long) (begin / 100000)) * 100000; i <= end; i+=100000) {
383N/A /* lines for each second */
383N/A if (i % 5000000 == 0)
383N/A svg(" <line class=\"sec5\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n"
383N/A " <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
383N/A SCALE_X * i, SCALE_X * i, SCALE_Y * height, SCALE_X * i, -5.0, 0.000001 * i);
383N/A else if (i % 1000000 == 0)
383N/A svg(" <line class=\"sec1\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n"
383N/A " <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
383N/A SCALE_X * i, SCALE_X * i, SCALE_Y * height, SCALE_X * i, -5.0, 0.000001 * i);
383N/A else
383N/A svg(" <line class=\"sec01\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n",
383N/A SCALE_X * i, SCALE_X * i, SCALE_Y * height);
383N/A }
383N/A}
383N/A
383N/Astatic int analyze_plot(sd_bus *bus) {
383N/A _cleanup_(free_host_infop) struct host_info *host = NULL;
383N/A struct unit_times *times;
383N/A struct boot_times *boot;
383N/A int n, m = 1, y=0;
383N/A double width;
383N/A _cleanup_free_ char *pretty_times = NULL;
383N/A struct unit_times *u;
383N/A
383N/A n = acquire_boot_times(bus, &boot);
383N/A if (n < 0)
383N/A return n;
383N/A
383N/A n = pretty_boot_time(bus, &pretty_times);
383N/A if (n < 0)
383N/A return n;
383N/A
383N/A n = acquire_host_info(bus, &host);
383N/A if (n < 0)
383N/A return n;
383N/A
383N/A n = acquire_time_data(bus, &times);
383N/A if (n <= 0)
383N/A return n;
383N/A
383N/A qsort(times, n, sizeof(struct unit_times), compare_unit_start);
383N/A
383N/A width = SCALE_X * (boot->firmware_time + boot->finish_time);
383N/A if (width < 800.0)
383N/A width = 800.0;
383N/A
383N/A if (boot->firmware_time > boot->loader_time)
383N/A m++;
383N/A if (boot->loader_time) {
383N/A m++;
383N/A if (width < 1000.0)
383N/A width = 1000.0;
383N/A }
383N/A if (boot->initrd_time)
383N/A m++;
383N/A if (boot->kernel_time)
383N/A m++;
383N/A
383N/A for (u = times; u < times + n; u++) {
383N/A double text_start, text_width;
383N/A
383N/A if (u->activating < boot->userspace_time ||
383N/A u->activating > boot->finish_time) {
383N/A u->name = mfree(u->name);
383N/A continue;
383N/A }
383N/A
383N/A /* If the text cannot fit on the left side then
383N/A * increase the svg width so it fits on the right.
383N/A * TODO: calculate the text width more accurately */
383N/A text_width = 8.0 * strlen(u->name);
383N/A text_start = (boot->firmware_time + u->activating) * SCALE_X;
383N/A if (text_width > text_start && text_width + text_start > width)
383N/A width = text_width + text_start;
383N/A
383N/A if (u->deactivated > u->activating && u->deactivated <= boot->finish_time
383N/A && u->activated == 0 && u->deactivating == 0)
383N/A u->activated = u->deactivating = u->deactivated;
383N/A if (u->activated < u->activating || u->activated > boot->finish_time)
383N/A u->activated = boot->finish_time;
383N/A if (u->deactivating < u->activated || u->activated > boot->finish_time)
383N/A u->deactivating = boot->finish_time;
383N/A if (u->deactivated < u->deactivating || u->deactivated > boot->finish_time)
383N/A u->deactivated = boot->finish_time;
383N/A m++;
383N/A }
383N/A
383N/A svg("<?xml version=\"1.0\" standalone=\"no\"?>\n"
383N/A "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
383N/A "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
383N/A
383N/A svg("<svg width=\"%.0fpx\" height=\"%.0fpx\" version=\"1.1\" "
383N/A "xmlns=\"http://www.w3.org/2000/svg\">\n\n",
383N/A 80.0 + width, 150.0 + (m * SCALE_Y) +
383N/A 5 * SCALE_Y /* legend */);
383N/A
383N/A /* write some basic info as a comment, including some help */
383N/A svg("<!-- This file is a systemd-analyze SVG file. It is best rendered in a -->\n"
383N/A "<!-- browser such as Chrome, Chromium or Firefox. Other applications -->\n"
383N/A "<!-- that render these files properly but much slower are ImageMagick, -->\n"
383N/A "<!-- gimp, inkscape, etc. To display the files on your system, just -->\n"
383N/A "<!-- point your browser to this file. -->\n\n"
383N/A "<!-- This plot was generated by systemd-analyze version %-16.16s -->\n\n", VERSION);
383N/A
383N/A /* style sheet */
383N/A svg("<defs>\n <style type=\"text/css\">\n <![CDATA[\n"
383N/A " rect { stroke-width: 1; stroke-opacity: 0; }\n"
383N/A " rect.background { fill: rgb(255,255,255); }\n"
383N/A " rect.activating { fill: rgb(255,0,0); fill-opacity: 0.7; }\n"
383N/A " rect.active { fill: rgb(200,150,150); fill-opacity: 0.7; }\n"
383N/A " rect.deactivating { fill: rgb(150,100,100); fill-opacity: 0.7; }\n"
383N/A " rect.kernel { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
383N/A " rect.initrd { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
383N/A " rect.firmware { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
383N/A " rect.loader { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
383N/A " rect.userspace { fill: rgb(150,150,150); fill-opacity: 0.7; }\n"
383N/A " rect.security { fill: rgb(144,238,144); fill-opacity: 0.7; }\n"
383N/A " rect.generators { fill: rgb(102,204,255); fill-opacity: 0.7; }\n"
383N/A " rect.unitsload { fill: rgb( 82,184,255); fill-opacity: 0.7; }\n"
383N/A " rect.box { fill: rgb(240,240,240); stroke: rgb(192,192,192); }\n"
383N/A " line { stroke: rgb(64,64,64); stroke-width: 1; }\n"
383N/A "// line.sec1 { }\n"
383N/A " line.sec5 { stroke-width: 2; }\n"
383N/A " line.sec01 { stroke: rgb(224,224,224); stroke-width: 1; }\n"
383N/A " text { font-family: Verdana, Helvetica; font-size: 14px; }\n"
383N/A " text.left { font-family: Verdana, Helvetica; font-size: 14px; text-anchor: start; }\n"
383N/A " text.right { font-family: Verdana, Helvetica; font-size: 14px; text-anchor: end; }\n"
383N/A " text.sec { font-size: 10px; }\n"
383N/A " ]]>\n </style>\n</defs>\n\n");
383N/A
383N/A svg("<rect class=\"background\" width=\"100%%\" height=\"100%%\" />\n");
383N/A svg("<text x=\"20\" y=\"50\">%s</text>", pretty_times);
383N/A svg("<text x=\"20\" y=\"30\">%s %s (%s %s %s) %s %s</text>",
383N/A isempty(host->os_pretty_name) ? "Linux" : host->os_pretty_name,
383N/A strempty(host->hostname),
383N/A strempty(host->kernel_name),
383N/A strempty(host->kernel_release),
383N/A strempty(host->kernel_version),
383N/A strempty(host->architecture),
383N/A strempty(host->virtualization));
383N/A
383N/A svg("<g transform=\"translate(%.3f,100)\">\n", 20.0 + (SCALE_X * boot->firmware_time));
383N/A svg_graph_box(m, -(double) boot->firmware_time, boot->finish_time);
383N/A
383N/A if (boot->firmware_time) {
383N/A svg_bar("firmware", -(double) boot->firmware_time, -(double) boot->loader_time, y);
383N/A svg_text(true, -(double) boot->firmware_time, y, "firmware");
383N/A y++;
383N/A }
383N/A if (boot->loader_time) {
383N/A svg_bar("loader", -(double) boot->loader_time, 0, y);
383N/A svg_text(true, -(double) boot->loader_time, y, "loader");
383N/A y++;
383N/A }
383N/A if (boot->kernel_time) {
383N/A svg_bar("kernel", 0, boot->kernel_done_time, y);
383N/A svg_text(true, 0, y, "kernel");
383N/A y++;
383N/A }
383N/A if (boot->initrd_time) {
383N/A svg_bar("initrd", boot->initrd_time, boot->userspace_time, y);
383N/A svg_text(true, boot->initrd_time, y, "initrd");
383N/A y++;
383N/A }
383N/A svg_bar("active", boot->userspace_time, boot->finish_time, y);
383N/A svg_bar("security", boot->security_start_time, boot->security_finish_time, y);
383N/A svg_bar("generators", boot->generators_start_time, boot->generators_finish_time, y);
383N/A svg_bar("unitsload", boot->unitsload_start_time, boot->unitsload_finish_time, y);
383N/A svg_text(true, boot->userspace_time, y, "systemd");
383N/A y++;
383N/A
383N/A for (u = times; u < times + n; u++) {
383N/A char ts[FORMAT_TIMESPAN_MAX];
383N/A bool b;
383N/A
383N/A if (!u->name)
383N/A continue;
383N/A
383N/A svg_bar("activating", u->activating, u->activated, y);
383N/A svg_bar("active", u->activated, u->deactivating, y);
383N/A svg_bar("deactivating", u->deactivating, u->deactivated, y);
383N/A
383N/A /* place the text on the left if we have passed the half of the svg width */
383N/A b = u->activating * SCALE_X < width / 2;
383N/A if (u->time)
383N/A svg_text(b, u->activating, y, "%s (%s)",
383N/A u->name, format_timespan(ts, sizeof(ts), u->time, USEC_PER_MSEC));
383N/A else
383N/A svg_text(b, u->activating, y, "%s", u->name);
383N/A y++;
383N/A }
383N/A
383N/A svg("</g>\n");
383N/A
383N/A /* Legend */
383N/A svg("<g transform=\"translate(20,100)\">\n");
383N/A y++;
383N/A svg_bar("activating", 0, 300000, y);
383N/A svg_text(true, 400000, y, "Activating");
383N/A y++;
383N/A svg_bar("active", 0, 300000, y);
383N/A svg_text(true, 400000, y, "Active");
383N/A y++;
383N/A svg_bar("deactivating", 0, 300000, y);
383N/A svg_text(true, 400000, y, "Deactivating");
383N/A y++;
383N/A svg_bar("security", 0, 300000, y);
383N/A svg_text(true, 400000, y, "Setting up security module");
383N/A y++;
383N/A svg_bar("generators", 0, 300000, y);
383N/A svg_text(true, 400000, y, "Generators");
383N/A y++;
383N/A svg_bar("unitsload", 0, 300000, y);
383N/A svg_text(true, 400000, y, "Loading unit files");
383N/A y++;
383N/A
383N/A svg("</g>\n\n");
383N/A
383N/A svg("</svg>\n");
383N/A
383N/A free_unit_times(times, (unsigned) n);
383N/A
383N/A n = 0;
383N/A return n;
383N/A}
383N/A
383N/Astatic int list_dependencies_print(const char *name, unsigned int level, unsigned int branches,
383N/A bool last, struct unit_times *times, struct boot_times *boot) {
383N/A unsigned int i;
383N/A char ts[FORMAT_TIMESPAN_MAX], ts2[FORMAT_TIMESPAN_MAX];
383N/A
383N/A for (i = level; i != 0; i--)
383N/A printf("%s", draw_special_char(branches & (1 << (i-1)) ? DRAW_TREE_VERTICAL : DRAW_TREE_SPACE));
383N/A
383N/A printf("%s", draw_special_char(last ? DRAW_TREE_RIGHT : DRAW_TREE_BRANCH));
383N/A
383N/A if (times) {
383N/A if (times->time)
383N/A printf("%s%s @%s +%s%s", ANSI_HIGHLIGHT_RED, name,
383N/A format_timespan(ts, sizeof(ts), times->activating - boot->userspace_time, USEC_PER_MSEC),
383N/A format_timespan(ts2, sizeof(ts2), times->time, USEC_PER_MSEC), ANSI_NORMAL);
383N/A else if (times->activated > boot->userspace_time)
383N/A printf("%s @%s", name, format_timespan(ts, sizeof(ts), times->activated - boot->userspace_time, USEC_PER_MSEC));
383N/A else
383N/A printf("%s", name);
383N/A } else
383N/A printf("%s", name);
383N/A printf("\n");
383N/A
383N/A return 0;
383N/A}
383N/A
383N/Astatic int list_dependencies_get_dependencies(sd_bus *bus, const char *name, char ***deps) {
383N/A _cleanup_free_ char *path = NULL;
383N/A
383N/A assert(bus);
383N/A assert(name);
383N/A assert(deps);
383N/A
383N/A path = unit_dbus_path_from_name(name);
383N/A if (path == NULL)
383N/A return -ENOMEM;
383N/A
383N/A return bus_get_unit_property_strv(bus, path, "After", deps);
383N/A}
383N/A
383N/Astatic Hashmap *unit_times_hashmap;
383N/A
383N/Astatic int list_dependencies_compare(const void *_a, const void *_b) {
383N/A const char **a = (const char**) _a, **b = (const char**) _b;
383N/A usec_t usa = 0, usb = 0;
383N/A struct unit_times *times;
383N/A
383N/A times = hashmap_get(unit_times_hashmap, *a);
383N/A if (times)
383N/A usa = times->activated;
383N/A times = hashmap_get(unit_times_hashmap, *b);
383N/A if (times)
383N/A usb = times->activated;
383N/A
383N/A return usb - usa;
383N/A}
383N/A
383N/Astatic int list_dependencies_one(sd_bus *bus, const char *name, unsigned int level, char ***units,
383N/A unsigned int branches) {
383N/A _cleanup_strv_free_ char **deps = NULL;
383N/A char **c;
383N/A int r = 0;
383N/A usec_t service_longest = 0;
383N/A int to_print = 0;
383N/A struct unit_times *times;
383N/A struct boot_times *boot;
383N/A
383N/A if (strv_extend(units, name))
383N/A return log_oom();
383N/A
383N/A r = list_dependencies_get_dependencies(bus, name, &deps);
383N/A if (r < 0)
383N/A return r;
383N/A
383N/A qsort_safe(deps, strv_length(deps), sizeof (char*), list_dependencies_compare);
383N/A
383N/A r = acquire_boot_times(bus, &boot);
383N/A if (r < 0)
383N/A return r;
383N/A
383N/A STRV_FOREACH(c, deps) {
383N/A times = hashmap_get(unit_times_hashmap, *c);
383N/A if (times
383N/A && times->activated
383N/A && times->activated <= boot->finish_time
383N/A && (times->activated >= service_longest
383N/A || service_longest == 0)) {
383N/A service_longest = times->activated;
383N/A break;
383N/A }
383N/A }
383N/A
383N/A if (service_longest == 0 )
383N/A return r;
383N/A
383N/A STRV_FOREACH(c, deps) {
383N/A times = hashmap_get(unit_times_hashmap, *c);
383N/A if (times && times->activated && times->activated <= boot->finish_time && (service_longest - times->activated) <= arg_fuzz)
383N/A to_print++;
383N/A }
383N/A
383N/A if (!to_print)
383N/A return r;
383N/A
383N/A STRV_FOREACH(c, deps) {
383N/A times = hashmap_get(unit_times_hashmap, *c);
383N/A if (!times
383N/A || !times->activated
383N/A || times->activated > boot->finish_time
383N/A || service_longest - times->activated > arg_fuzz)
383N/A continue;
383N/A
383N/A to_print--;
383N/A
383N/A r = list_dependencies_print(*c, level, branches, to_print == 0, times, boot);
383N/A if (r < 0)
383N/A return r;
383N/A
383N/A if (strv_contains(*units, *c)) {
383N/A r = list_dependencies_print("...", level + 1, (branches << 1) | (to_print ? 1 : 0),
383N/A true, NULL, boot);
383N/A if (r < 0)
383N/A return r;
383N/A continue;
383N/A }
383N/A
383N/A r = list_dependencies_one(bus, *c, level + 1, units,
383N/A (branches << 1) | (to_print ? 1 : 0));
383N/A if (r < 0)
383N/A return r;
383N/A
383N/A if (!to_print)
383N/A break;
383N/A }
383N/A return 0;
383N/A}
383N/A
383N/Astatic int list_dependencies(sd_bus *bus, const char *name) {
383N/A _cleanup_strv_free_ char **units = NULL;
383N/A char ts[FORMAT_TIMESPAN_MAX];
383N/A struct unit_times *times;
383N/A int r;
383N/A const char *id;
383N/A _cleanup_free_ char *path = NULL;
383N/A _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
383N/A _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
383N/A struct boot_times *boot;
383N/A
383N/A assert(bus);
383N/A
383N/A path = unit_dbus_path_from_name(name);
383N/A if (path == NULL)
383N/A return -ENOMEM;
383N/A
383N/A r = sd_bus_get_property(
383N/A bus,
383N/A "org.freedesktop.systemd1",
383N/A path,
383N/A "org.freedesktop.systemd1.Unit",
383N/A "Id",
383N/A &error,
383N/A &reply,
383N/A "s");
383N/A if (r < 0) {
383N/A log_error("Failed to get ID: %s", bus_error_message(&error, -r));
383N/A return r;
383N/A }
383N/A
383N/A r = sd_bus_message_read(reply, "s", &id);
383N/A if (r < 0)
383N/A return bus_log_parse_error(r);
383N/A
383N/A times = hashmap_get(unit_times_hashmap, id);
383N/A
383N/A r = acquire_boot_times(bus, &boot);
383N/A if (r < 0)
383N/A return r;
383N/A
383N/A if (times) {
383N/A if (times->time)
383N/A printf("%s%s +%s%s\n", ANSI_HIGHLIGHT_RED, id,
383N/A format_timespan(ts, sizeof(ts), times->time, USEC_PER_MSEC), ANSI_NORMAL);
383N/A else if (times->activated > boot->userspace_time)
383N/A printf("%s @%s\n", id, format_timespan(ts, sizeof(ts), times->activated - boot->userspace_time, USEC_PER_MSEC));
383N/A else
383N/A printf("%s\n", id);
383N/A }
383N/A
383N/A return list_dependencies_one(bus, name, 0, &units, 0);
383N/A}
383N/A
383N/Astatic int analyze_critical_chain(sd_bus *bus, char *names[]) {
383N/A struct unit_times *times;
383N/A unsigned int i;
383N/A Hashmap *h;
383N/A int n, r;
383N/A
383N/A n = acquire_time_data(bus, &times);
383N/A if (n <= 0)
383N/A return n;
383N/A
383N/A h = hashmap_new(&string_hash_ops);
383N/A if (!h)
383N/A return -ENOMEM;
383N/A
383N/A for (i = 0; i < (unsigned)n; i++) {
383N/A r = hashmap_put(h, times[i].name, &times[i]);
383N/A if (r < 0)
383N/A return r;
383N/A }
383N/A unit_times_hashmap = h;
383N/A
383N/A pager_open_if_enabled();
383N/A
383N/A puts("The time after the unit is active or started is printed after the \"@\" character.\n"
383N/A "The time the unit takes to start is printed after the \"+\" character.\n");
383N/A
383N/A if (!strv_isempty(names)) {
383N/A char **name;
383N/A STRV_FOREACH(name, names)
383N/A list_dependencies(bus, *name);
383N/A } else
383N/A list_dependencies(bus, SPECIAL_DEFAULT_TARGET);
383N/A
383N/A hashmap_free(h);
383N/A free_unit_times(times, (unsigned) n);
383N/A return 0;
383N/A}
383N/A
383N/Astatic int analyze_blame(sd_bus *bus) {
383N/A struct unit_times *times;
383N/A unsigned i;
383N/A int n;
383N/A
383N/A n = acquire_time_data(bus, &times);
383N/A if (n <= 0)
383N/A return n;
383N/A
383N/A qsort(times, n, sizeof(struct unit_times), compare_unit_time);
383N/A
383N/A pager_open_if_enabled();
383N/A
383N/A for (i = 0; i < (unsigned) n; i++) {
383N/A char ts[FORMAT_TIMESPAN_MAX];
383N/A
383N/A if (times[i].time > 0)
383N/A printf("%16s %s\n", format_timespan(ts, sizeof(ts), times[i].time, USEC_PER_MSEC), times[i].name);
383N/A }
383N/A
383N/A free_unit_times(times, (unsigned) n);
383N/A return 0;
383N/A}
383N/A
383N/Astatic int analyze_time(sd_bus *bus) {
383N/A _cleanup_free_ char *buf = NULL;
383N/A int r;
383N/A
383N/A r = pretty_boot_time(bus, &buf);
383N/A if (r < 0)
383N/A return r;
383N/A
383N/A puts(buf);
383N/A return 0;
383N/A}
383N/A
383N/Astatic int graph_one_property(sd_bus *bus, const UnitInfo *u, const char* prop, const char *color, char* patterns[], char* from_patterns[], char* to_patterns[]) {
383N/A _cleanup_strv_free_ char **units = NULL;
383N/A char **unit;
383N/A int r;
383N/A bool match_patterns;
383N/A
383N/A assert(u);
383N/A assert(prop);
383N/A assert(color);
383N/A
383N/A match_patterns = strv_fnmatch(patterns, u->id, 0);
383N/A
383N/A if (!strv_isempty(from_patterns) &&
383N/A !match_patterns &&
383N/A !strv_fnmatch(from_patterns, u->id, 0))
383N/A return 0;
383N/A
383N/A r = bus_get_unit_property_strv(bus, u->unit_path, prop, &units);
383N/A if (r < 0)
383N/A return r;
383N/A
383N/A STRV_FOREACH(unit, units) {
383N/A bool match_patterns2;
383N/A
383N/A match_patterns2 = strv_fnmatch(patterns, *unit, 0);
383N/A
383N/A if (!strv_isempty(to_patterns) &&
383N/A !match_patterns2 &&
383N/A !strv_fnmatch(to_patterns, *unit, 0))
383N/A continue;
383N/A
383N/A if (!strv_isempty(patterns) && !match_patterns && !match_patterns2)
383N/A continue;
383N/A
383N/A printf("\t\"%s\"->\"%s\" [color=\"%s\"];\n", u->id, *unit, color);
383N/A }
383N/A
383N/A return 0;
383N/A}
383N/A
383N/Astatic int graph_one(sd_bus *bus, const UnitInfo *u, char *patterns[], char *from_patterns[], char *to_patterns[]) {
383N/A int r;
383N/A
383N/A assert(bus);
383N/A assert(u);
383N/A
383N/A if (IN_SET(arg_dot, DEP_ORDER, DEP_ALL)) {
383N/A r = graph_one_property(bus, u, "After", "green", patterns, from_patterns, to_patterns);
383N/A if (r < 0)
383N/A return r;
383N/A }
383N/A
383N/A if (IN_SET(arg_dot, DEP_REQUIRE, DEP_ALL)) {
383N/A r = graph_one_property(bus, u, "Requires", "black", patterns, from_patterns, to_patterns);
383N/A if (r < 0)
383N/A return r;
383N/A r = graph_one_property(bus, u, "Requisite", "darkblue", patterns, from_patterns, to_patterns);
383N/A if (r < 0)
383N/A return r;
383N/A r = graph_one_property(bus, u, "Wants", "grey66", patterns, from_patterns, to_patterns);
383N/A if (r < 0)
383N/A return r;
383N/A r = graph_one_property(bus, u, "Conflicts", "red", patterns, from_patterns, to_patterns);
383N/A if (r < 0)
383N/A return r;
383N/A r = graph_one_property(bus, u, "ConflictedBy", "red", patterns, from_patterns, to_patterns);
383N/A if (r < 0)
383N/A return r;
383N/A }
383N/A
383N/A return 0;
383N/A}
383N/A
383N/Astatic int expand_patterns(sd_bus *bus, char **patterns, char ***ret) {
383N/A _cleanup_strv_free_ char **expanded_patterns = NULL;
383N/A char **pattern;
383N/A int r;
383N/A
383N/A STRV_FOREACH(pattern, patterns) {
383N/A _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
383N/A _cleanup_free_ char *unit = NULL, *unit_id = NULL;
383N/A
383N/A if (strv_extend(&expanded_patterns, *pattern) < 0)
383N/A return log_oom();
383N/A
383N/A if (string_is_glob(*pattern))
383N/A continue;
383N/A
383N/A unit = unit_dbus_path_from_name(*pattern);
383N/A if (!unit)
383N/A return log_oom();
383N/A
383N/A r = sd_bus_get_property_string(
383N/A bus,
383N/A "org.freedesktop.systemd1",
383N/A unit,
383N/A "org.freedesktop.systemd1.Unit",
383N/A "Id",
383N/A &error,
383N/A &unit_id);
383N/A if (r < 0)
383N/A return log_error_errno(r, "Failed to get ID: %s", bus_error_message(&error, r));
383N/A
383N/A if (!streq(*pattern, unit_id)) {
383N/A if (strv_extend(&expanded_patterns, unit_id) < 0)
383N/A return log_oom();
383N/A }
383N/A }
383N/A
383N/A *ret = expanded_patterns;
383N/A expanded_patterns = NULL; /* do not free */
383N/A
383N/A return 0;
383N/A}
383N/A
383N/Astatic int dot(sd_bus *bus, char* patterns[]) {
383N/A _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
383N/A _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
383N/A _cleanup_strv_free_ char **expanded_patterns = NULL;
383N/A _cleanup_strv_free_ char **expanded_from_patterns = NULL;
383N/A _cleanup_strv_free_ char **expanded_to_patterns = NULL;
383N/A int r;
383N/A UnitInfo u;
383N/A
383N/A r = expand_patterns(bus, patterns, &expanded_patterns);
383N/A if (r < 0)
383N/A return r;
383N/A
r = expand_patterns(bus, arg_dot_from_patterns, &expanded_from_patterns);
if (r < 0)
return r;
r = expand_patterns(bus, arg_dot_to_patterns, &expanded_to_patterns);
if (r < 0)
return r;
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"ListUnits",
&error,
&reply,
"");
if (r < 0) {
log_error("Failed to list units: %s", bus_error_message(&error, -r));
return r;
}
r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssssouso)");
if (r < 0)
return bus_log_parse_error(r);
printf("digraph systemd {\n");
while ((r = bus_parse_unit_info(reply, &u)) > 0) {
r = graph_one(bus, &u, expanded_patterns, expanded_from_patterns, expanded_to_patterns);
if (r < 0)
return r;
}
if (r < 0)
return bus_log_parse_error(r);
printf("}\n");
log_info(" Color legend: black = Requires\n"
" dark blue = Requisite\n"
" dark grey = Wants\n"
" red = Conflicts\n"
" green = After\n");
if (on_tty())
log_notice("-- You probably want to process this output with graphviz' dot tool.\n"
"-- Try a shell pipeline like 'systemd-analyze dot | dot -Tsvg > systemd.svg'!\n");
return 0;
}
static int dump(sd_bus *bus, char **args) {
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
const char *text = NULL;
int r;
if (!strv_isempty(args)) {
log_error("Too many arguments.");
return -E2BIG;
}
pager_open_if_enabled();
r = sd_bus_call_method(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"Dump",
&error,
&reply,
"");
if (r < 0)
return log_error_errno(r, "Failed issue method call: %s", bus_error_message(&error, r));
r = sd_bus_message_read(reply, "s", &text);
if (r < 0)
return bus_log_parse_error(r);
fputs(text, stdout);
return 0;
}
static int set_log_level(sd_bus *bus, char **args) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
assert(bus);
assert(args);
if (strv_length(args) != 1) {
log_error("This command expects one argument only.");
return -E2BIG;
}
r = sd_bus_set_property(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"LogLevel",
&error,
"s",
args[0]);
if (r < 0)
return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
return 0;
}
static int set_log_target(sd_bus *bus, char **args) {
_cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
assert(bus);
assert(args);
if (strv_length(args) != 1) {
log_error("This command expects one argument only.");
return -E2BIG;
}
r = sd_bus_set_property(
bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"LogTarget",
&error,
"s",
args[0]);
if (r < 0)
return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
return 0;
}
static void help(void) {
pager_open_if_enabled();
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
"Profile systemd, show unit dependencies, check unit files.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --no-pager Do not pipe output into a pager\n"
" --system Operate on system systemd instance\n"
" --user Operate on user systemd instance\n"
" -H --host=[USER@]HOST Operate on remote host\n"
" -M --machine=CONTAINER Operate on local container\n"
" --order Show only order in the graph\n"
" --require Show only requirement in the graph\n"
" --from-pattern=GLOB Show only origins in the graph\n"
" --to-pattern=GLOB Show only destinations in the graph\n"
" --fuzz=SECONDS Also print also services which finished SECONDS\n"
" earlier than the latest in the branch\n"
" --man[=BOOL] Do [not] check for existence of man pages\n\n"
"Commands:\n"
" time Print time spent in the kernel\n"
" blame Print list of running units ordered by time to init\n"
" critical-chain Print a tree of the time critical chain of units\n"
" plot Output SVG graphic showing service initialization\n"
" dot Output dependency graph in dot(1) format\n"
" set-log-level LEVEL Set logging threshold for manager\n"
" set-log-target TARGET Set logging target for manager\n"
" dump Output state serialization of service manager\n"
" verify FILE... Check unit files for correctness\n"
, program_invocation_short_name);
/* When updating this list, including descriptions, apply
* changes to shell-completion/bash/systemd-analyze and
* shell-completion/zsh/_systemd-analyze too. */
}
static int parse_argv(int argc, char *argv[]) {
enum {
ARG_VERSION = 0x100,
ARG_ORDER,
ARG_REQUIRE,
ARG_USER,
ARG_SYSTEM,
ARG_DOT_FROM_PATTERN,
ARG_DOT_TO_PATTERN,
ARG_FUZZ,
ARG_NO_PAGER,
ARG_MAN,
};
static const struct option options[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, ARG_VERSION },
{ "order", no_argument, NULL, ARG_ORDER },
{ "require", no_argument, NULL, ARG_REQUIRE },
{ "user", no_argument, NULL, ARG_USER },
{ "system", no_argument, NULL, ARG_SYSTEM },
{ "from-pattern", required_argument, NULL, ARG_DOT_FROM_PATTERN },
{ "to-pattern", required_argument, NULL, ARG_DOT_TO_PATTERN },
{ "fuzz", required_argument, NULL, ARG_FUZZ },
{ "no-pager", no_argument, NULL, ARG_NO_PAGER },
{ "man", optional_argument, NULL, ARG_MAN },
{ "host", required_argument, NULL, 'H' },
{ "machine", required_argument, NULL, 'M' },
{}
};
int r, c;
assert(argc >= 0);
assert(argv);
while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0)
switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return version();
case ARG_USER:
arg_user = true;
break;
case ARG_SYSTEM:
arg_user = false;
break;
case ARG_ORDER:
arg_dot = DEP_ORDER;
break;
case ARG_REQUIRE:
arg_dot = DEP_REQUIRE;
break;
case ARG_DOT_FROM_PATTERN:
if (strv_extend(&arg_dot_from_patterns, optarg) < 0)
return log_oom();
break;
case ARG_DOT_TO_PATTERN:
if (strv_extend(&arg_dot_to_patterns, optarg) < 0)
return log_oom();
break;
case ARG_FUZZ:
r = parse_sec(optarg, &arg_fuzz);
if (r < 0)
return r;
break;
case ARG_NO_PAGER:
arg_no_pager = true;
break;
case 'H':
arg_transport = BUS_TRANSPORT_REMOTE;
arg_host = optarg;
break;
case 'M':
arg_transport = BUS_TRANSPORT_MACHINE;
arg_host = optarg;
break;
case ARG_MAN:
if (optarg) {
r = parse_boolean(optarg);
if (r < 0) {
log_error("Failed to parse --man= argument.");
return -EINVAL;
}
arg_man = !!r;
} else
arg_man = true;
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option code.");
}
return 1; /* work to do */
}
int main(int argc, char *argv[]) {
int r;
setlocale(LC_ALL, "");
setlocale(LC_NUMERIC, "C"); /* we want to format/parse floats in C style */
log_parse_environment();
log_open();
r = parse_argv(argc, argv);
if (r <= 0)
goto finish;
if (streq_ptr(argv[optind], "verify"))
r = verify_units(argv+optind+1,
arg_user ? MANAGER_USER : MANAGER_SYSTEM,
arg_man);
else {
_cleanup_bus_flush_close_unref_ sd_bus *bus = NULL;
r = bus_connect_transport_systemd(arg_transport, arg_host, arg_user, &bus);
if (r < 0) {
log_error_errno(r, "Failed to create bus connection: %m");
goto finish;
}
if (!argv[optind] || streq(argv[optind], "time"))
r = analyze_time(bus);
else if (streq(argv[optind], "blame"))
r = analyze_blame(bus);
else if (streq(argv[optind], "critical-chain"))
r = analyze_critical_chain(bus, argv+optind+1);
else if (streq(argv[optind], "plot"))
r = analyze_plot(bus);
else if (streq(argv[optind], "dot"))
r = dot(bus, argv+optind+1);
else if (streq(argv[optind], "dump"))
r = dump(bus, argv+optind+1);
else if (streq(argv[optind], "set-log-level"))
r = set_log_level(bus, argv+optind+1);
else if (streq(argv[optind], "set-log-target"))
r = set_log_target(bus, argv+optind+1);
else
log_error("Unknown operation '%s'.", argv[optind]);
}
finish:
pager_close();
strv_free(arg_dot_from_patterns);
strv_free(arg_dot_to_patterns);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}