summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bootchart/README83
-rw-r--r--src/bootchart/bootchart.c352
-rw-r--r--src/bootchart/bootchart.conf20
-rw-r--r--src/bootchart/bootchart.h117
-rw-r--r--src/bootchart/log.c420
-rw-r--r--src/bootchart/svg.c1120
6 files changed, 2112 insertions, 0 deletions
diff --git a/src/bootchart/README b/src/bootchart/README
new file mode 100644
index 0000000000..a209caf46b
--- /dev/null
+++ b/src/bootchart/README
@@ -0,0 +1,83 @@
+
+Bootchart - a 'startup' graphing tool
+
+--
+
+Bootchart is a tool, usually run at system startup, that collects and graphs
+the CPU and disk load of the system as it works. The output of bootchart is
+an SVG graph. Normally, bootchart is invoked as `bootchartd` by the kernel
+by passing "init=/sbin/bootchartd" to the kernel. Bootchart will then fork
+init off to resume normal system startup, while monitoring and logging
+startup information in the background.
+
+After collecting a certain amount of data (usually 15-30 seconds) the logging
+stops and a graph is generated from the logged information. This graph
+contains vital clues to which resources are being used, in which order, and
+where possible problems exist in the startup sequence of the system.
+
+Of course, bootchart can also be used at any moment in time to collect and
+graph some data for an amount of time. Bootchart does not even require root
+privileges to do so, and will happily run as a normal user. Bootchart graphs
+are by default written time-stamped in /var/log.
+
+--
+
+This version of bootchart was implemented from scratch and inspired by former
+incantations of bootchart:
+
+- The original bash/shell code implemented bootchart. This version logged all
+data into a compressed tarball for later processing, and did not create a graph
+on it's own.
+
+- The C-code implementation found in Ubuntu. This version replaced above shell
+code version with a faster and efficient data logger, but still did not graph
+code itself.
+
+- the original Java-based bootchart, the original graphing program that created
+a bootchart graph from logged data.
+
+- the pybootchartgui.py program, which created a graph based on the data logged
+by either standalone data logger.
+
+The version you are looking at combines these 2 parts into a single program,
+which makes running it and creating graphs a bit more efficient and simple.
+You can now run a single program at startup instead of 2. There are no timing
+problems (the graphing stage will never run if the logging stage didn't
+finish). The logged data isn't being written to disc first, then read again.
+Also, the data kept in memory is reduced to the absolute minimum needed to
+keep memory use low.
+
+--
+
+Requirements: glibc. Your kernel must have procfs support and several
+proc output options enabled:
+ CONFIG_PROC_FS
+ CONFIG_SCHEDSTATS
+ CONFIG_SCHED_DEBUG
+at a minimum. bootchartd itself does not require any graphics library
+to generate the SVG output file.
+
+--
+
+Configuration: please see bootchartd --help, as well as /etc/bootchartd.conf
+and/or /usr/share/doc/bootchart/bootchartd.conf.example for a list of
+configurable options.
+
+--
+
+Many thanks to those who contributed ideas and code:
+ - Ziga Mahkovec - Original bootchart author
+ - Anders Norgaard - PyBootchartgui
+ - Michael Meeks - bootchart2
+ - Scott James Remnant - Ubuntu C-based logger
+ - Arjan van der Ven - for the idea to merge bootgraph.pl functionality
+
+--
+
+For bugs, please contact the author or current maintainer:
+Auke Kok <auke-jan.h.kok@intel.com>
+
+--
+
+Download bootchart releases here: http://foo-projects.org/~sofar/bootchart/
+Source code is hosted here: git://github.com/sofar/bootchart
diff --git a/src/bootchart/bootchart.c b/src/bootchart/bootchart.c
new file mode 100644
index 0000000000..2eeb37ae53
--- /dev/null
+++ b/src/bootchart/bootchart.c
@@ -0,0 +1,352 @@
+/*
+ * bootchart.c
+ *
+ * Copyright (C) 2009-2012 Intel Coproration
+ *
+ * Authors:
+ * Auke Kok <auke-jan.h.kok@intel.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; version 2
+ * of the License.
+ */
+
+
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/resource.h>
+#include <stdio.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <time.h>
+#include <getopt.h>
+#include <limits.h>
+#include <errno.h>
+
+
+#include "bootchart.h"
+
+double graph_start;
+double log_start;
+double sampletime[MAXSAMPLES];
+struct ps_struct *ps_first;
+struct block_stat_struct blockstat[MAXSAMPLES];
+int entropy_avail[MAXSAMPLES];
+struct cpu_stat_struct cpustat[MAXCPUS];
+int pscount;
+int cpus;
+double interval;
+FILE *of;
+int overrun = 0;
+static int exiting = 0;
+
+/* graph defaults */
+int entropy = 0;
+int initcall = 1;
+int relative;
+int filter = 1;
+int pss = 0;
+int samples;
+int len = 500; /* we record len+1 (1 start sample) */
+double hz = 25.0; /* 20 seconds log time */
+double scale_x = 100.0; /* 100px = 1sec */
+double scale_y = 20.0; /* 16px = 1 process bar */
+
+char init_path[PATH_MAX] = "/sbin/init";
+char output_path[PATH_MAX] = "/var/log";
+
+static struct rlimit rlim;
+
+static void signal_handler(int sig)
+{
+ if (sig++)
+ sig--;
+ exiting = 1;
+}
+
+
+int main(int argc, char *argv[])
+{
+ struct sigaction sig;
+ struct ps_struct *ps;
+ char output_file[PATH_MAX];
+ char datestr[200];
+ time_t t;
+ FILE *f;
+ int gind;
+ int i;
+
+ memset(&t, 0, sizeof(time_t));
+
+ rlim.rlim_cur = 4096;
+ rlim.rlim_max = 4096;
+ (void) setrlimit(RLIMIT_NOFILE, &rlim);
+
+ f = fopen("/etc/systemd/bootchart.conf", "r");
+ if (f) {
+ char buf[256];
+ char *key;
+ char *val;
+
+ while (fgets(buf, 80, f) != NULL) {
+ char *c;
+
+ c = strchr(buf, '\n');
+ if (c) *c = 0; /* remove trailing \n */
+
+ if (buf[0] == '#')
+ continue; /* comment line */
+
+ key = strtok(buf, "=");
+ if (!key)
+ continue;
+ val = strtok(NULL, "=");
+ if (!val)
+ continue;
+
+ // todo: filter leading/trailing whitespace
+
+ if (!strcmp(key, "samples"))
+ len = atoi(val);
+ if (!strcmp(key, "freq"))
+ hz = atof(val);
+ if (!strcmp(key, "rel"))
+ relative = atoi(val);
+ if (!strcmp(key, "filter"))
+ filter = atoi(val);
+ if (!strcmp(key, "pss"))
+ pss = atoi(val);
+ if (!strcmp(key, "output"))
+ strncpy(output_path, val, PATH_MAX - 1);
+ if (!strcmp(key, "init"))
+ strncpy(init_path, val, PATH_MAX - 1);
+ if (!strcmp(key, "scale_x"))
+ scale_x = atof(val);
+ if (!strcmp(key, "scale_y"))
+ scale_y = atof(val);
+ if (!strcmp(key, "entropy"))
+ entropy = atoi(val);
+ }
+ fclose(f);
+ }
+
+ while (1) {
+ static struct option opts[] = {
+ {"rel", 0, NULL, 'r'},
+ {"freq", 1, NULL, 'f'},
+ {"samples", 1, NULL, 'n'},
+ {"pss", 0, NULL, 'p'},
+ {"output", 1, NULL, 'o'},
+ {"init", 1, NULL, 'i'},
+ {"filter", 0, NULL, 'F'},
+ {"help", 0, NULL, 'h'},
+ {"scale-x", 1, NULL, 'x'},
+ {"scale-y", 1, NULL, 'y'},
+ {"entropy", 0, NULL, 'e'},
+ {NULL, 0, NULL, 0}
+ };
+
+ gind = 0;
+
+ i = getopt_long(argc, argv, "erpf:n:o:i:Fhx:y:", opts, &gind);
+ if (i == -1)
+ break;
+ switch (i) {
+ case 'r':
+ relative = 1;
+ break;
+ case 'f':
+ hz = atof(optarg);
+ break;
+ case 'F':
+ filter = 0;
+ break;
+ case 'n':
+ len = atoi(optarg);
+ break;
+ case 'o':
+ strncpy(output_path, optarg, PATH_MAX - 1);
+ break;
+ case 'i':
+ strncpy(init_path, optarg, PATH_MAX - 1);
+ break;
+ case 'p':
+ pss = 1;
+ break;
+ case 'x':
+ scale_x = atof(optarg);
+ break;
+ case 'y':
+ scale_y = atof(optarg);
+ break;
+ case 'e':
+ entropy = 1;
+ break;
+ case 'h':
+ fprintf(stderr, "Usage: %s [OPTIONS]\n", argv[0]);
+ fprintf(stderr, " --rel, -r Record time relative to recording\n");
+ fprintf(stderr, " --freq, -f N Sample frequency [%f]\n", hz);
+ fprintf(stderr, " --samples, -n N Stop sampling at [%d] samples\n", len);
+ fprintf(stderr, " --scale-x, -x N Scale the graph horizontally [%f] \n", scale_x);
+ fprintf(stderr, " --scale-y, -y N Scale the graph vertically [%f] \n", scale_y);
+ fprintf(stderr, " --pss, -p Enable PSS graph (CPU intensive)\n");
+ fprintf(stderr, " --entropy, -e Enable the entropy_avail graph\n");
+ fprintf(stderr, " --output, -o [PATH] Path to output files [%s]\n", output_path);
+ fprintf(stderr, " --init, -i [PATH] Path to init executable [%s]\n", init_path);
+ fprintf(stderr, " --filter, -F Disable filtering of processes from the graph\n");
+ fprintf(stderr, " that are of less importance or short-lived\n");
+ fprintf(stderr, " --help, -h Display this message\n");
+ fprintf(stderr, "See the installed README and bootchartd.conf.example for more information.\n");
+ exit (EXIT_SUCCESS);
+ break;
+ default:
+ break;
+ }
+ }
+
+ if (len > MAXSAMPLES) {
+ fprintf(stderr, "Error: samples exceeds maximum\n");
+ exit(EXIT_FAILURE);
+ }
+
+ if (hz <= 0.0) {
+ fprintf(stderr, "Error: Frequency needs to be > 0\n");
+ exit(EXIT_FAILURE);
+ }
+
+ /*
+ * If the kernel executed us through init=/sbin/bootchartd, then
+ * fork:
+ * - parent execs executable specified via init_path[] (/sbin/init by default) as pid=1
+ * - child logs data
+ */
+ if (getpid() == 1) {
+ if (fork()) {
+ /* parent */
+ execl(init_path, init_path, NULL);
+ }
+ }
+
+ /* start with empty ps LL */
+ ps_first = malloc(sizeof(struct ps_struct));
+ if (!ps_first) {
+ perror("malloc(ps_struct)");
+ exit(EXIT_FAILURE);
+ }
+ memset(ps_first, 0, sizeof(struct ps_struct));
+
+ /* handle TERM/INT nicely */
+ memset(&sig, 0, sizeof(struct sigaction));
+ sig.sa_handler = signal_handler;
+ sigaction(SIGHUP, &sig, NULL);
+
+ interval = (1.0 / hz) * 1000000000.0;
+
+ log_uptime();
+
+ /* main program loop */
+ while (!exiting) {
+ int res;
+ double sample_stop;
+ struct timespec req;
+ time_t newint_s;
+ long newint_ns;
+ double elapsed;
+ double timeleft;
+
+ sampletime[samples] = gettime_ns();
+
+ /* wait for /proc to become available, discarding samples */
+ if (!(graph_start > 0.0))
+ log_uptime();
+ else
+ log_sample(samples);
+
+ sample_stop = gettime_ns();
+
+ elapsed = (sample_stop - sampletime[samples]) * 1000000000.0;
+ timeleft = interval - elapsed;
+
+ newint_s = (time_t)(timeleft / 1000000000.0);
+ newint_ns = (long)(timeleft - (newint_s * 1000000000.0));
+
+ /*
+ * check if we have not consumed our entire timeslice. If we
+ * do, don't sleep and take a new sample right away.
+ * we'll lose all the missed samples and overrun our total
+ * time
+ */
+ if ((newint_ns > 0) || (newint_s > 0)) {
+ req.tv_sec = newint_s;
+ req.tv_nsec = newint_ns;
+
+ res = nanosleep(&req, NULL);
+ if (res) {
+ if (errno == EINTR) {
+ /* caught signal, probably HUP! */
+ break;
+ }
+ perror("nanosleep()");
+ exit (EXIT_FAILURE);
+ }
+ } else {
+ overrun++;
+ /* calculate how many samples we lost and scrap them */
+ len = len + ((int)(newint_ns / interval));
+ }
+
+ samples++;
+
+ if (samples > len)
+ break;
+
+ }
+
+ /* do some cleanup, close fd's */
+ ps = ps_first;
+ while (ps->next_ps) {
+ ps = ps->next_ps;
+ if (ps->schedstat)
+ close(ps->schedstat);
+ if (ps->sched)
+ close(ps->sched);
+ if (ps->smaps)
+ fclose(ps->smaps);
+ }
+ closedir(proc);
+
+ t = time(NULL);
+ strftime(datestr, sizeof(datestr), "%Y%m%d-%H%M", localtime(&t));
+ snprintf(output_file, PATH_MAX, "%s/bootchart-%s.svg", output_path, datestr);
+
+ of = fopen(output_file, "w");
+ if (!of) {
+ perror("open output_file");
+ exit (EXIT_FAILURE);
+ }
+
+ svg_do();
+
+ fprintf(stderr, "bootchartd: Wrote %s\n", output_file);
+ fclose(of);
+
+ /* nitpic cleanups */
+ ps = ps_first;
+ while (ps->next_ps) {
+ struct ps_struct *old = ps;
+ ps = ps->next_ps;
+ free(old->sample);
+ free(old);
+ }
+ free(ps->sample);
+ free(ps);
+
+ /* don't complain when overrun once, happens most commonly on 1st sample */
+ if (overrun > 1)
+ fprintf(stderr, "bootchartd: Warning: sample time overrun %i times\n", overrun);
+
+ return 0;
+}
diff --git a/src/bootchart/bootchart.conf b/src/bootchart/bootchart.conf
new file mode 100644
index 0000000000..06c1b47510
--- /dev/null
+++ b/src/bootchart/bootchart.conf
@@ -0,0 +1,20 @@
+
+# This file is part of systemd.
+#
+# systemd is free software; you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation; either version 2.1 of the License, or
+# (at your option) any later version.
+#
+# See systemd-bootchart.conf(5) for details
+
+#samples=500
+#freq=25
+#rel=0
+#filter=1
+#output=<folder name, defaults to /var/log>
+#init=/path/to/init-binary
+#pss=0
+#entropy=0
+#scale_x=100
+#scale_y=20
diff --git a/src/bootchart/bootchart.h b/src/bootchart/bootchart.h
new file mode 100644
index 0000000000..0d8bed16b0
--- /dev/null
+++ b/src/bootchart/bootchart.h
@@ -0,0 +1,117 @@
+/*
+ * bootchart.h
+ *
+ * Copyright (C) 2009-2012 Intel Coproration
+ *
+ * Authors:
+ * Auke Kok <auke-jan.h.kok@intel.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; version 2
+ * of the License.
+ */
+
+#include <dirent.h>
+
+#define MAXCPUS 16
+#define MAXPIDS 65535
+#define MAXSAMPLES 8192
+
+
+struct block_stat_struct {
+ /* /proc/vmstat pgpgin & pgpgout */
+ int bi;
+ int bo;
+};
+
+struct cpu_stat_sample_struct {
+ /* /proc/schedstat fields 10 & 11 (after name) */
+ double runtime;
+ double waittime;
+};
+
+struct cpu_stat_struct {
+ /* per cpu array */
+ struct cpu_stat_sample_struct sample[MAXSAMPLES];
+};
+
+/* per process, per sample data we will log */
+struct ps_sched_struct {
+ /* /proc/<n>/schedstat fields 1 & 2 */
+ double runtime;
+ double waittime;
+ int pss;
+};
+
+/* process info */
+struct ps_struct {
+ struct ps_struct *next_ps; /* SLL pointer */
+ struct ps_struct *parent; /* ppid ref */
+ struct ps_struct *children; /* children */
+ struct ps_struct *next; /* siblings */
+
+ /* must match - otherwise it's a new process with same PID */
+ char name[16];
+ int pid;
+ int ppid;
+
+ /* cache fd's */
+ int sched;
+ int schedstat;
+ FILE *smaps;
+
+ /* index to first/last seen timestamps */
+ int first;
+ int last;
+
+ /* records actual start time, may be way before bootchart runs */
+ double starttime;
+
+ /* record human readable total cpu time */
+ double total;
+
+ /* largest PSS size found */
+ int pss_max;
+
+ /* for drawing connection lines later */
+ double pos_x;
+ double pos_y;
+
+ struct ps_sched_struct *sample;
+};
+
+extern int entropy_avail[];
+
+extern double graph_start;
+extern double log_start;
+extern double sampletime[];
+extern struct ps_struct *ps_first;
+extern struct block_stat_struct blockstat[];
+extern struct cpu_stat_struct cpustat[];
+extern int pscount;
+extern int relative;
+extern int filter;
+extern int pss;
+extern int entropy;
+extern int initcall;
+extern int samples;
+extern int cpus;
+extern int len;
+extern double hz;
+extern double scale_x;
+extern double scale_y;
+extern int overrun;
+extern double interval;
+
+extern char output_path[PATH_MAX];
+extern char init_path[PATH_MAX];
+
+extern FILE *of;
+extern DIR *proc;
+
+extern double gettime_ns(void);
+extern void log_uptime(void);
+extern void log_sample(int sample);
+
+extern void svg_do(void);
diff --git a/src/bootchart/log.c b/src/bootchart/log.c
new file mode 100644
index 0000000000..89c7b3523c
--- /dev/null
+++ b/src/bootchart/log.c
@@ -0,0 +1,420 @@
+/*
+ * log.c
+ *
+ * Copyright (C) 2009-2012 Intel Coproration
+ *
+ * Authors:
+ * Auke Kok <auke-jan.h.kok@intel.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; version 2
+ * of the License.
+ */
+
+#define _GNU_SOURCE 1
+#include <unistd.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <stdio.h>
+#include <string.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <time.h>
+
+
+#include "bootchart.h"
+
+/*
+ * Alloc a static 4k buffer for stdio - primarily used to increase
+ * PSS buffering from the default 1k stdin buffer to reduce
+ * read() overhead.
+ */
+static char smaps_buf[4096];
+DIR *proc;
+
+
+double gettime_ns(void)
+{
+ struct timespec now;
+
+ clock_gettime(CLOCK_MONOTONIC, &now);
+
+ return (now.tv_sec + (now.tv_nsec / 1000000000.0));
+}
+
+
+void log_uptime(void)
+{
+ FILE *f;
+ char str[32];
+ double uptime;
+
+ f = fopen("/proc/uptime", "r");
+ if (!f)
+ return;
+ if (!fscanf(f, "%s %*s", str)) {
+ fclose(f);
+ return;
+ }
+ fclose(f);
+ uptime = strtod(str, NULL);
+
+ log_start = gettime_ns();
+
+ /* start graph at kernel boot time */
+ if (relative)
+ graph_start = log_start;
+ else
+ graph_start = log_start - uptime;
+}
+
+
+static char *bufgetline(char *buf)
+{
+ char *c;
+
+ if (!buf)
+ return NULL;
+
+ c = strchr(buf, '\n');
+ if (c)
+ c++;
+ return c;
+}
+
+
+void log_sample(int sample)
+{
+ static int vmstat;
+ static int schedstat;
+ FILE *st;
+ char buf[4095];
+ char key[256];
+ char val[256];
+ char rt[256];
+ char wt[256];
+ char *m;
+ int c;
+ int p;
+ int mod;
+ static int e_fd;
+ ssize_t s;
+ ssize_t n;
+ struct dirent *ent;
+
+ if (!vmstat) {
+ /* block stuff */
+ vmstat = open("/proc/vmstat", O_RDONLY);
+ if (vmstat == -1) {
+ perror("open /proc/vmstat");
+ exit (EXIT_FAILURE);
+ }
+ }
+
+ n = pread(vmstat, buf, sizeof(buf) - 1, 0);
+ if (n <= 0) {
+ close(vmstat);
+ return;
+ }
+ buf[n] = '\0';
+
+ m = buf;
+ while (m) {
+ if (sscanf(m, "%s %s", key, val) < 2)
+ goto vmstat_next;
+ if (!strcmp(key, "pgpgin"))
+ blockstat[sample].bi = atoi(val);
+ if (!strcmp(key, "pgpgout")) {
+ blockstat[sample].bo = atoi(val);
+ break;
+ }
+vmstat_next:
+ m = bufgetline(m);
+ if (!m)
+ break;
+ }
+
+ if (!schedstat) {
+ /* overall CPU utilization */
+ schedstat = open("/proc/schedstat", O_RDONLY);
+ if (schedstat == -1) {
+ perror("open /proc/schedstat");
+ exit (EXIT_FAILURE);
+ }
+ }
+
+ n = pread(schedstat, buf, sizeof(buf) - 1, 0);
+ if (n <= 0) {
+ close(schedstat);
+ return;
+ }
+ buf[n] = '\0';
+
+ m = buf;
+ while (m) {
+ if (sscanf(m, "%s %*s %*s %*s %*s %*s %*s %s %s", key, rt, wt) < 3)
+ goto schedstat_next;
+
+ if (strstr(key, "cpu")) {
+ c = atoi((const char*)(key+3));
+ if (c > MAXCPUS)
+ /* Oops, we only have room for MAXCPUS data */
+ break;
+ cpustat[c].sample[sample].runtime = atoll(rt);
+ cpustat[c].sample[sample].waittime = atoll(wt);
+
+ if (c == cpus)
+ cpus = c + 1;
+ }
+schedstat_next:
+ m = bufgetline(m);
+ if (!m)
+ break;
+ }
+
+ if (entropy) {
+ if (!e_fd) {
+ e_fd = open("/proc/sys/kernel/random/entropy_avail", O_RDONLY);
+ }
+
+ if (e_fd) {
+ n = pread(e_fd, buf, sizeof(buf) - 1, 0);
+ if (n > 0)
+ entropy_avail[sample] = atoi(buf);
+ }
+ }
+
+ /* all the per-process stuff goes here */
+ if (!proc) {
+ /* find all processes */
+ proc = opendir("/proc");
+ if (!proc)
+ return;
+ } else {
+ rewinddir(proc);
+ }
+
+ while ((ent = readdir(proc)) != NULL) {
+ char filename[PATH_MAX];
+ int pid;
+ struct ps_struct *ps;
+
+ if ((ent->d_name[0] < '0') || (ent->d_name[0] > '9'))
+ continue;
+
+ pid = atoi(ent->d_name);
+
+ if (pid >= MAXPIDS)
+ continue;
+
+ ps = ps_first;
+ while (ps->next_ps) {
+ ps = ps->next_ps;
+ if (ps->pid == pid)
+ break;
+ }
+
+ /* end of our LL? then append a new record */
+ if (ps->pid != pid) {
+ char t[32];
+ struct ps_struct *parent;
+
+ ps->next_ps = malloc(sizeof(struct ps_struct));
+ if (!ps->next_ps) {
+ perror("malloc(ps_struct)");
+ exit (EXIT_FAILURE);
+ }
+ memset(ps->next_ps, 0, sizeof(struct ps_struct));
+ ps = ps->next_ps;
+ ps->pid = pid;
+
+ ps->sample = malloc(sizeof(struct ps_sched_struct) * (len + 1));
+ if (!ps->sample) {
+ perror("malloc(ps_struct)");
+ exit (EXIT_FAILURE);
+ }
+ memset(ps->sample, 0, sizeof(struct ps_sched_struct) * (len + 1));
+
+ pscount++;
+
+ /* mark our first sample */
+ ps->first = sample;
+
+ /* get name, start time */
+ if (!ps->sched) {
+ sprintf(filename, "/proc/%d/sched", pid);
+ ps->sched = open(filename, O_RDONLY);
+ if (ps->sched == -1)
+ continue;
+ }
+
+ s = pread(ps->sched, buf, sizeof(buf) - 1, 0);
+ if (s <= 0) {
+ close(ps->sched);
+ continue;
+ }
+
+ if (!sscanf(buf, "%s %*s %*s", key))
+ continue;
+
+ strncpy(ps->name, key, 16);
+ /* discard line 2 */
+ m = bufgetline(buf);
+ if (!m)
+ continue;
+
+ m = bufgetline(m);
+ if (!m)
+ continue;
+
+ if (!sscanf(m, "%*s %*s %s", t))
+ continue;
+
+ ps->starttime = strtod(t, NULL) / 1000.0;
+
+ /* ppid */
+ sprintf(filename, "/proc/%d/stat", pid);
+ st = fopen(filename, "r");
+ if (!st)
+ continue;
+ if (!fscanf(st, "%*s %*s %*s %i", &p)) {
+ fclose(st);
+ continue;
+ }
+ fclose(st);
+ ps->ppid = p;
+
+ /*
+ * setup child pointers
+ *
+ * these are used to paint the tree coherently later
+ * each parent has a LL of children, and a LL of siblings
+ */
+ if (pid == 1)
+ continue; /* nothing to do for init atm */
+
+ /* kthreadd has ppid=0, which breaks our tree ordering */
+ if (ps->ppid == 0)
+ ps->ppid = 1;
+
+ parent = ps_first;
+ while ((parent->next_ps && parent->pid != ps->ppid))
+ parent = parent->next_ps;
+
+ if ((!parent) || (parent->pid != ps->ppid)) {
+ /* orphan */
+ ps->ppid = 1;
+ parent = ps_first->next_ps;
+ }
+
+ ps->parent = parent;
+
+ if (!parent->children) {
+ /* it's the first child */
+ parent->children = ps;
+ } else {
+ /* walk all children and append */
+ struct ps_struct *children;
+ children = parent->children;
+ while (children->next)
+ children = children->next;
+ children->next = ps;
+ }
+ }
+
+ /* else -> found pid, append data in ps */
+
+ /* below here is all continuous logging parts - we get here on every
+ * iteration */
+
+ /* rt, wt */
+ if (!ps->schedstat) {
+ sprintf(filename, "/proc/%d/schedstat", pid);
+ ps->schedstat = open(filename, O_RDONLY);
+ if (ps->schedstat == -1)
+ continue;
+ }
+
+ if (pread(ps->schedstat, buf, sizeof(buf) - 1, 0) <= 0) {
+ /* clean up our file descriptors - assume that the process exited */
+ close(ps->schedstat);
+ if (ps->sched)
+ close(ps->sched);
+ //if (ps->smaps)
+ // fclose(ps->smaps);
+ continue;
+ }
+ if (!sscanf(buf, "%s %s %*s", rt, wt))
+ continue;
+
+ ps->last = sample;
+ ps->sample[sample].runtime = atoll(rt);
+ ps->sample[sample].waittime = atoll(wt);
+
+ ps->total = (ps->sample[ps->last].runtime
+ - ps->sample[ps->first].runtime)
+ / 1000000000.0;
+
+ if (!pss)
+ goto catch_rename;
+ /* Pss */
+ if (!ps->smaps) {
+ sprintf(filename, "/proc/%d/smaps", pid);
+ ps->smaps = fopen(filename, "r");
+ setvbuf(ps->smaps, smaps_buf, _IOFBF, sizeof(smaps_buf));
+ if (!ps->smaps)
+ continue;
+ } else {
+ rewind(ps->smaps);
+ }
+
+ while (1) {
+ int pss_kb;
+
+ /* skip one line, this contains the object mapped */
+ if (fgets(buf, sizeof(buf), ps->smaps) == NULL)
+ break;
+ /* then there's a 28 char 14 line block */
+ if (fread(buf, 1, 28 * 14, ps->smaps) != 28 * 14)
+ break;
+
+ pss_kb = atoi(&buf[61]);
+ ps->sample[sample].pss += pss_kb;
+ }
+
+ if (ps->sample[sample].pss > ps->pss_max)
+ ps->pss_max = ps->sample[sample].pss;
+
+catch_rename:
+ /* catch process rename, try to randomize time */
+ mod = (hz < 4.0) ? 4.0 : (hz / 4.0);
+ if (((samples - ps->first) + pid) % (int)(mod) == 0) {
+
+ /* re-fetch name */
+ /* get name, start time */
+ if (!ps->sched) {
+ sprintf(filename, "/proc/%d/sched", pid);
+ ps->sched = open(filename, O_RDONLY);
+ if (ps->sched == -1)
+ continue;
+ }
+ if (pread(ps->sched, buf, sizeof(buf) - 1, 0) <= 0) {
+ /* clean up file descriptors */
+ close(ps->sched);
+ if (ps->schedstat)
+ close(ps->schedstat);
+ //if (ps->smaps)
+ // fclose(ps->smaps);
+ continue;
+ }
+
+ if (!sscanf(buf, "%s %*s %*s", key))
+ continue;
+
+ strncpy(ps->name, key, 16);
+ }
+ }
+}
diff --git a/src/bootchart/svg.c b/src/bootchart/svg.c
new file mode 100644
index 0000000000..68ec5399ac
--- /dev/null
+++ b/src/bootchart/svg.c
@@ -0,0 +1,1120 @@
+/*
+ * svg.c
+ *
+ * Copyright (C) 2009-2012 Intel Coproration
+ *
+ * Authors:
+ * Auke Kok <auke-jan.h.kok@intel.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; version 2
+ * of the License.
+ */
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <limits.h>
+#include <unistd.h>
+#include <sys/utsname.h>
+
+#include "bootchart.h"
+
+
+#define time_to_graph(t) ((t) * scale_x)
+#define ps_to_graph(n) ((n) * scale_y)
+#define kb_to_graph(m) ((m) * scale_y * 0.0001)
+#define to_color(n) (192.0 - ((n) * 192.0))
+
+#define max(x, y) (((x) > (y)) ? (x) : (y))
+#define min(x, y) (((x) < (y)) ? (x) : (y))
+
+static char str[8092];
+
+#define svg(a...) do { snprintf(str, 8092, ## a); fputs(str, of); fflush(of); } while (0)
+
+static const char *colorwheel[12] = {
+ "rgb(255,32,32)", // red
+ "rgb(32,192,192)", // cyan
+ "rgb(255,128,32)", // orange
+ "rgb(128,32,192)", // blue-violet
+ "rgb(255,255,32)", // yellow
+ "rgb(192,32,128)", // red-violet
+ "rgb(32,255,32)", // green
+ "rgb(255,64,32)", // red-orange
+ "rgb(32,32,255)", // blue
+ "rgb(255,192,32)", // yellow-orange
+ "rgb(192,32,192)", // violet
+ "rgb(32,192,32)" // yellow-green
+};
+
+static double idletime = -1.0;
+static int pfiltered = 0;
+static int pcount = 0;
+static int kcount = 0;
+static float psize = 0;
+static float ksize = 0;
+static float esize = 0;
+
+
+static void svg_header(void)
+{
+ float w;
+ float h;
+
+ /* min width is about 1600px due to the label */
+ w = 150.0 + 10.0 + time_to_graph(sampletime[samples-1] - graph_start);
+ w = ((w < 1600.0) ? 1600.0 : w);
+
+ /* height is variable based on pss, psize, ksize */
+ h = 400.0 + (scale_y * 30.0) /* base graphs and title */
+ + (pss ? (100.0 * scale_y) + (scale_y * 7.0) : 0.0) /* pss estimate */
+ + psize + ksize + esize;
+
+ svg("<?xml version=\"1.0\" standalone=\"no\"?>\n");
+ svg("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" ");
+ svg("\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
+
+ //svg("<g transform=\"translate(10,%d)\">\n", 1000 + 150 + (pcount * 20));
+ svg("<svg width=\"%.0fpx\" height=\"%.0fpx\" version=\"1.1\" ",
+ w, h);
+ svg("xmlns=\"http://www.w3.org/2000/svg\">\n\n");
+
+ /* write some basic info as a comment, including some help */
+ svg("<!-- This file is a bootchart SVG file. It is best rendered in a browser -->\n");
+ svg("<!-- such as Chrome/Chromium, firefox. Other applications that render -->\n");
+ svg("<!-- these files properly but much more slow are ImageMagick, gimp, -->\n");
+ svg("<!-- inkscape, etc.. To display the files on your system, just point -->\n");
+ svg("<!-- your browser to file:///var/log/ and click. This bootchart was -->\n\n");
+
+ svg("<!-- generated by bootchart version %s, running with options: -->\n", VERSION);
+ svg("<!-- hz=\"%f\" n=\"%d\" -->\n", hz, len);
+ svg("<!-- x=\"%f\" y=\"%f\" -->\n", scale_x, scale_y);
+ svg("<!-- rel=\"%d\" f=\"%d\" -->\n", relative, filter);
+ svg("<!-- p=\"%d\" e=\"%d\" -->\n", pss, entropy);
+ svg("<!-- o=\"%s\" i=\"%s\" -->\n\n", output_path, init_path);
+
+ /* style sheet */
+ svg("<defs>\n <style type=\"text/css\">\n <![CDATA[\n");
+
+ svg(" rect { stroke-width: 1; }\n");
+ svg(" rect.cpu { fill: rgb(64,64,240); stroke-width: 0; fill-opacity: 0.7; }\n");
+ svg(" rect.wait { fill: rgb(240,240,0); stroke-width: 0; fill-opacity: 0.7; }\n");
+ svg(" rect.bi { fill: rgb(240,128,128); stroke-width: 0; fill-opacity: 0.7; }\n");
+ svg(" rect.bo { fill: rgb(192,64,64); stroke-width: 0; fill-opacity: 0.7; }\n");
+ svg(" rect.ps { fill: rgb(192,192,192); stroke: rgb(128,128,128); fill-opacity: 0.7; }\n");
+ svg(" rect.krnl { fill: rgb(240,240,0); stroke: rgb(128,128,128); fill-opacity: 0.7; }\n");
+ svg(" rect.box { fill: rgb(240,240,240); stroke: rgb(192,192,192); }\n");
+ svg(" rect.clrw { stroke-width: 0; fill-opacity: 0.7;}\n");
+ svg(" line { stroke: rgb(64,64,64); stroke-width: 1; }\n");
+ svg("// line.sec1 { }\n");
+ svg(" line.sec5 { stroke-width: 2; }\n");
+ svg(" line.sec01 { stroke: rgb(224,224,224); stroke-width: 1; }\n");
+ svg(" line.dot { stroke-dasharray: 2 4; }\n");
+ svg(" line.idle { stroke: rgb(64,64,64); stroke-dasharray: 10 6; stroke-opacity: 0.7; }\n");
+
+ svg(" .run { font-size: 8; font-style: italic; }\n");
+ svg(" text { font-family: Verdana, Helvetica; font-size: 10; }\n");
+ svg(" text.sec { font-size: 8; }\n");
+ svg(" text.t1 { font-size: 24; }\n");
+ svg(" text.t2 { font-size: 12; }\n");
+ svg(" text.idle { font-size: 18; }\n");
+
+ svg(" ]]>\n </style>\n</defs>\n\n");
+
+}
+
+
+static void svg_title(void)
+{
+ char cmdline[256] = "";
+ char filename[PATH_MAX];
+ char buf[256];
+ char rootbdev[16] = "Unknown";
+ char model[256] = "Unknown";
+ char date[256] = "Unknown";
+ char cpu[256] = "Unknown";
+ char build[256] = "Unknown";
+ char *c;
+ FILE *f;
+ time_t t;
+ struct utsname uts;
+
+ /* grab /proc/cmdline */
+ f = fopen("/proc/cmdline", "r");
+ if (f) {
+ if (!fgets(cmdline, 255, f))
+ sprintf(cmdline, "Unknown");
+ fclose(f);
+ }
+
+ /* extract root fs so we can find disk model name in sysfs */
+ c = strstr(cmdline, "root=/dev/");
+ if (c) {
+ strncpy(rootbdev, &c[10], 3);
+ rootbdev[3] = '\0';
+ }
+ sprintf(filename, "/sys/block/%s/device/model", rootbdev);
+ f = fopen(filename, "r");
+ if (f) {
+ if (!fgets(model, 255, f))
+ fprintf(stderr, "Error reading disk model for %s\n", rootbdev);
+ fclose(f);
+ }
+
+ /* various utsname parameters */
+ if (uname(&uts))
+ fprintf(stderr, "Error getting uname info\n");
+
+ /* date */
+ t = time(NULL);
+ strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", localtime(&t));
+
+ /* CPU type */
+ f = fopen("/proc/cpuinfo", "r");
+ if (f) {
+ while (fgets(buf, 255, f)) {
+ if (strstr(buf, "model name")) {
+ strncpy(cpu, &buf[13], 255);
+ break;
+ }
+ }
+ fclose(f);
+ }
+
+ /* Build - 1st line from /etc/system-release */
+ f = fopen("/etc/system-release", "r");
+ if (f) {
+ if (fgets(buf, 255, f))
+ strncpy(build, buf, 255);
+ fclose(f);
+ }
+
+ svg("<text class=\"t1\" x=\"0\" y=\"30\">Bootchart for %s - %s</text>\n",
+ uts.nodename, date);
+ svg("<text class=\"t2\" x=\"20\" y=\"50\">System: %s %s %s %s</text>\n",
+ uts.sysname, uts.release, uts.version, uts.machine);
+ svg("<text class=\"t2\" x=\"20\" y=\"65\">CPU: %s</text>\n",
+ cpu);
+ svg("<text class=\"t2\" x=\"20\" y=\"80\">Disk: %s</text>\n",
+ model);
+ svg("<text class=\"t2\" x=\"20\" y=\"95\">Boot options: %s</text>\n",
+ cmdline);
+ svg("<text class=\"t2\" x=\"20\" y=\"110\">Build: %s</text>\n",
+ build);
+ svg("<text class=\"t2\" x=\"20\" y=\"125\">Log start time: %.03fs</text>\n", log_start);
+ svg("<text class=\"t2\" x=\"20\" y=\"140\">Idle time: ");
+
+ if (idletime >= 0.0)
+ svg("%.03fs", idletime);
+ else
+ svg("Not detected");
+ svg("</text>\n");
+ svg("<text class=\"sec\" x=\"20\" y=\"155\">Graph data: %.03f samples/sec, recorded %i total, dropped %i samples, %i processes, %i filtered</text>\n",
+ hz, len, overrun, pscount, pfiltered);
+}
+
+
+static void svg_graph_box(int height)
+{
+ double d = 0.0;
+ int i = 0;
+
+ /* outside box, fill */
+ svg("<rect class=\"box\" x=\"%.03f\" y=\"0\" width=\"%.03f\" height=\"%.03f\" />\n",
+ time_to_graph(0.0),
+ time_to_graph(sampletime[samples-1] - graph_start),
+ ps_to_graph(height));
+
+ for (d = graph_start; d <= sampletime[samples-1];
+ d += (scale_x < 2.0 ? 60.0 : scale_x < 10.0 ? 1.0 : 0.1)) {
+ /* lines for each second */
+ if (i % 50 == 0)
+ svg(" <line class=\"sec5\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n",
+ time_to_graph(d - graph_start),
+ time_to_graph(d - graph_start),
+ ps_to_graph(height));
+ else if (i % 10 == 0)
+ svg(" <line class=\"sec1\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n",
+ time_to_graph(d - graph_start),
+ time_to_graph(d - graph_start),
+ ps_to_graph(height));
+ else
+ svg(" <line class=\"sec01\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n",
+ time_to_graph(d - graph_start),
+ time_to_graph(d - graph_start),
+ ps_to_graph(height));
+
+ /* time label */
+ if (i % 10 == 0)
+ svg(" <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
+ time_to_graph(d - graph_start),
+ -5.0,
+ d - graph_start);
+
+ i++;
+ }
+}
+
+
+static void svg_pss_graph(void)
+{
+ struct ps_struct *ps;
+ int i;
+
+ svg("\n\n<!-- Pss memory size graph -->\n");
+
+ svg("\n <text class=\"t2\" x=\"5\" y=\"-15\">Memory allocation - Pss</text>\n");
+
+ /* vsize 1000 == 1000mb */
+ svg_graph_box(100);
+ /* draw some hlines for usable memory sizes */
+ for (i = 100000; i < 1000000; i += 100000) {
+ svg(" <line class=\"sec01\" x1=\"%.03f\" y1=\"%.0f\" x2=\"%.03f\" y2=\"%.0f\"/>\n",
+ time_to_graph(.0),
+ kb_to_graph(i),
+ time_to_graph(sampletime[samples-1] - graph_start),
+ kb_to_graph(i));
+ svg(" <text class=\"sec\" x=\"%.03f\" y=\"%.0f\">%dM</text>\n",
+ time_to_graph(sampletime[samples-1] - graph_start) + 5,
+ kb_to_graph(i), (1000000 - i) / 1000);
+ }
+ svg("\n");
+
+ /* now plot the graph itself */
+ for (i = 1; i < samples ; i++) {
+ int bottom;
+ int top;
+
+ bottom = 0;
+ top = 0;
+
+ /* put all the small pss blocks into the bottom */
+ ps = ps_first;
+ while (ps->next_ps) {
+ ps = ps->next_ps;
+ if (!ps)
+ continue;
+ if (ps->sample[i].pss <= (100 * scale_y))
+ top += ps->sample[i].pss;
+ };
+ svg(" <rect class=\"clrw\" style=\"fill: %s\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
+ "rgb(64,64,64)",
+ time_to_graph(sampletime[i - 1] - graph_start),
+ kb_to_graph(1000000.0 - top),
+ time_to_graph(sampletime[i] - sampletime[i - 1]),
+ kb_to_graph(top - bottom));
+
+ bottom = top;
+
+ /* now plot the ones that are of significant size */
+ ps = ps_first;
+ while (ps->next_ps) {
+ ps = ps->next_ps;
+ if (!ps)
+ continue;
+ /* don't draw anything smaller than 2mb */
+ if (ps->sample[i].pss > (100 * scale_y)) {
+ top = bottom + ps->sample[i].pss;
+ svg(" <rect class=\"clrw\" style=\"fill: %s\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
+ colorwheel[ps->pid % 12],
+ time_to_graph(sampletime[i - 1] - graph_start),
+ kb_to_graph(1000000.0 - top),
+ time_to_graph(sampletime[i] - sampletime[i - 1]),
+ kb_to_graph(top - bottom));
+ bottom = top;
+ }
+ }
+ }
+
+ /* overlay all the text labels */
+ for (i = 1; i < samples ; i++) {
+ int bottom;
+ int top;
+
+ bottom = 0;
+ top = 0;
+
+ /* put all the small pss blocks into the bottom */
+ ps = ps_first;
+ while (ps->next_ps) {
+ ps = ps->next_ps;
+ if (!ps)
+ continue;
+ if (ps->sample[i].pss <= (100 * scale_y))
+ top += ps->sample[i].pss;
+ };
+
+ bottom = top;
+
+ /* now plot the ones that are of significant size */
+ ps = ps_first;
+ while (ps->next_ps) {
+ ps = ps->next_ps;
+ if (!ps)
+ continue;
+ /* don't draw anything smaller than 2mb */
+ if (ps->sample[i].pss > (100 * scale_y)) {
+ top = bottom + ps->sample[i].pss;
+ /* draw a label with the process / PID */
+ if ((i == 1) || (ps->sample[i - 1].pss <= (100 * scale_y)))
+ svg(" <text x=\"%.03f\" y=\"%.03f\">%s [%i]</text>\n",
+ time_to_graph(sampletime[i] - graph_start),
+ kb_to_graph(1000000.0 - bottom - ((top - bottom) / 2)),
+ ps->name,
+ ps->pid);
+ bottom = top;
+ }
+ }
+ }
+
+ /* debug output - full data dump */
+ svg("\n\n<!-- PSS map - csv format -->\n");
+ ps = ps_first;
+ while (ps->next_ps) {
+ ps = ps->next_ps;
+ if (!ps)
+ continue;
+ svg("<!-- %s [%d] pss=", ps->name, ps->pid);
+ for (i = 0; i < samples ; i++) {
+ svg("%d," , ps->sample[i].pss);
+ }
+ svg(" -->\n");
+ }
+
+}
+
+static void svg_io_bi_bar(void)
+{
+ double max = 0.0;
+ double range;
+ int max_here = 0;
+ int i;
+
+ svg("<!-- IO utilization graph - In -->\n");
+
+ svg("<text class=\"t2\" x=\"5\" y=\"-15\">IO utilization - read</text>\n");
+
+ /*
+ * calculate rounding range
+ *
+ * We need to round IO data since IO block data is not updated on
+ * each poll. Applying a smoothing function loses some burst data,
+ * so keep the smoothing range short.
+ */
+ range = 0.25 / (1.0 / hz);
+ if (range < 2.0)
+ range = 2.0; /* no smoothing */
+
+ /* surrounding box */
+ svg_graph_box(5);
+
+ /* find the max IO first */
+ for (i = 1; i < samples; i++) {
+ int start;
+ int stop;
+ double tot;
+
+ start = max(i - ((range / 2) - 1), 0);
+ stop = min(i + (range / 2), samples - 1);
+
+ tot = (double)(blockstat[stop].bi - blockstat[start].bi)
+ / (stop - start);
+ if (tot > max) {
+ max = tot;
+ max_here = i;
+ }
+ tot = (double)(blockstat[stop].bo - blockstat[start].bo)
+ / (stop - start);
+ if (tot > max)
+ max = tot;
+ }
+
+ /* plot bi */
+ for (i = 1; i < samples; i++) {
+ int start;
+ int stop;
+ double tot;
+ double pbi;
+
+ start = max(i - ((range / 2) - 1), 0);
+ stop = min(i + (range / 2), samples);
+
+ tot = (double)(blockstat[stop].bi - blockstat[start].bi)
+ / (stop - start);
+ pbi = tot / max;
+
+ if (pbi > 0.001)
+ svg("<rect class=\"bi\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
+ time_to_graph(sampletime[i - 1] - graph_start),
+ (scale_y * 5) - (pbi * (scale_y * 5)),
+ time_to_graph(sampletime[i] - sampletime[i - 1]),
+ pbi * (scale_y * 5));
+
+ /* labels around highest value */
+ if (i == max_here) {
+ svg(" <text class=\"sec\" x=\"%.03f\" y=\"%.03f\">%0.2fmb/sec</text>\n",
+ time_to_graph(sampletime[i] - graph_start) + 5,
+ ((scale_y * 5) - (pbi * (scale_y * 5))) + 15,
+ max / 1024.0 / (interval / 1000000000.0));
+ }
+ }
+}
+
+static void svg_io_bo_bar(void)
+{
+ double max = 0.0;
+ double range;
+ int max_here = 0;
+ int i;
+
+ svg("<!-- IO utilization graph - out -->\n");
+
+ svg("<text class=\"t2\" x=\"5\" y=\"-15\">IO utilization - write</text>\n");
+
+ /*
+ * calculate rounding range
+ *
+ * We need to round IO data since IO block data is not updated on
+ * each poll. Applying a smoothing function loses some burst data,
+ * so keep the smoothing range short.
+ */
+ range = 0.25 / (1.0 / hz);
+ if (range < 2.0)
+ range = 2.0; /* no smoothing */
+
+ /* surrounding box */
+ svg_graph_box(5);
+
+ /* find the max IO first */
+ for (i = 1; i < samples; i++) {
+ int start;
+ int stop;
+ double tot;
+
+ start = max(i - ((range / 2) - 1), 0);
+ stop = min(i + (range / 2), samples - 1);
+
+ tot = (double)(blockstat[stop].bi - blockstat[start].bi)
+ / (stop - start);
+ if (tot > max)
+ max = tot;
+ tot = (double)(blockstat[stop].bo - blockstat[start].bo)
+ / (stop - start);
+ if (tot > max) {
+ max = tot;
+ max_here = i;
+ }
+ }
+
+ /* plot bo */
+ for (i = 1; i < samples; i++) {
+ int start;
+ int stop;
+ double tot;
+ double pbo;
+
+ start = max(i - ((range / 2) - 1), 0);
+ stop = min(i + (range / 2), samples);
+
+ tot = (double)(blockstat[stop].bo - blockstat[start].bo)
+ / (stop - start);
+ pbo = tot / max;
+
+ if (pbo > 0.001)
+ svg("<rect class=\"bo\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
+ time_to_graph(sampletime[i - 1] - graph_start),
+ (scale_y * 5) - (pbo * (scale_y * 5)),
+ time_to_graph(sampletime[i] - sampletime[i - 1]),
+ pbo * (scale_y * 5));
+
+ /* labels around highest bo value */
+ if (i == max_here) {
+ svg(" <text class=\"sec\" x=\"%.03f\" y=\"%.03f\">%0.2fmb/sec</text>\n",
+ time_to_graph(sampletime[i] - graph_start) + 5,
+ ((scale_y * 5) - (pbo * (scale_y * 5))),
+ max / 1024.0 / (interval / 1000000000.0));
+ }
+ }
+}
+
+
+static void svg_cpu_bar(void)
+{
+ int i;
+
+ svg("<!-- CPU utilization graph -->\n");
+
+ svg("<text class=\"t2\" x=\"5\" y=\"-15\">CPU utilization</text>\n");
+ /* surrounding box */
+ svg_graph_box(5);
+
+ /* bars for each sample, proportional to the CPU util. */
+ for (i = 1; i < samples; i++) {
+ int c;
+ double trt;
+ double ptrt;
+
+ ptrt = trt = 0.0;
+
+ for (c = 0; c < cpus; c++)
+ trt += cpustat[c].sample[i].runtime - cpustat[c].sample[i - 1].runtime;
+
+ trt = trt / 1000000000.0;
+
+ trt = trt / (double)cpus;
+
+ if (trt > 0.0)
+ ptrt = trt / (sampletime[i] - sampletime[i - 1]);
+
+ if (ptrt > 1.0)
+ ptrt = 1.0;
+
+ if (ptrt > 0.001) {
+ svg("<rect class=\"cpu\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
+ time_to_graph(sampletime[i - 1] - graph_start),
+ (scale_y * 5) - (ptrt * (scale_y * 5)),
+ time_to_graph(sampletime[i] - sampletime[i - 1]),
+ ptrt * (scale_y * 5));
+ }
+ }
+}
+
+static void svg_wait_bar(void)
+{
+ int i;
+
+ svg("<!-- Wait time aggregation box -->\n");
+
+ svg("<text class=\"t2\" x=\"5\" y=\"-15\">CPU wait</text>\n");
+
+ /* surrounding box */
+ svg_graph_box(5);
+
+ /* bars for each sample, proportional to the CPU util. */
+ for (i = 1; i < samples; i++) {
+ int c;
+ double twt;
+ double ptwt;
+
+ ptwt = twt = 0.0;
+
+ for (c = 0; c < cpus; c++)
+ twt += cpustat[c].sample[i].waittime - cpustat[c].sample[i - 1].waittime;
+
+ twt = twt / 1000000000.0;
+
+ twt = twt / (double)cpus;
+
+ if (twt > 0.0)
+ ptwt = twt / (sampletime[i] - sampletime[i - 1]);
+
+ if (ptwt > 1.0)
+ ptwt = 1.0;
+
+ if (ptwt > 0.001) {
+ svg("<rect class=\"wait\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
+ time_to_graph(sampletime[i - 1] - graph_start),
+ ((scale_y * 5) - (ptwt * (scale_y * 5))),
+ time_to_graph(sampletime[i] - sampletime[i - 1]),
+ ptwt * (scale_y * 5));
+ }
+ }
+}
+
+
+static void svg_entropy_bar(void)
+{
+ int i;
+
+ svg("<!-- entropy pool graph -->\n");
+
+ svg("<text class=\"t2\" x=\"5\" y=\"-15\">Entropy pool size</text>\n");
+ /* surrounding box */
+ svg_graph_box(5);
+
+ /* bars for each sample, scale 0-4096 */
+ for (i = 1; i < samples; i++) {
+ /* svg("<!-- entropy %.03f %i -->\n", sampletime[i], entropy_avail[i]); */
+ svg("<rect class=\"cpu\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
+ time_to_graph(sampletime[i - 1] - graph_start),
+ ((scale_y * 5) - ((entropy_avail[i] / 4096.) * (scale_y * 5))),
+ time_to_graph(sampletime[i] - sampletime[i - 1]),
+ (entropy_avail[i] / 4096.) * (scale_y * 5));
+ }
+}
+
+
+static struct ps_struct *get_next_ps(struct ps_struct *ps)
+{
+ /*
+ * walk the list of processes and return the next one to be
+ * painted
+ */
+ if (ps == ps_first)
+ return ps->next_ps;
+
+ /* go deep */
+ if (ps->children)
+ return ps->children;
+
+ /* find siblings */
+ if (ps->next)
+ return ps->next;
+
+ /* go back for parent siblings */
+ while (1) {
+ if (ps->parent)
+ if (ps->parent->next)
+ return ps->parent->next;
+ ps = ps->parent;
+ if (!ps)
+ return ps;
+ }
+
+ return NULL;
+}
+
+
+static int ps_filter(struct ps_struct *ps)
+{
+ if (!filter)
+ return 0;
+
+ /* can't draw data when there is only 1 sample (need start + stop) */
+ if (ps->first == ps->last)
+ return -1;
+
+ /* don't filter kthreadd */
+ if (ps->pid == 2)
+ return 0;
+
+ /* drop stuff that doesn't use any real CPU time */
+ if (ps->total <= 0.001)
+ return -1;
+
+ return 0;
+}
+
+
+static void svg_do_initcall(int count_only)
+{
+ FILE *f;
+ double t;
+ char func[256];
+ int ret;
+ int usecs;
+
+ /* can't plot initcall when disabled or in relative mode */
+ if (!initcall || relative) {
+ kcount = 0;
+ return;
+ }
+
+ if (!count_only) {
+ svg("<!-- initcall -->\n");
+
+ svg("<text class=\"t2\" x=\"5\" y=\"-15\">Kernel init threads</text>\n");
+ /* surrounding box */
+ svg_graph_box(kcount);
+ }
+
+ kcount = 0;
+
+ /*
+ * Initcall graphing - parses dmesg buffer and displays kernel threads
+ * This somewhat uses the same methods and scaling to show processes
+ * but looks a lot simpler. It's overlaid entirely onto the PS graph
+ * when appropriate.
+ */
+
+ f = popen("dmesg", "r");
+ if (!f)
+ return;
+
+ while (!feof(f)) {
+ int c;
+ int z = 0;
+ char l[256];
+
+ if (fgets(l, sizeof(l) - 1, f) == NULL)
+ continue;
+
+ c = sscanf(l, "[%lf] initcall %s %*s %d %*s %d %*s",
+ &t, func, &ret, &usecs);
+ if (c != 4) {
+ /* also parse initcalls done by module loading */
+ c = sscanf(l, "[%lf] initcall %s %*s %*s %d %*s %d %*s",
+ &t, func, &ret, &usecs);
+ if (c != 4)
+ continue;
+ }
+
+ /* chop the +0xXX/0xXX stuff */
+ while(func[z] != '+')
+ z++;
+ func[z] = 0;
+
+ if (count_only) {
+ /* filter out irrelevant stuff */
+ if (usecs >= 1000)
+ kcount++;
+ continue;
+ }
+
+ svg("<!-- thread=\"%s\" time=\"%.3f\" elapsed=\"%d\" result=\"%d\" -->\n",
+ func, t, usecs, ret);
+
+ if (usecs < 1000)
+ continue;
+
+ /* rect */
+ svg(" <rect class=\"krnl\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
+ time_to_graph(t - (usecs / 1000000.0)),
+ ps_to_graph(kcount),
+ time_to_graph(usecs / 1000000.0),
+ ps_to_graph(1));
+
+ /* label */
+ svg(" <text x=\"%.03f\" y=\"%.03f\">%s <tspan class=\"run\">%.03fs</tspan></text>\n",
+ time_to_graph(t - (usecs / 1000000.0)) + 5,
+ ps_to_graph(kcount) + 15,
+ func,
+ usecs / 1000000.0);
+
+ kcount++;
+ }
+
+ fclose(f);
+}
+
+
+static void svg_ps_bars(void)
+{
+ struct ps_struct *ps;
+ int i = 0;
+ int j = 0;
+ int w;
+ int pid;
+
+ svg("<!-- Process graph -->\n");
+
+ svg("<text class=\"t2\" x=\"5\" y=\"-15\">Processes</text>\n");
+
+ /* surrounding box */
+ svg_graph_box(pcount);
+
+ /* pass 2 - ps boxes */
+ ps = ps_first;
+ while ((ps = get_next_ps(ps))) {
+ double starttime;
+ int t;
+
+ if (!ps)
+ continue;
+
+ /* leave some trace of what we actually filtered etc. */
+ svg("<!-- %s [%i] ppid=%i runtime=%.03fs -->\n", ps->name, ps->pid,
+ ps->ppid, ps->total);
+
+ /* it would be nice if we could use exec_start from /proc/pid/sched,
+ * but it's unreliable and gives bogus numbers */
+ starttime = sampletime[ps->first];
+
+ if (!ps_filter(ps)) {
+ /* remember where _to_ our children need to draw a line */
+ ps->pos_x = time_to_graph(starttime - graph_start);
+ ps->pos_y = ps_to_graph(j+1); /* bottom left corner */
+ } else {
+ /* hook children to our parent coords instead */
+ ps->pos_x = ps->parent->pos_x;
+ ps->pos_y = ps->parent->pos_y;
+
+ /* if this is the last child, we might still need to draw a connecting line */
+ if ((!ps->next) && (ps->parent))
+ svg(" <line class=\"dot\" x1=\"%.03f\" y1=\"%.03f\" x2=\"%.03f\" y2=\"%.03f\" />\n",
+ ps->parent->pos_x,
+ ps_to_graph(j-1) + 10.0, /* whee, use the last value here */
+ ps->parent->pos_x,
+ ps->parent->pos_y);
+ continue;
+ }
+
+ svg(" <rect class=\"ps\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
+ time_to_graph(starttime - graph_start),
+ ps_to_graph(j),
+ time_to_graph(sampletime[ps->last] - starttime),
+ ps_to_graph(1));
+
+ /* paint cpu load over these */
+ for (t = ps->first + 1; t < ps->last; t++) {
+ double rt, prt;
+ double wt, wrt;
+
+ /* calculate over interval */
+ rt = ps->sample[t].runtime - ps->sample[t-1].runtime;
+ wt = ps->sample[t].waittime - ps->sample[t-1].waittime;
+
+ prt = (rt / 1000000000) / (sampletime[t] - sampletime[t-1]);
+ wrt = (wt / 1000000000) / (sampletime[t] - sampletime[t-1]);
+
+ /* this can happen if timekeeping isn't accurate enough */
+ if (prt > 1.0)
+ prt = 1.0;
+ if (wrt > 1.0)
+ wrt = 1.0;
+
+ if ((prt < 0.1) && (wrt < 0.1)) /* =~ 26 (color threshold) */
+ continue;
+
+ svg(" <rect class=\"wait\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
+ time_to_graph(sampletime[t - 1] - graph_start),
+ ps_to_graph(j),
+ time_to_graph(sampletime[t] - sampletime[t - 1]),
+ ps_to_graph(wrt));
+
+ /* draw cpu over wait - TODO figure out how/why run + wait > interval */
+ svg(" <rect class=\"cpu\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
+ time_to_graph(sampletime[t - 1] - graph_start),
+ ps_to_graph(j + (1.0 - prt)),
+ time_to_graph(sampletime[t] - sampletime[t - 1]),
+ ps_to_graph(prt));
+ }
+
+ /* determine where to display the process name */
+ if (sampletime[ps->last] - sampletime[ps->first] < 1.5)
+ /* too small to fit label inside the box */
+ w = ps->last;
+ else
+ w = ps->first;
+
+ /* text label of process name */
+ svg(" <text x=\"%.03f\" y=\"%.03f\">%s [%i] <tspan class=\"run\">%.03fs</tspan></text>\n",
+ time_to_graph(sampletime[w] - graph_start) + 5.0,
+ ps_to_graph(j) + 14.0,
+ ps->name,
+ ps->pid,
+ (ps->sample[ps->last].runtime - ps->sample[ps->first].runtime) / 1000000000.0);
+ /* paint lines to the parent process */
+ if (ps->parent) {
+ /* horizontal part */
+ svg(" <line class=\"dot\" x1=\"%.03f\" y1=\"%.03f\" x2=\"%.03f\" y2=\"%.03f\" />\n",
+ time_to_graph(starttime - graph_start),
+ ps_to_graph(j) + 10.0,
+ ps->parent->pos_x,
+ ps_to_graph(j) + 10.0);
+
+ /* one vertical line connecting all the horizontal ones up */
+ if (!ps->next)
+ svg(" <line class=\"dot\" x1=\"%.03f\" y1=\"%.03f\" x2=\"%.03f\" y2=\"%.03f\" />\n",
+ ps->parent->pos_x,
+ ps_to_graph(j) + 10.0,
+ ps->parent->pos_x,
+ ps->parent->pos_y);
+ }
+
+ j++; /* count boxes */
+
+ svg("\n");
+ }
+
+ /* last pass - determine when idle */
+ pid = getpid();
+ /* make sure we start counting from the point where we actually have
+ * data: assume that bootchart's first sample is when data started
+ */
+ ps = ps_first;
+ while (ps->next_ps) {
+ ps = ps->next_ps;
+ if (ps->pid == pid)
+ break;
+ }
+
+ for (i = ps->first; i < samples - (hz / 2); i++) {
+ double crt;
+ double brt;
+ int c;
+
+ /* subtract bootchart cpu utilization from total */
+ crt = 0.0;
+ for (c = 0; c < cpus; c++)
+ crt += cpustat[c].sample[i + ((int)hz / 2)].runtime - cpustat[c].sample[i].runtime;
+ brt = ps->sample[i + ((int)hz / 2)].runtime - ps->sample[i].runtime;
+
+ /*
+ * our definition of "idle":
+ *
+ * if for (hz / 2) we've used less CPU than (interval / 2) ...
+ * defaults to 4.0%, which experimentally, is where atom idles
+ */
+ if ((crt - brt) < (interval / 2.0)) {
+ idletime = sampletime[i] - graph_start;
+ svg("\n<!-- idle detected at %.03f seconds -->\n",
+ idletime);
+ svg("<line class=\"idle\" x1=\"%.03f\" y1=\"%.03f\" x2=\"%.03f\" y2=\"%.03f\" />\n",
+ time_to_graph(idletime),
+ -scale_y,
+ time_to_graph(idletime),
+ ps_to_graph(pcount) + scale_y);
+ svg("<text class=\"idle\" x=\"%.03f\" y=\"%.03f\">%.01fs</text>\n",
+ time_to_graph(idletime) + 5.0,
+ ps_to_graph(pcount) + scale_y,
+ idletime);
+ break;
+ }
+ }
+}
+
+
+static void svg_top_ten_cpu(void)
+{
+ struct ps_struct *top[10];
+ struct ps_struct emptyps;
+ struct ps_struct *ps;
+ int n, m;
+
+ memset(&emptyps, 0, sizeof(struct ps_struct));
+ for (n=0; n < 10; n++)
+ top[n] = &emptyps;
+
+ /* walk all ps's and setup ptrs */
+ ps = ps_first;
+ while ((ps = get_next_ps(ps))) {
+ for (n = 0; n < 10; n++) {
+ if (ps->total <= top[n]->total)
+ continue;
+ /* cascade insert */
+ for (m = 9; m > n; m--)
+ top[m] = top[m-1];
+ top[n] = ps;
+ break;
+ }
+ }
+
+ svg("<text class=\"t2\" x=\"20\" y=\"0\">Top CPU consumers:</text>\n");
+ for (n = 0; n < 10; n++)
+ svg("<text class=\"t3\" x=\"20\" y=\"%d\">%3.03fs - %s[%d]</text>\n",
+ 20 + (n * 13),
+ top[n]->total,
+ top[n]->name,
+ top[n]->pid);
+}
+
+
+static void svg_top_ten_pss(void)
+{
+ struct ps_struct *top[10];
+ struct ps_struct emptyps;
+ struct ps_struct *ps;
+ int n, m;
+
+ memset(&emptyps, 0, sizeof(struct ps_struct));
+ for (n=0; n < 10; n++)
+ top[n] = &emptyps;
+
+ /* walk all ps's and setup ptrs */
+ ps = ps_first;
+ while ((ps = get_next_ps(ps))) {
+ for (n = 0; n < 10; n++) {
+ if (ps->pss_max <= top[n]->pss_max)
+ continue;
+ /* cascade insert */
+ for (m = 9; m > n; m--)
+ top[m] = top[m-1];
+ top[n] = ps;
+ break;
+ }
+ }
+
+ svg("<text class=\"t2\" x=\"20\" y=\"0\">Top PSS consumers:</text>\n");
+ for (n = 0; n < 10; n++)
+ svg("<text class=\"t3\" x=\"20\" y=\"%d\">%dK - %s[%d]</text>\n",
+ 20 + (n * 13),
+ top[n]->pss_max,
+ top[n]->name,
+ top[n]->pid);
+}
+
+
+void svg_do(void)
+{
+ struct ps_struct *ps;
+
+ memset(&str, 0, sizeof(str));
+
+ ps = ps_first;
+
+ /* count initcall thread count first */
+ svg_do_initcall(1);
+ ksize = (kcount ? ps_to_graph(kcount) + (scale_y * 2) : 0);
+
+ /* then count processes */
+ while ((ps = get_next_ps(ps))) {
+ if (!ps_filter(ps))
+ pcount++;
+ else
+ pfiltered++;
+ }
+ psize = ps_to_graph(pcount) + (scale_y * 2);
+
+ esize = (entropy ? scale_y * 7 : 0);
+
+ /* after this, we can draw the header with proper sizing */
+ svg_header();
+
+ svg("<g transform=\"translate(10,400)\">\n");
+ svg_io_bi_bar();
+ svg("</g>\n\n");
+
+ svg("<g transform=\"translate(10,%.03f)\">\n", 400.0 + (scale_y * 7.0));
+ svg_io_bo_bar();
+ svg("</g>\n\n");
+
+ svg("<g transform=\"translate(10,%.03f)\">\n", 400.0 + (scale_y * 14.0));
+ svg_cpu_bar();
+ svg("</g>\n\n");
+
+ svg("<g transform=\"translate(10,%.03f)\">\n", 400.0 + (scale_y * 21.0));
+ svg_wait_bar();
+ svg("</g>\n\n");
+
+ if (kcount) {
+ svg("<g transform=\"translate(10,%.03f)\">\n", 400.0 + (scale_y * 28.0));
+ svg_do_initcall(0);
+ svg("</g>\n\n");
+ }
+
+ svg("<g transform=\"translate(10,%.03f)\">\n", 400.0 + (scale_y * 28.0) + ksize);
+ svg_ps_bars();
+ svg("</g>\n\n");
+
+ svg("<g transform=\"translate(10, 0)\">\n");
+ svg_title();
+ svg("</g>\n\n");
+
+ svg("<g transform=\"translate(10,200)\">\n");
+ svg_top_ten_cpu();
+ svg("</g>\n\n");
+
+ if (entropy) {
+ svg("<g transform=\"translate(10,%.03f)\">\n", 400.0 + (scale_y * 28.0) + ksize + psize);
+ svg_entropy_bar();
+ svg("</g>\n\n");
+ }
+
+ if (pss) {
+ svg("<g transform=\"translate(10,%.03f)\">\n", 400.0 + (scale_y * 28.0) + ksize + psize + esize);
+ svg_pss_graph();
+ svg("</g>\n\n");
+
+ svg("<g transform=\"translate(410,200)\">\n");
+ svg_top_ten_pss();
+ svg("</g>\n\n");
+ }
+
+ /* svg footer */
+ svg("\n</svg>\n");
+}