blob: 9f70fa8e764339f6cdf3fc612202423893c77e9c [file] [log] [blame]
Arjan van de Venf48d55c2009-09-12 12:52:11 +02001/*
2 * svghelper.c - helper functions for outputting svg
3 *
4 * (C) Copyright 2009 Intel Corporation
5 *
6 * Authors:
7 * Arjan van de Ven <arjan@linux.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 <stdlib.h>
17#include <unistd.h>
18#include <string.h>
19
20#include "svghelper.h"
21
22static u64 first_time, last_time;
23static u64 turbo_frequency, max_freq;
24
25
26#define SLOT_MULT 30.0
27#define SLOT_HEIGHT 25.0
28#define WIDTH 1000.0
29
Arjan van de Ven964a0b32009-09-19 13:35:07 +020030#define MIN_TEXT_SIZE 0.001
31
Arjan van de Venf48d55c2009-09-12 12:52:11 +020032static u64 total_height;
33static FILE *svgfile;
34
35static double cpu2slot(int cpu)
36{
37 return 2 * cpu + 1;
38}
39
40static double cpu2y(int cpu)
41{
42 return cpu2slot(cpu) * SLOT_MULT;
43}
44
45static double time2pixels(u64 time)
46{
47 double X;
48
49 X = WIDTH * (time - first_time) / (last_time - first_time);
50 return X;
51}
52
53void open_svg(const char *filename, int cpus, int rows)
54{
55
56 svgfile = fopen(filename, "w");
57 if (!svgfile) {
58 fprintf(stderr, "Cannot open %s for output\n", filename);
59 return;
60 }
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);
64
65 fprintf(svgfile, "<defs>\n <style type=\"text/css\">\n <![CDATA[\n");
66
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");
82
83 fprintf(svgfile, " ]]>\n </style>\n</defs>\n");
84}
85
86void svg_box(int Yslot, u64 start, u64 end, const char *type)
87{
88 if (!svgfile)
89 return;
90
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);
93}
94
95void svg_sample(int Yslot, int cpu, u64 start, u64 end, const char *type)
96{
97 double text_size;
98 if (!svgfile)
99 return;
100
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);
103
104 text_size = (time2pixels(end)-time2pixels(start));
105 if (cpu > 9)
106 text_size = text_size/2;
107 if (text_size > 1.25)
108 text_size = 1.25;
Arjan van de Ven964a0b32009-09-19 13:35:07 +0200109 if (text_size > MIN_TEXT_SIZE)
110 fprintf(svgfile, "<text transform=\"translate(%1.8f,%1.8f)\" font-size=\"%1.6fpt\">%i</text>\n",
Arjan van de Venf48d55c2009-09-12 12:52:11 +0200111 time2pixels(start), Yslot * SLOT_MULT + SLOT_HEIGHT - 1, text_size, cpu + 1);
112
113}
114
115static char *cpu_model(void)
116{
117 static char cpu_m[255];
118 char buf[256];
119 FILE *file;
120
121 cpu_m[0] = 0;
122 /* CPU type */
123 file = fopen("/proc/cpuinfo", "r");
124 if (file) {
125 while (fgets(buf, 255, file)) {
126 if (strstr(buf, "model name")) {
127 strncpy(cpu_m, &buf[13], 255);
128 break;
129 }
130 }
131 fclose(file);
132 }
133 return cpu_m;
134}
135
136void svg_cpu_box(int cpu, u64 __max_freq, u64 __turbo_freq)
137{
138 char cpu_string[80];
139 if (!svgfile)
140 return;
141
142 max_freq = __max_freq;
143 turbo_frequency = __turbo_freq;
144
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);
149
150 sprintf(cpu_string, "CPU %i", (int)cpu+1);
Arjan van de Ven964a0b32009-09-19 13:35:07 +0200151 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\">%s</text>\n",
Arjan van de Venf48d55c2009-09-12 12:52:11 +0200152 10+time2pixels(first_time), cpu2y(cpu) + SLOT_HEIGHT/2, cpu_string);
153
Arjan van de Ven964a0b32009-09-19 13:35:07 +0200154 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\" font-size=\"1.25pt\">%s</text>\n",
Arjan van de Venf48d55c2009-09-12 12:52:11 +0200155 10+time2pixels(first_time), cpu2y(cpu) + SLOT_MULT + SLOT_HEIGHT - 4, cpu_model());
156}
157
158void svg_process(int cpu, u64 start, u64 end, const char *type, const char *name)
159{
160 double width;
161
162 if (!svgfile)
163 return;
164
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);
168 if (width > 6)
169 width = 6;
170
Arjan van de Ven964a0b32009-09-19 13:35:07 +0200171 if (width > MIN_TEXT_SIZE)
172 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f) rotate(90)\" font-size=\"%3.4fpt\">%s</text>\n",
Arjan van de Venf48d55c2009-09-12 12:52:11 +0200173 time2pixels(start), cpu2y(cpu), width, name);
174}
175
176void svg_cstate(int cpu, u64 start, u64 end, int type)
177{
178 double width;
179 char style[128];
180
181 if (!svgfile)
182 return;
183
184
185 if (type > 6)
186 type = 6;
187 sprintf(style, "c%i", type);
188
189 fprintf(svgfile, "<rect class=\"%s\" x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\"/>\n",
190 style,
191 time2pixels(start), time2pixels(end)-time2pixels(start),
192 cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT);
193
194 width = time2pixels(end)-time2pixels(start);
195 if (width > 6)
196 width = 6;
197
Arjan van de Ven964a0b32009-09-19 13:35:07 +0200198 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",
Arjan van de Venf48d55c2009-09-12 12:52:11 +0200200 time2pixels(start), cpu2y(cpu), width, type);
201}
202
203static char *HzToHuman(unsigned long hz)
204{
205 static char buffer[1024];
206 unsigned long long Hz;
207
208 memset(buffer, 0, 1024);
209
210 Hz = hz;
211
212 /* default: just put the Number in */
213 sprintf(buffer, "%9lli", Hz);
214
215 if (Hz > 1000)
216 sprintf(buffer, " %6lli Mhz", (Hz+500)/1000);
217
218 if (Hz > 1500000)
219 sprintf(buffer, " %6.2f Ghz", (Hz+5000.0)/1000000);
220
221 if (Hz == turbo_frequency)
222 sprintf(buffer, "Turbo");
223
224 return buffer;
225}
226
227void svg_pstate(int cpu, u64 start, u64 end, u64 freq)
228{
229 double height = 0;
230
231 if (!svgfile)
232 return;
233
234 if (max_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);
Arjan van de Ven964a0b32009-09-19 13:35:07 +0200239 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\" font-size=\"0.25pt\">%s</text>\n",
Arjan van de Venf48d55c2009-09-12 12:52:11 +0200240 time2pixels(start), height+0.9, HzToHuman(freq));
241
242}
243
244
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200245void svg_partial_wakeline(u64 start, int row1, char *desc1, int row2, char *desc2)
Arjan van de Venf48d55c2009-09-12 12:52:11 +0200246{
247 double height;
248
249 if (!svgfile)
250 return;
251
252
253 if (row1 < row2) {
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200254 if (row1) {
Arjan van de Venf48d55c2009-09-12 12:52:11 +0200255 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);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200257 if (desc2)
258 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f) rotate(90)\" font-size=\"0.02pt\">%s &gt;</text>\n",
259 time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT + SLOT_HEIGHT/48, desc2);
260 }
261 if (row2) {
Arjan van de Venf48d55c2009-09-12 12:52:11 +0200262 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",
263 time2pixels(start), row2 * SLOT_MULT - SLOT_MULT/32, time2pixels(start), row2 * SLOT_MULT);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200264 if (desc1)
265 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f) rotate(90)\" font-size=\"0.02pt\">%s &gt;</text>\n",
266 time2pixels(start), row2 * SLOT_MULT - SLOT_MULT/32, desc1);
267 }
Arjan van de Venf48d55c2009-09-12 12:52:11 +0200268 } else {
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200269 if (row2) {
Arjan van de Venf48d55c2009-09-12 12:52:11 +0200270 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",
271 time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/32);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200272 if (desc1)
273 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f) rotate(90)\" font-size=\"0.02pt\">%s &lt;</text>\n",
274 time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/48, desc1);
275 }
276 if (row1) {
Arjan van de Venf48d55c2009-09-12 12:52:11 +0200277 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",
278 time2pixels(start), row1 * SLOT_MULT - SLOT_MULT/32, time2pixels(start), row1 * SLOT_MULT);
Arjan van de Ven4f1202c2009-09-20 18:13:28 +0200279 if (desc2)
280 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f) rotate(90)\" font-size=\"0.02pt\">%s &lt;</text>\n",
281 time2pixels(start), row1 * SLOT_MULT - SLOT_HEIGHT/32, desc2);
282 }
Arjan van de Venf48d55c2009-09-12 12:52:11 +0200283 }
284 height = row1 * SLOT_MULT;
285 if (row2 > row1)
286 height += SLOT_HEIGHT;
287 if (row1)
288 fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(32,255,32)\"/>\n",
289 time2pixels(start), height);
290}
291
292void svg_wakeline(u64 start, int row1, int row2)
293{
294 double height;
295
296 if (!svgfile)
297 return;
298
299
300 if (row1 < row2)
301 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",
302 time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row2 * SLOT_MULT);
303 else
304 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",
305 time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row1 * SLOT_MULT);
306
307 height = row1 * SLOT_MULT;
308 if (row2 > row1)
309 height += SLOT_HEIGHT;
310 fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(32,255,32)\"/>\n",
311 time2pixels(start), height);
312}
313
314void svg_interrupt(u64 start, int row)
315{
316 if (!svgfile)
317 return;
318
319 fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(255,128,128)\"/>\n",
320 time2pixels(start), row * SLOT_MULT);
321 fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(255,128,128)\"/>\n",
322 time2pixels(start), row * SLOT_MULT + SLOT_HEIGHT);
323}
324
325void svg_text(int Yslot, u64 start, const char *text)
326{
327 if (!svgfile)
328 return;
329
Arjan van de Ven964a0b32009-09-19 13:35:07 +0200330 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\">%s</text>\n",
Arjan van de Venf48d55c2009-09-12 12:52:11 +0200331 time2pixels(start), Yslot * SLOT_MULT+SLOT_HEIGHT/2, text);
332}
333
334static void svg_legenda_box(int X, const char *text, const char *style)
335{
336 double boxsize;
337 boxsize = SLOT_HEIGHT / 2;
338
339 fprintf(svgfile, "<rect x=\"%i\" width=\"%4.8f\" y=\"0\" height=\"%4.1f\" class=\"%s\"/>\n",
340 X, boxsize, boxsize, style);
Arjan van de Ven964a0b32009-09-19 13:35:07 +0200341 fprintf(svgfile, "<text transform=\"translate(%4.8f, %4.8f)\" font-size=\"%4.4fpt\">%s</text>\n",
Arjan van de Venf48d55c2009-09-12 12:52:11 +0200342 X + boxsize + 5, boxsize, 0.8 * boxsize, text);
343}
344
345void svg_legenda(void)
346{
347 if (!svgfile)
348 return;
349
350 svg_legenda_box(0, "Running", "sample");
351 svg_legenda_box(100, "Idle","rect.c1");
352 svg_legenda_box(200, "Deeper Idle", "rect.c3");
353 svg_legenda_box(350, "Deepest Idle", "rect.c6");
354 svg_legenda_box(550, "Sleeping", "process2");
355 svg_legenda_box(650, "Waiting for cpu", "waiting");
356 svg_legenda_box(800, "Blocked on IO", "blocked");
357}
358
359void svg_time_grid(u64 start, u64 end)
360{
361 u64 i;
362
363 first_time = start;
364 last_time = end;
365
366 first_time = first_time / 100000000 * 100000000;
367
368 if (!svgfile)
369 return;
370
371 i = first_time;
372 while (i < last_time) {
373 int color = 220;
374 double thickness = 0.075;
375 if ((i % 100000000) == 0) {
376 thickness = 0.5;
377 color = 192;
378 }
379 if ((i % 1000000000) == 0) {
380 thickness = 2.0;
381 color = 128;
382 }
383
384 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",
385 time2pixels(i), SLOT_MULT/2, time2pixels(i), total_height, color, color, color, thickness);
386
387 i += 10000000;
388 }
389}
390
391void svg_close(void)
392{
393 if (svgfile) {
394 fprintf(svgfile, "</svg>\n");
395 fclose(svgfile);
396 svgfile = NULL;
397 }
398}