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