bootchart.c revision 34a4071e998f327945993ea6e6cbcaa0292b4093
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright (C) 2009-2013 Intel Corporation
Authors:
Auke Kok <auke-jan.h.kok@intel.com>
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.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
/***
Many thanks to those who contributed ideas and code:
- Ziga Mahkovec - Original bootchart author
- Anders Norgaard - PyBootchartgui
- Michael Meeks - bootchart2
- Scott James Remnant - Ubuntu C-based logger
- Arjan van der Ven - for the idea to merge bootgraph.pl functionality
***/
#include <sys/resource.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <getopt.h>
#include <limits.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include "systemd/sd-journal.h"
#include "util.h"
#include "fileio.h"
#include "macro.h"
#include "conf-parser.h"
#include "strxcpyx.h"
#include "path-util.h"
#include "store.h"
#include "svg.h"
#include "bootchart.h"
#include "list.h"
double graph_start;
double log_start;
int pscount;
int cpus;
double interval;
int overrun = 0;
static int exiting = 0;
int sysfd=-1;
#define DEFAULT_SAMPLES_LEN 500
#define DEFAULT_HZ 25.0
#define DEFAULT_OUTPUT "/run/log"
/* graph defaults */
bool arg_entropy = false;
bool initcall = true;
bool arg_relative = false;
bool arg_filter = true;
bool arg_show_cmdline = false;
bool arg_show_cgroup = false;
bool arg_pss = false;
bool arg_percpu = false;
int samples;
double arg_hz = DEFAULT_HZ;
double arg_scale_x = DEFAULT_SCALE_X;
double arg_scale_y = DEFAULT_SCALE_Y;
static struct list_sample_data *sampledata;
struct list_sample_data *head;
static void signal_handler(int sig) {
if (sig++)
sig--;
exiting = 1;
}
#define BOOTCHART_CONF "/etc/systemd/bootchart.conf"
static void parse_conf(void) {
const ConfigTableItem items[] = {
};
}
static void help(void) {
printf("Usage: %s [OPTIONS]\n\n"
"Options:\n"
" -r --rel Record time relative to recording\n"
" -f --freq=FREQ Sample frequency [%g]\n"
" -n --samples=N Stop sampling at [%d] samples\n"
" -x --scale-x=N Scale the graph horizontally [%g] \n"
" -y --scale-y=N Scale the graph vertically [%g] \n"
" -p --pss Enable PSS graph (CPU intensive)\n"
" -e --entropy Enable the entropy_avail graph\n"
" -o --output=PATH Path to output files [%s]\n"
" -i --init=PATH Path to init executable [%s]\n"
" -F --no-filter Disable filtering of unimportant or ephemeral processes\n"
" -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"
" -h --help Display this message\n\n"
"See bootchart.conf for more information.\n",
}
enum {
ARG_PERCPU = 0x100,
};
{}
};
int c, r;
if (getpid() == 1)
opterr = 0;
switch (c) {
case 'r':
arg_relative = true;
break;
case 'f':
if (r < 0)
log_warning_errno(r, "failed to parse --freq/-f argument '%s': %m",
optarg);
break;
case 'F':
arg_filter = false;
break;
case 'C':
arg_show_cmdline = true;
break;
case 'c':
arg_show_cgroup = true;
break;
case 'n':
if (r < 0)
log_warning_errno(r, "failed to parse --samples/-n argument '%s': %m",
optarg);
break;
case 'o':
break;
case 'i':
break;
case 'p':
arg_pss = true;
break;
case 'x':
if (r < 0)
log_warning_errno(r, "failed to parse --scale-x/-x argument '%s': %m",
optarg);
break;
case 'y':
if (r < 0)
log_warning_errno(r, "failed to parse --scale-y/-y argument '%s': %m",
optarg);
break;
case 'e':
arg_entropy = true;
break;
case ARG_PERCPU:
arg_percpu = true;
break;
case 'h':
help();
return 0;
case '?':
if (getpid() != 1)
return -EINVAL;
else
return 0;
default:
assert_not_reached("Unhandled option code.");
}
if (arg_hz <= 0) {
log_error("Frequency needs to be > 0");
return -EINVAL;
}
return 1;
}
static void do_journal_append(char *file) {
int r, j = 0;
ssize_t n;
*p = NULL;
if (bootchart_file)
if (bootchart_message)
if (!p) {
log_oom();
return;
}
if (fd < 0) {
return;
}
if (n < 0) {
log_error_errno(n, "Failed to read bootchart data: %m");
return;
}
j++;
r = sd_journal_sendv(iovec, j);
if (r < 0)
log_error_errno(r, "Failed to send bootchart: %m");
}
};
char output_file[PATH_MAX];
char datestr[200];
time_t t = 0;
int r;
bool has_procfs = false;
parse_conf();
if (r < 0)
return EXIT_FAILURE;
if (r == 0)
return EXIT_SUCCESS;
/*
* If the kernel executed us through init=/usr/lib/systemd/systemd-bootchart, then
* fork:
* - parent execs executable specified via init_path[] (/usr/lib/systemd/systemd by default) as pid=1
* - child logs data
*/
if (getpid() == 1) {
if (fork()) {
/* parent */
}
}
argv[0][0] = '@';
/* start with empty ps LL */
if (!ps_first) {
log_oom();
return EXIT_FAILURE;
}
log_uptime();
if (graph_start < 0.0) {
log_error("Failed to setup graph start time.\n\n"
"The system uptime probably includes time that the system was suspended. "
"Use --rel to bypass this issue.");
return EXIT_FAILURE;
}
/* main program loop */
int res;
double sample_stop;
long newint_ns;
double elapsed;
double timeleft;
if (sampledata == NULL) {
log_oom();
return EXIT_FAILURE;
}
if (sysfd < 0)
if (!build) {
}
if (has_procfs) {
if (r < 0)
return EXIT_FAILURE;
} else {
/* wait for /proc to become available, discarding samples */
}
sample_stop = gettime_ns();
/*
* check if we have not consumed our entire timeslice. If we
* do, don't sleep and take a new sample right away.
* we'll lose all the missed samples and overrun our total
* time
*/
if (res) {
/* caught signal, probably HUP! */
break;
}
return EXIT_FAILURE;
}
} else {
overrun++;
/* calculate how many samples we lost and scrap them */
}
}
/* do some cleanup, close fd's */
}
if (!of) {
assert_se(r > 0);
}
if (!of) {
exit (EXIT_FAILURE);
}
if (of)
if (sysfd >= 0)
/* nitpic cleanups */
}
}
sampledata = head;
while (sampledata->link_prev) {
}
/* don't complain when overrun once, happens most commonly on 1st sample */
if (overrun > 1)
return 0;
}