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