chiark / gitweb /
bootchart: Convert malloc/memset to calloc
[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;
79         FILE *f;
80         int gind;
81         int i;
82
83         memset(&t, 0, sizeof(time_t));
84
85         rlim.rlim_cur = 4096;
86         rlim.rlim_max = 4096;
87         (void) setrlimit(RLIMIT_NOFILE, &rlim);
88
89         f = fopen("/etc/systemd/bootchart.conf", "r");
90         if (f) {
91                 char buf[256];
92                 char *key;
93                 char *val;
94
95                 while (fgets(buf, 80, f) != NULL) {
96                         char *c;
97
98                         c = strchr(buf, '\n');
99                         if (c) *c = 0; /* remove trailing \n */
100
101                         if (buf[0] == '#')
102                                 continue; /* comment line */
103
104                         key = strtok(buf, "=");
105                         if (!key)
106                                 continue;
107                         val = strtok(NULL, "=");
108                         if (!val)
109                                 continue;
110
111                         // todo: filter leading/trailing whitespace
112
113                         if (streq(key, "samples"))
114                                 len = atoi(val);
115                         if (streq(key, "freq"))
116                                 hz = atof(val);
117                         if (streq(key, "rel"))
118                                 relative = atoi(val);
119                         if (streq(key, "filter"))
120                                 filter = atoi(val);
121                         if (streq(key, "pss"))
122                                 pss = atoi(val);
123                         if (streq(key, "output"))
124                                 strncpy(output_path, val, PATH_MAX - 1);
125                         if (streq(key, "init"))
126                                 strncpy(init_path, val, PATH_MAX - 1);
127                         if (streq(key, "scale_x"))
128                                 scale_x = atof(val);
129                         if (streq(key, "scale_y"))
130                                 scale_y = atof(val);
131                         if (streq(key, "entropy"))
132                                 entropy = atoi(val);
133                 }
134                 fclose(f);
135         }
136
137         while (1) {
138                 static struct option opts[] = {
139                         {"rel", 0, NULL, 'r'},
140                         {"freq", 1, NULL, 'f'},
141                         {"samples", 1, NULL, 'n'},
142                         {"pss", 0, NULL, 'p'},
143                         {"output", 1, NULL, 'o'},
144                         {"init", 1, NULL, 'i'},
145                         {"filter", 0, NULL, 'F'},
146                         {"help", 0, NULL, 'h'},
147                         {"scale-x", 1, NULL, 'x'},
148                         {"scale-y", 1, NULL, 'y'},
149                         {"entropy", 0, NULL, 'e'},
150                         {NULL, 0, NULL, 0}
151                 };
152
153                 gind = 0;
154
155                 i = getopt_long(argc, argv, "erpf:n:o:i:Fhx:y:", opts, &gind);
156                 if (i == -1)
157                         break;
158                 switch (i) {
159                 case 'r':
160                         relative = 1;
161                         break;
162                 case 'f':
163                         hz = atof(optarg);
164                         break;
165                 case 'F':
166                         filter = 0;
167                         break;
168                 case 'n':
169                         len = atoi(optarg);
170                         break;
171                 case 'o':
172                         strncpy(output_path, optarg, PATH_MAX - 1);
173                         break;
174                 case 'i':
175                         strncpy(init_path, optarg, PATH_MAX - 1);
176                         break;
177                 case 'p':
178                         pss = 1;
179                         break;
180                 case 'x':
181                         scale_x = atof(optarg);
182                         break;
183                 case 'y':
184                         scale_y = atof(optarg);
185                         break;
186                 case 'e':
187                         entropy = 1;
188                         break;
189                 case 'h':
190                         fprintf(stderr, "Usage: %s [OPTIONS]\n", argv[0]);
191                         fprintf(stderr, " --rel,     -r            Record time relative to recording\n");
192                         fprintf(stderr, " --freq,    -f N          Sample frequency [%f]\n", hz);
193                         fprintf(stderr, " --samples, -n N          Stop sampling at [%d] samples\n", len);
194                         fprintf(stderr, " --scale-x, -x N          Scale the graph horizontally [%f] \n", scale_x);
195                         fprintf(stderr, " --scale-y, -y N          Scale the graph vertically [%f] \n", scale_y);
196                         fprintf(stderr, " --pss,     -p            Enable PSS graph (CPU intensive)\n");
197                         fprintf(stderr, " --entropy, -e            Enable the entropy_avail graph\n");
198                         fprintf(stderr, " --output,  -o [PATH]     Path to output files [%s]\n", output_path);
199                         fprintf(stderr, " --init,    -i [PATH]     Path to init executable [%s]\n", init_path);
200                         fprintf(stderr, " --filter,  -F            Disable filtering of processes from the graph\n");
201                         fprintf(stderr, "                          that are of less importance or short-lived\n");
202                         fprintf(stderr, " --help,    -h            Display this message\n");
203                         fprintf(stderr, "See the installed README and bootchartd.conf.example for more information.\n");
204                         exit (EXIT_SUCCESS);
205                         break;
206                 default:
207                         break;
208                 }
209         }
210
211         if (len > MAXSAMPLES) {
212                 fprintf(stderr, "Error: samples exceeds maximum\n");
213                 exit(EXIT_FAILURE);
214         }
215
216         if (hz <= 0.0) {
217                 fprintf(stderr, "Error: Frequency needs to be > 0\n");
218                 exit(EXIT_FAILURE);
219         }
220
221         /*
222          * If the kernel executed us through init=/sbin/bootchartd, then
223          * fork:
224          * - parent execs executable specified via init_path[] (/sbin/init by default) as pid=1
225          * - child logs data
226          */
227         if (getpid() == 1) {
228                 if (fork()) {
229                         /* parent */
230                         execl(init_path, init_path, NULL);
231                 }
232         }
233
234         /* start with empty ps LL */
235         ps_first = calloc(1, sizeof(struct ps_struct));
236         if (!ps_first) {
237                 perror("calloc(ps_struct)");
238                 exit(EXIT_FAILURE);
239         }
240
241         /* handle TERM/INT nicely */
242         memset(&sig, 0, sizeof(struct sigaction));
243         sig.sa_handler = signal_handler;
244         sigaction(SIGHUP, &sig, NULL);
245
246         interval = (1.0 / hz) * 1000000000.0;
247
248         log_uptime();
249
250         /* main program loop */
251         while (!exiting) {
252                 int res;
253                 double sample_stop;
254                 struct timespec req;
255                 time_t newint_s;
256                 long newint_ns;
257                 double elapsed;
258                 double timeleft;
259
260                 sampletime[samples] = gettime_ns();
261
262                 /* wait for /proc to become available, discarding samples */
263                 if (!(graph_start > 0.0))
264                         log_uptime();
265                 else
266                         log_sample(samples);
267
268                 sample_stop = gettime_ns();
269
270                 elapsed = (sample_stop - sampletime[samples]) * 1000000000.0;
271                 timeleft = interval - elapsed;
272
273                 newint_s = (time_t)(timeleft / 1000000000.0);
274                 newint_ns = (long)(timeleft - (newint_s * 1000000000.0));
275
276                 /*
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
280                  * time
281                  */
282                 if ((newint_ns > 0) || (newint_s > 0)) {
283                         req.tv_sec = newint_s;
284                         req.tv_nsec = newint_ns;
285
286                         res = nanosleep(&req, NULL);
287                         if (res) {
288                                 if (errno == EINTR) {
289                                         /* caught signal, probably HUP! */
290                                         break;
291                                 }
292                                 perror("nanosleep()");
293                                 exit (EXIT_FAILURE);
294                         }
295                 } else {
296                         overrun++;
297                         /* calculate how many samples we lost and scrap them */
298                         len = len + ((int)(newint_ns / interval));
299                 }
300
301                 samples++;
302
303                 if (samples > len)
304                         break;
305
306         }
307
308         /* do some cleanup, close fd's */
309         ps = ps_first;
310         while (ps->next_ps) {
311                 ps = ps->next_ps;
312                 if (ps->schedstat)
313                         close(ps->schedstat);
314                 if (ps->sched)
315                         close(ps->sched);
316                 if (ps->smaps)
317                         fclose(ps->smaps);
318         }
319         closedir(proc);
320
321         t = time(NULL);
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);
324
325         of = fopen(output_file, "w");
326         if (!of) {
327                 perror("open output_file");
328                 exit (EXIT_FAILURE);
329         }
330
331         svg_do();
332
333         fprintf(stderr, "bootchartd: Wrote %s\n", output_file);
334         fclose(of);
335
336         /* nitpic cleanups */
337         ps = ps_first;
338         while (ps->next_ps) {
339                 struct ps_struct *old = ps;
340                 ps = ps->next_ps;
341                 free(old->sample);
342                 free(old);
343         }
344         free(ps->sample);
345         free(ps);
346
347         /* don't complain when overrun once, happens most commonly on 1st sample */
348         if (overrun > 1)
349                 fprintf(stderr, "bootchartd: Warning: sample time overrun %i times\n", overrun);
350
351         return 0;
352 }