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
8 changes: 6 additions & 2 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ EXTRA_DIST = \
tests/identity \
tests/short-lived \
tests/io \
tests/pressure
tests/pressure \
tests/json

MANPAGES = man/bootchart.conf.5 man/systemd-bootchart.1
MANPAGES_ALIAS = man/bootchart.conf.d.5
Expand Down Expand Up @@ -185,6 +186,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 @@ -200,7 +203,8 @@ TESTS = \
tests/identity \
tests/short-lived \
tests/io \
tests/pressure
tests/pressure \
tests/json

substitutions = \
'|rootlibexecdir=$(rootlibexecdir)|'
Expand Down
7 changes: 6 additions & 1 deletion man/systemd-bootchart.xml
Original file line number Diff line number Diff line change
Expand Up @@ -218,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
18 changes: 18 additions & 0 deletions src/bootchart.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include "fd-util.h"
#include "fileio.h"
#include "io-util.h"
#include "json.h"
#include "list.h"
#include "log.h"
#include "macro.h"
Expand Down Expand Up @@ -326,12 +327,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 Down Expand Up @@ -514,6 +517,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);
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
132 changes: 132 additions & 0 deletions src/json.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/***
This file is part of systemd-bootchart.

systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
***/

/*
* The chart's numbers, beside the SVG, for a program to compare two boots by: the system's
* counters at every sample, and every process's totals. Counters are written as the kernel
* keeps them, cumulative, so a reader takes the differences it needs over the intervals it
* chooses. Times are seconds on the chart's clock: since boot, or since the sampler started
* with --rel.
*
* "samples" holds one row per sample, oldest first, whose values "sample_columns" names:
* the sample's time; /proc/schedstat's run and wait time summed over the CPUs, in ns;
* /proc/vmstat's pgpgin and pgpgout, in KiB; and /proc/pressure's some and full totals for
* cpu, io and memory, in us.
*
* A process's cpu_ns, wait_ns, read_bytes and write_bytes are its totals at the last sample
* that saw it: for a process already running when the chart started, they include what it did
* before. first_seen and last_seen are the times of the samples that saw it first and last.
*/

#include <errno.h>
#include <inttypes.h>
#include <string.h>

#include "alloc-util.h"
#include "json.h"
#include "list.h"
#include "store.h"
#include "utf8.h"

static void json_string(FILE *of, const char *s) {
_cleanup_free_ char *escaped = NULL;
const unsigned char *p;

if (!s) {
fputs("null", of);
return;
}

if (!utf8_is_printable(s, strlen(s))) {
escaped = utf8_escape_non_printable(s);
if (escaped)
s = escaped;
}

fputc('"', of);
for (p = (const unsigned char *) s; *p; p++) {
if (*p == '"' || *p == '\\')
fprintf(of, "\\%c", *p);
else if (*p < 0x20)
fprintf(of, "\\u%04x", *p);
else
fputc(*p, of);
}
fputc('"', of);
}

static struct list_sample_data *oldest(struct list_sample_data *head) {
struct list_sample_data *tail = head;

LIST_FIND_TAIL(link, head, tail);
return tail;
}

int json_do(FILE *of,
struct list_sample_data *head,
struct ps_struct *ps_first,
int n_cpus,
double graph_start,
double interval,
int overrun) {

struct list_sample_data *s;
struct ps_struct *ps;
const char *sep;

fprintf(of, "{\"version\": 1, \"hz\": %g, \"interval\": %g, \"overrun\": %d, \"relative\": %s, \"cpus\": %d,\n",
arg_hz, interval / 1000000000.0, overrun, arg_relative ? "true" : "false", n_cpus);

fputs("\"sample_columns\": [\"time\", \"cpu_run_ns\", \"cpu_wait_ns\", \"pgpgin_kb\", \"pgpgout_kb\", "
"\"cpu_some_us\", \"cpu_full_us\", \"io_some_us\", \"io_full_us\", \"memory_some_us\", \"memory_full_us\"],\n", of);

fputs("\"samples\": [", of);
sep = "\n";
for (s = oldest(head); s; s = s->link_prev) {
double run = 0, wait = 0;
int c;

for (c = 0; c < n_cpus; c++) {
run += s->runtime[c];
wait += s->waittime[c];
}

fprintf(of, "%s[%.4f,%.0f,%.0f,%d,%d,%" PRIu64 ",%" PRIu64 ",%" PRIu64 ",%" PRIu64 ",%" PRIu64 ",%" PRIu64 "]",
sep, s->sampletime - graph_start, run, wait, s->blockstat.bi, s->blockstat.bo,
s->pressure[PRESSURE_CPU][PRESSURE_SOME], s->pressure[PRESSURE_CPU][PRESSURE_FULL],
s->pressure[PRESSURE_IO][PRESSURE_SOME], s->pressure[PRESSURE_IO][PRESSURE_FULL],
s->pressure[PRESSURE_MEMORY][PRESSURE_SOME], s->pressure[PRESSURE_MEMORY][PRESSURE_FULL]);
sep = ",\n";
}
fputs("\n],\n", of);

fputs("\"processes\": [", of);
sep = "\n";
for (ps = ps_first->next_ps; ps; ps = ps->next_ps) {
fprintf(of, "%s{\"pid\": %d, \"ppid\": %d, \"name\": ", sep, ps->pid, ps->ppid);
json_string(of, ps->name);
fputs(", \"cgroup\": ", of);
json_string(of, ps->cgroup);
fputs(", \"unit\": ", of);
json_string(of, ps_unit(ps));
fprintf(of, ", \"first_seen\": %.4f, \"last_seen\": %.4f, \"cpu_ns\": %.0f, \"wait_ns\": %.0f, "
"\"read_bytes\": %" PRIu64 ", \"write_bytes\": %" PRIu64 ", \"pss_max_kb\": %d}",
ps->first->sampledata->sampletime - graph_start,
ps->last->sampledata->sampletime - graph_start,
ps->last->runtime - ps->first->runtime,
ps->last->waittime - ps->first->waittime,
ps->last->read_bytes,
ps->last->write_bytes,
ps->pss_max);
sep = ",\n";
}
fputs("\n]}\n", of);

return ferror(of) ? -EIO : 0;
}
22 changes: 22 additions & 0 deletions src/json.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

