Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ EXTRA_DIST = \
man/standard-conf.xml \
man/standard-options.xml \
units/systemd-bootchart.service.in \
tests/run
tests/run \
tests/identity \
tests/short-lived \
tests/io \
tests/pressure \
tests/json \
tests/header

MANPAGES = man/bootchart.conf.5 man/systemd-bootchart.1
MANPAGES_ALIAS = man/bootchart.conf.d.5
Expand Down Expand Up @@ -110,8 +116,6 @@ libutils_la_SOURCES = \
src/architecture.h \
src/attributes.h \
src/build.h \
src/cgroup-util.c \
src/cgroup-util.h \
src/conf-files.c \
src/conf-files.h \
src/conf-parser.c \
Expand Down Expand Up @@ -183,6 +187,8 @@ noinst_LTLIBRARIES = libutils.la
systemd_bootchart_SOURCES = \
src/bootchart.c \
src/bootchart.h \
src/json.c \
src/json.h \
src/store.c \
src/store.h \
src/svg.c \
Expand All @@ -193,7 +199,14 @@ systemd_bootchart_LDADD = \

#####################################################

TESTS = tests/run
TESTS = \
tests/run \
tests/identity \
tests/short-lived \
tests/io \
tests/pressure \
tests/json \
tests/header

substitutions = \
'|rootlibexecdir=$(rootlibexecdir)|'
Expand Down
11 changes: 5 additions & 6 deletions man/bootchart.conf.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,9 @@
<varlistentry>
<term><varname>Filter=no</varname></term>
<listitem><para>Configures whether the resulting graph should
omit tasks that did not contribute significantly to the boot.
Processes that are too short-lived (only seen in one sample)
or that do not consume any significant CPU time (less than
0.001sec) will not be displayed in the output
graph.</para></listitem>
omit tasks that did not run while it was recorded, such as idle
kernel threads. A process that used CPU time in any sample's
interval is drawn, however briefly it ran.</para></listitem>
</varlistentry>

<varlistentry>
Expand Down Expand Up @@ -154,7 +152,8 @@

<varlistentry>
<term><varname>ControlGroup=no</varname></term>
<listitem><para>Display process control group.
<listitem><para>Label each process with the unit whose control
group it is in, and list the units that used the most CPU time.
</para></listitem>
</varlistentry>

Expand Down
35 changes: 27 additions & 8 deletions man/systemd-bootchart.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,12 @@
<varlistentry>
<term><option>-F</option></term>
<term><option>--no-filter</option></term>
<listitem><para>Disable filtering of tasks that did not
contribute significantly to the boot. Processes that are too
short-lived (only seen in one sample) or that do not consume
any significant CPU time (less than 0.001 s) will not be
displayed in the output graph. </para></listitem>
<listitem><para>Disable filtering of tasks that did not run
while the chart was recorded. By default a process is drawn when
it used CPU time in any sample's interval, however briefly, and
processes that did not, such as idle kernel threads, are left out.
A process seen in a single sample is drawn over the interval
before that sample, in which it started.</para></listitem>
</varlistentry>

<varlistentry>
Expand All @@ -193,9 +194,22 @@
</varlistentry>

<varlistentry>
<term><option>-g</option></term>
<term><option>-c</option></term>
<term><option>--control-group</option></term>
<listitem><para>Display process control group
<listitem><para>Label each process with the unit whose control
group it is in, such as <filename>systemd-journald.service</filename>,
and list the units that used the most CPU time. The control group is
read from the cgroup v2 hierarchy, or from systemd's own hierarchy on
a v1 or hybrid one, and read again whenever the process has run, since
a service's processes move into its unit's control group after they
start.</para></listitem>
</varlistentry>

<varlistentry>
<term><option>--proc <replaceable>path</replaceable></option></term>
<listitem><para>Sample processes from this directory instead of
<filename>/proc</filename>. Its layout is
<filename>/proc</filename>'s, and its use is a test's.
</para></listitem>
</varlistentry>

Expand All @@ -204,7 +218,12 @@
<term><option>--output <replaceable>path</replaceable></option></term>
<listitem><para>Specify the output directory for the graphs.
By default, bootchart writes the graphs to
<filename>/run/log</filename>.</para></listitem>
<filename>/run/log</filename>. Beside each
<filename>bootchart-<replaceable>date</replaceable>.svg</filename>
it writes <filename>bootchart-<replaceable>date</replaceable>.json</filename>,
the chart's numbers for a program to read: the system's cumulative
counters at every sample, and every process's totals. Its layout is
described at the top of <filename>src/json.c</filename>.</para></listitem>
</varlistentry>

