1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright (C) 2009-2013 Intel Corporation
9 Auke Kok <auke-jan.h.kok@intel.com>
11 systemd is free software; you can redistribute it and/or modify it
12 under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 2.1 of the License, or
14 (at your option) any later version.
16 systemd is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public License
22 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 Many thanks to those who contributed ideas and code:
28 - Ziga Mahkovec - Original bootchart author
29 - Anders Norgaard - PyBootchartgui
30 - Michael Meeks - bootchart2
31 - Scott James Remnant - Ubuntu C-based logger
32 - Arjan van der Ven - for the idea to merge bootgraph.pl functionality
37 #include <sys/types.h>
38 #include <sys/resource.h>
51 #include "systemd/sd-journal.h"
56 #include "conf-parser.h"
58 #include "path-util.h"
61 #include "bootchart.h"
66 struct ps_struct *ps_first;
72 static int exiting = 0;
75 #define DEFAULT_SAMPLES_LEN 500
76 #define DEFAULT_HZ 25.0
77 #define DEFAULT_SCALE_X 100.0 /* 100px = 1sec */
78 #define DEFAULT_SCALE_Y 20.0 /* 16px = 1 process bar */
79 #define DEFAULT_INIT "/sbin/init"
80 #define DEFAULT_OUTPUT "/run/log"
83 bool arg_entropy = false;
85 bool arg_relative = false;
86 bool arg_filter = true;
87 bool arg_show_cmdline = false;
88 bool arg_show_cgroup = false;
91 int arg_samples_len = DEFAULT_SAMPLES_LEN; /* we record len+1 (1 start sample) */
92 double arg_hz = DEFAULT_HZ;
93 double arg_scale_x = DEFAULT_SCALE_X;
94 double arg_scale_y = DEFAULT_SCALE_Y;
95 static struct list_sample_data *sampledata;
96 struct list_sample_data *head;
98 char arg_init_path[PATH_MAX] = DEFAULT_INIT;
99 char arg_output_path[PATH_MAX] = DEFAULT_OUTPUT;
101 static void signal_handler(int sig) {
107 #define BOOTCHART_CONF "/etc/systemd/bootchart.conf"
109 #define BOOTCHART_MAX (16*1024*1024)
111 static void parse_conf(void) {
112 char *init = NULL, *output = NULL;
113 const ConfigTableItem items[] = {
114 { "Bootchart", "Samples", config_parse_int, 0, &arg_samples_len },
115 { "Bootchart", "Frequency", config_parse_double, 0, &arg_hz },
116 { "Bootchart", "Relative", config_parse_bool, 0, &arg_relative },
117 { "Bootchart", "Filter", config_parse_bool, 0, &arg_filter },
118 { "Bootchart", "Output", config_parse_path, 0, &output },
119 { "Bootchart", "Init", config_parse_path, 0, &init },
120 { "Bootchart", "PlotMemoryUsage", config_parse_bool, 0, &arg_pss },
121 { "Bootchart", "PlotEntropyGraph", config_parse_bool, 0, &arg_entropy },
122 { "Bootchart", "ScaleX", config_parse_double, 0, &arg_scale_x },
123 { "Bootchart", "ScaleY", config_parse_double, 0, &arg_scale_y },
124 { "Bootchart", "ControlGroup", config_parse_bool, 0, &arg_show_cgroup },
125 { NULL, NULL, NULL, 0, NULL }
128 config_parse(NULL, BOOTCHART_CONF, NULL,
130 config_item_table_lookup, items,
131 true, false, true, NULL);
134 strscpy(arg_init_path, sizeof(arg_init_path), init);
136 strscpy(arg_output_path, sizeof(arg_output_path), output);
139 static void help(void) {
141 "Usage: %s [OPTIONS]\n\n"
143 " -r, --rel Record time relative to recording\n"
144 " -f, --freq=FREQ Sample frequency [%g]\n"
145 " -n, --samples=N Stop sampling at [%d] samples\n"
146 " -x, --scale-x=N Scale the graph horizontally [%g] \n"
147 " -y, --scale-y=N Scale the graph vertically [%g] \n"
148 " -p, --pss Enable PSS graph (CPU intensive)\n"
149 " -e, --entropy Enable the entropy_avail graph\n"
150 " -o, --output=PATH Path to output files [%s]\n"
151 " -i, --init=PATH Path to init executable [%s]\n"
152 " -F, --no-filter Disable filtering of unimportant or ephemeral processes\n"
153 " -C, --cmdline Display full command lines with arguments\n"
154 " -c, --control-group Display process control group\n"
155 " -h, --help Display this message\n\n"
156 "See bootchart.conf for more information.\n",
157 program_invocation_short_name,
166 static int parse_argv(int argc, char *argv[]) {
167 static const struct option options[] = {
168 {"rel", no_argument, NULL, 'r'},
169 {"freq", required_argument, NULL, 'f'},
170 {"samples", required_argument, NULL, 'n'},
171 {"pss", no_argument, NULL, 'p'},
172 {"output", required_argument, NULL, 'o'},
173 {"init", required_argument, NULL, 'i'},
174 {"no-filter", no_argument, NULL, 'F'},
175 {"cmdline", no_argument, NULL, 'C'},
176 {"control-group", no_argument, NULL, 'c'},
177 {"help", no_argument, NULL, 'h'},
178 {"scale-x", required_argument, NULL, 'x'},
179 {"scale-y", required_argument, NULL, 'y'},
180 {"entropy", no_argument, NULL, 'e'},
188 while ((c = getopt_long(argc, argv, "erpf:n:o:i:FCchx:y:", options, NULL)) >= 0)
195 r = safe_atod(optarg, &arg_hz);
197 log_warning("failed to parse --freq/-f argument '%s': %s",
198 optarg, strerror(-r));
204 arg_show_cmdline = true;
207 arg_show_cgroup = true;
210 r = safe_atoi(optarg, &arg_samples_len);
212 log_warning("failed to parse --samples/-n argument '%s': %s",
213 optarg, strerror(-r));
216 path_kill_slashes(optarg);
217 strscpy(arg_output_path, sizeof(arg_output_path), optarg);
220 path_kill_slashes(optarg);
221 strscpy(arg_init_path, sizeof(arg_init_path), optarg);
227 r = safe_atod(optarg, &arg_scale_x);
229 log_warning("failed to parse --scale-x/-x argument '%s': %s",
230 optarg, strerror(-r));
233 r = safe_atod(optarg, &arg_scale_y);
235 log_warning("failed to parse --scale-y/-y argument '%s': %s",
236 optarg, strerror(-r));
250 assert_not_reached("Unhandled option code.");
254 log_error("Frequency needs to be > 0");
261 static void do_journal_append(char *file) {
262 struct iovec iovec[5];
265 _cleanup_free_ char *bootchart_file = NULL, *bootchart_message = NULL,
268 bootchart_file = strappend("BOOTCHART_FILE=", file);
270 IOVEC_SET_STRING(iovec[j++], bootchart_file);
272 IOVEC_SET_STRING(iovec[j++], "MESSAGE_ID=9f26aa562cf440c2b16c773d0479b518");
273 IOVEC_SET_STRING(iovec[j++], "PRIORITY=7");
274 bootchart_message = strjoin("MESSAGE=Bootchart created: ", file, NULL);
275 if (bootchart_message)
276 IOVEC_SET_STRING(iovec[j++], bootchart_message);
278 p = malloc(9 + BOOTCHART_MAX);
284 memcpy(p, "BOOTCHART=", 10);
286 f = open(file, O_RDONLY|O_CLOEXEC);
288 log_error("Failed to read bootchart data: %m");
291 n = loop_read(f, p + 10, BOOTCHART_MAX, false);
293 log_error("Failed to read bootchart data: %s", strerror(-n));
299 iovec[j].iov_base = p;
300 iovec[j].iov_len = 10 + n;
303 r = sd_journal_sendv(iovec, j);
305 log_error("Failed to send bootchart: %s", strerror(-r));
308 int main(int argc, char *argv[]) {
309 _cleanup_free_ char *build = NULL;
310 struct sigaction sig = {
311 .sa_handler = signal_handler,
313 struct ps_struct *ps;
314 char output_file[PATH_MAX];
319 bool has_procfs = false;
323 r = parse_argv(argc, argv);
325 return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
328 * If the kernel executed us through init=/usr/lib/systemd/systemd-bootchart, then
330 * - parent execs executable specified via init_path[] (/sbin/init by default) as pid=1
336 execl(arg_init_path, arg_init_path, NULL);
341 rlim.rlim_cur = 4096;
342 rlim.rlim_max = 4096;
343 (void) setrlimit(RLIMIT_NOFILE, &rlim);
345 /* start with empty ps LL */
346 ps_first = new0(struct ps_struct, 1);
352 /* handle TERM/INT nicely */
353 sigaction(SIGHUP, &sig, NULL);
355 interval = (1.0 / arg_hz) * 1000000000.0;
359 if (graph_start < 0.0) {
361 "Failed to setup graph start time.\n\nThe system uptime "
362 "probably includes time that the system was suspended. "
363 "Use --rel to bypass this issue.\n");
367 has_procfs = access("/proc/vmstat", F_OK) == 0;
369 LIST_HEAD_INIT(head);
371 /* main program loop */
372 for (samples = 0; !exiting && samples < arg_samples_len; samples++) {
381 sampledata = new0(struct list_sample_data, 1);
382 if (sampledata == NULL) {
387 sampledata->sampletime = gettime_ns();
388 sampledata->counter = samples;
390 if (!of && (access(arg_output_path, R_OK|W_OK|X_OK) == 0)) {
392 strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M", localtime(&t));
393 snprintf(output_file, PATH_MAX, "%s/bootchart-%s.svg", arg_output_path, datestr);
394 of = fopen(output_file, "we");
398 sysfd = open("/sys", O_RDONLY|O_CLOEXEC);
401 if (parse_env_file("/etc/os-release", NEWLINE, "PRETTY_NAME", &build, NULL) == -ENOENT)
402 parse_env_file("/usr/lib/os-release", NEWLINE, "PRETTY_NAME", &build, NULL);
406 log_sample(samples, &sampledata);
408 /* wait for /proc to become available, discarding samples */
409 has_procfs = access("/proc/vmstat", F_OK) == 0;
411 sample_stop = gettime_ns();
413 elapsed = (sample_stop - sampledata->sampletime) * 1000000000.0;
414 timeleft = interval - elapsed;
416 newint_s = (time_t)(timeleft / 1000000000.0);
417 newint_ns = (long)(timeleft - (newint_s * 1000000000.0));
420 * check if we have not consumed our entire timeslice. If we
421 * do, don't sleep and take a new sample right away.
422 * we'll lose all the missed samples and overrun our total
425 if (newint_ns > 0 || newint_s > 0) {
426 req.tv_sec = newint_s;
427 req.tv_nsec = newint_ns;
429 res = nanosleep(&req, NULL);
431 if (errno == EINTR) {
432 /* caught signal, probably HUP! */
435 log_error("nanosleep() failed: %m");
440 /* calculate how many samples we lost and scrap them */
441 arg_samples_len -= (int)(newint_ns / interval);
443 LIST_PREPEND(link, head, sampledata);
446 /* do some cleanup, close fd's */
448 while (ps->next_ps) {
451 close(ps->schedstat);
460 strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M", localtime(&t));
461 snprintf(output_file, PATH_MAX, "%s/bootchart-%s.svg", arg_output_path, datestr);
462 of = fopen(output_file, "we");
466 fprintf(stderr, "opening output file '%s': %m\n", output_file);
472 fprintf(stderr, "systemd-bootchart wrote %s\n", output_file);
474 do_journal_append(output_file);
483 /* nitpic cleanups */
484 ps = ps_first->next_ps;
485 while (ps->next_ps) {
486 struct ps_struct *old;
489 old->sample = ps->first;
491 while (old->sample->next) {
492 struct ps_sched_struct *oldsample = old->sample;
494 old->sample = old->sample->next;
506 while (sampledata->link_prev) {
507 struct list_sample_data *old_sampledata = sampledata;
508 sampledata = sampledata->link_prev;
509 free(old_sampledata);
512 /* don't complain when overrun once, happens most commonly on 1st sample */
514 fprintf(stderr, "systemd-boochart: Warning: sample time overrun %i times\n", overrun);