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