chiark / gitweb /
b9636e20a34e2a61246c6d08bb85595ecacae614
[elogind.git] / src / bootchart / svg.c
1 /*
2  * svg.c
3  *
4  * Copyright (C) 2009-2012 Intel Coproration
5  *
6  * Authors:
7  *   Auke Kok <auke-jan.h.kok@intel.com>
8  *
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
12  * of the License.
13  */
14
15 #include <stdio.h>
16 #include <stdarg.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <time.h>
20 #include <limits.h>
21 #include <unistd.h>
22 #include <sys/utsname.h>
23
24 #include "bootchart.h"
25 #include "util.h"
26 #include "macro.h"
27
28
29 #define time_to_graph(t) ((t) * scale_x)
30 #define ps_to_graph(n) ((n) * scale_y)
31 #define kb_to_graph(m) ((m) * scale_y * 0.0001)
32 #define to_color(n) (192.0 - ((n) * 192.0))
33
34 #define max(x, y) (((x) > (y)) ? (x) : (y))
35 #define min(x, y) (((x) < (y)) ? (x) : (y))
36
37 static char str[8092];
38
39 #define svg(a...) do { snprintf(str, 8092, ## a); fputs(str, of); fflush(of); } while (0)
40
41 static const char *colorwheel[12] = {
42         "rgb(255,32,32)",  // red
43         "rgb(32,192,192)", // cyan
44         "rgb(255,128,32)", // orange
45         "rgb(128,32,192)", // blue-violet
46         "rgb(255,255,32)", // yellow
47         "rgb(192,32,128)", // red-violet
48         "rgb(32,255,32)",  // green
49         "rgb(255,64,32)",  // red-orange
50         "rgb(32,32,255)",  // blue
51         "rgb(255,192,32)", // yellow-orange
52         "rgb(192,32,192)", // violet
53         "rgb(32,192,32)"   // yellow-green
54 };
55
56 static double idletime = -1.0;
57 static int pfiltered = 0;
58 static int pcount = 0;
59 static int kcount = 0;
60 static float psize = 0;
61 static float ksize = 0;
62 static float esize = 0;
63
64
65 static void svg_header(void)
66 {
67         float w;
68         float h;
69
70         /* min width is about 1600px due to the label */
71         w = 150.0 + 10.0 + time_to_graph(sampletime[samples-1] - graph_start);
72         w = ((w < 1600.0) ? 1600.0 : w);
73
74         /* height is variable based on pss, psize, ksize */
75         h = 400.0 + (scale_y * 30.0) /* base graphs and title */
76             + (pss ? (100.0 * scale_y) + (scale_y * 7.0) : 0.0) /* pss estimate */
77             + psize + ksize + esize;
78
79         svg("<?xml version=\"1.0\" standalone=\"no\"?>\n");
80         svg("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" ");
81         svg("\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
82
83         //svg("<g transform=\"translate(10,%d)\">\n", 1000 + 150 + (pcount * 20));
84         svg("<svg width=\"%.0fpx\" height=\"%.0fpx\" version=\"1.1\" ",
85             w, h);
86         svg("xmlns=\"http://www.w3.org/2000/svg\">\n\n");
87
88         /* write some basic info as a comment, including some help */
89         svg("<!-- This file is a bootchart SVG file. It is best rendered in a browser -->\n");
90         svg("<!-- such as Chrome/Chromium, firefox. Other applications that render    -->\n");
91         svg("<!-- these files properly but much more slow are ImageMagick, gimp,      -->\n");
92         svg("<!-- inkscape, etc.. To display the files on your system, just point     -->\n");
93         svg("<!-- your browser to file:///var/log/ and click. This bootchart was      -->\n\n");
94
95         svg("<!-- generated by bootchart version %s, running with options:  -->\n", VERSION);
96         svg("<!-- hz=\"%f\" n=\"%d\" -->\n", hz, len);
97         svg("<!-- x=\"%f\" y=\"%f\" -->\n", scale_x, scale_y);
98         svg("<!-- rel=\"%d\" f=\"%d\" -->\n", relative, filter);
99         svg("<!-- p=\"%d\" e=\"%d\" -->\n", pss, entropy);
100         svg("<!-- o=\"%s\" i=\"%s\" -->\n\n", output_path, init_path);
101
102         /* style sheet */
103         svg("<defs>\n  <style type=\"text/css\">\n    <![CDATA[\n");
104
105         svg("      rect       { stroke-width: 1; }\n");
106         svg("      rect.cpu   { fill: rgb(64,64,240); stroke-width: 0; fill-opacity: 0.7; }\n");
107         svg("      rect.wait  { fill: rgb(240,240,0); stroke-width: 0; fill-opacity: 0.7; }\n");
108         svg("      rect.bi    { fill: rgb(240,128,128); stroke-width: 0; fill-opacity: 0.7; }\n");
109         svg("      rect.bo    { fill: rgb(192,64,64); stroke-width: 0; fill-opacity: 0.7; }\n");
110         svg("      rect.ps    { fill: rgb(192,192,192); stroke: rgb(128,128,128); fill-opacity: 0.7; }\n");
111         svg("      rect.krnl  { fill: rgb(240,240,0); stroke: rgb(128,128,128); fill-opacity: 0.7; }\n");
112         svg("      rect.box   { fill: rgb(240,240,240); stroke: rgb(192,192,192); }\n");
113         svg("      rect.clrw  { stroke-width: 0; fill-opacity: 0.7;}\n");
114         svg("      line       { stroke: rgb(64,64,64); stroke-width: 1; }\n");
115         svg("//    line.sec1  { }\n");
116         svg("      line.sec5  { stroke-width: 2; }\n");
117         svg("      line.sec01 { stroke: rgb(224,224,224); stroke-width: 1; }\n");
118         svg("      line.dot   { stroke-dasharray: 2 4; }\n");
119         svg("      line.idle  { stroke: rgb(64,64,64); stroke-dasharray: 10 6; stroke-opacity: 0.7; }\n");
120
121         svg("      .run       { font-size: 8; font-style: italic; }\n");
122         svg("      text       { font-family: Verdana, Helvetica; font-size: 10; }\n");
123         svg("      text.sec   { font-size: 8; }\n");
124         svg("      text.t1    { font-size: 24; }\n");
125         svg("      text.t2    { font-size: 12; }\n");
126         svg("      text.idle  { font-size: 18; }\n");
127
128         svg("    ]]>\n   </style>\n</defs>\n\n");
129
130 }
131
132
133 static void svg_title(void)
134 {
135         char cmdline[256] = "";
136         char filename[PATH_MAX];
137         char buf[256];
138         char rootbdev[16] = "Unknown";
139         char model[256] = "Unknown";
140         char date[256] = "Unknown";
141         char cpu[256] = "Unknown";
142         char build[256] = "Unknown";
143         char *c;
144         FILE *f;
145         time_t t;
146         struct utsname uts;
147
148         /* grab /proc/cmdline */
149         f = fopen("/proc/cmdline", "r");
150         if (f) {
151                 if (!fgets(cmdline, 255, f))
152                         sprintf(cmdline, "Unknown");
153                 fclose(f);
154         }
155
156         /* extract root fs so we can find disk model name in sysfs */
157         c = strstr(cmdline, "root=/dev/");
158         if (c) {
159                 strncpy(rootbdev, &c[10], 3);
160                 rootbdev[3] = '\0';
161         }
162         sprintf(filename, "/sys/block/%s/device/model", rootbdev);
163         f = fopen(filename, "r");
164         if (f) {
165                 if (!fgets(model, 255, f))
166                         fprintf(stderr, "Error reading disk model for %s\n", rootbdev);
167                 fclose(f);
168         }
169
170         /* various utsname parameters */
171         if (uname(&uts))
172                 fprintf(stderr, "Error getting uname info\n");
173
174         /* date */
175         t = time(NULL);
176         strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", localtime(&t));
177
178         /* CPU type */
179         f = fopen("/proc/cpuinfo", "r");
180         if (f) {
181                 while (fgets(buf, 255, f)) {
182                         if (strstr(buf, "model name")) {
183                                 strncpy(cpu, &buf[13], 255);
184                                 break;
185                         }
186                 }
187                 fclose(f);
188         }
189
190         /* Build - 1st line from /etc/system-release */
191         f = fopen("/etc/system-release", "r");
192         if (f) {
193                 if (fgets(buf, 255, f))
194                         strncpy(build, buf, 255);
195                 fclose(f);
196         }
197
198         svg("<text class=\"t1\" x=\"0\" y=\"30\">Bootchart for %s - %s</text>\n",
199             uts.nodename, date);
200         svg("<text class=\"t2\" x=\"20\" y=\"50\">System: %s %s %s %s</text>\n",
201             uts.sysname, uts.release, uts.version, uts.machine);
202         svg("<text class=\"t2\" x=\"20\" y=\"65\">CPU: %s</text>\n",
203             cpu);
204         svg("<text class=\"t2\" x=\"20\" y=\"80\">Disk: %s</text>\n",
205             model);
206         svg("<text class=\"t2\" x=\"20\" y=\"95\">Boot options: %s</text>\n",
207             cmdline);
208         svg("<text class=\"t2\" x=\"20\" y=\"110\">Build: %s</text>\n",
209             build);
210         svg("<text class=\"t2\" x=\"20\" y=\"125\">Log start time: %.03fs</text>\n", log_start);
211         svg("<text class=\"t2\" x=\"20\" y=\"140\">Idle time: ");
212
213         if (idletime >= 0.0)
214                 svg("%.03fs", idletime);
215         else
216                 svg("Not detected");
217         svg("</text>\n");
218         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",
219             hz, len, overrun, pscount, pfiltered);
220 }
221
222
223 static void svg_graph_box(int height)
224 {
225         double d = 0.0;
226         int i = 0;
227
228         /* outside box, fill */
229         svg("<rect class=\"box\" x=\"%.03f\" y=\"0\" width=\"%.03f\" height=\"%.03f\" />\n",
230             time_to_graph(0.0),
231             time_to_graph(sampletime[samples-1] - graph_start),
232             ps_to_graph(height));
233
234         for (d = graph_start; d <= sampletime[samples-1];
235              d += (scale_x < 2.0 ? 60.0 : scale_x < 10.0 ? 1.0 : 0.1)) {
236                 /* lines for each second */
237                 if (i % 50 == 0)
238                         svg("  <line class=\"sec5\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n",
239                             time_to_graph(d - graph_start),
240                             time_to_graph(d - graph_start),
241                             ps_to_graph(height));
242                 else if (i % 10 == 0)
243                         svg("  <line class=\"sec1\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n",
244                             time_to_graph(d - graph_start),
245                             time_to_graph(d - graph_start),
246                             ps_to_graph(height));
247                 else
248                         svg("  <line class=\"sec01\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n",
249                             time_to_graph(d - graph_start),
250                             time_to_graph(d - graph_start),
251                             ps_to_graph(height));
252
253                 /* time label */
254                 if (i % 10 == 0)
255                         svg("  <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
256                             time_to_graph(d - graph_start),
257                             -5.0,
258                             d - graph_start);
259
260                 i++;
261         }
262 }
263
264
265 static void svg_pss_graph(void)
266 {
267         struct ps_struct *ps;
268         int i;
269
270         svg("\n\n<!-- Pss memory size graph -->\n");
271
272         svg("\n  <text class=\"t2\" x=\"5\" y=\"-15\">Memory allocation - Pss</text>\n");
273
274         /* vsize 1000 == 1000mb */
275         svg_graph_box(100);
276         /* draw some hlines for usable memory sizes */
277         for (i = 100000; i < 1000000; i += 100000) {
278                 svg("  <line class=\"sec01\" x1=\"%.03f\" y1=\"%.0f\" x2=\"%.03f\" y2=\"%.0f\"/>\n",
279                         time_to_graph(.0),
280                         kb_to_graph(i),
281                         time_to_graph(sampletime[samples-1] - graph_start),
282                         kb_to_graph(i));
283                 svg("  <text class=\"sec\" x=\"%.03f\" y=\"%.0f\">%dM</text>\n",
284                     time_to_graph(sampletime[samples-1] - graph_start) + 5,
285                     kb_to_graph(i), (1000000 - i) / 1000);
286         }
287         svg("\n");
288
289         /* now plot the graph itself */
290         for (i = 1; i < samples ; i++) {
291                 int bottom;
292                 int top;
293
294                 bottom = 0;
295                 top = 0;
296
297                 /* put all the small pss blocks into the bottom */
298                 ps = ps_first;
299                 while (ps->next_ps) {
300                         ps = ps->next_ps;
301                         if (!ps)
302                                 continue;
303                         if (ps->sample[i].pss <= (100 * scale_y))
304                                 top += ps->sample[i].pss;
305                 };
306                 svg("    <rect class=\"clrw\" style=\"fill: %s\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
307                     "rgb(64,64,64)",
308                     time_to_graph(sampletime[i - 1] - graph_start),
309                     kb_to_graph(1000000.0 - top),
310                     time_to_graph(sampletime[i] - sampletime[i - 1]),
311                     kb_to_graph(top - bottom));
312
313                 bottom = top;
314
315                 /* now plot the ones that are of significant size */
316                 ps = ps_first;
317                 while (ps->next_ps) {
318                         ps = ps->next_ps;
319                         if (!ps)
320                                 continue;
321                         /* don't draw anything smaller than 2mb */
322                         if (ps->sample[i].pss > (100 * scale_y)) {
323                                 top = bottom + ps->sample[i].pss;
324                                 svg("    <rect class=\"clrw\" style=\"fill: %s\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
325                                     colorwheel[ps->pid % 12],
326                                     time_to_graph(sampletime[i - 1] - graph_start),
327                                     kb_to_graph(1000000.0 - top),
328                                     time_to_graph(sampletime[i] - sampletime[i - 1]),
329                                     kb_to_graph(top - bottom));
330                                 bottom = top;
331                         }
332                 }
333         }
334
335         /* overlay all the text labels */
336         for (i = 1; i < samples ; i++) {
337                 int bottom;
338                 int top;
339
340                 bottom = 0;
341                 top = 0;
342
343                 /* put all the small pss blocks into the bottom */
344                 ps = ps_first;
345                 while (ps->next_ps) {
346                         ps = ps->next_ps;
347                         if (!ps)
348                                 continue;
349                         if (ps->sample[i].pss <= (100 * scale_y))
350                                 top += ps->sample[i].pss;
351                 };
352
353                 bottom = top;
354
355                 /* now plot the ones that are of significant size */
356                 ps = ps_first;
357                 while (ps->next_ps) {
358                         ps = ps->next_ps;
359                         if (!ps)
360                                 continue;
361                         /* don't draw anything smaller than 2mb */
362                         if (ps->sample[i].pss > (100 * scale_y)) {
363                                 top = bottom + ps->sample[i].pss;
364                                 /* draw a label with the process / PID */
365                                 if ((i == 1) || (ps->sample[i - 1].pss <= (100 * scale_y)))
366                                         svg("  <text x=\"%.03f\" y=\"%.03f\">%s [%i]</text>\n",
367                                             time_to_graph(sampletime[i] - graph_start),
368                                             kb_to_graph(1000000.0 - bottom - ((top -  bottom) / 2)),
369                                             ps->name,
370                                             ps->pid);
371                                 bottom = top;
372                         }
373                 }
374         }
375
376         /* debug output - full data dump */
377         svg("\n\n<!-- PSS map - csv format -->\n");
378         ps = ps_first;
379         while (ps->next_ps) {
380                 ps = ps->next_ps;
381                 if (!ps)
382                         continue;
383                 svg("<!-- %s [%d] pss=", ps->name, ps->pid);
384                 for (i = 0; i < samples ; i++) {
385                         svg("%d," , ps->sample[i].pss);
386                 }
387                 svg(" -->\n");
388         }
389
390 }
391
392 static void svg_io_bi_bar(void)
393 {
394         double max = 0.0;
395         double range;
396         int max_here = 0;
397         int i;
398
399         svg("<!-- IO utilization graph - In -->\n");
400
401         svg("<text class=\"t2\" x=\"5\" y=\"-15\">IO utilization - read</text>\n");
402
403         /*
404          * calculate rounding range
405          *
406          * We need to round IO data since IO block data is not updated on
407          * each poll. Applying a smoothing function loses some burst data,
408          * so keep the smoothing range short.
409          */
410         range = 0.25 / (1.0 / hz);
411         if (range < 2.0)
412                 range = 2.0; /* no smoothing */
413
414         /* surrounding box */
415         svg_graph_box(5);
416
417         /* find the max IO first */
418         for (i = 1; i < samples; i++) {
419                 int start;
420                 int stop;
421                 double tot;
422
423                 start = max(i - ((range / 2) - 1), 0);
424                 stop = min(i + (range / 2), samples - 1);
425
426                 tot = (double)(blockstat[stop].bi - blockstat[start].bi)
427                       / (stop - start);
428                 if (tot > max) {
429                         max = tot;
430                         max_here = i;
431                 }
432                 tot = (double)(blockstat[stop].bo - blockstat[start].bo)
433                       / (stop - start);
434                 if (tot > max)
435                         max = tot;
436         }
437
438         /* plot bi */
439         for (i = 1; i < samples; i++) {
440                 int start;
441                 int stop;
442                 double tot;
443                 double pbi;
444
445                 start = max(i - ((range / 2) - 1), 0);
446                 stop = min(i + (range / 2), samples);
447
448                 tot = (double)(blockstat[stop].bi - blockstat[start].bi)
449                       / (stop - start);
450                 pbi = tot / max;
451
452                 if (pbi > 0.001)
453                         svg("<rect class=\"bi\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
454                             time_to_graph(sampletime[i - 1] - graph_start),
455                             (scale_y * 5) - (pbi * (scale_y * 5)),
456                             time_to_graph(sampletime[i] - sampletime[i - 1]),
457                             pbi * (scale_y * 5));
458
459                 /* labels around highest value */
460                 if (i == max_here) {
461                         svg("  <text class=\"sec\" x=\"%.03f\" y=\"%.03f\">%0.2fmb/sec</text>\n",
462                             time_to_graph(sampletime[i] - graph_start) + 5,
463                             ((scale_y * 5) - (pbi * (scale_y * 5))) + 15,
464                             max / 1024.0 / (interval / 1000000000.0));
465                 }
466         }
467 }
468
469 static void svg_io_bo_bar(void)
470 {
471         double max = 0.0;
472         double range;
473         int max_here = 0;
474         int i;
475
476         svg("<!-- IO utilization graph - out -->\n");
477
478         svg("<text class=\"t2\" x=\"5\" y=\"-15\">IO utilization - write</text>\n");
479
480         /*
481          * calculate rounding range
482          *
483          * We need to round IO data since IO block data is not updated on
484          * each poll. Applying a smoothing function loses some burst data,
485          * so keep the smoothing range short.
486          */
487         range = 0.25 / (1.0 / hz);
488         if (range < 2.0)
489                 range = 2.0; /* no smoothing */
490
491         /* surrounding box */
492         svg_graph_box(5);
493
494         /* find the max IO first */
495         for (i = 1; i < samples; i++) {
496                 int start;
497                 int stop;
498                 double tot;
499
500                 start = max(i - ((range / 2) - 1), 0);
501                 stop = min(i + (range / 2), samples - 1);
502
503                 tot = (double)(blockstat[stop].bi - blockstat[start].bi)
504                       / (stop - start);
505                 if (tot > max)
506                         max = tot;
507                 tot = (double)(blockstat[stop].bo - blockstat[start].bo)
508                       / (stop - start);
509                 if (tot > max) {
510                         max = tot;
511                         max_here = i;
512                 }
513         }
514
515         /* plot bo */
516         for (i = 1; i < samples; i++) {
517                 int start;
518                 int stop;
519                 double tot;
520                 double pbo;
521
522                 start = max(i - ((range / 2) - 1), 0);
523                 stop = min(i + (range / 2), samples);
524
525                 tot = (double)(blockstat[stop].bo - blockstat[start].bo)
526                       / (stop - start);
527                 pbo = tot / max;
528
529                 if (pbo > 0.001)
530                         svg("<rect class=\"bo\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
531                             time_to_graph(sampletime[i - 1] - graph_start),
532                             (scale_y * 5) - (pbo * (scale_y * 5)),
533                             time_to_graph(sampletime[i] - sampletime[i - 1]),
534                             pbo * (scale_y * 5));
535
536                 /* labels around highest bo value */
537                 if (i == max_here) {
538                         svg("  <text class=\"sec\" x=\"%.03f\" y=\"%.03f\">%0.2fmb/sec</text>\n",
539                             time_to_graph(sampletime[i] - graph_start) + 5,
540                             ((scale_y * 5) - (pbo * (scale_y * 5))),
541                             max / 1024.0 / (interval / 1000000000.0));
542                 }
543         }
544 }
545
546
547 static void svg_cpu_bar(void)
548 {
549         int i;
550
551         svg("<!-- CPU utilization graph -->\n");
552
553         svg("<text class=\"t2\" x=\"5\" y=\"-15\">CPU utilization</text>\n");
554         /* surrounding box */
555         svg_graph_box(5);
556
557         /* bars for each sample, proportional to the CPU util. */
558         for (i = 1; i < samples; i++) {
559                 int c;
560                 double trt;
561                 double ptrt;
562
563                 ptrt = trt = 0.0;
564
565                 for (c = 0; c < cpus; c++)
566                         trt += cpustat[c].sample[i].runtime - cpustat[c].sample[i - 1].runtime;
567
568                 trt = trt / 1000000000.0;
569
570                 trt = trt / (double)cpus;
571
572                 if (trt > 0.0)
573                         ptrt = trt / (sampletime[i] - sampletime[i - 1]);
574
575                 if (ptrt > 1.0)
576                         ptrt = 1.0;
577
578                 if (ptrt > 0.001) {
579                         svg("<rect class=\"cpu\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
580                             time_to_graph(sampletime[i - 1] - graph_start),
581                             (scale_y * 5) - (ptrt * (scale_y * 5)),
582                             time_to_graph(sampletime[i] - sampletime[i - 1]),
583                             ptrt * (scale_y * 5));
584                 }
585         }
586 }
587
588 static void svg_wait_bar(void)
589 {
590         int i;
591
592         svg("<!-- Wait time aggregation box -->\n");
593
594         svg("<text class=\"t2\" x=\"5\" y=\"-15\">CPU wait</text>\n");
595
596         /* surrounding box */
597         svg_graph_box(5);
598
599         /* bars for each sample, proportional to the CPU util. */
600         for (i = 1; i < samples; i++) {
601                 int c;
602                 double twt;
603                 double ptwt;
604
605                 ptwt = twt = 0.0;
606
607                 for (c = 0; c < cpus; c++)
608                         twt += cpustat[c].sample[i].waittime - cpustat[c].sample[i - 1].waittime;
609
610                 twt = twt / 1000000000.0;
611
612                 twt = twt / (double)cpus;
613
614                 if (twt > 0.0)
615                         ptwt = twt / (sampletime[i] - sampletime[i - 1]);
616
617                 if (ptwt > 1.0)
618                         ptwt = 1.0;
619
620                 if (ptwt > 0.001) {
621                         svg("<rect class=\"wait\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
622                             time_to_graph(sampletime[i - 1] - graph_start),
623                             ((scale_y * 5) - (ptwt * (scale_y * 5))),
624                             time_to_graph(sampletime[i] - sampletime[i - 1]),
625                             ptwt * (scale_y * 5));
626                 }
627         }
628 }
629
630
631 static void svg_entropy_bar(void)
632 {
633         int i;
634
635         svg("<!-- entropy pool graph -->\n");
636
637         svg("<text class=\"t2\" x=\"5\" y=\"-15\">Entropy pool size</text>\n");
638         /* surrounding box */
639         svg_graph_box(5);
640
641         /* bars for each sample, scale 0-4096 */
642         for (i = 1; i < samples; i++) {
643                 /* svg("<!-- entropy %.03f %i -->\n", sampletime[i], entropy_avail[i]); */
644                 svg("<rect class=\"cpu\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
645                     time_to_graph(sampletime[i - 1] - graph_start),
646                     ((scale_y * 5) - ((entropy_avail[i] / 4096.) * (scale_y * 5))),
647                     time_to_graph(sampletime[i] - sampletime[i - 1]),
648                     (entropy_avail[i] / 4096.) * (scale_y * 5));
649         }
650 }
651
652
653 static struct ps_struct *get_next_ps(struct ps_struct *ps)
654 {
655         /*
656          * walk the list of processes and return the next one to be
657          * painted
658          */
659         if (ps == ps_first)
660                 return ps->next_ps;
661
662         /* go deep */
663         if (ps->children)
664                 return ps->children;
665
666         /* find siblings */
667         if (ps->next)
668                 return ps->next;
669
670         /* go back for parent siblings */
671         while (1) {
672                 if (ps->parent)
673                         if (ps->parent->next)
674                                 return ps->parent->next;
675                 ps = ps->parent;
676                 if (!ps)
677                         return ps;
678         }
679
680         return NULL;
681 }
682
683
684 static int ps_filter(struct ps_struct *ps)
685 {
686         if (!filter)
687                 return 0;
688
689         /* can't draw data when there is only 1 sample (need start + stop) */
690         if (ps->first == ps->last)
691                 return -1;
692
693         /* don't filter kthreadd */
694         if (ps->pid == 2)
695                 return 0;
696
697         /* drop stuff that doesn't use any real CPU time */
698         if (ps->total <= 0.001)
699                 return -1;
700
701         return 0;
702 }
703
704
705 static void svg_do_initcall(int count_only)
706 {
707         FILE _cleanup_pclose_ *f = NULL;
708         double t;
709         char func[256];
710         int ret;
711         int usecs;
712
713         /* can't plot initcall when disabled or in relative mode */
714         if (!initcall || relative) {
715                 kcount = 0;
716                 return;
717         }
718
719         if (!count_only) {
720                 svg("<!-- initcall -->\n");
721
722                 svg("<text class=\"t2\" x=\"5\" y=\"-15\">Kernel init threads</text>\n");
723                 /* surrounding box */
724                 svg_graph_box(kcount);
725         }
726
727         kcount = 0;
728
729         /*
730          * Initcall graphing - parses dmesg buffer and displays kernel threads
731          * This somewhat uses the same methods and scaling to show processes
732          * but looks a lot simpler. It's overlaid entirely onto the PS graph
733          * when appropriate.
734          */
735
736         f = popen("dmesg", "r");
737         if (!f)
738                 return;
739
740         while (!feof(f)) {
741                 int c;
742                 int z = 0;
743                 char l[256];
744
745                 if (fgets(l, sizeof(l) - 1, f) == NULL)
746                         continue;
747
748                 c = sscanf(l, "[%lf] initcall %s %*s %d %*s %d %*s",
749                            &t, func, &ret, &usecs);
750                 if (c != 4) {
751                         /* also parse initcalls done by module loading */
752                         c = sscanf(l, "[%lf] initcall %s %*s %*s %d %*s %d %*s",
753                                    &t, func, &ret, &usecs);
754                         if (c != 4)
755                                 continue;
756                 }
757
758                 /* chop the +0xXX/0xXX stuff */
759                 while(func[z] != '+')
760                         z++;
761                 func[z] = 0;
762
763                 if (count_only) {
764                         /* filter out irrelevant stuff */
765                         if (usecs >= 1000)
766                                 kcount++;
767                         continue;
768                 }
769
770                 svg("<!-- thread=\"%s\" time=\"%.3f\" elapsed=\"%d\" result=\"%d\" -->\n",
771                     func, t, usecs, ret);
772
773                 if (usecs < 1000)
774                         continue;
775
776                 /* rect */
777                 svg("  <rect class=\"krnl\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
778                     time_to_graph(t - (usecs / 1000000.0)),
779                     ps_to_graph(kcount),
780                     time_to_graph(usecs / 1000000.0),
781                     ps_to_graph(1));
782
783                 /* label */
784                 svg("  <text x=\"%.03f\" y=\"%.03f\">%s <tspan class=\"run\">%.03fs</tspan></text>\n",
785                     time_to_graph(t - (usecs / 1000000.0)) + 5,
786                     ps_to_graph(kcount) + 15,
787                     func,
788                     usecs / 1000000.0);
789
790                 kcount++;
791         }
792 }
793
794
795 static void svg_ps_bars(void)
796 {
797         struct ps_struct *ps;
798         int i = 0;
799         int j = 0;
800         int w;
801         int pid;
802
803         svg("<!-- Process graph -->\n");
804
805         svg("<text class=\"t2\" x=\"5\" y=\"-15\">Processes</text>\n");
806
807         /* surrounding box */
808         svg_graph_box(pcount);
809
810         /* pass 2 - ps boxes */
811         ps = ps_first;
812         while ((ps = get_next_ps(ps))) {
813                 double starttime;
814                 int t;
815
816                 if (!ps)
817                         continue;
818
819                 /* leave some trace of what we actually filtered etc. */
820                 svg("<!-- %s [%i] ppid=%i runtime=%.03fs -->\n", ps->name, ps->pid,
821                     ps->ppid, ps->total);
822
823                 /* it would be nice if we could use exec_start from /proc/pid/sched,
824                  * but it's unreliable and gives bogus numbers */
825                 starttime = sampletime[ps->first];
826
827                 if (!ps_filter(ps)) {
828                         /* remember where _to_ our children need to draw a line */
829                         ps->pos_x = time_to_graph(starttime - graph_start);
830                         ps->pos_y = ps_to_graph(j+1); /* bottom left corner */
831                 } else {
832                         /* hook children to our parent coords instead */
833                         ps->pos_x = ps->parent->pos_x;
834                         ps->pos_y = ps->parent->pos_y;
835
836                         /* if this is the last child, we might still need to draw a connecting line */
837                         if ((!ps->next) && (ps->parent))
838                                 svg("  <line class=\"dot\" x1=\"%.03f\" y1=\"%.03f\" x2=\"%.03f\" y2=\"%.03f\" />\n",
839                                     ps->parent->pos_x,
840                                     ps_to_graph(j-1) + 10.0, /* whee, use the last value here */
841                                     ps->parent->pos_x,
842                                     ps->parent->pos_y);
843                         continue;
844                 }
845
846                 svg("  <rect class=\"ps\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
847                     time_to_graph(starttime - graph_start),
848                     ps_to_graph(j),
849                     time_to_graph(sampletime[ps->last] - starttime),
850                     ps_to_graph(1));
851
852                 /* paint cpu load over these */
853                 for (t = ps->first + 1; t < ps->last; t++) {
854                         double rt, prt;
855                         double wt, wrt;
856
857                         /* calculate over interval */
858                         rt = ps->sample[t].runtime - ps->sample[t-1].runtime;
859                         wt = ps->sample[t].waittime - ps->sample[t-1].waittime;
860
861                         prt = (rt / 1000000000) / (sampletime[t] - sampletime[t-1]);
862                         wrt = (wt / 1000000000) / (sampletime[t] - sampletime[t-1]);
863
864                         /* this can happen if timekeeping isn't accurate enough */
865                         if (prt > 1.0)
866                                 prt = 1.0;
867                         if (wrt > 1.0)
868                                 wrt = 1.0;
869
870                         if ((prt < 0.1) && (wrt < 0.1)) /* =~ 26 (color threshold) */
871                                 continue;
872
873                         svg("    <rect class=\"wait\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
874                             time_to_graph(sampletime[t - 1] - graph_start),
875                             ps_to_graph(j),
876                             time_to_graph(sampletime[t] - sampletime[t - 1]),
877                             ps_to_graph(wrt));
878
879                         /* draw cpu over wait - TODO figure out how/why run + wait > interval */
880                         svg("    <rect class=\"cpu\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
881                             time_to_graph(sampletime[t - 1] - graph_start),
882                             ps_to_graph(j + (1.0 - prt)),
883                             time_to_graph(sampletime[t] - sampletime[t - 1]),
884                             ps_to_graph(prt));
885                 }
886
887                 /* determine where to display the process name */
888                 if (sampletime[ps->last] - sampletime[ps->first] < 1.5)
889                         /* too small to fit label inside the box */
890                         w = ps->last;
891                 else
892                         w = ps->first;
893
894                 /* text label of process name */
895                 svg("  <text x=\"%.03f\" y=\"%.03f\">%s [%i] <tspan class=\"run\">%.03fs</tspan></text>\n",
896                     time_to_graph(sampletime[w] - graph_start) + 5.0,
897                     ps_to_graph(j) + 14.0,
898                     ps->name,
899                     ps->pid,
900                     (ps->sample[ps->last].runtime - ps->sample[ps->first].runtime) / 1000000000.0);
901                 /* paint lines to the parent process */
902                 if (ps->parent) {
903                         /* horizontal part */
904                         svg("  <line class=\"dot\" x1=\"%.03f\" y1=\"%.03f\" x2=\"%.03f\" y2=\"%.03f\" />\n",
905                             time_to_graph(starttime - graph_start),
906                             ps_to_graph(j) + 10.0,
907                             ps->parent->pos_x,
908                             ps_to_graph(j) + 10.0);
909
910                         /* one vertical line connecting all the horizontal ones up */
911                         if (!ps->next)
912                                 svg("  <line class=\"dot\" x1=\"%.03f\" y1=\"%.03f\" x2=\"%.03f\" y2=\"%.03f\" />\n",
913                                     ps->parent->pos_x,
914                                     ps_to_graph(j) + 10.0,
915                                     ps->parent->pos_x,
916                                     ps->parent->pos_y);
917                 }
918
919                 j++; /* count boxes */
920
921                 svg("\n");
922         }
923
924         /* last pass - determine when idle */
925         pid = getpid();
926         /* make sure we start counting from the point where we actually have
927          * data: assume that bootchart's first sample is when data started
928          */
929         ps = ps_first;
930         while (ps->next_ps) {
931                 ps = ps->next_ps;
932                 if (ps->pid == pid)
933                         break;
934         }
935
936         for (i = ps->first; i < samples - (hz / 2); i++) {
937                 double crt;
938                 double brt;
939                 int c;
940
941                 /* subtract bootchart cpu utilization from total */
942                 crt = 0.0;
943                 for (c = 0; c < cpus; c++)
944                         crt += cpustat[c].sample[i + ((int)hz / 2)].runtime - cpustat[c].sample[i].runtime;
945                 brt = ps->sample[i + ((int)hz / 2)].runtime - ps->sample[i].runtime;
946
947                 /*
948                  * our definition of "idle":
949                  *
950                  * if for (hz / 2) we've used less CPU than (interval / 2) ...
951                  * defaults to 4.0%, which experimentally, is where atom idles
952                  */
953                 if ((crt - brt) < (interval / 2.0)) {
954                         idletime = sampletime[i] - graph_start;
955                         svg("\n<!-- idle detected at %.03f seconds -->\n",
956                             idletime);
957                         svg("<line class=\"idle\" x1=\"%.03f\" y1=\"%.03f\" x2=\"%.03f\" y2=\"%.03f\" />\n",
958                             time_to_graph(idletime),
959                             -scale_y,
960                             time_to_graph(idletime),
961                             ps_to_graph(pcount) + scale_y);
962                         svg("<text class=\"idle\" x=\"%.03f\" y=\"%.03f\">%.01fs</text>\n",
963                             time_to_graph(idletime) + 5.0,
964                             ps_to_graph(pcount) + scale_y,
965                             idletime);
966                         break;
967                 }
968         }
969 }
970
971
972 static void svg_top_ten_cpu(void)
973 {
974         struct ps_struct *top[10];
975         struct ps_struct emptyps;
976         struct ps_struct *ps;
977         int n, m;
978
979         memset(&emptyps, 0, sizeof(struct ps_struct));
980         for (n=0; n < 10; n++)
981                 top[n] = &emptyps;
982
983         /* walk all ps's and setup ptrs */
984         ps = ps_first;
985         while ((ps = get_next_ps(ps))) {
986                 for (n = 0; n < 10; n++) {
987                         if (ps->total <= top[n]->total)
988                                 continue;
989                         /* cascade insert */
990                         for (m = 9; m > n; m--)
991                                 top[m] = top[m-1];
992                         top[n] = ps;
993                         break;
994                 }
995         }
996
997         svg("<text class=\"t2\" x=\"20\" y=\"0\">Top CPU consumers:</text>\n");
998         for (n = 0; n < 10; n++)
999                 svg("<text class=\"t3\" x=\"20\" y=\"%d\">%3.03fs - %s[%d]</text>\n",
1000                     20 + (n * 13),
1001                     top[n]->total,
1002                     top[n]->name,
1003                     top[n]->pid);
1004 }
1005
1006
1007 static void svg_top_ten_pss(void)
1008 {
1009         struct ps_struct *top[10];
1010         struct ps_struct emptyps;
1011         struct ps_struct *ps;
1012         int n, m;
1013
1014         memset(&emptyps, 0, sizeof(struct ps_struct));
1015         for (n=0; n < 10; n++)
1016                 top[n] = &emptyps;
1017
1018         /* walk all ps's and setup ptrs */
1019         ps = ps_first;
1020         while ((ps = get_next_ps(ps))) {
1021                 for (n = 0; n < 10; n++) {
1022                         if (ps->pss_max <= top[n]->pss_max)
1023                                 continue;
1024                         /* cascade insert */
1025                         for (m = 9; m > n; m--)
1026                                 top[m] = top[m-1];
1027                         top[n] = ps;
1028                         break;
1029                 }
1030         }
1031
1032         svg("<text class=\"t2\" x=\"20\" y=\"0\">Top PSS consumers:</text>\n");
1033         for (n = 0; n < 10; n++)
1034                 svg("<text class=\"t3\" x=\"20\" y=\"%d\">%dK - %s[%d]</text>\n",
1035                     20 + (n * 13),
1036                     top[n]->pss_max,
1037                     top[n]->name,
1038                     top[n]->pid);
1039 }
1040
1041
1042 void svg_do(void)
1043 {
1044         struct ps_struct *ps;
1045
1046         memset(&str, 0, sizeof(str));
1047
1048         ps = ps_first;
1049
1050         /* count initcall thread count first */
1051         svg_do_initcall(1);
1052         ksize = (kcount ? ps_to_graph(kcount) + (scale_y * 2) : 0);
1053
1054         /* then count processes */
1055         while ((ps = get_next_ps(ps))) {
1056                 if (!ps_filter(ps))
1057                         pcount++;
1058                 else
1059                         pfiltered++;
1060         }
1061         psize = ps_to_graph(pcount) + (scale_y * 2);
1062
1063         esize = (entropy ? scale_y * 7 : 0);
1064
1065         /* after this, we can draw the header with proper sizing */
1066         svg_header();
1067
1068         svg("<g transform=\"translate(10,400)\">\n");
1069         svg_io_bi_bar();
1070         svg("</g>\n\n");
1071
1072         svg("<g transform=\"translate(10,%.03f)\">\n", 400.0 + (scale_y * 7.0));
1073         svg_io_bo_bar();
1074         svg("</g>\n\n");
1075
1076         svg("<g transform=\"translate(10,%.03f)\">\n", 400.0 + (scale_y * 14.0));
1077         svg_cpu_bar();
1078         svg("</g>\n\n");
1079
1080         svg("<g transform=\"translate(10,%.03f)\">\n", 400.0 + (scale_y * 21.0));
1081         svg_wait_bar();
1082         svg("</g>\n\n");
1083
1084         if (kcount) {
1085                 svg("<g transform=\"translate(10,%.03f)\">\n", 400.0 + (scale_y * 28.0));
1086                 svg_do_initcall(0);
1087                 svg("</g>\n\n");
1088         }
1089
1090         svg("<g transform=\"translate(10,%.03f)\">\n", 400.0 + (scale_y * 28.0) + ksize);
1091         svg_ps_bars();
1092         svg("</g>\n\n");
1093
1094         svg("<g transform=\"translate(10,  0)\">\n");
1095         svg_title();
1096         svg("</g>\n\n");
1097
1098         svg("<g transform=\"translate(10,200)\">\n");
1099         svg_top_ten_cpu();
1100         svg("</g>\n\n");
1101
1102         if (entropy) {
1103                 svg("<g transform=\"translate(10,%.03f)\">\n", 400.0 + (scale_y * 28.0) + ksize + psize);
1104                 svg_entropy_bar();
1105                 svg("</g>\n\n");
1106         }
1107
1108         if (pss) {
1109                 svg("<g transform=\"translate(10,%.03f)\">\n", 400.0 + (scale_y * 28.0) + ksize + psize + esize);
1110                 svg_pss_graph();
1111                 svg("</g>\n\n");
1112
1113                 svg("<g transform=\"translate(410,200)\">\n");
1114                 svg_top_ten_pss();
1115                 svg("</g>\n\n");
1116         }
1117
1118         /* svg footer */
1119         svg("\n</svg>\n");
1120 }