]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/bootchart/svg.c
bootchart: merge bootchart
[thirdparty/systemd.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
26
27 #define time_to_graph(t) ((t) * scale_x)
28 #define ps_to_graph(n) ((n) * scale_y)
29 #define kb_to_graph(m) ((m) * scale_y * 0.0001)
30 #define to_color(n) (192.0 - ((n) * 192.0))
31
32 #define max(x, y) (((x) > (y)) ? (x) : (y))
33 #define min(x, y) (((x) < (y)) ? (x) : (y))
34
35 static char str[8092];
36
37 #define svg(a...) do { snprintf(str, 8092, ## a); fputs(str, of); fflush(of); } while (0)
38
39 static const char *colorwheel[12] = {
40 "rgb(255,32,32)", // red
41 "rgb(32,192,192)", // cyan
42 "rgb(255,128,32)", // orange
43 "rgb(128,32,192)", // blue-violet
44 "rgb(255,255,32)", // yellow
45 "rgb(192,32,128)", // red-violet
46 "rgb(32,255,32)", // green
47 "rgb(255,64,32)", // red-orange
48 "rgb(32,32,255)", // blue
49 "rgb(255,192,32)", // yellow-orange
50 "rgb(192,32,192)", // violet
51 "rgb(32,192,32)" // yellow-green
52 };
53
54 static double idletime = -1.0;
55 static int pfiltered = 0;
56 static int pcount = 0;
57 static int kcount = 0;
58 static float psize = 0;
59 static float ksize = 0;
60 static float esize = 0;
61
62
63 static void svg_header(void)
64 {
65 float w;
66 float h;
67
68 /* min width is about 1600px due to the label */
69 w = 150.0 + 10.0 + time_to_graph(sampletime[samples-1] - graph_start);
70 w = ((w < 1600.0) ? 1600.0 : w);
71
72 /* height is variable based on pss, psize, ksize */
73 h = 400.0 + (scale_y * 30.0) /* base graphs and title */
74 + (pss ? (100.0 * scale_y) + (scale_y * 7.0) : 0.0) /* pss estimate */
75 + psize + ksize + esize;
76
77 svg("<?xml version=\"1.0\" standalone=\"no\"?>\n");
78 svg("<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" ");
79 svg("\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
80
81 //svg("<g transform=\"translate(10,%d)\">\n", 1000 + 150 + (pcount * 20));
82 svg("<svg width=\"%.0fpx\" height=\"%.0fpx\" version=\"1.1\" ",
83 w, h);
84 svg("xmlns=\"http://www.w3.org/2000/svg\">\n\n");
85
86 /* write some basic info as a comment, including some help */
87 svg("<!-- This file is a bootchart SVG file. It is best rendered in a browser -->\n");
88 svg("<!-- such as Chrome/Chromium, firefox. Other applications that render -->\n");
89 svg("<!-- these files properly but much more slow are ImageMagick, gimp, -->\n");
90 svg("<!-- inkscape, etc.. To display the files on your system, just point -->\n");
91 svg("<!-- your browser to file:///var/log/ and click. This bootchart was -->\n\n");
92
93 svg("<!-- generated by bootchart version %s, running with options: -->\n", VERSION);
94 svg("<!-- hz=\"%f\" n=\"%d\" -->\n", hz, len);
95 svg("<!-- x=\"%f\" y=\"%f\" -->\n", scale_x, scale_y);
96 svg("<!-- rel=\"%d\" f=\"%d\" -->\n", relative, filter);
97 svg("<!-- p=\"%d\" e=\"%d\" -->\n", pss, entropy);
98 svg("<!-- o=\"%s\" i=\"%s\" -->\n\n", output_path, init_path);
99
100 /* style sheet */
101 svg("<defs>\n <style type=\"text/css\">\n <![CDATA[\n");
102
103 svg(" rect { stroke-width: 1; }\n");
104 svg(" rect.cpu { fill: rgb(64,64,240); stroke-width: 0; fill-opacity: 0.7; }\n");
105 svg(" rect.wait { fill: rgb(240,240,0); stroke-width: 0; fill-opacity: 0.7; }\n");
106 svg(" rect.bi { fill: rgb(240,128,128); stroke-width: 0; fill-opacity: 0.7; }\n");
107 svg(" rect.bo { fill: rgb(192,64,64); stroke-width: 0; fill-opacity: 0.7; }\n");
108 svg(" rect.ps { fill: rgb(192,192,192); stroke: rgb(128,128,128); fill-opacity: 0.7; }\n");
109 svg(" rect.krnl { fill: rgb(240,240,0); stroke: rgb(128,128,128); fill-opacity: 0.7; }\n");
110 svg(" rect.box { fill: rgb(240,240,240); stroke: rgb(192,192,192); }\n");
111 svg(" rect.clrw { stroke-width: 0; fill-opacity: 0.7;}\n");
112 svg(" line { stroke: rgb(64,64,64); stroke-width: 1; }\n");
113 svg("// line.sec1 { }\n");
114 svg(" line.sec5 { stroke-width: 2; }\n");
115 svg(" line.sec01 { stroke: rgb(224,224,224); stroke-width: 1; }\n");
116 svg(" line.dot { stroke-dasharray: 2 4; }\n");
117 svg(" line.idle { stroke: rgb(64,64,64); stroke-dasharray: 10 6; stroke-opacity: 0.7; }\n");
118
119 svg(" .run { font-size: 8; font-style: italic; }\n");
120 svg(" text { font-family: Verdana, Helvetica; font-size: 10; }\n");
121 svg(" text.sec { font-size: 8; }\n");
122 svg(" text.t1 { font-size: 24; }\n");
123 svg(" text.t2 { font-size: 12; }\n");
124 svg(" text.idle { font-size: 18; }\n");
125
126 svg(" ]]>\n </style>\n</defs>\n\n");
127
128 }
129
130
131 static void svg_title(void)
132 {
133 char cmdline[256] = "";
134 char filename[PATH_MAX];
135 char buf[256];
136 char rootbdev[16] = "Unknown";
137 char model[256] = "Unknown";
138 char date[256] = "Unknown";
139 char cpu[256] = "Unknown";
140 char build[256] = "Unknown";
141 char *c;
142 FILE *f;
143 time_t t;
144 struct utsname uts;
145
146 /* grab /proc/cmdline */
147 f = fopen("/proc/cmdline", "r");
148 if (f) {
149 if (!fgets(cmdline, 255, f))
150 sprintf(cmdline, "Unknown");
151 fclose(f);
152 }
153
154 /* extract root fs so we can find disk model name in sysfs */
155 c = strstr(cmdline, "root=/dev/");
156 if (c) {
157 strncpy(rootbdev, &c[10], 3);
158 rootbdev[3] = '\0';
159 }
160 sprintf(filename, "/sys/block/%s/device/model", rootbdev);
161 f = fopen(filename, "r");
162 if (f) {
163 if (!fgets(model, 255, f))
164 fprintf(stderr, "Error reading disk model for %s\n", rootbdev);
165 fclose(f);
166 }
167
168 /* various utsname parameters */
169 if (uname(&uts))
170 fprintf(stderr, "Error getting uname info\n");
171
172 /* date */
173 t = time(NULL);
174 strftime(date, sizeof(date), "%a, %d %b %Y %H:%M:%S %z", localtime(&t));
175
176 /* CPU type */
177 f = fopen("/proc/cpuinfo", "r");
178 if (f) {
179 while (fgets(buf, 255, f)) {
180 if (strstr(buf, "model name")) {
181 strncpy(cpu, &buf[13], 255);
182 break;
183 }
184 }
185 fclose(f);
186 }
187
188 /* Build - 1st line from /etc/system-release */
189 f = fopen("/etc/system-release", "r");
190 if (f) {
191 if (fgets(buf, 255, f))
192 strncpy(build, buf, 255);
193 fclose(f);
194 }
195
196 svg("<text class=\"t1\" x=\"0\" y=\"30\">Bootchart for %s - %s</text>\n",
197 uts.nodename, date);
198 svg("<text class=\"t2\" x=\"20\" y=\"50\">System: %s %s %s %s</text>\n",
199 uts.sysname, uts.release, uts.version, uts.machine);
200 svg("<text class=\"t2\" x=\"20\" y=\"65\">CPU: %s</text>\n",
201 cpu);
202 svg("<text class=\"t2\" x=\"20\" y=\"80\">Disk: %s</text>\n",
203 model);
204 svg("<text class=\"t2\" x=\"20\" y=\"95\">Boot options: %s</text>\n",
205 cmdline);
206 svg("<text class=\"t2\" x=\"20\" y=\"110\">Build: %s</text>\n",
207 build);
208 svg("<text class=\"t2\" x=\"20\" y=\"125\">Log start time: %.03fs</text>\n", log_start);
209 svg("<text class=\"t2\" x=\"20\" y=\"140\">Idle time: ");
210
211 if (idletime >= 0.0)
212 svg("%.03fs", idletime);
213 else
214 svg("Not detected");
215 svg("</text>\n");
216 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",
217 hz, len, overrun, pscount, pfiltered);
218 }
219
220
221 static void svg_graph_box(int height)
222 {
223 double d = 0.0;
224 int i = 0;
225
226 /* outside box, fill */
227 svg("<rect class=\"box\" x=\"%.03f\" y=\"0\" width=\"%.03f\" height=\"%.03f\" />\n",
228 time_to_graph(0.0),
229 time_to_graph(sampletime[samples-1] - graph_start),
230 ps_to_graph(height));
231
232 for (d = graph_start; d <= sampletime[samples-1];
233 d += (scale_x < 2.0 ? 60.0 : scale_x < 10.0 ? 1.0 : 0.1)) {
234 /* lines for each second */
235 if (i % 50 == 0)
236 svg(" <line class=\"sec5\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n",
237 time_to_graph(d - graph_start),
238 time_to_graph(d - graph_start),
239 ps_to_graph(height));
240 else if (i % 10 == 0)
241 svg(" <line class=\"sec1\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n",
242 time_to_graph(d - graph_start),
243 time_to_graph(d - graph_start),
244 ps_to_graph(height));
245 else
246 svg(" <line class=\"sec01\" x1=\"%.03f\" y1=\"0\" x2=\"%.03f\" y2=\"%.03f\" />\n",
247 time_to_graph(d - graph_start),
248 time_to_graph(d - graph_start),
249 ps_to_graph(height));
250
251 /* time label */
252 if (i % 10 == 0)
253 svg(" <text class=\"sec\" x=\"%.03f\" y=\"%.03f\" >%.01fs</text>\n",
254 time_to_graph(d - graph_start),
255 -5.0,
256 d - graph_start);
257
258 i++;
259 }
260 }
261
262
263 static void svg_pss_graph(void)
264 {
265 struct ps_struct *ps;
266 int i;
267
268 svg("\n\n<!-- Pss memory size graph -->\n");
269
270 svg("\n <text class=\"t2\" x=\"5\" y=\"-15\">Memory allocation - Pss</text>\n");
271
272 /* vsize 1000 == 1000mb */
273 svg_graph_box(100);
274 /* draw some hlines for usable memory sizes */
275 for (i = 100000; i < 1000000; i += 100000) {
276 svg(" <line class=\"sec01\" x1=\"%.03f\" y1=\"%.0f\" x2=\"%.03f\" y2=\"%.0f\"/>\n",
277 time_to_graph(.0),
278 kb_to_graph(i),
279 time_to_graph(sampletime[samples-1] - graph_start),
280 kb_to_graph(i));
281 svg(" <text class=\"sec\" x=\"%.03f\" y=\"%.0f\">%dM</text>\n",
282 time_to_graph(sampletime[samples-1] - graph_start) + 5,
283 kb_to_graph(i), (1000000 - i) / 1000);
284 }
285 svg("\n");
286
287 /* now plot the graph itself */
288 for (i = 1; i < samples ; i++) {
289 int bottom;
290 int top;
291
292 bottom = 0;
293 top = 0;
294
295 /* put all the small pss blocks into the bottom */
296 ps = ps_first;
297 while (ps->next_ps) {
298 ps = ps->next_ps;
299 if (!ps)
300 continue;
301 if (ps->sample[i].pss <= (100 * scale_y))
302 top += ps->sample[i].pss;
303 };
304 svg(" <rect class=\"clrw\" style=\"fill: %s\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
305 "rgb(64,64,64)",
306 time_to_graph(sampletime[i - 1] - graph_start),
307 kb_to_graph(1000000.0 - top),
308 time_to_graph(sampletime[i] - sampletime[i - 1]),
309 kb_to_graph(top - bottom));
310
311 bottom = top;
312
313 /* now plot the ones that are of significant size */
314 ps = ps_first;
315 while (ps->next_ps) {
316 ps = ps->next_ps;
317 if (!ps)
318 continue;
319 /* don't draw anything smaller than 2mb */
320 if (ps->sample[i].pss > (100 * scale_y)) {
321 top = bottom + ps->sample[i].pss;
322 svg(" <rect class=\"clrw\" style=\"fill: %s\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
323 colorwheel[ps->pid % 12],
324 time_to_graph(sampletime[i - 1] - graph_start),
325 kb_to_graph(1000000.0 - top),
326 time_to_graph(sampletime[i] - sampletime[i - 1]),
327 kb_to_graph(top - bottom));
328 bottom = top;
329 }
330 }
331 }
332
333 /* overlay all the text labels */
334 for (i = 1; i < samples ; i++) {
335 int bottom;
336 int top;
337
338 bottom = 0;
339 top = 0;
340
341 /* put all the small pss blocks into the bottom */
342 ps = ps_first;
343 while (ps->next_ps) {
344 ps = ps->next_ps;
345 if (!ps)
346 continue;
347 if (ps->sample[i].pss <= (100 * scale_y))
348 top += ps->sample[i].pss;
349 };
350
351 bottom = top;
352
353 /* now plot the ones that are of significant size */
354 ps = ps_first;
355 while (ps->next_ps) {
356 ps = ps->next_ps;
357 if (!ps)
358 continue;
359 /* don't draw anything smaller than 2mb */
360 if (ps->sample[i].pss > (100 * scale_y)) {
361 top = bottom + ps->sample[i].pss;
362 /* draw a label with the process / PID */
363 if ((i == 1) || (ps->sample[i - 1].pss <= (100 * scale_y)))
364 svg(" <text x=\"%.03f\" y=\"%.03f\">%s [%i]</text>\n",
365 time_to_graph(sampletime[i] - graph_start),
366 kb_to_graph(1000000.0 - bottom - ((top - bottom) / 2)),
367 ps->name,
368 ps->pid);
369 bottom = top;
370 }
371 }
372 }
373
374 /* debug output - full data dump */
375 svg("\n\n<!-- PSS map - csv format -->\n");
376 ps = ps_first;
377 while (ps->next_ps) {
378 ps = ps->next_ps;
379 if (!ps)
380 continue;
381 svg("<!-- %s [%d] pss=", ps->name, ps->pid);
382 for (i = 0; i < samples ; i++) {
383 svg("%d," , ps->sample[i].pss);
384 }
385 svg(" -->\n");
386 }
387
388 }
389
390 static void svg_io_bi_bar(void)
391 {
392 double max = 0.0;
393 double range;
394 int max_here = 0;
395 int i;
396
397 svg("<!-- IO utilization graph - In -->\n");
398
399 svg("<text class=\"t2\" x=\"5\" y=\"-15\">IO utilization - read</text>\n");
400
401 /*
402 * calculate rounding range
403 *
404 * We need to round IO data since IO block data is not updated on
405 * each poll. Applying a smoothing function loses some burst data,
406 * so keep the smoothing range short.
407 */
408 range = 0.25 / (1.0 / hz);
409 if (range < 2.0)
410 range = 2.0; /* no smoothing */
411
412 /* surrounding box */
413 svg_graph_box(5);
414
415 /* find the max IO first */
416 for (i = 1; i < samples; i++) {
417 int start;
418 int stop;
419 double tot;
420
421 start = max(i - ((range / 2) - 1), 0);
422 stop = min(i + (range / 2), samples - 1);
423
424 tot = (double)(blockstat[stop].bi - blockstat[start].bi)
425 / (stop - start);
426 if (tot > max) {
427 max = tot;
428 max_here = i;
429 }
430 tot = (double)(blockstat[stop].bo - blockstat[start].bo)
431 / (stop - start);
432 if (tot > max)
433 max = tot;
434 }
435
436 /* plot bi */
437 for (i = 1; i < samples; i++) {
438 int start;
439 int stop;
440 double tot;
441 double pbi;
442
443 start = max(i - ((range / 2) - 1), 0);
444 stop = min(i + (range / 2), samples);
445
446 tot = (double)(blockstat[stop].bi - blockstat[start].bi)
447 / (stop - start);
448 pbi = tot / max;
449
450 if (pbi > 0.001)
451 svg("<rect class=\"bi\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
452 time_to_graph(sampletime[i - 1] - graph_start),
453 (scale_y * 5) - (pbi * (scale_y * 5)),
454 time_to_graph(sampletime[i] - sampletime[i - 1]),
455 pbi * (scale_y * 5));
456
457 /* labels around highest value */
458 if (i == max_here) {
459 svg(" <text class=\"sec\" x=\"%.03f\" y=\"%.03f\">%0.2fmb/sec</text>\n",
460 time_to_graph(sampletime[i] - graph_start) + 5,
461 ((scale_y * 5) - (pbi * (scale_y * 5))) + 15,
462 max / 1024.0 / (interval / 1000000000.0));
463 }
464 }
465 }
466
467 static void svg_io_bo_bar(void)
468 {
469 double max = 0.0;
470 double range;
471 int max_here = 0;
472 int i;
473
474 svg("<!-- IO utilization graph - out -->\n");
475
476 svg("<text class=\"t2\" x=\"5\" y=\"-15\">IO utilization - write</text>\n");
477
478 /*
479 * calculate rounding range
480 *
481 * We need to round IO data since IO block data is not updated on
482 * each poll. Applying a smoothing function loses some burst data,
483 * so keep the smoothing range short.
484 */
485 range = 0.25 / (1.0 / hz);
486 if (range < 2.0)
487 range = 2.0; /* no smoothing */
488
489 /* surrounding box */
490 svg_graph_box(5);
491
492 /* find the max IO first */
493 for (i = 1; i < samples; i++) {
494 int start;
495 int stop;
496 double tot;
497
498 start = max(i - ((range / 2) - 1), 0);
499 stop = min(i + (range / 2), samples - 1);
500
501 tot = (double)(blockstat[stop].bi - blockstat[start].bi)
502 / (stop - start);
503 if (tot > max)
504 max = tot;
505 tot = (double)(blockstat[stop].bo - blockstat[start].bo)
506 / (stop - start);
507 if (tot > max) {
508 max = tot;
509 max_here = i;
510 }
511 }
512
513 /* plot bo */
514 for (i = 1; i < samples; i++) {
515 int start;
516 int stop;
517 double tot;
518 double pbo;
519
520 start = max(i - ((range / 2) - 1), 0);
521 stop = min(i + (range / 2), samples);
522
523 tot = (double)(blockstat[stop].bo - blockstat[start].bo)
524 / (stop - start);
525 pbo = tot / max;
526
527 if (pbo > 0.001)
528 svg("<rect class=\"bo\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
529 time_to_graph(sampletime[i - 1] - graph_start),
530 (scale_y * 5) - (pbo * (scale_y * 5)),
531 time_to_graph(sampletime[i] - sampletime[i - 1]),
532 pbo * (scale_y * 5));
533
534 /* labels around highest bo value */
535 if (i == max_here) {
536 svg(" <text class=\"sec\" x=\"%.03f\" y=\"%.03f\">%0.2fmb/sec</text>\n",
537 time_to_graph(sampletime[i] - graph_start) + 5,
538 ((scale_y * 5) - (pbo * (scale_y * 5))),
539 max / 1024.0 / (interval / 1000000000.0));
540 }
541 }
542 }
543
544
545 static void svg_cpu_bar(void)
546 {
547 int i;
548
549 svg("<!-- CPU utilization graph -->\n");
550
551 svg("<text class=\"t2\" x=\"5\" y=\"-15\">CPU utilization</text>\n");
552 /* surrounding box */
553 svg_graph_box(5);
554
555 /* bars for each sample, proportional to the CPU util. */
556 for (i = 1; i < samples; i++) {
557 int c;
558 double trt;
559 double ptrt;
560
561 ptrt = trt = 0.0;
562
563 for (c = 0; c < cpus; c++)
564 trt += cpustat[c].sample[i].runtime - cpustat[c].sample[i - 1].runtime;
565
566 trt = trt / 1000000000.0;
567
568 trt = trt / (double)cpus;
569
570 if (trt > 0.0)
571 ptrt = trt / (sampletime[i] - sampletime[i - 1]);
572
573 if (ptrt > 1.0)
574 ptrt = 1.0;
575
576 if (ptrt > 0.001) {
577 svg("<rect class=\"cpu\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
578 time_to_graph(sampletime[i - 1] - graph_start),
579 (scale_y * 5) - (ptrt * (scale_y * 5)),
580 time_to_graph(sampletime[i] - sampletime[i - 1]),
581 ptrt * (scale_y * 5));
582 }
583 }
584 }
585
586 static void svg_wait_bar(void)
587 {
588 int i;
589
590 svg("<!-- Wait time aggregation box -->\n");
591
592 svg("<text class=\"t2\" x=\"5\" y=\"-15\">CPU wait</text>\n");
593
594 /* surrounding box */
595 svg_graph_box(5);
596
597 /* bars for each sample, proportional to the CPU util. */
598 for (i = 1; i < samples; i++) {
599 int c;
600 double twt;
601 double ptwt;
602
603 ptwt = twt = 0.0;
604
605 for (c = 0; c < cpus; c++)
606 twt += cpustat[c].sample[i].waittime - cpustat[c].sample[i - 1].waittime;
607
608 twt = twt / 1000000000.0;
609
610 twt = twt / (double)cpus;
611
612 if (twt > 0.0)
613 ptwt = twt / (sampletime[i] - sampletime[i - 1]);
614
615 if (ptwt > 1.0)
616 ptwt = 1.0;
617
618 if (ptwt > 0.001) {
619 svg("<rect class=\"wait\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
620 time_to_graph(sampletime[i - 1] - graph_start),
621 ((scale_y * 5) - (ptwt * (scale_y * 5))),
622 time_to_graph(sampletime[i] - sampletime[i - 1]),
623 ptwt * (scale_y * 5));
624 }
625 }
626 }
627
628
629 static void svg_entropy_bar(void)
630 {
631 int i;
632
633 svg("<!-- entropy pool graph -->\n");
634
635 svg("<text class=\"t2\" x=\"5\" y=\"-15\">Entropy pool size</text>\n");
636 /* surrounding box */
637 svg_graph_box(5);
638
639 /* bars for each sample, scale 0-4096 */
640 for (i = 1; i < samples; i++) {
641 /* svg("<!-- entropy %.03f %i -->\n", sampletime[i], entropy_avail[i]); */
642 svg("<rect class=\"cpu\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
643 time_to_graph(sampletime[i - 1] - graph_start),
644 ((scale_y * 5) - ((entropy_avail[i] / 4096.) * (scale_y * 5))),
645 time_to_graph(sampletime[i] - sampletime[i - 1]),
646 (entropy_avail[i] / 4096.) * (scale_y * 5));
647 }
648 }
649
650
651 static struct ps_struct *get_next_ps(struct ps_struct *ps)
652 {
653 /*
654 * walk the list of processes and return the next one to be
655 * painted
656 */
657 if (ps == ps_first)
658 return ps->next_ps;
659
660 /* go deep */
661 if (ps->children)
662 return ps->children;
663
664 /* find siblings */
665 if (ps->next)
666 return ps->next;
667
668 /* go back for parent siblings */
669 while (1) {
670 if (ps->parent)
671 if (ps->parent->next)
672 return ps->parent->next;
673 ps = ps->parent;
674 if (!ps)
675 return ps;
676 }
677
678 return NULL;
679 }
680
681
682 static int ps_filter(struct ps_struct *ps)
683 {
684 if (!filter)
685 return 0;
686
687 /* can't draw data when there is only 1 sample (need start + stop) */
688 if (ps->first == ps->last)
689 return -1;
690
691 /* don't filter kthreadd */
692 if (ps->pid == 2)
693 return 0;
694
695 /* drop stuff that doesn't use any real CPU time */
696 if (ps->total <= 0.001)
697 return -1;
698
699 return 0;
700 }
701
702
703 static void svg_do_initcall(int count_only)
704 {
705 FILE *f;
706 double t;
707 char func[256];
708 int ret;
709 int usecs;
710
711 /* can't plot initcall when disabled or in relative mode */
712 if (!initcall || relative) {
713 kcount = 0;
714 return;
715 }
716
717 if (!count_only) {
718 svg("<!-- initcall -->\n");
719
720 svg("<text class=\"t2\" x=\"5\" y=\"-15\">Kernel init threads</text>\n");
721 /* surrounding box */
722 svg_graph_box(kcount);
723 }
724
725 kcount = 0;
726
727 /*
728 * Initcall graphing - parses dmesg buffer and displays kernel threads
729 * This somewhat uses the same methods and scaling to show processes
730 * but looks a lot simpler. It's overlaid entirely onto the PS graph
731 * when appropriate.
732 */
733
734 f = popen("dmesg", "r");
735 if (!f)
736 return;
737
738 while (!feof(f)) {
739 int c;
740 int z = 0;
741 char l[256];
742
743 if (fgets(l, sizeof(l) - 1, f) == NULL)
744 continue;
745
746 c = sscanf(l, "[%lf] initcall %s %*s %d %*s %d %*s",
747 &t, func, &ret, &usecs);
748 if (c != 4) {
749 /* also parse initcalls done by module loading */
750 c = sscanf(l, "[%lf] initcall %s %*s %*s %d %*s %d %*s",
751 &t, func, &ret, &usecs);
752 if (c != 4)
753 continue;
754 }
755
756 /* chop the +0xXX/0xXX stuff */
757 while(func[z] != '+')
758 z++;
759 func[z] = 0;
760
761 if (count_only) {
762 /* filter out irrelevant stuff */
763 if (usecs >= 1000)
764 kcount++;
765 continue;
766 }
767
768 svg("<!-- thread=\"%s\" time=\"%.3f\" elapsed=\"%d\" result=\"%d\" -->\n",
769 func, t, usecs, ret);
770
771 if (usecs < 1000)
772 continue;
773
774 /* rect */
775 svg(" <rect class=\"krnl\" x=\"%.03f\" y=\"%.03f\" width=\"%.03f\" height=\"%.03f\" />\n",
776 time_to_graph(t - (usecs / 1000000.0)),
777 ps_to_graph(kcount),
778 time_to_graph(usecs / 1000000.0),
779 ps_to_graph(1));
780
781 /* label */
782 svg(" <text x=\"%.03f\" y=\"%.03f\">%s <tspan class=\"run\">%.03fs</tspan></text>\n",
783 time_to_graph(t - (usecs / 1000000.0)) + 5,
784 ps_to_graph(kcount) + 15,
785 func,
786 usecs / 1000000.0);
787
788 kcount++;
789 }
790
791 fclose(f);
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 }