4 * Copyright (c) 2009 Intel Coproration
6 * Auke Kok <auke-jan.h.kok@intel.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; version 2
16 #include <sys/types.h>
17 #include <sys/resource.h>
28 #include "bootchart.h"
32 double sampletime[MAXSAMPLES];
33 struct ps_struct *ps[MAXPIDS]; /* ll */
34 struct block_stat_struct blockstat[MAXSAMPLES];
35 struct cpu_stat_struct cpustat[MAXCPUS];
41 static int exiting = 0;
49 int len = 500; /* we record len+1 (1 start sample) */
50 int hz = 25; /* 20 seconds log time */
51 int scale_x = 100; /* 100px = 1sec */
52 int scale_y = 20; /* 16px = 1 process bar */
54 char init_path[PATH_MAX] = "/sbin/init";
55 char output_path[PATH_MAX] = "/var/log";
57 static struct rlimit rlim;
59 static void signal_handler(int sig)
67 int main(int argc, char *argv[])
70 char output_file[PATH_MAX];
76 memset(&t, 0, sizeof(time_t));
80 (void) setrlimit(RLIMIT_NOFILE, &rlim);
82 f = fopen("/etc/bootchartd.conf", "r");
88 while (fgets(buf, 80, f) != NULL) {
91 c = strchr(buf, '\n');
92 if (c) *c = 0; /* remove trailing \n */
95 continue; /* comment line */
97 key = strtok(buf, "=");
100 val = strtok(NULL, "=");
104 // todo: filter leading/trailing whitespace
106 if (!strcmp(key, "samples"))
108 if (!strcmp(key, "freq"))
110 if (!strcmp(key, "rel"))
111 relative = atoi(val);
112 if (!strcmp(key, "filter"))
114 if (!strcmp(key, "pss"))
116 if (!strcmp(key, "output"))
117 strncpy(output_path, val, PATH_MAX - 1);
118 if (!strcmp(key, "init"))
119 strncpy(init_path, val, PATH_MAX - 1);
120 if (!strcmp(key, "scale_x"))
122 if (!strcmp(key, "scale_y"))
129 static struct option opts[] = {
130 {"rel", 0, NULL, 'r'},
131 {"freq", 1, NULL, 'f'},
132 {"samples", 1, NULL, 'n'},
133 {"pss", 0, NULL, 'p'},
134 {"output", 1, NULL, 'o'},
135 {"init", 1, NULL, 'i'},
136 {"filter", 0, NULL, 'F'},
137 {"help", 0, NULL, 'h'},
138 {"scale-x", 1, NULL, 'x'},
139 {"scale-y", 1, NULL, 'y'},
145 c = getopt_long(argc, argv, "rpf:n:o:i:Fhx:y:", opts, &index);
162 strncpy(output_path, optarg, PATH_MAX - 1);
165 strncpy(init_path, optarg, PATH_MAX - 1);
171 scale_x = atoi(optarg);
174 scale_y = atoi(optarg);
177 fprintf(stderr, "Usage: %s [OPTIONS]\n", argv[0]);
178 fprintf(stderr, " --rel, -r Record time relative to recording\n");
179 fprintf(stderr, " --freq, -f N Sample frequency [%d]\n", hz);
180 fprintf(stderr, " --samples, -n N Stop sampling at [%d] samples\n", len);
181 fprintf(stderr, " --scale-x, -x N Scale the graph horizontally [%d] \n", scale_x);
182 fprintf(stderr, " --scale-y, -y N Scale the graph vertically [%d] \n", scale_y);
183 fprintf(stderr, " --pss, -p Enable PSS graph (CPU intensive)\n");
184 fprintf(stderr, " --output, -o [PATH] Path to output files [%s]\n", output_path);
185 fprintf(stderr, " --init, -i [PATH] Path to init executable [%s]\n", init_path);
186 fprintf(stderr, " --filter, -F Disable filtering of processes from the graph\n");
187 fprintf(stderr, " that are of less importance or short-lived\n");
188 fprintf(stderr, " --help, -h Display this message\n");
189 fprintf(stderr, "See the installed README and bootchartd.conf.example for more information.\n");
197 if (len > MAXSAMPLES) {
198 fprintf(stderr, "Error: samples exceeds maximum\n");
203 * If the kernel executed us through init=/sbin/bootchartd, then
205 * - parent execs executable specified via init_path[] (/sbin/init by default) as pid=1
211 execl(init_path, init_path, NULL);
215 /* handle TERM/INT nicely */
216 memset(&sig, 0, sizeof(struct sigaction));
217 sig.sa_handler = signal_handler;
218 sigaction(SIGTERM, &sig, NULL);
219 sigaction(SIGINT, &sig, NULL);
221 interval = (1.0 / hz) * 1000000000.0;
225 /* main program loop */
232 sampletime[samples] = gettime_ns();
234 /* wait for /proc to become available, discarding samples */
240 sample_stop = gettime_ns();
243 newint_ns = interval - ((sample_stop - sampletime[samples]) * 1000000000);
246 * check if we have not consumed our entire timeslice. If we
247 * do, don't sleep and take a new sample right away.
248 * we'll lose all the missed samples and overrun our total
252 req.tv_nsec = newint_ns;
254 res = nanosleep(&req, NULL);
256 perror("nanosleep()");
261 /* calculate how many samples we lost and scrap them */
262 len = len + ((int)(newint_ns / interval));
272 /* do some cleanup, close fd's */
273 for ( i = 0; i < MAXPIDS ; i++) {
277 if (ps[i]->schedstat)
278 close(ps[i]->schedstat);
282 fclose(ps[i]->smaps);
287 strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M", localtime(&t));
288 sprintf(output_file, "%s/bootchart-%s.svg", output_path, datestr);
290 of = fopen(output_file, "w");
292 perror("open output_file");
298 fprintf(stderr, "bootchartd: Wrote %s\n", output_file);
301 /* don't complain when overrun once, happens most commonly on 1st sample */
303 fprintf(stderr, "bootchartd: Warning: sample time overrun %i times\n", overrun);