summaryrefslogtreecommitdiff
path: root/src/bootchart/log.c
blob: c697121814e7d6e848f01c5a6aaf0bf820f7c5dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
 * log.c
 *
 * Copyright (C) 2009-2012 Intel Coproration
 *
 * Authors:
 *   Auke Kok <auke-jan.h.kok@intel.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2
 * of the License.
 */

#define _GNU_SOURCE 1
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <fcntl.h>
#include <time.h>


#include "bootchart.h"
#include "util.h"

/*
 * Alloc a static 4k buffer for stdio - primarily used to increase
 * PSS buffering from the default 1k stdin buffer to reduce
 * read() overhead.
 */
static char smaps_buf[4096];
DIR *proc;


double gettime_ns(void)
{
        struct timespec now;

        clock_gettime(CLOCK_MONOTONIC, &now);

        return (now.tv_sec + (now.tv_nsec / 1000000000.0));
}


void log_uptime(void)
{
        FILE *f;
        char str[32];
        double uptime;

        f = fopen("/proc/uptime", "r");
        if (!f)
                return;
        if (!fscanf(f, "%s %*s", str)) {
                fclose(f);
                return;
        }
        fclose(f);
        uptime = strtod(str, NULL);

        log_start = gettime_ns();

        /* start graph at kernel boot time */
        if (relative)
                graph_start = log_start;
        else
                graph_start = log_start - uptime;
}


static char *bufgetline(char *buf)
{
        char *c;

        if (!buf)
                return NULL;

        c = strchr(buf, '\n');
        if (c)
                c++;
        return c;
}