/***
This file is part of systemd-bootchart.

systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
***/

#include <stdio.h>

#include "bootchart.h"

int json_do(FILE *of,
struct list_sample_data *head,
struct ps_struct *ps_first,
int n_cpus,
double graph_start,
double interval,
int overrun);
9 changes: 7 additions & 2 deletions src/svg.c
Original file line number Diff line number Diff line change
Expand Up @@ -864,12 +864,17 @@ static const char *const pressure_names[_PRESSURE_MAX] = {
};

/* Whether the kernel reported pressure: a kernel without it leaves every total at zero, and a
* kernel with it has had some task wait for a CPU by the time a chart ends. */
* kernel with it has had some task wait for a CPU by the time a chart ends. HEAD is the oldest
* sample, as svg_do() walks them. */
static bool svg_has_pressure(struct list_sample_data *head) {
struct list_sample_data *newest = head;
int r;

while (newest->link_prev)
newest = newest->link_prev;

for (r = 0; r < _PRESSURE_MAX; r++)
if (head->pressure[r][PRESSURE_SOME] > 0)
if (newest->pressure[r][PRESSURE_SOME] > 0)
return true;
return false;
}
Expand Down
61 changes: 61 additions & 0 deletions tests/json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#! /bin/sh
# Two boots are compared by numbers, not by eye: the sampler writes the chart's numbers as JSON
# beside the SVG, and a program reads them back.
set -e
d=$(mktemp -d)
trap 'rm -rf "$d"' EXIT
proc=$d/proc
mkdir -p "$proc/pressure" "$d/out"

put() {
printf "$2" > "$proc/$1"
}

process() { # pid ppid comm cgroup runtime-ns waittime-ns
mkdir -p "$proc/$1/task"
put "$1/comm" "$3\n"
put "$1/stat" "$1 ($3) S $2 0 0\n"
put "$1/cgroup" "0::$4\n"
put "$1/cmdline" ""
put "$1/schedstat" "$5 $6 1\n"
put "$1/io" "read_bytes: 4096\nwrite_bytes: 0\n"
}

put vmstat "pgpgin 10\npgpgout 20\n"
put schedstat "version 15\ntimestamp 0\ncpu0 0 0 0 0 0 0 1000 2000 3\n"
put pressure/cpu "some avg10=0.00 avg60=0.00 avg300=0.00 total=500\nfull avg10=0.00 avg60=0.00 avg300=0.00 total=0\n"
process 1 0 systemd /init.scope 5000000 0
# a name a JSON writer has to escape
process 62 1 'say "hi"\\' /system.slice/systemd-journald.service 1000000 250000

timeout 30 ./systemd-bootchart --proc "$proc" -o "$d/out" -r -c -f 100 -n 20

python3 - "$d"/out/*.json <<'PY'
import json, sys
chart = json.load(open(sys.argv[1]))
errors = []
def check(ok, what):
if not ok:
errors.append(what)
cols = chart["sample_columns"]
check(cols[:3] == ["time", "cpu_run_ns", "cpu_wait_ns"], "sample columns start with time, run and wait")
check(all(len(row) == len(cols) for row in chart["samples"]), "every sample row has every column")
check(len(chart["samples"]) >= 10, "a row for each of the samples taken")
times = [row[0] for row in chart["samples"]]
check(times == sorted(times), "samples are oldest first")
last = dict(zip(cols, chart["samples"][-1]))
check(last["cpu_run_ns"] == 1000 and last["pgpgin_kb"] == 10 and last["cpu_some_us"] == 500,
"the counters as /proc/schedstat, vmstat and pressure have them: %s" % last)
ps = {p["pid"]: p for p in chart["processes"]}
j = ps.get(62, {})
check(j.get("name") == 'say "hi"\\', "the name read back: %r" % j.get("name"))
check(j.get("unit") == "systemd-journald.service", "the unit: %r" % j.get("unit"))
check(j.get("cgroup") == "/system.slice/systemd-journald.service", "the cgroup: %r" % j.get("cgroup"))
check(j.get("cpu_ns") == 1000000 and j.get("wait_ns") == 250000, "cpu and wait: %r" % j)
check(j.get("read_bytes") == 4096, "read_bytes: %r" % j.get("read_bytes"))
check(0 <= j.get("first_seen", -1) <= j.get("last_seen", -2), "first_seen before last_seen: %r" % j)
check(chart["hz"] == 100 and chart["relative"] is True, "the settings: %r" % chart)
for e in errors:
print("not ok - " + e)
sys.exit(1 if errors else 0)
PY