chiark / gitweb /
bootchart: merge bootchart
[elogind.git] / src / bootchart / log.c
1 /*
2  * log.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 #define _GNU_SOURCE 1
16 #include <unistd.h>
17 #include <stdlib.h>
18 #include <limits.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <dirent.h>
24 #include <fcntl.h>
25 #include <time.h>
26
27
28 #include "bootchart.h"
29
30 /*
31  * Alloc a static 4k buffer for stdio - primarily used to increase
32  * PSS buffering from the default 1k stdin buffer to reduce
33  * read() overhead.
34  */
35 static char smaps_buf[4096];
36 DIR *proc;
37
38
39 double gettime_ns(void)
40 {
41         struct timespec now;
42
43         clock_gettime(CLOCK_MONOTONIC, &now);
44
45         return (now.tv_sec + (now.tv_nsec / 1000000000.0));
46 }
47
48
49 void log_uptime(void)
50 {
51         FILE *f;
52         char str[32];
53         double uptime;
54
55         f = fopen("/proc/uptime", "r");
56         if (!f)
57                 return;
58         if (!fscanf(f, "%s %*s", str)) {
59                 fclose(f);
60                 return;
61         }
62         fclose(f);
63         uptime = strtod(str, NULL);
64
65         log_start = gettime_ns();
66
67         /* start graph at kernel boot time */
68         if (relative)
69                 graph_start = log_start;
70         else
71                 graph_start = log_start - uptime;
72 }
73
74
75 static char *bufgetline(char *buf)
76 {
77         char *c;
78
79         if (!buf)
80                 return NULL;
81
82         c = strchr(buf, '\n');
83         if (c)
84                 c++;
85         return c;
86 }
87
88
89 void log_sample(int sample)
90 {
91         static int vmstat;
92         static int schedstat;
93         FILE *st;
94         char buf[4095];
95         char key[256];
96         char val[256];
97         char rt[256];
98         char wt[256];
99         char *m;
100         int c;
101         int p;
102         int mod;
103         static int e_fd;
104         ssize_t s;
105         ssize_t n;
106         struct dirent *ent;
107
108         if (!vmstat) {
109                 /* block stuff */
110                 vmstat = open("/proc/vmstat", O_RDONLY);
111                 if (vmstat == -1) {
112                         perror("open /proc/vmstat");
113                         exit (EXIT_FAILURE);
114                 }
115         }
116
117         n = pread(vmstat, buf, sizeof(buf) - 1, 0);
118         if (n <= 0) {
119                 close(vmstat);
120                 return;
121         }
122         buf[n] = '\0';
123
124         m = buf;
125         while (m) {
126                 if (sscanf(m, "%s %s", key, val) < 2)
127                         goto vmstat_next;
128                 if (!strcmp(key, "pgpgin"))
129                         blockstat[sample].bi = atoi(val);
130                 if (!strcmp(key, "pgpgout")) {
131                         blockstat[sample].bo = atoi(val);
132                         break;
133                 }
134 vmstat_next:
135                 m = bufgetline(m);
136                 if (!m)
137                         break;
138         }
139
140         if (!schedstat) {
141                 /* overall CPU utilization */
142                 schedstat = open("/proc/schedstat", O_RDONLY);
143                 if (schedstat == -1) {
144                         perror("open /proc/schedstat");
145                         exit (EXIT_FAILURE);
146                 }
147         }
148
149         n = pread(schedstat, buf, sizeof(buf) - 1, 0);
150         if (n <= 0) {
151                 close(schedstat);
152                 return;
153         }
154         buf[n] = '\0';
155
156         m = buf;
157         while (m) {
158                 if (sscanf(m, "%s %*s %*s %*s %*s %*s %*s %s %s", key, rt, wt) < 3)
159                         goto schedstat_next;
160
161                 if (strstr(key, "cpu")) {
162                         c = atoi((const char*)(key+3));
163                         if (c > MAXCPUS)
164                                 /* Oops, we only have room for MAXCPUS data */
165                                 break;
166                         cpustat[c].sample[sample].runtime = atoll(rt);
167                         cpustat[c].sample[sample].waittime = atoll(wt);
168
169                         if (c == cpus)
170                                 cpus = c + 1;
171                 }
172 schedstat_next:
173                 m = bufgetline(m);
174                 if (!m)
175                         break;
176         }
177
178         if (entropy) {
179                 if (!e_fd) {
180                         e_fd = open("/proc/sys/kernel/random/entropy_avail", O_RDONLY);
181                 }
182
183                 if (e_fd) {
184                         n = pread(e_fd, buf, sizeof(buf) - 1, 0);
185                         if (n > 0)
186                                 entropy_avail[sample] = atoi(buf);
187                 }
188         }
189
190         /* all the per-process stuff goes here */
191         if (!proc) {
192                 /* find all processes */
193                 proc = opendir("/proc");
194                 if (!proc)
195                         return;
196         } else {
197                 rewinddir(proc);
198         }
199
200         while ((ent = readdir(proc)) != NULL) {
201                 char filename[PATH_MAX];
202                 int pid;
203                 struct ps_struct *ps;
204
205                 if ((ent->d_name[0] < '0') || (ent->d_name[0] > '9'))
206                         continue;
207
208                 pid = atoi(ent->d_name);
209
210                 if (pid >= MAXPIDS)
211                         continue;
212
213                 ps = ps_first;
214                 while (ps->next_ps) {
215                         ps = ps->next_ps;
216                         if (ps->pid == pid)
217                                 break;
218                 }
219
220                 /* end of our LL? then append a new record */
221                 if (ps->pid != pid) {
222                         char t[32];
223                         struct ps_struct *parent;
224
225                         ps->next_ps = malloc(sizeof(struct ps_struct));
226                         if (!ps->next_ps) {
227                                 perror("malloc(ps_struct)");
228                                 exit (EXIT_FAILURE);
229                         }
230                         memset(ps->next_ps, 0, sizeof(struct ps_struct));
231                         ps = ps->next_ps;
232                         ps->pid = pid;
233
234                         ps->sample = malloc(sizeof(struct ps_sched_struct) * (len + 1));
235                         if (!ps->sample) {
236                                 perror("malloc(ps_struct)");
237                                 exit (EXIT_FAILURE);
238                         }
239                         memset(ps->sample, 0, sizeof(struct ps_sched_struct) * (len + 1));
240
241                         pscount++;
242
243                         /* mark our first sample */
244                         ps->first = sample;
245
246                         /* get name, start time */
247                         if (!ps->sched) {
248                                 sprintf(filename, "/proc/%d/sched", pid);
249                                 ps->sched = open(filename, O_RDONLY);
250                                 if (ps->sched == -1)
251                                         continue;
252                         }
253
254                         s = pread(ps->sched, buf, sizeof(buf) - 1, 0);
255                         if (s <= 0) {
256                                 close(ps->sched);
257                                 continue;
258                         }
259
260                         if (!sscanf(buf, "%s %*s %*s", key))
261                                 continue;
262
263                         strncpy(ps->name, key, 16);
264                         /* discard line 2 */
265                         m = bufgetline(buf);
266                         if (!m)
267                                 continue;
268
269                         m = bufgetline(m);
270                         if (!m)
271                                 continue;
272
273                         if (!sscanf(m, "%*s %*s %s", t))
274                                 continue;
275
276                         ps->starttime = strtod(t, NULL) / 1000.0;
277
278                         /* ppid */
279                         sprintf(filename, "/proc/%d/stat", pid);
280                         st = fopen(filename, "r");
281                         if (!st)
282                                 continue;
283                         if (!fscanf(st, "%*s %*s %*s %i", &p)) {
284                                 fclose(st);
285                                 continue;
286                         }
287                         fclose(st);
288                         ps->ppid = p;
289
290                         /*
291                          * setup child pointers
292                          *
293                          * these are used to paint the tree coherently later
294                          * each parent has a LL of children, and a LL of siblings
295                          */
296                         if (pid == 1)
297                                 continue; /* nothing to do for init atm */
298
299                         /* kthreadd has ppid=0, which breaks our tree ordering */
300                         if (ps->ppid == 0)
301                                 ps->ppid = 1;
302
303                         parent = ps_first;
304                         while ((parent->next_ps && parent->pid != ps->ppid))
305                                 parent = parent->next_ps;
306
307                         if ((!parent) || (parent->pid != ps->ppid)) {
308                                 /* orphan */
309                                 ps->ppid = 1;
310                                 parent = ps_first->next_ps;
311                         }
312
313                         ps->parent = parent;
314
315                         if (!parent->children) {
316                                 /* it's the first child */
317                                 parent->children = ps;
318                         } else {
319                                 /* walk all children and append */
320                                 struct ps_struct *children;
321                                 children = parent->children;
322                                 while (children->next)
323                                         children = children->next;
324                                 children->next = ps;
325                         }
326                 }
327
328                 /* else -> found pid, append data in ps */
329
330                 /* below here is all continuous logging parts - we get here on every
331                  * iteration */
332
333                 /* rt, wt */
334                 if (!ps->schedstat) {
335                         sprintf(filename, "/proc/%d/schedstat", pid);
336                         ps->schedstat = open(filename, O_RDONLY);
337                         if (ps->schedstat == -1)
338                                 continue;
339                 }
340
341                 if (pread(ps->schedstat, buf, sizeof(buf) - 1, 0) <= 0) {
342                         /* clean up our file descriptors - assume that the process exited */
343                         close(ps->schedstat);
344                         if (ps->sched)
345                                 close(ps->sched);
346                         //if (ps->smaps)
347                         //      fclose(ps->smaps);
348                         continue;
349                 }
350                 if (!sscanf(buf, "%s %s %*s", rt, wt))
351                         continue;
352
353                 ps->last = sample;
354                 ps->sample[sample].runtime = atoll(rt);
355                 ps->sample[sample].waittime = atoll(wt);
356
357                 ps->total = (ps->sample[ps->last].runtime
358                                  - ps->sample[ps->first].runtime)
359                                  / 1000000000.0;
360
361                 if (!pss)
362                         goto catch_rename;
363                 /* Pss */
364                 if (!ps->smaps) {
365                         sprintf(filename, "/proc/%d/smaps", pid);
366                         ps->smaps = fopen(filename, "r");
367                         setvbuf(ps->smaps, smaps_buf, _IOFBF, sizeof(smaps_buf));
368                         if (!ps->smaps)
369                                 continue;
370                 } else {
371                         rewind(ps->smaps);
372                 }
373
374                 while (1) {
375                         int pss_kb;
376
377                         /* skip one line, this contains the object mapped */
378                         if (fgets(buf, sizeof(buf), ps->smaps) == NULL)
379                                 break;
380                         /* then there's a 28 char 14 line block */
381                         if (fread(buf, 1, 28 * 14, ps->smaps) != 28 * 14)
382                                 break;
383
384                         pss_kb = atoi(&buf[61]);
385                         ps->sample[sample].pss += pss_kb;
386                 }
387
388                 if (ps->sample[sample].pss > ps->pss_max)
389                         ps->pss_max = ps->sample[sample].pss;
390
391 catch_rename:
392                 /* catch process rename, try to randomize time */
393                 mod = (hz < 4.0) ? 4.0 : (hz / 4.0);
394                 if (((samples - ps->first) + pid) % (int)(mod) == 0) {
395
396                         /* re-fetch name */
397                         /* get name, start time */
398                         if (!ps->sched) {
399                                 sprintf(filename, "/proc/%d/sched", pid);
400                                 ps->sched = open(filename, O_RDONLY);
401                                 if (ps->sched == -1)
402                                         continue;
403                         }
404                         if (pread(ps->sched, buf, sizeof(buf) - 1, 0) <= 0) {
405                                 /* clean up file descriptors */
406                                 close(ps->sched);
407                                 if (ps->schedstat)
408                                         close(ps->schedstat);
409                                 //if (ps->smaps)
410                                 //      fclose(ps->smaps);
411                                 continue;
412                         }
413
414                         if (!sscanf(buf, "%s %*s %*s", key))
415                                 continue;
416
417                         strncpy(ps->name, key, 16);
418                 }
419         }
420 }