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"
34 double sampletime
[MAXSAMPLES
];
35 struct ps_struct
*ps_first
;
36 struct block_stat_struct blockstat
[MAXSAMPLES
];
37 int entropy_avail
[MAXSAMPLES
];
38 struct cpu_stat_struct cpustat
[MAXCPUS
];
44 static int exiting
= 0;
53 int len
= 500; /* we record len+1 (1 start sample) */
54 double hz
= 25.0; /* 20 seconds log time */
55 double scale_x
= 100.0; /* 100px = 1sec */
56 double scale_y
= 20.0; /* 16px = 1 process bar */
58 char init_path
[PATH_MAX
] = "/sbin/init";
59 char output_path
[PATH_MAX
] = "/var/log";
61 static struct rlimit rlim
;
63 static void signal_handler(int sig
)
71 int main(int argc
, char *argv
[])
75 char output_file
[PATH_MAX
];
82 memset(&t
, 0, sizeof(time_t));
86 (void) setrlimit(RLIMIT_NOFILE
, &rlim
);
88 f
= fopen("/etc/systemd/bootchart.conf", "r");
94 while (fgets(buf
, 80, f
) != NULL
) {
97 c
= strchr(buf
, '\n');
98 if (c
) *c
= 0; /* remove trailing \n */
101 continue; /* comment line */
103 key
= strtok(buf
, "=");
106 val
= strtok(NULL
, "=");
110 // todo: filter leading/trailing whitespace
112 if (!strcmp(key
, "samples"))
114 if (!strcmp(key
, "freq"))
116 if (!strcmp(key
, "rel"))
117 relative
= atoi(val
);
118 if (!strcmp(key
, "filter"))
120 if (!strcmp(key
, "pss"))
122 if (!strcmp(key
, "output"))
123 strncpy(output_path
, val
, PATH_MAX
- 1);
124 if (!strcmp(key
, "init"))
125 strncpy(init_path
, val
, PATH_MAX
- 1);
126 if (!strcmp(key
, "scale_x"))
128 if (!strcmp(key
, "scale_y"))
130 if (!strcmp(key
, "entropy"))
137 static struct option opts
[] = {
138 {"rel", 0, NULL
, 'r'},
139 {"freq", 1, NULL
, 'f'},
140 {"samples", 1, NULL
, 'n'},
141 {"pss", 0, NULL
, 'p'},
142 {"output", 1, NULL
, 'o'},
143 {"init", 1, NULL
, 'i'},
144 {"filter", 0, NULL
, 'F'},
145 {"help", 0, NULL
, 'h'},
146 {"scale-x", 1, NULL
, 'x'},
147 {"scale-y", 1, NULL
, 'y'},
148 {"entropy", 0, NULL
, 'e'},
154 i
= getopt_long(argc
, argv
, "erpf:n:o:i:Fhx:y:", opts
, &gind
);
171 strncpy(output_path
, optarg
, PATH_MAX
- 1);
174 strncpy(init_path
, optarg
, PATH_MAX
- 1);
180 scale_x
= atof(optarg
);
183 scale_y
= atof(optarg
);
189 fprintf(stderr
, "Usage: %s [OPTIONS]\n", argv
[0]);
190 fprintf(stderr
, " --rel, -r Record time relative to recording\n");
191 fprintf(stderr
, " --freq, -f N Sample frequency [%f]\n", hz
);
192 fprintf(stderr
, " --samples, -n N Stop sampling at [%d] samples\n", len
);
193 fprintf(stderr
, " --scale-x, -x N Scale the graph horizontally [%f] \n", scale_x
);
194 fprintf(stderr
, " --scale-y, -y N Scale the graph vertically [%f] \n", scale_y
);
195 fprintf(stderr
, " --pss, -p Enable PSS graph (CPU intensive)\n");
196 fprintf(stderr
, " --entropy, -e Enable the entropy_avail graph\n");
197 fprintf(stderr
, " --output, -o [PATH] Path to output files [%s]\n", output_path
);
198 fprintf(stderr
, " --init, -i [PATH] Path to init executable [%s]\n", init_path
);
199 fprintf(stderr
, " --filter, -F Disable filtering of processes from the graph\n");
200 fprintf(stderr
, " that are of less importance or short-lived\n");
201 fprintf(stderr
, " --help, -h Display this message\n");
202 fprintf(stderr
, "See the installed README and bootchartd.conf.example for more information.\n");
210 if (len
> MAXSAMPLES
) {
211 fprintf(stderr
, "Error: samples exceeds maximum\n");
216 fprintf(stderr
, "Error: Frequency needs to be > 0\n");
221 * If the kernel executed us through init=/sbin/bootchartd, then
223 * - parent execs executable specified via init_path[] (/sbin/init by default) as pid=1
229 execl(init_path
, init_path
, NULL
);
233 /* start with empty ps LL */
234 ps_first
= malloc(sizeof(struct ps_struct
));
236 perror("malloc(ps_struct)");
239 memset(ps_first
, 0, sizeof(struct ps_struct
));
241 /* handle TERM/INT nicely */
242 memset(&sig
, 0, sizeof(struct sigaction
));
243 sig
.sa_handler
= signal_handler
;
244 sigaction(SIGHUP
, &sig
, NULL
);
246 interval
= (1.0 / hz
) * 1000000000.0;
250 /* main program loop */
260 sampletime
[samples
] = gettime_ns();
262 /* wait for /proc to become available, discarding samples */
263 if (!(graph_start
> 0.0))
268 sample_stop
= gettime_ns();
270 elapsed
= (sample_stop
- sampletime
[samples
]) * 1000000000.0;
271 timeleft
= interval
- elapsed
;
273 newint_s
= (time_t)(timeleft
/ 1000000000.0);
274 newint_ns
= (long)(timeleft
- (newint_s
* 1000000000.0));
277 * check if we have not consumed our entire timeslice. If we
278 * do, don't sleep and take a new sample right away.
279 * we'll lose all the missed samples and overrun our total
282 if ((newint_ns
> 0) || (newint_s
> 0)) {
283 req
.tv_sec
= newint_s
;
284 req
.tv_nsec
= newint_ns
;
286 res
= nanosleep(&req
, NULL
);
288 if (errno
== EINTR
) {
289 /* caught signal, probably HUP! */
292 perror("nanosleep()");
297 /* calculate how many samples we lost and scrap them */
298 len
= len
+ ((int)(newint_ns
/ interval
));
308 /* do some cleanup, close fd's */
310 while (ps
->next_ps
) {
313 close(ps
->schedstat
);
322 strftime(datestr
, sizeof(datestr
), "%Y%m%d-%H%M", localtime(&t
));
323 snprintf(output_file
, PATH_MAX
, "%s/bootchart-%s.svg", output_path
, datestr
);
325 of
= fopen(output_file
, "w");
327 perror("open output_file");
333 fprintf(stderr
, "bootchartd: Wrote %s\n", output_file
);
336 /* nitpic cleanups */
338 while (ps
->next_ps
) {
339 struct ps_struct
*old
= ps
;
347 /* don't complain when overrun once, happens most commonly on 1st sample */
349 fprintf(stderr
, "bootchartd: Warning: sample time overrun %i times\n", overrun
);