void log_sample(int sample)
{
        static int vmstat;
        static int schedstat;
        FILE *st;
        char buf[4095];
        char key[256];
        char val[256];
        char rt[256];
        char wt[256];
        char *m;
        int c;
        int p;
        int mod;
        static int e_fd;
        ssize_t s;
        ssize_t n;
        struct dirent *ent;

        if (!vmstat) {
                /* block stuff */
                vmstat = open("/proc/vmstat", O_RDONLY);
                if (vmstat == -1) {
                        perror("open /proc/vmstat");
                        exit (EXIT_FAILURE);
                }
        }

        n = pread(vmstat, buf, sizeof(buf) - 1, 0);
        if (n <= 0) {
                close(vmstat);
                return;
        }
        buf[n] = '\0';

        m = buf;
        while (m) {
                if (sscanf(m, "%s %s", key, val) < 2)
                        goto vmstat_next;
                if (streq(key, "pgpgin"))
                        blockstat[sample].bi = atoi(val);
                if (streq(key, "pgpgout")) {
                        blockstat[sample].bo = atoi(val);
                        break;
                }
vmstat_next:
                m = bufgetline(m);
                if (!m)
                        break;
        }

        if (!schedstat) {
                /* overall CPU utilization */
                schedstat = open("/proc/schedstat", O_RDONLY);
                if (schedstat == -1) {
                        perror("open /proc/schedstat");
                        exit (EXIT_FAILURE);
                }
        }

        n = pread(schedstat, buf, sizeof(buf) - 1, 0);
        if (n <= 0) {
                close(schedstat);
                return;
        }
        buf[n] = '\0';

        m = buf;
        while (m) {
                if (sscanf(m, "%s %*s %*s %*s %*s %*s %*s %s %s", key, rt, wt) < 3)
                        goto schedstat_next;

                if (strstr(key, "cpu")) {
                        c = atoi((const char*)(key+3));
                        if (c > MAXCPUS)
                                /* Oops, we only have room for MAXCPUS data */
                                break;
                        cpustat[c].sample[sample].runtime = atoll(rt);
                        cpustat[c].sample[sample].waittime = atoll(wt);

                        if (c == cpus)
                                cpus = c + 1;
                }
schedstat_next:
                m = bufgetline(m);
                if (!m)
                        break;
        }

        if (entropy) {
                if (!e_fd) {
                        e_fd = open("/proc/sys/kernel/random/entropy_avail", O_RDONLY);
                }

                if (e_fd) {
                        n = pread(e_fd, buf, sizeof(buf) - 1, 0);
                        if (n > 0) {
                                buf[n] = '\0';
                                entropy_avail[sample] = atoi(buf);
                        }
                }
        }

        /* all the per-process stuff goes here */
        if (!proc) {
                /* find all processes */
                proc = opendir("/proc");
                if (!proc)
                        return;
        } else {
                rewinddir(proc);
        }

        while ((ent = readdir(proc)) != NULL) {
                char filename[PATH_MAX];
                int pid;
                struct ps_struct *ps;

                if ((ent->d_name[0] < '0') || (ent->d_name[0] > '9'))
                        continue;

                pid = atoi(ent->d_name);

                if (pid >= MAXPIDS)
                        continue;

                ps = ps_first;
                while (ps->next_ps) {
                        ps = ps->next_ps;
                        if (ps->pid == pid)
                                break;
                }

                /* end of our LL? then append a new record */
                if (ps->pid != pid) {
                        char t[32];
                        struct ps_struct *parent;

                        ps->next_ps = malloc(sizeof(struct ps_struct));
                        if (!ps->next_ps) {
                                perror("malloc(ps_struct)");
                                exit (EXIT_FAILURE);
                        }
                        memset(ps->next_ps, 0, sizeof(struct ps_struct));
                        ps = ps->next_ps;
                        ps->pid = pid;

                        ps->sample = malloc(sizeof(struct ps_sched_struct) * (len + 1));
                        if (!ps->sample) {
                                perror("malloc(ps_struct)");
                                exit (EXIT_FAILURE);
                        }
                        memset(ps->sample, 0, sizeof(struct ps_sched_struct) * (len + 1));

                        pscount++;

                        /* mark our first sample */
                        ps->first = sample;

                        /* get name, start time */
                        if (!ps->sched) {
                                sprintf(filename, "/proc/%d/sched", pid);
                                ps->sched = open(filename, O_RDONLY);
                                if (ps->sched == -1)
                                        continue;
                        }

                        s = pread(ps->sched, buf, sizeof(buf) - 1, 0);
                        if (s <= 0) {
                                close(ps->sched);
                                continue;
                        }
                        buf[s] = '\0';

                        if (!sscanf(buf, "%s %*s %*s", key))
                                continue;

                        strncpy(ps->name, key, 16);
                        /* discard line 2 */
                        m = bufgetline(buf);
                        if (!m)
                                continue;

                        m = bufgetline(m);
                        if (!m)
                                continue;

                        if (!sscanf(m, "%*s %*s %s", t))
                                continue;

                        ps->starttime = strtod(t, NULL) / 1000.0;

                        /* ppid */
                        sprintf(filename, "/proc/%d/stat", pid);
                        st = fopen(filename, "r");
                        if (!st)
                                continue;
                        if (!fscanf(st, "%*s %*s %*s %i", &p)) {
                                fclose(st);
                                continue;
                        }
                        fclose(st);
                        ps->ppid = p;

                        /*
                         * setup child pointers
                         *
                         * these are used to paint the tree coherently later
                         * each parent has a LL of children, and a LL of siblings
                         */
                        if (pid == 1)
                                continue; /* nothing to do for init atm */

                        /* kthreadd has ppid=0, which breaks our tree ordering */
                        if (ps->ppid == 0)
                                ps->ppid = 1;

                        parent = ps_first;
                        while ((parent->next_ps && parent->pid != ps->ppid))
                                parent = parent->next_ps;

                        if ((!parent) || (parent->pid != ps->ppid)) {
                                /* orphan */
                                ps->ppid = 1;
                                parent = ps_first->next_ps;
                        }

                        ps->parent = parent;

                        if (!parent->children) {
                                /* it's the first child */
                                parent->children = ps;
                        } else {
                                /* walk all children and append */
                                struct ps_struct *children;
                                children = parent->children;
                                while (children->next)
                                        children = children->next;
                                children->next = ps;
                        }
                }

                /* else -> found pid, append data in ps */

                /* below here is all continuous logging parts - we get here on every
                 * iteration */

                /* rt, wt */
                if (!ps->schedstat) {
                        sprintf(filename, "/proc/%d/schedstat", pid);
                        ps->schedstat = open(filename, O_RDONLY);
                        if (ps->schedstat == -1)
                                continue;
                }
                s = pread(ps->schedstat, buf, sizeof(buf) - 1, 0);
                if (s <= 0) {
                        /* clean up our file descriptors - assume that the process exited */
                        close(ps->schedstat);
                        if (ps->sched)
                                close(ps->sched);
                        //if (ps->smaps)
                        //        fclose(ps->smaps);
                        continue;
                }
                buf[s] = '\0';

                if (!sscanf(buf, "%s %s %*s", rt, wt))
                        continue;

                ps->last = sample;
                ps->sample[sample].runtime = atoll(rt);
                ps->sample[sample].waittime = atoll(wt);

                ps->total = (ps->sample[ps->last].runtime
                                 - ps->sample[ps->first].runtime)
                                 / 1000000000.0;

                if (!pss)
                        goto catch_rename;
                /* Pss */
                if (!ps->smaps) {
                        sprintf(filename, "/proc/%d/smaps", pid);
                        ps->smaps = fopen(filename, "r");
                        if (!ps->smaps)
                                continue;
                        setvbuf(ps->smaps, smaps_buf, _IOFBF, sizeof(smaps_buf));
                } else {
                        rewind(ps->smaps);
                }

                while (1) {
                        int pss_kb;

                        /* skip one line, this contains the object mapped */
                        if (fgets(buf, sizeof(buf), ps->smaps) == NULL)
                                break;
                        /* then there's a 28 char 14 line block */
                        if (fread(buf, 1, 28 * 14, ps->smaps) != 28 * 14)
                                break;

                        pss_kb = atoi(&buf[61]);
                        ps->sample[sample].pss += pss_kb;
                }

                if (ps->sample[sample].pss > ps->pss_max)
                        ps->pss_max = ps->sample[sample].pss;

catch_rename:
                /* catch process rename, try to randomize time */
                mod = (hz < 4.0) ? 4.0 : (hz / 4.0);
                if (((samples - ps->first) + pid) % (int)(mod) == 0) {

                        /* re-fetch name */
                        /* get name, start time */
                        if (!ps->sched) {
                                sprintf(filename, "/proc/%d/sched", pid);
                                ps->sched = open(filename, O_RDONLY);
                                if (ps->sched == -1)
                                        continue;
                        }
                        s = pread(ps->sched, buf, sizeof(buf) - 1, 0);
                        if (s <= 0) {
                                /* clean up file descriptors */
                                close(ps->sched);
                                if (ps->schedstat)
                                        close(ps->schedstat);
                                //if (ps->smaps)
                                //        fclose(ps->smaps);
                                continue;
                        }
                        buf[s] = '\0';

                        if (!sscanf(buf, "%s %*s %*s", key))
                                continue;

                        strncpy(ps->name, key, 16);
                }
        }
}