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