chiark / gitweb /
doc: disable "make check" for gtk-doc
[elogind.git] / src / bootchart / bootchart.c
1 /*
2  * bootchart.c
3  *
4  * Copyright (C) 2009-2012 Intel Coproration
5  *
6  * Authors:
7  *   Auke Kok <auke-jan.h.kok@intel.com>
8  *
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
12  * of the License.
13  */
14
15
16 #include <sys/time.h>
17 #include <sys/types.h>
18 #include <sys/resource.h>
19 #include <stdio.h>
20 #include <signal.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <time.h>
25 #include <getopt.h>
26 #include <limits.h>
27 #include <errno.h>
28
29
30 #include "bootchart.h"
31 #include "util.h"
32
33 double graph_start;
34 double log_start;
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];
40 int pscount;
41 int cpus;
42 double interval;
43 FILE *of;
44 int overrun = 0;
45 static int exiting = 0;
46
47 /* graph defaults */
48 int entropy = 0;
49 int initcall = 1;
50 int relative;
51 int filter = 1;
52 int pss = 0;
53 int samples;
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 */
58
59 char init_path[PATH_MAX] = "/sbin/init";
60 char output_path[PATH_MAX] = "/var/log";
61
62 static struct rlimit rlim;
63
64 static void signal_handler(int sig)
65 {
66         if (sig++)
67                 sig--;
68         exiting = 1;
69 }
70
71
72 int main(int argc, char *argv[])
73 {
74         struct sigaction sig;
75         struct ps_struct *ps;
76         char output_file[PATH_MAX];
77         char datestr[200];
78         time_t t = 0;
79         FILE *f;
80         int gind;
81         int i;
82
83         rlim.rlim_cur = 4096;
84         rlim.rlim_max = 4096;
85         (void) setrlimit(RLIMIT_NOFILE, &rlim);
86
87         f = fopen("/etc/systemd/bootchart.conf", "r");
88         if (f) {
89                 char buf[256];
90                 char *key;
91                 char *val;
92
93                 while (fgets(buf, 80, f) != NULL) {
94                         char *c;
95
96                         c = strchr(buf, '\n');
97                         if (c) *c = 0; /* remove trailing \n */
98
99                         if (buf[0] == '#')
100                                 continue; /* comment line */
101
102                         key = strtok(buf, "=");
103                         if (!key)
104                                 continue;
105                         val = strtok(NULL, "=");
106                         if (!val)
107                                 continue;
108
109                         // todo: filter leading/trailing whitespace
110
111                         if (streq(key, "samples"))
112                                 len = atoi(val);
113                         if (streq(key, "freq"))
114                                 hz = atof(val);
115                         if (streq(key, "rel"))
116                                 relative = atoi(val);
117                         if (streq(key, "filter"))
118                                 filter = atoi(val);
119                         if (streq(key, "pss"))
120                                 pss = atoi(val);
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"))
126                                 scale_x = atof(val);
127                         if (streq(key, "scale_y"))
128                                 scale_y = atof(val);
129                         if (streq(key, "entropy"))
130                                 entropy = atoi(val);
131                 }
132                 fclose(f);
133         }
134
135         while (1) {
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'},
148                         {NULL, 0, NULL, 0}
149                 };
150
151                 gind = 0;
152
153                 i = getopt_long(argc, argv, "erpf:n:o:i:Fhx:y:", opts, &gind);
154                 if (i == -1)
155                         break;
156                 switch (i) {
157                 case 'r':
158                         relative = 1;
159                         break;
160                 case 'f':
161                         hz = atof(optarg);
162                         break;
163                 case 'F':
164                         filter = 0;
165                         break;
166                 case 'n':
167                         len = atoi(optarg);
168                         break;
169                 case 'o':
170                         strncpy(output_path, optarg, PATH_MAX - 1);
171                         break;
172                 case 'i':
173                         strncpy(init_path, optarg, PATH_MAX - 1);
174                         break;
175                 case 'p':
176                         pss = 1;
177                         break;
178                 case 'x':
179                         scale_x = atof(optarg);
180                         break;
181                 case 'y':
182                         scale_y = atof(optarg);
183                         break;
184                 case 'e':
185                         entropy = 1;
186                         break;
187                 case 'h':
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");
202                         exit (EXIT_SUCCESS);
203                         break;
204                 default:
205                         break;
206                 }
207         }
208
209         if (len > MAXSAMPLES) {
210                 fprintf(stderr, "Error: samples exceeds maximum\n");
211                 exit(EXIT_FAILURE);
212         }
213
214         if (hz <= 0.0) {
215                 fprintf(stderr, "Error: Frequency needs to be > 0\n");
216                 exit(EXIT_FAILURE);
217         }
218
219         /*
220          * If the kernel executed us through init=/sbin/bootchartd, then
221          * fork:
222          * - parent execs executable specified via init_path[] (/sbin/init by default) as pid=1
223          * - child logs data
224          */
225         if (getpid() == 1) {
226                 if (fork()) {
227                         /* parent */
228                         execl(init_path, init_path, NULL);
229                 }
230         }
231
232         /* start with empty ps LL */
233         ps_first = calloc(1, sizeof(struct ps_struct));
234         if (!ps_first) {
235                 perror("calloc(ps_struct)");
236                 exit(EXIT_FAILURE);
237         }
238
239         /* handle TERM/INT nicely */
240         memset(&sig, 0, sizeof(struct sigaction));
241         sig.sa_handler = signal_handler;
242         sigaction(SIGHUP, &sig, NULL);
243
244         interval = (1.0 / hz) * 1000000000.0;
245
246         log_uptime();
247
248         /* main program loop */
249         while (!exiting) {
250                 int res;
251                 double sample_stop;
252                 struct timespec req;
253                 time_t newint_s;
254                 long newint_ns;
255                 double elapsed;
256                 double timeleft;
257
258                 sampletime[samples] = gettime_ns();
259
260                 /* wait for /proc to become available, discarding samples */
261                 if (!(graph_start > 0.0))
262                         log_uptime();
263                 else
264                         log_sample(samples);
265
266                 sample_stop = gettime_ns();
267
268                 elapsed = (sample_stop - sampletime[samples]) * 1000000000.0;
269                 timeleft = interval - elapsed;
270
271                 newint_s = (time_t)(timeleft / 1000000000.0);
272                 newint_ns = (long)(timeleft - (newint_s * 1000000000.0));
273
274                 /*
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
278                  * time
279                  */
280                 if ((newint_ns > 0) || (newint_s > 0)) {
281                         req.tv_sec = newint_s;
282                         req.tv_nsec = newint_ns;
283
284                         res = nanosleep(&req, NULL);
285                         if (res) {
286                                 if (errno == EINTR) {
287                                         /* caught signal, probably HUP! */
288                                         break;
289                                 }
290                                 perror("nanosleep()");
291                                 exit (EXIT_FAILURE);
292                         }
293                 } else {
294                         overrun++;
295                         /* calculate how many samples we lost and scrap them */
296                         len = len + ((int)(newint_ns / interval));
297                 }
298
299                 samples++;
300
301                 if (samples > len)
302                         break;
303
304         }
305
306         /* do some cleanup, close fd's */
307         ps = ps_first;
308         while (ps->next_ps) {
309                 ps = ps->next_ps;
310                 if (ps->schedstat)
311                         close(ps->schedstat);
312                 if (ps->sched)
313                         close(ps->sched);
314                 if (ps->smaps)
315                         fclose(ps->smaps);
316         }
317         closedir(proc);
318
319         t = time(NULL);
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);
322
323         of = fopen(output_file, "w");
324         if (!of) {
325                 perror("open output_file");
326                 exit (EXIT_FAILURE);
327         }
328
329         svg_do();
330
331         fprintf(stderr, "bootchartd: Wrote %s\n", output_file);
332         fclose(of);
333
334         /* nitpic cleanups */
335         ps = ps_first;
336         while (ps->next_ps) {
337                 struct ps_struct *old = ps;
338                 ps = ps->next_ps;
339                 free(old->sample);
340                 free(old);
341         }
342         free(ps->sample);
343         free(ps);
344
345         /* don't complain when overrun once, happens most commonly on 1st sample */
346         if (overrun > 1)
347                 fprintf(stderr, "bootchartd: Warning: sample time overrun %i times\n", overrun);
348
349         return 0;
350 }