2 * svghelper.c - helper functions for outputting svg
4 * (C) Copyright 2009 Intel Corporation
7 * Arjan van de Ven <arjan@linux.intel.com>
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
20 #include "svghelper.h"
22 static u64 first_time, last_time;
23 static u64 turbo_frequency, max_freq;
26 #define SLOT_MULT 30.0
27 #define SLOT_HEIGHT 25.0
30 #define MIN_TEXT_SIZE 0.001
32 static u64 total_height;
35 static double cpu2slot(int cpu)
40 static double cpu2y(int cpu)
42 return cpu2slot(cpu) * SLOT_MULT;
45 static double time2pixels(u64 time)
49 X = WIDTH * (time - first_time) / (last_time - first_time);
53 void open_svg(const char *filename, int cpus, int rows)
56 svgfile = fopen(filename, "w");
58 fprintf(stderr, "Cannot open %s for output\n", filename);
61 total_height = (1 + rows + cpu2slot(cpus)) * SLOT_MULT;
62 fprintf(svgfile, "<?xml version=\"1.0\" standalone=\"no\"?> \n");
63 fprintf(svgfile, "<svg width=\"%4.1f\" height=\"%llu\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n", WIDTH, total_height);
65 fprintf(svgfile, "<defs>\n <style type=\"text/css\">\n <![CDATA[\n");
67 fprintf(svgfile, " rect { stroke-width: 1; }\n");
68 fprintf(svgfile, " rect.process { fill:rgb(180,180,180); fill-opacity:0.9; stroke-width:1; stroke:rgb( 0, 0, 0); } \n");
69 fprintf(svgfile, " rect.process2 { fill:rgb(180,180,180); fill-opacity:0.9; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
70 fprintf(svgfile, " rect.sample { fill:rgb( 0, 0,255); fill-opacity:0.8; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
71 fprintf(svgfile, " rect.blocked { fill:rgb(255, 0, 0); fill-opacity:0.5; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
72 fprintf(svgfile, " rect.waiting { fill:rgb(255,255, 0); fill-opacity:0.3; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
73 fprintf(svgfile, " rect.cpu { fill:rgb(192,192,192); fill-opacity:0.2; stroke-width:0.5; stroke:rgb(128,128,128); } \n");
74 fprintf(svgfile, " rect.pstate { fill:rgb(128,128,128); fill-opacity:0.8; stroke-width:0; } \n");
75 fprintf(svgfile, " rect.c1 { fill:rgb(255,214,214); fill-opacity:0.5; stroke-width:0; } \n");
76 fprintf(svgfile, " rect.c2 { fill:rgb(255,172,172); fill-opacity:0.5; stroke-width:0; } \n");
77 fprintf(svgfile, " rect.c3 { fill:rgb(255,130,130); fill-opacity:0.5; stroke-width:0; } \n");
78 fprintf(svgfile, " rect.c4 { fill:rgb(255, 88, 88); fill-opacity:0.5; stroke-width:0; } \n");
79 fprintf(svgfile, " rect.c5 { fill:rgb(255, 44, 44); fill-opacity:0.5; stroke-width:0; } \n");
80 fprintf(svgfile, " rect.c6 { fill:rgb(255, 0, 0); fill-opacity:0.5; stroke-width:0; } \n");
81 fprintf(svgfile, " line.pstate { stroke:rgb(255,255, 0); stroke-opacity:0.8; stroke-width:2; } \n");
83 fprintf(svgfile, " ]]>\n </style>\n</defs>\n");
86 void svg_box(int Yslot, u64 start, u64 end, const char *type)
91 fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"%s\"/>\n",
92 time2pixels(start), time2pixels(end)-time2pixels(start), Yslot * SLOT_MULT, SLOT_HEIGHT, type);
95 void svg_sample(int Yslot, int cpu, u64 start, u64 end, const char *type)
101 fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"%s\"/>\n",
102 time2pixels(start), time2pixels(end)-time2pixels(start), Yslot * SLOT_MULT, SLOT_HEIGHT, type);
104 text_size = (time2pixels(end)-time2pixels(start));
106 text_size = text_size/2;
107 if (text_size > 1.25)
109 if (text_size > MIN_TEXT_SIZE)
110 fprintf(svgfile, "<text transform=\"translate(%1.8f,%1.8f)\" font-size=\"%1.6fpt\">%i</text>\n",
111 time2pixels(start), Yslot * SLOT_MULT + SLOT_HEIGHT - 1, text_size, cpu + 1);
115 static char *cpu_model(void)
117 static char cpu_m[255];
123 file = fopen("/proc/cpuinfo", "r");
125 while (fgets(buf, 255, file)) {
126 if (strstr(buf, "model name")) {
127 strncpy(cpu_m, &buf[13], 255);
136 void svg_cpu_box(int cpu, u64 __max_freq, u64 __turbo_freq)
142 max_freq = __max_freq;
143 turbo_frequency = __turbo_freq;
145 fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"cpu\"/>\n",
146 time2pixels(first_time),
147 time2pixels(last_time)-time2pixels(first_time),
148 cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT);
150 sprintf(cpu_string, "CPU %i", (int)cpu+1);
151 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\">%s</text>\n",
152 10+time2pixels(first_time), cpu2y(cpu) + SLOT_HEIGHT/2, cpu_string);
154 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\" font-size=\"1.25pt\">%s</text>\n",
155 10+time2pixels(first_time), cpu2y(cpu) + SLOT_MULT + SLOT_HEIGHT - 4, cpu_model());
158 void svg_process(int cpu, u64 start, u64 end, const char *type, const char *name)
165 fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"%s\"/>\n",
166 time2pixels(start), time2pixels(end)-time2pixels(start), cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT, type);
167 width = time2pixels(end)-time2pixels(start);
171 if (width > MIN_TEXT_SIZE)
172 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f) rotate(90)\" font-size=\"%3.4fpt\">%s</text>\n",
173 time2pixels(start), cpu2y(cpu), width, name);
176 void svg_cstate(int cpu, u64 start, u64 end, int type)
187 sprintf(style, "c%i", type);
189 fprintf(svgfile, "<rect class=\"%s\" x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\"/>\n",
191 time2pixels(start), time2pixels(end)-time2pixels(start),
192 cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT);
194 width = time2pixels(end)-time2pixels(start);
198 if (width > MIN_TEXT_SIZE)
199 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f) rotate(90)\" font-size=\"%3.4fpt\">C%i</text>\n",
200 time2pixels(start), cpu2y(cpu), width, type);
203 static char *HzToHuman(unsigned long hz)
205 static char buffer[1024];
206 unsigned long long Hz;
208 memset(buffer, 0, 1024);
212 /* default: just put the Number in */
213 sprintf(buffer, "%9lli", Hz);
216 sprintf(buffer, " %6lli Mhz", (Hz+500)/1000);
219 sprintf(buffer, " %6.2f Ghz", (Hz+5000.0)/1000000);
221 if (Hz == turbo_frequency)
222 sprintf(buffer, "Turbo");
227 void svg_pstate(int cpu, u64 start, u64 end, u64 freq)
235 height = freq * 1.0 / max_freq * (SLOT_HEIGHT + SLOT_MULT);
236 height = 1 + cpu2y(cpu) + SLOT_MULT + SLOT_HEIGHT - height;
237 fprintf(svgfile, "<line x1=\"%4.8f\" x2=\"%4.8f\" y1=\"%4.1f\" y2=\"%4.1f\" class=\"pstate\"/>\n",
238 time2pixels(start), time2pixels(end), height, height);
239 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\" font-size=\"0.25pt\">%s</text>\n",
240 time2pixels(start), height+0.9, HzToHuman(freq));
245 void svg_partial_wakeline(u64 start, int row1, int row2)
255 fprintf(svgfile, "<line x1=\"%4.8f\" y1=\"%4.2f\" x2=\"%4.8f\" y2=\"%4.2f\" style=\"stroke:rgb(32,255,32);stroke-width:0.009\"/>\n",
256 time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/32);
259 fprintf(svgfile, "<line x1=\"%4.8f\" y1=\"%4.2f\" x2=\"%4.8f\" y2=\"%4.2f\" style=\"stroke:rgb(32,255,32);stroke-width:0.009\"/>\n",
260 time2pixels(start), row2 * SLOT_MULT - SLOT_MULT/32, time2pixels(start), row2 * SLOT_MULT);
263 fprintf(svgfile, "<line x1=\"%4.8f\" y1=\"%4.2f\" x2=\"%4.8f\" y2=\"%4.2f\" style=\"stroke:rgb(32,255,32);stroke-width:0.009\"/>\n",
264 time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/32);
267 fprintf(svgfile, "<line x1=\"%4.8f\" y1=\"%4.2f\" x2=\"%4.8f\" y2=\"%4.2f\" style=\"stroke:rgb(32,255,32);stroke-width:0.009\"/>\n",
268 time2pixels(start), row1 * SLOT_MULT - SLOT_MULT/32, time2pixels(start), row1 * SLOT_MULT);
270 height = row1 * SLOT_MULT;
272 height += SLOT_HEIGHT;
274 fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(32,255,32)\"/>\n",
275 time2pixels(start), height);
278 void svg_wakeline(u64 start, int row1, int row2)
287 fprintf(svgfile, "<line x1=\"%4.8f\" y1=\"%4.2f\" x2=\"%4.8f\" y2=\"%4.2f\" style=\"stroke:rgb(32,255,32);stroke-width:0.009\"/>\n",
288 time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row2 * SLOT_MULT);
290 fprintf(svgfile, "<line x1=\"%4.8f\" y1=\"%4.2f\" x2=\"%4.8f\" y2=\"%4.2f\" style=\"stroke:rgb(32,255,32);stroke-width:0.009\"/>\n",
291 time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row1 * SLOT_MULT);
293 height = row1 * SLOT_MULT;
295 height += SLOT_HEIGHT;
296 fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(32,255,32)\"/>\n",
297 time2pixels(start), height);
300 void svg_interrupt(u64 start, int row)
305 fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(255,128,128)\"/>\n",
306 time2pixels(start), row * SLOT_MULT);
307 fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(255,128,128)\"/>\n",
308 time2pixels(start), row * SLOT_MULT + SLOT_HEIGHT);
311 void svg_text(int Yslot, u64 start, const char *text)
316 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\">%s</text>\n",
317 time2pixels(start), Yslot * SLOT_MULT+SLOT_HEIGHT/2, text);
320 static void svg_legenda_box(int X, const char *text, const char *style)
323 boxsize = SLOT_HEIGHT / 2;
325 fprintf(svgfile, "<rect x=\"%i\" width=\"%4.8f\" y=\"0\" height=\"%4.1f\" class=\"%s\"/>\n",
326 X, boxsize, boxsize, style);
327 fprintf(svgfile, "<text transform=\"translate(%4.8f, %4.8f)\" font-size=\"%4.4fpt\">%s</text>\n",
328 X + boxsize + 5, boxsize, 0.8 * boxsize, text);
331 void svg_legenda(void)
336 svg_legenda_box(0, "Running", "sample");
337 svg_legenda_box(100, "Idle","rect.c1");
338 svg_legenda_box(200, "Deeper Idle", "rect.c3");
339 svg_legenda_box(350, "Deepest Idle", "rect.c6");
340 svg_legenda_box(550, "Sleeping", "process2");
341 svg_legenda_box(650, "Waiting for cpu", "waiting");
342 svg_legenda_box(800, "Blocked on IO", "blocked");
345 void svg_time_grid(u64 start, u64 end)
352 first_time = first_time / 100000000 * 100000000;
358 while (i < last_time) {
360 double thickness = 0.075;
361 if ((i % 100000000) == 0) {
365 if ((i % 1000000000) == 0) {
370 fprintf(svgfile, "<line x1=\"%4.8f\" y1=\"%4.2f\" x2=\"%4.8f\" y2=\"%llu\" style=\"stroke:rgb(%i,%i,%i);stroke-width:%1.3f\"/>\n",
371 time2pixels(i), SLOT_MULT/2, time2pixels(i), total_height, color, color, color, thickness);
380 fprintf(svgfile, "</svg>\n");