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
37 double gettime_ns(void)
41 clock_gettime(CLOCK_MONOTONIC, &now);
43 return (now.tv_sec + (now.tv_nsec / 1000000000.0));
53 f = fopen("/proc/uptime", "r");
56 if (!fscanf(f, "%s %*s", str)) {
61 uptime = strtod(str, NULL);
63 log_start = gettime_ns();
65 /* start graph at kernel boot time */
67 graph_start = log_start;
69 graph_start = log_start - uptime;
73 static char *bufgetline(char *buf)
80 c = strchr(buf, '\n');
87 void log_sample(int sample)
107 vmstat = open("/proc/vmstat", O_RDONLY);
109 perror("open /proc/vmstat");
114 n = pread(vmstat, buf, sizeof(buf) - 1, 0);
123 if (sscanf(m, "%s %s", key, val) < 2)
125 if (!strcmp(key, "pgpgin"))
126 blockstat[sample].bi = atoi(val);
127 if (!strcmp(key, "pgpgout")) {
128 blockstat[sample].bo = atoi(val);
138 /* overall CPU utilization */
139 schedstat = open("/proc/schedstat", O_RDONLY);
140 if (schedstat == -1) {
141 perror("open /proc/schedstat");
146 n = pread(schedstat, buf, sizeof(buf) - 1, 0);
155 if (sscanf(m, "%s %*s %*s %*s %*s %*s %*s %s %s", key, rt, wt) < 3)
158 if (strstr(key, "cpu")) {
159 c = atoi((const char*)(key+3));
161 /* Oops, we only have room for MAXCPUS data */
163 cpustat[c].sample[sample].runtime = atoll(rt);
164 cpustat[c].sample[sample].waittime = atoll(wt);
175 /* all the per-process stuff goes here */
177 /* find all processes */
178 proc = opendir("/proc");
185 while ((ent = readdir(proc)) != NULL) {
186 char filename[PATH_MAX];
189 if ((ent->d_name[0] < '0') || (ent->d_name[0] > '9'))
192 pid = atoi(ent->d_name);
199 struct ps_struct *parent;
204 ps[pid] = malloc(sizeof(struct ps_struct));
206 perror("malloc ps[pid]");
209 memset(ps[pid], 0, sizeof(struct ps_struct));
211 /* mark our first sample */
212 ps[pid]->first = sample;
214 /* get name, start time */
215 if (!ps[pid]->sched) {
216 sprintf(filename, "/proc/%d/sched", pid);
217 ps[pid]->sched = open(filename, O_RDONLY);
218 if (ps[pid]->sched == -1)
222 s = pread(ps[pid]->sched, buf, sizeof(buf) - 1, 0);
224 close(ps[pid]->sched);
228 if (!sscanf(buf, "%s %*s %*s", key))
231 strncpy(ps[pid]->name, key, 16);
241 if (!sscanf(m, "%*s %*s %s", t))
244 ps[pid]->starttime = strtod(t, NULL) / 1000.0;
247 sprintf(filename, "/proc/%d/stat", pid);
248 stat = fopen(filename, "r");
251 if (!fscanf(stat, "%*s %*s %*s %i", &p)) {
259 * setup child pointers
261 * these are used to paint the tree coherently later
262 * each parent has a LL of children, and a LL of siblings
265 continue; /* nothing to do for init atm */
267 parent = ps[ps[pid]->ppid];
275 if (!parent->children) {
276 /* it's the first child */
277 parent->children = ps[pid];
279 /* walk all children and append */
280 struct ps_struct *children;
281 children = parent->children;
282 while (children->next)
283 children = children->next;
284 children->next = ps[pid];
290 /* below here is all continuous logging parts - we get here on every
294 if (!ps[pid]->schedstat) {
295 sprintf(filename, "/proc/%d/schedstat", pid);
296 ps[pid]->schedstat = open(filename, O_RDONLY);
297 if (ps[pid]->schedstat == -1)
301 if (pread(ps[pid]->schedstat, buf, sizeof(buf) - 1, 0) <= 0) {
302 /* clean up our file descriptors - assume that the process exited */
303 close(ps[pid]->schedstat);
305 close(ps[pid]->sched);
306 //if (ps[pid]->smaps)
307 // fclose(ps[pid]->smaps);
310 if (!sscanf(buf, "%s %s %*s", rt, wt))
314 ps[pid]->last = sample;
315 ps[pid]->sample[sample].runtime = atoll(rt);
316 ps[pid]->sample[sample].waittime = atoll(wt);
318 ps[pid]->total = (ps[pid]->sample[ps[pid]->last].runtime
319 - ps[pid]->sample[ps[pid]->first].runtime)
325 if (!ps[pid]->smaps) {
326 sprintf(filename, "/proc/%d/smaps", pid);
327 ps[pid]->smaps = fopen(filename, "r");
328 setvbuf(ps[pid]->smaps, smaps_buf, _IOFBF, sizeof(smaps_buf));
332 rewind(ps[pid]->smaps);
336 /* skip one line, this contains the object mapped */
337 if (fgets(buf, sizeof(buf) -1, ps[pid]->smaps) == NULL)
339 /* then there's a 28 char 14 line block */
340 if (fread(buf, 1, 28 * 14, ps[pid]->smaps) != 28 * 14)
345 ps[pid]->sample[sample].pss += p;
348 if (ps[pid]->sample[sample].pss > ps[pid]->pss_max)
349 ps[pid]->pss_max = ps[pid]->sample[sample].pss;
352 /* catch process rename, try to randomize time */
353 if (((samples - ps[pid]->first) + pid) % (hz / 4) == 0) {
356 /* get name, start time */
357 if (!ps[pid]->sched) {
358 sprintf(filename, "/proc/%d/sched", pid);
359 ps[pid]->sched = open(filename, O_RDONLY);
360 if (ps[pid]->sched == -1)
363 if (pread(ps[pid]->sched, buf, sizeof(buf) - 1, 0) <= 0) {
364 /* clean up file descriptors */
365 close(ps[pid]->sched);
366 if (ps[pid]->schedstat)
367 close(ps[pid]->schedstat);
368 //if (ps[pid]->smaps)
369 // fclose(ps[pid]->smaps);
373 if (!sscanf(buf, "%s %*s %*s", key))
376 strncpy(ps[pid]->name, key, 16);