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"
32 * Alloc a static 4k buffer for stdio - primarily used to increase
33 * PSS buffering from the default 1k stdin buffer to reduce
36 static char smaps_buf[4096];
40 double gettime_ns(void)
44 clock_gettime(CLOCK_MONOTONIC, &n);
46 return (n.tv_sec + (n.tv_nsec / 1000000000.0));
56 f = fopen("/proc/uptime", "r");
59 if (!fscanf(f, "%s %*s", str)) {
64 uptime = strtod(str, NULL);
66 log_start = gettime_ns();
68 /* start graph at kernel boot time */
70 graph_start = log_start;
72 graph_start = log_start - uptime;
76 static char *bufgetline(char *buf)
83 c = strchr(buf, '\n');
90 void log_sample(int sample)
111 vmstat = open("/proc/vmstat", O_RDONLY);
113 perror("open /proc/vmstat");
118 n = pread(vmstat, buf, sizeof(buf) - 1, 0);
127 if (sscanf(m, "%s %s", key, val) < 2)
129 if (streq(key, "pgpgin"))
130 blockstat[sample].bi = atoi(val);
131 if (streq(key, "pgpgout")) {
132 blockstat[sample].bo = atoi(val);
142 /* overall CPU utilization */
143 schedstat = open("/proc/schedstat", O_RDONLY);
144 if (schedstat == -1) {
145 perror("open /proc/schedstat");
150 n = pread(schedstat, buf, sizeof(buf) - 1, 0);
159 if (sscanf(m, "%s %*s %*s %*s %*s %*s %*s %s %s", key, rt, wt) < 3)
162 if (strstr(key, "cpu")) {
163 c = atoi((const char*)(key+3));
165 /* Oops, we only have room for MAXCPUS data */
167 cpustat[c].sample[sample].runtime = atoll(rt);
168 cpustat[c].sample[sample].waittime = atoll(wt);
181 e_fd = open("/proc/sys/kernel/random/entropy_avail", O_RDONLY);
185 n = pread(e_fd, buf, sizeof(buf) - 1, 0);
188 entropy_avail[sample] = atoi(buf);
193 /* all the per-process stuff goes here */
195 /* find all processes */
196 proc = opendir("/proc");
203 while ((ent = readdir(proc)) != NULL) {
204 char filename[PATH_MAX];
206 struct ps_struct *ps;
208 if ((ent->d_name[0] < '0') || (ent->d_name[0] > '9'))
211 pid = atoi(ent->d_name);
217 while (ps->next_ps) {
223 /* end of our LL? then append a new record */
224 if (ps->pid != pid) {
226 struct ps_struct *parent;
228 ps->next_ps = calloc(1, sizeof(struct ps_struct));
230 perror("calloc(ps_struct)");
236 ps->sample = calloc(len + 1, sizeof(struct ps_sched_struct));
238 perror("calloc(ps_struct)");
244 /* mark our first sample */
247 /* get name, start time */
249 sprintf(filename, "/proc/%d/sched", pid);
250 ps->sched = open(filename, O_RDONLY);
255 s = pread(ps->sched, buf, sizeof(buf) - 1, 0);
262 if (!sscanf(buf, "%s %*s %*s", key))
265 strncpy(ps->name, key, 16);
275 if (!sscanf(m, "%*s %*s %s", t))
278 ps->starttime = strtod(t, NULL) / 1000.0;
281 sprintf(filename, "/proc/%d/stat", pid);
282 st = fopen(filename, "r");
285 if (!fscanf(st, "%*s %*s %*s %i", &p)) {
293 * setup child pointers
295 * these are used to paint the tree coherently later
296 * each parent has a LL of children, and a LL of siblings
299 continue; /* nothing to do for init atm */
301 /* kthreadd has ppid=0, which breaks our tree ordering */
306 while ((parent->next_ps && parent->pid != ps->ppid))
307 parent = parent->next_ps;
309 if ((!parent) || (parent->pid != ps->ppid)) {
312 parent = ps_first->next_ps;
317 if (!parent->children) {
318 /* it's the first child */
319 parent->children = ps;
321 /* walk all children and append */
322 struct ps_struct *children;
323 children = parent->children;
324 while (children->next)
325 children = children->next;
330 /* else -> found pid, append data in ps */
332 /* below here is all continuous logging parts - we get here on every
336 if (!ps->schedstat) {
337 sprintf(filename, "/proc/%d/schedstat", pid);
338 ps->schedstat = open(filename, O_RDONLY);
339 if (ps->schedstat == -1)
342 s = pread(ps->schedstat, buf, sizeof(buf) - 1, 0);
344 /* clean up our file descriptors - assume that the process exited */
345 close(ps->schedstat);
349 // fclose(ps->smaps);
354 if (!sscanf(buf, "%s %s %*s", rt, wt))
358 ps->sample[sample].runtime = atoll(rt);
359 ps->sample[sample].waittime = atoll(wt);
361 ps->total = (ps->sample[ps->last].runtime
362 - ps->sample[ps->first].runtime)
369 sprintf(filename, "/proc/%d/smaps", pid);
370 ps->smaps = fopen(filename, "r");
373 setvbuf(ps->smaps, smaps_buf, _IOFBF, sizeof(smaps_buf));
381 /* skip one line, this contains the object mapped */
382 if (fgets(buf, sizeof(buf), ps->smaps) == NULL)
384 /* then there's a 28 char 14 line block */
385 if (fread(buf, 1, 28 * 14, ps->smaps) != 28 * 14)
388 pss_kb = atoi(&buf[61]);
389 ps->sample[sample].pss += pss_kb;
392 if (ps->sample[sample].pss > ps->pss_max)
393 ps->pss_max = ps->sample[sample].pss;
396 /* catch process rename, try to randomize time */
397 mod = (hz < 4.0) ? 4.0 : (hz / 4.0);
398 if (((samples - ps->first) + pid) % (int)(mod) == 0) {
401 /* get name, start time */
403 sprintf(filename, "/proc/%d/sched", pid);
404 ps->sched = open(filename, O_RDONLY);
408 s = pread(ps->sched, buf, sizeof(buf) - 1, 0);
410 /* clean up file descriptors */
413 close(ps->schedstat);
415 // fclose(ps->smaps);
420 if (!sscanf(buf, "%s %*s %*s", key))
423 strncpy(ps->name, key, 16);