4 * Copyright (C) 2009-2012 Intel Coproration
7 * Auke Kok <auke-jan.h.kok@intel.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; version 2
17 #include <sys/types.h>
18 #include <sys/resource.h>
30 #include "bootchart.h"
35 double sampletime[MAXSAMPLES];
36 struct ps_struct *ps_first;
37 struct block_stat_struct blockstat[MAXSAMPLES];
38 int entropy_avail[MAXSAMPLES];
39 struct cpu_stat_struct cpustat[MAXCPUS];
45 static int exiting = 0;
54 int len = 500; /* we record len+1 (1 start sample) */
55 double hz = 25.0; /* 20 seconds log time */
56 double scale_x = 100.0; /* 100px = 1sec */
57 double scale_y = 20.0; /* 16px = 1 process bar */
59 char init_path[PATH_MAX] = "/sbin/init";
60 char output_path[PATH_MAX] = "/var/log";
62 static struct rlimit rlim;
64 static void signal_handler(int sig)
72 int main(int argc, char *argv[])
76 char output_file[PATH_MAX];
85 (void) setrlimit(RLIMIT_NOFILE, &rlim);
87 f = fopen("/etc/systemd/bootchart.conf", "r");
93 while (fgets(buf, 80, f) != NULL) {
96 c = strchr(buf, '\n');
97 if (c) *c = 0; /* remove trailing \n */
100 continue; /* comment line */
102 key = strtok(buf, "=");
105 val = strtok(NULL, "=");
109 // todo: filter leading/trailing whitespace
111 if (streq(key, "samples"))
113 if (streq(key, "freq"))
115 if (streq(key, "rel"))
116 relative = atoi(val);
117 if (streq(key, "filter"))
119 if (streq(key, "pss"))
121 if (streq(key, "output"))
122 strncpy(output_path, val, PATH_MAX - 1);
123 if (streq(key, "init"))
124 strncpy(init_path, val, PATH_MAX - 1);
125 if (streq(key, "scale_x"))
127 if (streq(key, "scale_y"))
129 if (streq(key, "entropy"))
136 static struct option opts[] = {
137 {"rel", 0, NULL, 'r'},
138 {"freq", 1, NULL, 'f'},
139 {"samples", 1, NULL, 'n'},
140 {"pss", 0, NULL, 'p'},
141 {"output", 1, NULL, 'o'},
142 {"init", 1, NULL, 'i'},
143 {"filter", 0, NULL, 'F'},
144 {"help", 0, NULL, 'h'},
145 {"scale-x", 1, NULL, 'x'},
146 {"scale-y", 1, NULL, 'y'},
147 {"entropy", 0, NULL, 'e'},
153 i = getopt_long(argc, argv, "erpf:n:o:i:Fhx:y:", opts, &gind);
170 strncpy(output_path, optarg, PATH_MAX - 1);
173 strncpy(init_path, optarg, PATH_MAX - 1);
179 scale_x = atof(optarg);
182 scale_y = atof(optarg);
188 fprintf(stderr, "Usage: %s [OPTIONS]\n", argv[0]);
189 fprintf(stderr, " --rel, -r Record time relative to recording\n");
190 fprintf(stderr, " --freq, -f N Sample frequency [%f]\n", hz);
191 fprintf(stderr, " --samples, -n N Stop sampling at [%d] samples\n", len);
192 fprintf(stderr, " --scale-x, -x N Scale the graph horizontally [%f] \n", scale_x);
193 fprintf(stderr, " --scale-y, -y N Scale the graph vertically [%f] \n", scale_y);
194 fprintf(stderr, " --pss, -p Enable PSS graph (CPU intensive)\n");
195 fprintf(stderr, " --entropy, -e Enable the entropy_avail graph\n");
196 fprintf(stderr, " --output, -o [PATH] Path to output files [%s]\n", output_path);
197 fprintf(stderr, " --init, -i [PATH] Path to init executable [%s]\n", init_path);
198 fprintf(stderr, " --filter, -F Disable filtering of processes from the graph\n");
199 fprintf(stderr, " that are of less importance or short-lived\n");
200 fprintf(stderr, " --help, -h Display this message\n");
201 fprintf(stderr, "See the installed README and bootchartd.conf.example for more information.\n");
209 if (len > MAXSAMPLES) {
210 fprintf(stderr, "Error: samples exceeds maximum\n");
215 fprintf(stderr, "Error: Frequency needs to be > 0\n");
220 * If the kernel executed us through init=/sbin/bootchartd, then
222 * - parent execs executable specified via init_path[] (/sbin/init by default) as pid=1
228 execl(init_path, init_path, NULL);
232 /* start with empty ps LL */
233 ps_first = calloc(1, sizeof(struct ps_struct));
235 perror("calloc(ps_struct)");
239 /* handle TERM/INT nicely */
240 memset(&sig, 0, sizeof(struct sigaction));
241 sig.sa_handler = signal_handler;
242 sigaction(SIGHUP, &sig, NULL);
244 interval = (1.0 / hz) * 1000000000.0;
248 /* main program loop */
258 sampletime[samples] = gettime_ns();
260 /* wait for /proc to become available, discarding samples */
261 if (!(graph_start > 0.0))
266 sample_stop = gettime_ns();
268 elapsed = (sample_stop - sampletime[samples]) * 1000000000.0;
269 timeleft = interval - elapsed;
271 newint_s = (time_t)(timeleft / 1000000000.0);
272 newint_ns = (long)(timeleft - (newint_s * 1000000000.0));
275 * check if we have not consumed our entire timeslice. If we
276 * do, don't sleep and take a new sample right away.
277 * we'll lose all the missed samples and overrun our total
280 if ((newint_ns > 0) || (newint_s > 0)) {
281 req.tv_sec = newint_s;
282 req.tv_nsec = newint_ns;
284 res = nanosleep(&req, NULL);
286 if (errno == EINTR) {
287 /* caught signal, probably HUP! */
290 perror("nanosleep()");
295 /* calculate how many samples we lost and scrap them */
296 len = len + ((int)(newint_ns / interval));
306 /* do some cleanup, close fd's */
308 while (ps->next_ps) {
311 close(ps->schedstat);
320 strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M", localtime(&t));
321 snprintf(output_file, PATH_MAX, "%s/bootchart-%s.svg", output_path, datestr);
323 of = fopen(output_file, "w");
325 perror("open output_file");
331 fprintf(stderr, "bootchartd: Wrote %s\n", output_file);
334 /* nitpic cleanups */
336 while (ps->next_ps) {
337 struct ps_struct *old = ps;
345 /* don't complain when overrun once, happens most commonly on 1st sample */
347 fprintf(stderr, "bootchartd: Warning: sample time overrun %i times\n", overrun);