be1555e241b21545f21f61cfc863fa1c420116bd
[safe/jmp/linux-2.6] / tools / perf / util / svghelper.c
1 /*
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
22 static u64 first_time, last_time;
23 static u64 turbo_frequency, max_freq;
24
25
26 #define SLOT_MULT 30.0
27 #define SLOT_HEIGHT 25.0
28
29 int svg_page_width = 1000;
30
31 #define MIN_TEXT_SIZE 0.001
32
33 static u64 total_height;
34 static FILE *svgfile;
35
36 static double cpu2slot(int cpu)
37 {
38         return 2 * cpu + 1;
39 }
40
41 static double cpu2y(int cpu)
42 {
43         return cpu2slot(cpu) * SLOT_MULT;
44 }
45
46 static double time2pixels(u64 time)
47 {
48         double X;
49
50         X = 1.0 * svg_page_width * (time - first_time) / (last_time - first_time);
51         return X;
52 }
53
54 void open_svg(const char *filename, int cpus, int rows, u64 start, u64 end)
55 {
56         int new_width;
57
58         svgfile = fopen(filename, "w");
59         if (!svgfile) {
60                 fprintf(stderr, "Cannot open %s for output\n", filename);
61                 return;
62         }
63         first_time = start;
64         first_time = first_time / 100000000 * 100000000;
65         last_time = end;
66
67         /*
68          * if the recording is short, we default to a width of 1000, but
69          * for longer recordings we want at least 200 units of width per second
70          */
71         new_width = (last_time - first_time) / 5000000;
72
73         if (new_width > svg_page_width)
74                 svg_page_width = new_width;
75
76         total_height = (1 + rows + cpu2slot(cpus)) * SLOT_MULT;
77         fprintf(svgfile, "<?xml version=\"1.0\" standalone=\"no\"?> \n");
78         fprintf(svgfile, "<svg width=\"%i\" height=\"%llu\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n", svg_page_width, total_height);
79
80         fprintf(svgfile, "<defs>\n  <style type=\"text/css\">\n    <![CDATA[\n");
81
82         fprintf(svgfile, "      rect          { stroke-width: 1; }\n");
83         fprintf(svgfile, "      rect.process  { fill:rgb(180,180,180); fill-opacity:0.9; stroke-width:1;   stroke:rgb(  0,  0,  0); } \n");
84         fprintf(svgfile, "      rect.process2 { fill:rgb(180,180,180); fill-opacity:0.9; stroke-width:0;   stroke:rgb(  0,  0,  0); } \n");
85         fprintf(svgfile, "      rect.sample   { fill:rgb(  0,  0,255); fill-opacity:0.8; stroke-width:0;   stroke:rgb(  0,  0,  0); } \n");
86         fprintf(svgfile, "      rect.blocked  { fill:rgb(255,  0,  0); fill-opacity:0.5; stroke-width:0;   stroke:rgb(  0,  0,  0); } \n");
87         fprintf(svgfile, "      rect.waiting  { fill:rgb(214,214,  0); fill-opacity:0.3; stroke-width:0;   stroke:rgb(  0,  0,  0); } \n");
88         fprintf(svgfile, "      rect.WAITING  { fill:rgb(255,214, 48); fill-opacity:0.6; stroke-width:0;   stroke:rgb(  0,  0,  0); } \n");
89         fprintf(svgfile, "      rect.cpu      { fill:rgb(192,192,192); fill-opacity:0.2; stroke-width:0.5; stroke:rgb(128,128,128); } \n");
90         fprintf(svgfile, "      rect.pstate   { fill:rgb(128,128,128); fill-opacity:0.8; stroke-width:0; } \n");
91         fprintf(svgfile, "      rect.c1       { fill:rgb(255,214,214); fill-opacity:0.5; stroke-width:0; } \n");
92         fprintf(svgfile, "      rect.c2       { fill:rgb(255,172,172); fill-opacity:0.5; stroke-width:0; } \n");
93         fprintf(svgfile, "      rect.c3       { fill:rgb(255,130,130); fill-opacity:0.5; stroke-width:0; } \n");
94         fprintf(svgfile, "      rect.c4       { fill:rgb(255, 88, 88); fill-opacity:0.5; stroke-width:0; } \n");
95         fprintf(svgfile, "      rect.c5       { fill:rgb(255, 44, 44); fill-opacity:0.5; stroke-width:0; } \n");
96         fprintf(svgfile, "      rect.c6       { fill:rgb(255,  0,  0); fill-opacity:0.5; stroke-width:0; } \n");
97         fprintf(svgfile, "      line.pstate   { stroke:rgb(255,255,  0); stroke-opacity:0.8; stroke-width:2; } \n");
98
99         fprintf(svgfile, "    ]]>\n   </style>\n</defs>\n");
100 }
101
102 void svg_box(int Yslot, u64 start, u64 end, const char *type)
103 {
104         if (!svgfile)
105                 return;
106
107         fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"%s\"/>\n",
108                 time2pixels(start), time2pixels(end)-time2pixels(start), Yslot * SLOT_MULT, SLOT_HEIGHT, type);
109 }
110
111 void svg_sample(int Yslot, int cpu, u64 start, u64 end)
112 {
113         double text_size;
114         if (!svgfile)
115                 return;
116
117         fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"sample\"/>\n",
118                 time2pixels(start), time2pixels(end)-time2pixels(start), Yslot * SLOT_MULT, SLOT_HEIGHT);
119
120         text_size = (time2pixels(end)-time2pixels(start));
121         if (cpu > 9)
122                 text_size = text_size/2;
123         if (text_size > 1.25)
124                 text_size = 1.25;
125         if (text_size > MIN_TEXT_SIZE)
126                 fprintf(svgfile, "<text transform=\"translate(%1.8f,%1.8f)\" font-size=\"%1.6fpt\">%i</text>\n",
127                         time2pixels(start), Yslot *  SLOT_MULT + SLOT_HEIGHT - 1, text_size,  cpu + 1);
128
129 }
130
131 static char *time_to_string(u64 duration)
132 {
133         static char text[80];
134
135         text[0] = 0;
136
137         if (duration < 1000) /* less than 1 usec */
138                 return text;
139
140         if (duration < 1000 * 1000) { /* less than 1 msec */
141                 sprintf(text, "%4.1f us", duration / 1000.0);
142                 return text;
143         }
144         sprintf(text, "%4.1f ms", duration / 1000.0 / 1000);
145
146         return text;
147 }
148
149 void svg_waiting(int Yslot, u64 start, u64 end)
150 {
151         char *text;
152         const char *style;
153         double font_size;
154
155         if (!svgfile)
156                 return;
157
158         style = "waiting";
159
160         if (end-start > 10 * 1000000) /* 10 msec */
161                 style = "WAITING";
162
163         text = time_to_string(end-start);
164
165         font_size = 1.0 * (time2pixels(end)-time2pixels(start)) / strlen(text);
166
167         if (font_size > 0.2)
168                 font_size = 0.2;
169
170
171         fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"%s\"/>\n",
172                 time2pixels(start), time2pixels(end)-time2pixels(start), Yslot * SLOT_MULT, SLOT_HEIGHT, style);
173         if (font_size > MIN_TEXT_SIZE)
174                 fprintf(svgfile, "<text transform=\"translate(%1.8f,%1.8f)\" font-size=\"%1.6fpt\">%s</text>\n",
175                         time2pixels(start), Yslot * SLOT_MULT + 2, font_size, text);
176 }
177
178 static char *cpu_model(void)
179 {
180         static char cpu_m[255];
181         char buf[256];
182         FILE *file;
183
184         cpu_m[0] = 0;
185         /* CPU type */
186         file = fopen("/proc/cpuinfo", "r");
187         if (file) {
188                 while (fgets(buf, 255, file)) {
189                         if (strstr(buf, "model name")) {
190                                 strncpy(cpu_m, &buf[13], 255);
191                                 break;
192                         }
193                 }
194                 fclose(file);
195         }
196         return cpu_m;
197 }
198
199 void svg_cpu_box(int cpu, u64 __max_freq, u64 __turbo_freq)
200 {
201         char cpu_string[80];
202         if (!svgfile)
203                 return;
204
205         max_freq = __max_freq;
206         turbo_frequency = __turbo_freq;
207
208         fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"cpu\"/>\n",
209                 time2pixels(first_time),
210                 time2pixels(last_time)-time2pixels(first_time),
211                 cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT);
212
213         sprintf(cpu_string, "CPU %i", (int)cpu+1);
214         fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\">%s</text>\n",
215                 10+time2pixels(first_time), cpu2y(cpu) + SLOT_HEIGHT/2, cpu_string);
216
217         fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\" font-size=\"1.25pt\">%s</text>\n",
218                 10+time2pixels(first_time), cpu2y(cpu) + SLOT_MULT + SLOT_HEIGHT - 4, cpu_model());
219 }
220
221 void svg_process(int cpu, u64 start, u64 end, const char *type, const char *name)
222 {
223         double width;
224
225         if (!svgfile)
226                 return;
227
228         fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"%s\"/>\n",
229                 time2pixels(start), time2pixels(end)-time2pixels(start), cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT, type);
230         width = time2pixels(end)-time2pixels(start);
231         if (width > 6)
232                 width = 6;
233
234         if (width > MIN_TEXT_SIZE)
235                 fprintf(svgfile, "<text  transform=\"translate(%4.8f,%4.8f) rotate(90)\" font-size=\"%3.4fpt\">%s</text>\n",
236                         time2pixels(start), cpu2y(cpu), width, name);
237 }
238
239 void svg_cstate(int cpu, u64 start, u64 end, int type)
240 {
241         double width;
242         char style[128];
243
244         if (!svgfile)
245                 return;
246
247
248         if (type > 6)
249                 type = 6;
250         sprintf(style, "c%i", type);
251
252         fprintf(svgfile, "<rect class=\"%s\" x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\"/>\n",
253                 style,
254                 time2pixels(start), time2pixels(end)-time2pixels(start),
255                 cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT);
256
257         width = time2pixels(end)-time2pixels(start);
258         if (width > 6)
259                 width = 6;
260
261         if (width > MIN_TEXT_SIZE)
262                 fprintf(svgfile, "<text  transform=\"translate(%4.8f,%4.8f) rotate(90)\" font-size=\"%3.4fpt\">C%i</text>\n",
263                         time2pixels(start), cpu2y(cpu), width, type);
264 }
265
266 static char *HzToHuman(unsigned long hz)
267 {
268         static char buffer[1024];
269         unsigned long long Hz;
270
271         memset(buffer, 0, 1024);
272
273         Hz = hz;
274
275         /* default: just put the Number in */
276         sprintf(buffer, "%9lli", Hz);
277
278         if (Hz > 1000)
279                 sprintf(buffer, " %6lli Mhz", (Hz+500)/1000);
280
281         if (Hz > 1500000)
282                 sprintf(buffer, " %6.2f Ghz", (Hz+5000.0)/1000000);
283
284         if (Hz == turbo_frequency)
285                 sprintf(buffer, "Turbo");
286
287         return buffer;
288 }
289
290 void svg_pstate(int cpu, u64 start, u64 end, u64 freq)
291 {
292         double height = 0;
293
294         if (!svgfile)
295                 return;
296
297         if (max_freq)
298                 height = freq * 1.0 / max_freq * (SLOT_HEIGHT + SLOT_MULT);
299         height = 1 + cpu2y(cpu) + SLOT_MULT + SLOT_HEIGHT - height;
300         fprintf(svgfile, "<line x1=\"%4.8f\" x2=\"%4.8f\" y1=\"%4.1f\" y2=\"%4.1f\" class=\"pstate\"/>\n",
301                 time2pixels(start), time2pixels(end), height, height);
302         fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\" font-size=\"0.25pt\">%s</text>\n",
303                 time2pixels(start), height+0.9, HzToHuman(freq));
304
305 }
306
307
308 void svg_partial_wakeline(u64 start, int row1, char *desc1, int row2, char *desc2)
309 {
310         double height;
311
312         if (!svgfile)
313                 return;
314
315
316         if (row1 < row2) {
317                 if (row1) {
318                         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",
319                                 time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT,  time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/32);
320                         if (desc2)
321                                 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f) rotate(90)\" font-size=\"0.02pt\">%s &gt;</text>\n",
322                                         time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT + SLOT_HEIGHT/48, desc2);
323                 }
324                 if (row2) {
325                         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",
326                                 time2pixels(start), row2 * SLOT_MULT - SLOT_MULT/32,  time2pixels(start), row2 * SLOT_MULT);
327                         if (desc1)
328                                 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f) rotate(90)\" font-size=\"0.02pt\">%s &gt;</text>\n",
329                                         time2pixels(start), row2 * SLOT_MULT - SLOT_MULT/32, desc1);
330                 }
331         } else {
332                 if (row2) {
333                         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",
334                                 time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT,  time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/32);
335                         if (desc1)
336                                 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f) rotate(90)\" font-size=\"0.02pt\">%s &lt;</text>\n",
337                                         time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/48, desc1);
338                 }
339                 if (row1) {
340                         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",
341                                 time2pixels(start), row1 * SLOT_MULT - SLOT_MULT/32,  time2pixels(start), row1 * SLOT_MULT);
342                         if (desc2)
343                                 fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f) rotate(90)\" font-size=\"0.02pt\">%s &lt;</text>\n",
344                                         time2pixels(start), row1 * SLOT_MULT - SLOT_HEIGHT/32, desc2);
345                 }
346         }
347         height = row1 * SLOT_MULT;
348         if (row2 > row1)
349                 height += SLOT_HEIGHT;
350         if (row1)
351                 fprintf(svgfile, "<circle  cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\"  style=\"fill:rgb(32,255,32)\"/>\n",
352                         time2pixels(start), height);
353 }
354
355 void svg_wakeline(u64 start, int row1, int row2)
356 {
357         double height;
358
359         if (!svgfile)
360                 return;
361
362
363         if (row1 < row2)
364                 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",
365                         time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT,  time2pixels(start), row2 * SLOT_MULT);
366         else
367                 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",
368                         time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT,  time2pixels(start), row1 * SLOT_MULT);
369
370         height = row1 * SLOT_MULT;
371         if (row2 > row1)
372                 height += SLOT_HEIGHT;
373         fprintf(svgfile, "<circle  cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\"  style=\"fill:rgb(32,255,32)\"/>\n",
374                         time2pixels(start), height);
375 }
376
377 void svg_interrupt(u64 start, int row)
378 {
379         if (!svgfile)
380                 return;
381
382         fprintf(svgfile, "<circle  cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\"  style=\"fill:rgb(255,128,128)\"/>\n",
383                         time2pixels(start), row * SLOT_MULT);
384         fprintf(svgfile, "<circle  cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\"  style=\"fill:rgb(255,128,128)\"/>\n",
385                         time2pixels(start), row * SLOT_MULT + SLOT_HEIGHT);
386 }
387
388 void svg_text(int Yslot, u64 start, const char *text)
389 {
390         if (!svgfile)
391                 return;
392
393         fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\">%s</text>\n",
394                 time2pixels(start), Yslot * SLOT_MULT+SLOT_HEIGHT/2, text);
395 }
396
397 static void svg_legenda_box(int X, const char *text, const char *style)
398 {
399         double boxsize;
400         boxsize = SLOT_HEIGHT / 2;
401
402         fprintf(svgfile, "<rect x=\"%i\" width=\"%4.8f\" y=\"0\" height=\"%4.1f\" class=\"%s\"/>\n",
403                 X, boxsize, boxsize, style);
404         fprintf(svgfile, "<text transform=\"translate(%4.8f, %4.8f)\" font-size=\"%4.4fpt\">%s</text>\n",
405                 X + boxsize + 5, boxsize, 0.8 * boxsize, text);
406 }
407
408 void svg_legenda(void)
409 {
410         if (!svgfile)
411                 return;
412
413         svg_legenda_box(0,      "Running", "sample");
414         svg_legenda_box(100,    "Idle","rect.c1");
415         svg_legenda_box(200,    "Deeper Idle", "rect.c3");
416         svg_legenda_box(350,    "Deepest Idle", "rect.c6");
417         svg_legenda_box(550,    "Sleeping", "process2");
418         svg_legenda_box(650,    "Waiting for cpu", "waiting");
419         svg_legenda_box(800,    "Blocked on IO", "blocked");
420 }
421
422 void svg_time_grid(void)
423 {
424         u64 i;
425
426         if (!svgfile)
427                 return;
428
429         i = first_time;
430         while (i < last_time) {
431                 int color = 220;
432                 double thickness = 0.075;
433                 if ((i % 100000000) == 0) {
434                         thickness = 0.5;
435                         color = 192;
436                 }
437                 if ((i % 1000000000) == 0) {
438                         thickness = 2.0;
439                         color = 128;
440                 }
441
442                 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",
443                         time2pixels(i), SLOT_MULT/2, time2pixels(i), total_height, color, color, color, thickness);
444
445                 i += 10000000;
446         }
447 }
448
449 void svg_close(void)
450 {
451         if (svgfile) {
452                 fprintf(svgfile, "</svg>\n");
453                 fclose(svgfile);
454                 svgfile = NULL;
455         }
456 }