<varlistentry>
Expand Down
56 changes: 50 additions & 6 deletions src/bootchart.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/uio.h>
#include <time.h>
#include <unistd.h>
Expand All @@ -56,12 +58,14 @@
#include "fd-util.h"
#include "fileio.h"
#include "io-util.h"
#include "json.h"
#include "list.h"
#include "log.h"
#include "macro.h"
#include "parse-util.h"
#include "path-util.h"
#include "store.h"
#include "stdio-util.h"
#include "string-util.h"
#include "strxcpyx.h"
#include "svg.h"
Expand All @@ -75,6 +79,7 @@ static int exiting = 0;
#define DEFAULT_SCALE_Y 20.0 /* 16px = 1 process bar */
#define DEFAULT_INIT ROOTLIBEXECDIR "/systemd"
#define DEFAULT_OUTPUT "/run/log"
#define DEFAULT_PROC "/proc"

/* graph defaults */
bool arg_entropy = false;
Expand All @@ -92,6 +97,7 @@ double arg_scale_y = DEFAULT_SCALE_Y;

char arg_init_path[PATH_MAX] = DEFAULT_INIT;
char arg_output_path[PATH_MAX] = DEFAULT_OUTPUT;
char arg_proc_path[PATH_MAX] = DEFAULT_PROC;

static void signal_handler(int sig) {
exiting = 1;
Expand Down Expand Up @@ -144,6 +150,7 @@ static void help(void) {
" -C --cmdline Display full command lines with arguments\n"
" -c --control-group Display process control group\n"
" --per-cpu Draw each CPU utilization and wait bar also\n"
" --proc=PATH Sample processes from PATH instead of [%s]\n"
" -h --help Display this message\n\n"
"See bootchart.conf for more information.\n",
program_invocation_short_name,
Expand All @@ -152,13 +159,15 @@ static void help(void) {
DEFAULT_SCALE_X,
DEFAULT_SCALE_Y,
DEFAULT_OUTPUT,
DEFAULT_INIT);
DEFAULT_INIT,
DEFAULT_PROC);
}

