4 * Copyright (C) 2009-2012 Intel Coproration
7 * Auke Kok <auke-jan.h.kok@intel.com>
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
19 #include <sys/types.h>
28 #include "bootchart.h"
31 * Alloc a static 4k buffer for stdio - primarily used to increase
32 * PSS buffering from the default 1k stdin buffer to reduce
35 static char smaps_buf[4096];
39 double gettime_ns(void)
43 clock_gettime(CLOCK_MONOTONIC, &now);
45 return (now.tv_sec + (now.tv_nsec / 1000000000.0));
55 f = fopen("/proc/uptime", "r");
58 if (!fscanf(f, "%s %*s", str)) {
63 uptime = strtod(str, NULL);
65 log_start = gettime_ns();
67 /* start graph at kernel boot time */
69 graph_start = log_start;
71 graph_start = log_start - uptime;
75 static char *bufgetline(char *buf)
82 c = strchr(buf, '\n');
89 void log_sample(int sample)
110 vmstat = open("/proc/vmstat", O_RDONLY);
112 perror("open /proc/vmstat");
117 n = pread(vmstat, buf, sizeof(buf) - 1, 0);
126 if (sscanf(m, "%s %s", key, val) < 2)
128 if (!strcmp(key, "pgpgin"))
129 blockstat[sample].bi = atoi(val);
130 if (!strcmp(key, "pgpgout")) {
131 blockstat[sample].bo = atoi(val);
141 /* overall CPU utilization */
142 schedstat = open("/proc/schedstat", O_RDONLY);
143 if (schedstat == -1) {
144 perror("open /proc/schedstat");
149 n = pread(schedstat, buf, sizeof(buf) - 1, 0);
158 if (sscanf(m, "%s %*s %*s %*s %*s %*s %*s %s %s", key, rt, wt) < 3)
161 if (strstr(key, "cpu")) {
162 c = atoi((const char*)(key+3));
164 /* Oops, we only have room for MAXCPUS data */
166 cpustat[c].sample[sample].runtime = atoll(rt);
167 cpustat[c].sample[sample].waittime = atoll(wt);
180 e_fd = open("/proc/sys/kernel/random/entropy_avail", O_RDONLY);
184 n = pread(e_fd, buf, sizeof(buf) - 1, 0);
187 entropy_avail[sample] = atoi(buf);
192 /* all the per-process stuff goes here */
194 /* find all processes */
195 proc = opendir("/proc");
202 while ((ent = readdir(proc)) != NULL) {
203 char filename[PATH_MAX];
205 struct ps_struct *ps;
207 if ((ent->d_name[0] < '0') || (ent->d_name[0] > '9'))
210 pid = atoi(ent->d_name);
216 while (ps->next_ps) {
222 /* end of our LL? then append a new record */
223 if (ps->pid != pid) {
225 struct ps_struct *parent;
227 ps->next_ps = malloc(sizeof(struct ps_struct));
229 perror("malloc(ps_struct)");
232 memset(ps->next_ps, 0, sizeof(struct ps_struct));
236 ps->sample = malloc(sizeof(struct ps_sched_struct) * (len + 1));
238 perror("malloc(ps_struct)");
241 memset(ps->sample, 0, sizeof(struct ps_sched_struct) * (len + 1));
245 /* mark our first sample */
248 /* get name, start time */
250 sprintf(filename, "/proc/%d/sched", pid);
251 ps->sched = open(filename, O_RDONLY);
256 s = pread(ps->sched, buf, sizeof(buf) - 1, 0);
263 if (!sscanf(buf, "%s %*s %*s", key))
266 strncpy(ps->name, key, 16);
276 if (!sscanf(m, "%*s %*s %s", t))
279 ps->starttime = strtod(t, NULL) / 1000.0;
282 sprintf(filename, "/proc/%d/stat", pid);
283 st = fopen(filename, "r");
286 if (!fscanf(st, "%*s %*s %*s %i", &p)) {
294 * setup child pointers
296 * these are used to paint the tree coherently later
297 * each parent has a LL of children, and a LL of siblings
300 continue; /* nothing to do for init atm */
302 /* kthreadd has ppid=0, which breaks our tree ordering */
307 while ((parent->next_ps && parent->pid != ps->ppid))
308 parent = parent->next_ps;
310 if ((!parent) || (parent->pid != ps->ppid)) {
313 parent = ps_first->next_ps;
318 if (!parent->children) {
319 /* it's the first child */
320 parent->children = ps;
322 /* walk all children and append */
323 struct ps_struct *children;
324 children = parent->children;
325 while (children->next)
326 children = children->next;
331 /* else -> found pid, append data in ps */
333 /* below here is all continuous logging parts - we get here on every
337 if (!ps->schedstat) {
338 sprintf(filename, "/proc/%d/schedstat", pid);
339 ps->schedstat = open(filename, O_RDONLY);
340 if (ps->schedstat == -1)
343 s = pread(ps->schedstat, buf, sizeof(buf) - 1, 0);
345 /* clean up our file descriptors - assume that the process exited */
346 close(ps->schedstat);
350 // fclose(ps->smaps);
355 if (!sscanf(buf, "%s %s %*s", rt, wt))
359 ps->sample[sample].runtime = atoll(rt);
360 ps->sample[sample].waittime = atoll(wt);
362 ps->total = (ps->sample[ps->last].runtime
363 - ps->sample[ps->first].runtime)
370 sprintf(filename, "/proc/%d/smaps", pid);
371 ps->smaps = fopen(filename, "r");
374 setvbuf(ps->smaps, smaps_buf, _IOFBF, sizeof(smaps_buf));
382 /* skip one line, this contains the object mapped */
383 if (fgets(buf, sizeof(buf), ps->smaps) == NULL)
385 /* then there's a 28 char 14 line block */
386 if (fread(buf, 1, 28 * 14, ps->smaps) != 28 * 14)
389 pss_kb = atoi(&buf[61]);
390 ps->sample[sample].pss += pss_kb;
393 if (ps->sample[sample].pss > ps->pss_max)
394 ps->pss_max = ps->sample[sample].pss;
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) {
402 /* get name, start time */
404 sprintf(filename, "/proc/%d/sched", pid);
405 ps->sched = open(filename, O_RDONLY);
409 s = pread(ps->sched, buf, sizeof(buf) - 1, 0);
411 /* clean up file descriptors */
414 close(ps->schedstat);
416 // fclose(ps->smaps);
421 if (!sscanf(buf, "%s %*s %*s", key))
424 strncpy(ps->name, key, 16);