4 * Copyright (c) 2009 Intel Coproration
6 * Auke Kok <auke-jan.h.kok@intel.com>
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
18 #include <sys/types.h>
27 #include "bootchart.h"
30 * Alloc a static 4k buffer for stdio - primarily used to increase
31 * PSS buffering from the default 1k stdin buffer to reduce
34 static char smaps_buf[4096];
38 double gettime_ns(void)
42 clock_gettime(CLOCK_MONOTONIC, &now);
44 return (now.tv_sec + (now.tv_nsec / 1000000000.0));
54 f = fopen("/proc/uptime", "r");
57 if (!fscanf(f, "%s %*s", str)) {
62 uptime = strtod(str, NULL);
64 log_start = gettime_ns();
66 /* start graph at kernel boot time */
68 graph_start = log_start;
70 graph_start = log_start - uptime;
74 static char *bufgetline(char *buf)
81 c = strchr(buf, '\n');
88 void log_sample(int sample)
109 vmstat = open("/proc/vmstat", O_RDONLY);
111 perror("open /proc/vmstat");
116 n = pread(vmstat, buf, sizeof(buf) - 1, 0);
125 if (sscanf(m, "%s %s", key, val) < 2)
127 if (!strcmp(key, "pgpgin"))
128 blockstat[sample].bi = atoi(val);
129 if (!strcmp(key, "pgpgout")) {
130 blockstat[sample].bo = atoi(val);
140 /* overall CPU utilization */
141 schedstat = open("/proc/schedstat", O_RDONLY);
142 if (schedstat == -1) {
143 perror("open /proc/schedstat");
148 n = pread(schedstat, buf, sizeof(buf) - 1, 0);
157 if (sscanf(m, "%s %*s %*s %*s %*s %*s %*s %s %s", key, rt, wt) < 3)
160 if (strstr(key, "cpu")) {
161 c = atoi((const char*)(key+3));
163 /* Oops, we only have room for MAXCPUS data */
165 cpustat[c].sample[sample].runtime = atoll(rt);
166 cpustat[c].sample[sample].waittime = atoll(wt);
179 e_fd = open("/proc/sys/kernel/random/entropy_avail", O_RDONLY);
183 n = pread(e_fd, buf, sizeof(buf) - 1, 0);
185 entropy_avail[sample] = atoi(buf);
189 /* all the per-process stuff goes here */
191 /* find all processes */
192 proc = opendir("/proc");
199 while ((ent = readdir(proc)) != NULL) {
200 char filename[PATH_MAX];
202 struct ps_struct *ps;
204 if ((ent->d_name[0] < '0') || (ent->d_name[0] > '9'))
207 pid = atoi(ent->d_name);
213 while (ps->next_ps) {
219 /* end of our LL? then append a new record */
220 if (ps->pid != pid) {
222 struct ps_struct *parent;
224 ps->next_ps = malloc(sizeof(struct ps_struct));
226 perror("malloc(ps_struct)");
229 memset(ps->next_ps, 0, sizeof(struct ps_struct));
233 ps->sample = malloc(sizeof(struct ps_sched_struct) * (len + 1));
235 perror("malloc(ps_struct)");
238 memset(ps->sample, 0, sizeof(struct ps_sched_struct) * (len + 1));
242 /* mark our first sample */
245 /* get name, start time */
247 sprintf(filename, "/proc/%d/sched", pid);
248 ps->sched = open(filename, O_RDONLY);
253 s = pread(ps->sched, buf, sizeof(buf) - 1, 0);
259 if (!sscanf(buf, "%s %*s %*s", key))
262 strncpy(ps->name, key, 16);
272 if (!sscanf(m, "%*s %*s %s", t))
275 ps->starttime = strtod(t, NULL) / 1000.0;
278 sprintf(filename, "/proc/%d/stat", pid);
279 stat = fopen(filename, "r");
282 if (!fscanf(stat, "%*s %*s %*s %i", &p)) {
290 * setup child pointers
292 * these are used to paint the tree coherently later
293 * each parent has a LL of children, and a LL of siblings
296 continue; /* nothing to do for init atm */
298 /* kthreadd has ppid=0, which breaks our tree ordering */
303 while ((parent->next_ps && parent->pid != ps->ppid))
304 parent = parent->next_ps;
306 if ((!parent) || (parent->pid != ps->ppid)) {
309 parent = ps_first->next_ps;
314 if (!parent->children) {
315 /* it's the first child */
316 parent->children = ps;
318 /* walk all children and append */
319 struct ps_struct *children;
320 children = parent->children;
321 while (children->next)
322 children = children->next;
327 /* else -> found pid, append data in ps */
329 /* below here is all continuous logging parts - we get here on every
333 if (!ps->schedstat) {
334 sprintf(filename, "/proc/%d/schedstat", pid);
335 ps->schedstat = open(filename, O_RDONLY);
336 if (ps->schedstat == -1)
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);
346 // fclose(ps->smaps);
349 if (!sscanf(buf, "%s %s %*s", rt, wt))
353 ps->sample[sample].runtime = atoll(rt);
354 ps->sample[sample].waittime = atoll(wt);
356 ps->total = (ps->sample[ps->last].runtime
357 - ps->sample[ps->first].runtime)
364 sprintf(filename, "/proc/%d/smaps", pid);
365 ps->smaps = fopen(filename, "r");
366 setvbuf(ps->smaps, smaps_buf, _IOFBF, sizeof(smaps_buf));
376 /* skip one line, this contains the object mapped */
377 if (fgets(buf, sizeof(buf), ps->smaps) == NULL)
379 /* then there's a 28 char 14 line block */
380 if (fread(buf, 1, 28 * 14, ps->smaps) != 28 * 14)
384 ps->sample[sample].pss += p;
387 if (ps->sample[sample].pss > ps->pss_max)
388 ps->pss_max = ps->sample[sample].pss;
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) {
396 /* get name, start time */
398 sprintf(filename, "/proc/%d/sched", pid);
399 ps->sched = open(filename, O_RDONLY);
403 if (pread(ps->sched, buf, sizeof(buf) - 1, 0) <= 0) {
404 /* clean up file descriptors */
407 close(ps->schedstat);
409 // fclose(ps->smaps);
413 if (!sscanf(buf, "%s %*s %*s", key))
416 strncpy(ps->name, key, 16);