static int parse_argv(int argc, char *argv[]) {

enum {
ARG_PERCPU = 0x100,
ARG_PROC,
};

static const struct option options[] = {
Expand All @@ -176,6 +185,7 @@ static int parse_argv(int argc, char *argv[]) {
{"scale-y", required_argument, NULL, 'y' },
{"entropy", no_argument, NULL, 'e' },
{"per-cpu", no_argument, NULL, ARG_PERCPU},
{"proc", required_argument, NULL, ARG_PROC },
{}
};
int c, r;
Expand Down Expand Up @@ -239,6 +249,10 @@ static int parse_argv(int argc, char *argv[]) {
case ARG_PERCPU:
arg_percpu = true;
break;
case ARG_PROC:
path_kill_slashes(optarg);
strscpy(arg_proc_path, sizeof(arg_proc_path), optarg);
break;
case 'h':
help();
return 0;
Expand Down Expand Up @@ -315,12 +329,14 @@ int main(int argc, char *argv[]) {
_cleanup_closedir_ DIR *proc = NULL;
_cleanup_free_ char *build = NULL;
_cleanup_fclose_ FILE *of = NULL;
_cleanup_fclose_ FILE *json = NULL;
int schfd;
struct ps_struct *ps_first;
double graph_start;
double log_start;
double interval;
char output_file[PATH_MAX];
char json_file[PATH_MAX];
char datestr[200];
int pscount = 0;
int n_cpus = 0;
Expand All @@ -333,6 +349,9 @@ int main(int argc, char *argv[]) {
.sa_handler = signal_handler,
};
bool has_procfs = false;
struct rusage usage;
double sampler_cpu = 0.0;
char vmstat_path[PATH_MAX + 8];

parse_conf();

Expand All @@ -353,6 +372,10 @@ int main(int argc, char *argv[]) {
if (fork())
/* parent */
execl(arg_init_path, arg_init_path, NULL);

/* The sampler was exec'd as the kernel's init and would be charted as "init",
* beside the systemd it forked from. */
(void) prctl(PR_SET_NAME, "bootchart");
}
argv[0][0] = '@';

Expand Down Expand Up @@ -394,7 +417,8 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}

has_procfs = access("/proc/vmstat", F_OK) == 0;
xsprintf(vmstat_path, "%s/vmstat", arg_proc_path);
has_procfs = access(vmstat_path, F_OK) == 0;

LIST_HEAD_INIT(head);

Expand All @@ -421,11 +445,12 @@ int main(int argc, char *argv[]) {

if (!has_procfs) {
/* wait for /proc to become available, discarding samples */
has_procfs = access("/proc/vmstat", F_OK) == 0;
xsprintf(vmstat_path, "%s/vmstat", arg_proc_path);
has_procfs = access(vmstat_path, F_OK) == 0;
} else if (proc) {
rewinddir(proc);
} else {
proc = opendir("/proc");
proc = opendir(arg_proc_path);
}

if (proc) {
Expand Down Expand Up @@ -467,12 +492,16 @@ int main(int argc, char *argv[]) {
LIST_PREPEND(link, head, sampledata);
}

/* what sampling cost, before drawing adds to it */
if (getrusage(RUSAGE_SELF, &usage) == 0)
sampler_cpu = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec +
(usage.ru_utime.tv_usec + usage.ru_stime.tv_usec) / 1000000.0;

/* do some cleanup, close fd's */
ps = ps_first;
while (ps->next_running) {
ps = ps->next_running;
ps->schedstat = safe_close(ps->schedstat);
ps->sched = safe_close(ps->sched);
ps->smaps = safe_fclose(ps->smaps);
}

Expand All @@ -492,7 +521,7 @@ int main(int argc, char *argv[]) {

r = svg_do(of, strna(build), head, ps_first,
samples, pscount, n_cpus, graph_start,
log_start, interval, overrun);
log_start, interval, overrun, sampler_cpu);

if (r < 0) {
log_error_errno(r, "Error generating svg file: %m");
Expand All @@ -501,6 +530,21 @@ int main(int argc, char *argv[]) {

log_info("systemd-bootchart wrote %s\n", output_file);

snprintf(json_file, PATH_MAX, "%s/bootchart-%s.json", arg_output_path, datestr);
json = fopen(json_file, "we");
if (!json) {
log_error("Error opening output file '%s': %m\n", json_file);
return EXIT_FAILURE;
}

r = json_do(json, head, ps_first, n_cpus, graph_start, interval, overrun, sampler_cpu);
if (r < 0) {
log_error_errno(r, "Error writing %s: %m", json_file);
return EXIT_FAILURE;
}

log_info("systemd-bootchart wrote %s\n", json_file);

r = do_journal_append(output_file);
if (r < 0)
return EXIT_FAILURE;
Expand Down
24 changes: 20 additions & 4 deletions src/bootchart.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

#include "list.h"
Expand All @@ -42,6 +43,10 @@ struct ps_sched_struct {
/* /proc/<n>/schedstat fields 1 & 2 */
double runtime;
double waittime;
/* /proc/<n>/io read_bytes & write_bytes: what the process had the block layer read for
* it, and the bytes it dirtied, since it started */
uint64_t read_bytes;
uint64_t write_bytes;
int pss;
struct list_sample_data *sampledata;
struct ps_sched_struct *next;
Expand All @@ -50,10 +55,24 @@ struct ps_sched_struct {
struct ps_struct *ps_new;
};

enum {
PRESSURE_CPU,
PRESSURE_IO,
PRESSURE_MEMORY,
_PRESSURE_MAX,
};

enum {
PRESSURE_SOME,
PRESSURE_FULL,
};

struct list_sample_data {
double runtime[MAXCPUS];
double waittime[MAXCPUS];
double sampletime;
/* /proc/pressure/{cpu,io,memory} some and full totals, in microseconds */
uint64_t pressure[_PRESSURE_MAX][2];
int entropy_avail;
struct block_stat_struct blockstat;
LIST_FIELDS(struct list_sample_data, link); /* DLL */
Expand All @@ -75,7 +94,6 @@ struct ps_struct {
char *cgroup;

/* cache fd's */
int sched;
int schedstat;
FILE *smaps;

Expand All @@ -86,9 +104,6 @@ struct ps_struct {
struct ps_sched_struct *first;
struct ps_sched_struct *last;

/* records actual start time, may be way before bootchart runs */
double starttime;

/* record human readable total cpu time */
double total;

Expand Down Expand Up @@ -117,3 +132,4 @@ extern double arg_scale_y;

extern char arg_output_path[PATH_MAX];
extern char arg_init_path[PATH_MAX];
extern char arg_proc_path[PATH_MAX];
Loading