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);
186 entropy_avail[sample] = atoi(buf);
190 /* all the per-process stuff goes here */
192 /* find all processes */
193 proc = opendir("/proc");
200 while ((ent = readdir(proc)) != NULL) {
201 char filename[PATH_MAX];
203 struct ps_struct *ps;
205 if ((ent->d_name[0] < '0') || (ent->d_name[0] > '9'))
208 pid = atoi(ent->d_name);
214 while (ps->next_ps) {
220 /* end of our LL? then append a new record */
221 if (ps->pid != pid) {
223 struct ps_struct *parent;
225 ps->next_ps = malloc(sizeof(struct ps_struct));
227 perror("malloc(ps_struct)");
230 memset(ps->next_ps, 0, sizeof(struct ps_struct));
234 ps->sample = malloc(sizeof(struct ps_sched_struct) * (len + 1));
236 perror("malloc(ps_struct)");
239 memset(ps->sample, 0, sizeof(struct ps_sched_struct) * (len + 1));
243 /* mark our first sample */
246 /* get name, start time */
248 sprintf(filename, "/proc/%d/sched", pid);
249 ps->sched = open(filename, O_RDONLY);
254 s = pread(ps->sched, buf, sizeof(buf) - 1, 0);
260 if (!sscanf(buf, "%s %*s %*s", key))
263 strncpy(ps->name, key, 16);
273 if (!sscanf(m, "%*s %*s %s", t))
276 ps->starttime = strtod(t, NULL) / 1000.0;
279 sprintf(filename, "/proc/%d/stat", pid);
280 st = fopen(filename, "r");
283 if (!fscanf(st, "%*s %*s %*s %i", &p)) {
291 * setup child pointers
293 * these are used to paint the tree coherently later
294 * each parent has a LL of children, and a LL of siblings
297 continue; /* nothing to do for init atm */
299 /* kthreadd has ppid=0, which breaks our tree ordering */
304 while ((parent->next_ps && parent->pid != ps->ppid))
305 parent = parent->next_ps;
307 if ((!parent) || (parent->pid != ps->ppid)) {
310 parent = ps_first->next_ps;
315 if (!parent->children) {
316 /* it's the first child */
317 parent->children = ps;
319 /* walk all children and append */
320 struct ps_struct *children;
321 children = parent->children;
322 while (children->next)
323 children = children->next;
328 /* else -> found pid, append data in ps */
330 /* below here is all continuous logging parts - we get here on every
334 if (!ps->schedstat) {
335 sprintf(filename, "/proc/%d/schedstat", pid);
336 ps->schedstat = open(filename, O_RDONLY);
337 if (ps->schedstat == -1)
341 if (pread(ps->schedstat, buf, sizeof(buf) - 1, 0) <= 0) {
342 /* clean up our file descriptors - assume that the process exited */
343 close(ps->schedstat);
347 // fclose(ps->smaps);
350 if (!sscanf(buf, "%s %s %*s", rt, wt))
354 ps->sample[sample].runtime = atoll(rt);
355 ps->sample[sample].waittime = atoll(wt);
357 ps->total = (ps->sample[ps->last].runtime
358 - ps->sample[ps->first].runtime)
365 sprintf(filename, "/proc/%d/smaps", pid);
366 ps->smaps = fopen(filename, "r");
369 setvbuf(ps->smaps, smaps_buf, _IOFBF, sizeof(smaps_buf));
377 /* skip one line, this contains the object mapped */
378 if (fgets(buf, sizeof(buf), ps->smaps) == NULL)
380 /* then there's a 28 char 14 line block */
381 if (fread(buf, 1, 28 * 14, ps->smaps) != 28 * 14)
384 pss_kb = atoi(&buf[61]);
385 ps->sample[sample].pss += pss_kb;
388 if (ps->sample[sample].pss > ps->pss_max)
389 ps->pss_max = ps->sample[sample].pss;
392 /* catch process rename, try to randomize time */
393 mod = (hz < 4.0) ? 4.0 : (hz / 4.0);
394 if (((samples - ps->first) + pid) % (int)(mod) == 0) {
397 /* get name, start time */
399 sprintf(filename, "/proc/%d/sched", pid);
400 ps->sched = open(filename, O_RDONLY);
404 if (pread(ps->sched, buf, sizeof(buf) - 1, 0) <= 0) {
405 /* clean up file descriptors */
408 close(ps->schedstat);
410 // fclose(ps->smaps);
414 if (!sscanf(buf, "%s %*s %*s", key))
417 strncpy(ps->name, key, 16);