chiark / gitweb /
78f0cab17833c4015f9117bbb67a6caf9fc9c1a6
[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                                 buf[n] = '\0';
187                                 entropy_avail[sample] = atoi(buf);
188                         }
189                 }
190         }
191
192         /* all the per-process stuff goes here */
193         if (!proc) {
194                 /* find all processes */
195                 proc = opendir("/proc");
196                 if (!proc)
197                         return;
198         } else {
199                 rewinddir(proc);
200         }
201
202         while ((ent = readdir(proc)) != NULL) {
203                 char filename[PATH_MAX];
204                 int pid;
205                 struct ps_struct *ps;
206
207                 if ((ent->d_name[0] < '0') || (ent->d_name[0] > '9'))
208                         continue;
209
210                 pid = atoi(ent->d_name);
211
212                 if (pid >= MAXPIDS)
213                         continue;
214
215                 ps = ps_first;
216                 while (ps->next_ps) {
217                         ps = ps->next_ps;
218                         if (ps->pid == pid)
219                                 break;
220                 }
221
222                 /* end of our LL? then append a new record */
223                 if (ps->pid != pid) {
224                         char t[32];
225                         struct ps_struct *parent;
226
227                         ps->next_ps = malloc(sizeof(struct ps_struct));
228                         if (!ps->next_ps) {
229                                 perror("malloc(ps_struct)");
230                                 exit (EXIT_FAILURE);
231                         }
232                         memset(ps->next_ps, 0, sizeof(struct ps_struct));
233                         ps = ps->next_ps;
234                         ps->pid = pid;
235
236                         ps->sample = malloc(sizeof(struct ps_sched_struct) * (len + 1));
237                         if (!ps->sample) {
238                                 perror("malloc(ps_struct)");
239                                 exit (EXIT_FAILURE);
240                         }
241                         memset(ps->sample, 0, sizeof(struct ps_sched_struct) * (len + 1));
242
243                         pscount++;
244
245                         /* mark our first sample */
246                         ps->first = sample;
247
248                         /* get name, start time */
249                         if (!ps->sched) {
250                                 sprintf(filename, "/proc/%d/sched", pid);
251                                 ps->sched = open(filename, O_RDONLY);
252                                 if (ps->sched == -1)
253                                         continue;
254                         }
255
256                         s = pread(ps->sched, buf, sizeof(buf) - 1, 0);
257                         if (s <= 0) {
258                                 close(ps->sched);
259                                 continue;
260                         }
261                         buf[s] = '\0';
262
263                         if (!sscanf(buf, "%s %*s %*s", key))
264                                 continue;
265
266                         strncpy(ps->name, key, 16);
267                         /* discard line 2 */
268                         m = bufgetline(buf);
269                         if (!m)
270                                 continue;
271
272                         m = bufgetline(m);
273                         if (!m)
274                                 continue;
275
276                         if (!sscanf(m, "%*s %*s %s", t))
277                                 continue;
278
279                         ps->starttime = strtod(t, NULL) / 1000.0;
280
281                         /* ppid */
282                         sprintf(filename, "/proc/%d/stat", pid);
283                         st = fopen(filename, "r");
284                         if (!st)
285                                 continue;
286                         if (!fscanf(st, "%*s %*s %*s %i", &p)) {
287                                 fclose(st);
288                                 continue;
289                         }
290                         fclose(st);
291                         ps->ppid = p;
292
293                         /*
294                          * setup child pointers
295                          *
296                          * these are used to paint the tree coherently later
297                          * each parent has a LL of children, and a LL of siblings
298                          */
299                         if (pid == 1)
300                                 continue; /* nothing to do for init atm */
301
302                         /* kthreadd has ppid=0, which breaks our tree ordering */
303                         if (ps->ppid == 0)
304                                 ps->ppid = 1;
305
306                         parent = ps_first;
307                         while ((parent->next_ps && parent->pid != ps->ppid))
308                                 parent = parent->next_ps;
309
310                         if ((!parent) || (parent->pid != ps->ppid)) {
311                                 /* orphan */
312                                 ps->ppid = 1;
313                                 parent = ps_first->next_ps;
314                         }
315
316                         ps->parent = parent;
317
318                         if (!parent->children) {
319                                 /* it's the first child */
320                                 parent->children = ps;
321                         } else {
322                                 /* walk all children and append */
323                                 struct ps_struct *children;
324                                 children = parent->children;
325                                 while (children->next)
326                                         children = children->next;
327                                 children->next = ps;
328                         }
329                 }
330
331                 /* else -> found pid, append data in ps */
332
333                 /* below here is all continuous logging parts - we get here on every
334                  * iteration */
335
336                 /* rt, wt */
337                 if (!ps->schedstat) {
338                         sprintf(filename, "/proc/%d/schedstat", pid);
339                         ps->schedstat = open(filename, O_RDONLY);
340                         if (ps->schedstat == -1)
341                                 continue;
342                 }
343                 s = pread(ps->schedstat, buf, sizeof(buf) - 1, 0);
344                 if (s <= 0) {
345                         /* clean up our file descriptors - assume that the process exited */
346                         close(ps->schedstat);
347                         if (ps->sched)
348                                 close(ps->sched);
349                         //if (ps->smaps)
350                         //        fclose(ps->smaps);
351                         continue;
352                 }
353                 buf[s] = '\0';
354
355                 if (!sscanf(buf, "%s %s %*s", rt, wt))
356                         continue;
357
358                 ps->last = sample;
359                 ps->sample[sample].runtime = atoll(rt);
360                 ps->sample[sample].waittime = atoll(wt);
361
362                 ps->total = (ps->sample[ps->last].runtime
363                                  - ps->sample[ps->first].runtime)
364                                  / 1000000000.0;
365
366                 if (!pss)
367                         goto catch_rename;
368                 /* Pss */
369                 if (!ps->smaps) {
370                         sprintf(filename, "/proc/%d/smaps", pid);
371                         ps->smaps = fopen(filename, "r");
372                         if (!ps->smaps)
373                                 continue;
374                         setvbuf(ps->smaps, smaps_buf, _IOFBF, sizeof(smaps_buf));
375                 } else {
376                         rewind(ps->smaps);
377                 }
378
379                 while (1) {
380                         int pss_kb;
381
382                         /* skip one line, this contains the object mapped */
383                         if (fgets(buf, sizeof(buf), ps->smaps) == NULL)
384                                 break;
385                         /* then there's a 28 char 14 line block */
386                         if (fread(buf, 1, 28 * 14, ps->smaps) != 28 * 14)
387                                 break;
388
389                         pss_kb = atoi(&buf[61]);
390                         ps->sample[sample].pss += pss_kb;
391                 }
392
393                 if (ps->sample[sample].pss > ps->pss_max)
394                         ps->pss_max = ps->sample[sample].pss;
395
396 catch_rename:
397                 /* catch process rename, try to randomize time */
398                 mod = (hz < 4.0) ? 4.0 : (hz / 4.0);
399                 if (((samples - ps->first) + pid) % (int)(mod) == 0) {
400
401                         /* re-fetch name */
402                         /* get name, start time */
403                         if (!ps->sched) {
404                                 sprintf(filename, "/proc/%d/sched", pid);
405                                 ps->sched = open(filename, O_RDONLY);
406                                 if (ps->sched == -1)
407                                         continue;
408                         }
409                         s = pread(ps->sched, buf, sizeof(buf) - 1, 0);
410                         if (s <= 0) {
411                                 /* clean up file descriptors */
412                                 close(ps->sched);
413                                 if (ps->schedstat)
414                                         close(ps->schedstat);
415                                 //if (ps->smaps)
416                                 //        fclose(ps->smaps);
417                                 continue;
418                         }
419                         buf[s] = '\0';
420
421                         if (!sscanf(buf, "%s %*s %*s", key))
422                                 continue;
423
424                         strncpy(ps->name, key, 16);
425                 }
426         }
427 }