svghelper.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <string.h>
  18. #include "svghelper.h"
  19. static u64 first_time, last_time;
  20. static u64 turbo_frequency, max_freq;
  21. #define SLOT_MULT 30.0
  22. #define SLOT_HEIGHT 25.0
  23. #define WIDTH 1000.0
  24. #define MIN_TEXT_SIZE 0.001
  25. static u64 total_height;
  26. static FILE *svgfile;
  27. static double cpu2slot(int cpu)
  28. {
  29. return 2 * cpu + 1;
  30. }
  31. static double cpu2y(int cpu)
  32. {
  33. return cpu2slot(cpu) * SLOT_MULT;
  34. }
  35. static double time2pixels(u64 time)
  36. {
  37. double X;
  38. X = WIDTH * (time - first_time) / (last_time - first_time);
  39. return X;
  40. }
  41. void open_svg(const char *filename, int cpus, int rows)
  42. {
  43. svgfile = fopen(filename, "w");
  44. if (!svgfile) {
  45. fprintf(stderr, "Cannot open %s for output\n", filename);
  46. return;
  47. }
  48. total_height = (1 + rows + cpu2slot(cpus)) * SLOT_MULT;
  49. fprintf(svgfile, "<?xml version=\"1.0\" standalone=\"no\"?> \n");
  50. fprintf(svgfile, "<svg width=\"%4.1f\" height=\"%llu\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n", WIDTH, total_height);
  51. fprintf(svgfile, "<defs>\n <style type=\"text/css\">\n <![CDATA[\n");
  52. fprintf(svgfile, " rect { stroke-width: 1; }\n");
  53. fprintf(svgfile, " rect.process { fill:rgb(180,180,180); fill-opacity:0.9; stroke-width:1; stroke:rgb( 0, 0, 0); } \n");
  54. fprintf(svgfile, " rect.process2 { fill:rgb(180,180,180); fill-opacity:0.9; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
  55. fprintf(svgfile, " rect.sample { fill:rgb( 0, 0,255); fill-opacity:0.8; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
  56. fprintf(svgfile, " rect.blocked { fill:rgb(255, 0, 0); fill-opacity:0.5; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
  57. fprintf(svgfile, " rect.waiting { fill:rgb(255,255, 0); fill-opacity:0.3; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
  58. fprintf(svgfile, " rect.cpu { fill:rgb(192,192,192); fill-opacity:0.2; stroke-width:0.5; stroke:rgb(128,128,128); } \n");
  59. fprintf(svgfile, " rect.pstate { fill:rgb(128,128,128); fill-opacity:0.8; stroke-width:0; } \n");
  60. fprintf(svgfile, " rect.c1 { fill:rgb(255,214,214); fill-opacity:0.5; stroke-width:0; } \n");
  61. fprintf(svgfile, " rect.c2 { fill:rgb(255,172,172); fill-opacity:0.5; stroke-width:0; } \n");
  62. fprintf(svgfile, " rect.c3 { fill:rgb(255,130,130); fill-opacity:0.5; stroke-width:0; } \n");
  63. fprintf(svgfile, " rect.c4 { fill:rgb(255, 88, 88); fill-opacity:0.5; stroke-width:0; } \n");
  64. fprintf(svgfile, " rect.c5 { fill:rgb(255, 44, 44); fill-opacity:0.5; stroke-width:0; } \n");
  65. fprintf(svgfile, " rect.c6 { fill:rgb(255, 0, 0); fill-opacity:0.5; stroke-width:0; } \n");
  66. fprintf(svgfile, " line.pstate { stroke:rgb(255,255, 0); stroke-opacity:0.8; stroke-width:2; } \n");
  67. fprintf(svgfile, " ]]>\n </style>\n</defs>\n");
  68. }
  69. void svg_box(int Yslot, u64 start, u64 end, const char *type)
  70. {
  71. if (!svgfile)
  72. return;
  73. fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"%s\"/>\n",
  74. time2pixels(start), time2pixels(end)-time2pixels(start), Yslot * SLOT_MULT, SLOT_HEIGHT, type);
  75. }
  76. void svg_sample(int Yslot, int cpu, u64 start, u64 end, const char *type)
  77. {
  78. double text_size;
  79. if (!svgfile)
  80. return;
  81. fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"%s\"/>\n",
  82. time2pixels(start), time2pixels(end)-time2pixels(start), Yslot * SLOT_MULT, SLOT_HEIGHT, type);
  83. text_size = (time2pixels(end)-time2pixels(start));
  84. if (cpu > 9)
  85. text_size = text_size/2;
  86. if (text_size > 1.25)
  87. text_size = 1.25;
  88. if (text_size > MIN_TEXT_SIZE)
  89. fprintf(svgfile, "<text transform=\"translate(%1.8f,%1.8f)\" font-size=\"%1.6fpt\">%i</text>\n",
  90. time2pixels(start), Yslot * SLOT_MULT + SLOT_HEIGHT - 1, text_size, cpu + 1);
  91. }
  92. static char *cpu_model(void)
  93. {
  94. static char cpu_m[255];
  95. char buf[256];
  96. FILE *file;
  97. cpu_m[0] = 0;
  98. /* CPU type */
  99. file = fopen("/proc/cpuinfo", "r");
  100. if (file) {
  101. while (fgets(buf, 255, file)) {
  102. if (strstr(buf, "model name")) {
  103. strncpy(cpu_m, &buf[13], 255);
  104. break;
  105. }
  106. }
  107. fclose(file);
  108. }
  109. return cpu_m;
  110. }
  111. void svg_cpu_box(int cpu, u64 __max_freq, u64 __turbo_freq)
  112. {
  113. char cpu_string[80];
  114. if (!svgfile)
  115. return;
  116. max_freq = __max_freq;
  117. turbo_frequency = __turbo_freq;
  118. fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"cpu\"/>\n",
  119. time2pixels(first_time),
  120. time2pixels(last_time)-time2pixels(first_time),
  121. cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT);
  122. sprintf(cpu_string, "CPU %i", (int)cpu+1);
  123. fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\">%s</text>\n",
  124. 10+time2pixels(first_time), cpu2y(cpu) + SLOT_HEIGHT/2, cpu_string);
  125. fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\" font-size=\"1.25pt\">%s</text>\n",
  126. 10+time2pixels(first_time), cpu2y(cpu) + SLOT_MULT + SLOT_HEIGHT - 4, cpu_model());
  127. }
  128. void svg_process(int cpu, u64 start, u64 end, const char *type, const char *name)
  129. {
  130. double width;
  131. if (!svgfile)
  132. return;
  133. fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"%s\"/>\n",
  134. time2pixels(start), time2pixels(end)-time2pixels(start), cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT, type);
  135. width = time2pixels(end)-time2pixels(start);
  136. if (width > 6)
  137. width = 6;
  138. if (width > MIN_TEXT_SIZE)
  139. fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f) rotate(90)\" font-size=\"%3.4fpt\">%s</text>\n",
  140. time2pixels(start), cpu2y(cpu), width, name);
  141. }
  142. void svg_cstate(int cpu, u64 start, u64 end, int type)
  143. {
  144. double width;
  145. char style[128];
  146. if (!svgfile)
  147. return;
  148. if (type > 6)
  149. type = 6;
  150. sprintf(style, "c%i", type);
  151. fprintf(svgfile, "<rect class=\"%s\" x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\"/>\n",
  152. style,
  153. time2pixels(start), time2pixels(end)-time2pixels(start),
  154. cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT);
  155. width = time2pixels(end)-time2pixels(start);
  156. if (width > 6)
  157. width = 6;
  158. if (width > MIN_TEXT_SIZE)
  159. fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f) rotate(90)\" font-size=\"%3.4fpt\">C%i</text>\n",
  160. time2pixels(start), cpu2y(cpu), width, type);
  161. }
  162. static char *HzToHuman(unsigned long hz)
  163. {
  164. static char buffer[1024];
  165. unsigned long long Hz;
  166. memset(buffer, 0, 1024);
  167. Hz = hz;
  168. /* default: just put the Number in */
  169. sprintf(buffer, "%9lli", Hz);
  170. if (Hz > 1000)
  171. sprintf(buffer, " %6lli Mhz", (Hz+500)/1000);
  172. if (Hz > 1500000)
  173. sprintf(buffer, " %6.2f Ghz", (Hz+5000.0)/1000000);
  174. if (Hz == turbo_frequency)
  175. sprintf(buffer, "Turbo");
  176. return buffer;
  177. }
  178. void svg_pstate(int cpu, u64 start, u64 end, u64 freq)
  179. {
  180. double height = 0;
  181. if (!svgfile)
  182. return;
  183. if (max_freq)
  184. height = freq * 1.0 / max_freq * (SLOT_HEIGHT + SLOT_MULT);
  185. height = 1 + cpu2y(cpu) + SLOT_MULT + SLOT_HEIGHT - height;
  186. fprintf(svgfile, "<line x1=\"%4.8f\" x2=\"%4.8f\" y1=\"%4.1f\" y2=\"%4.1f\" class=\"pstate\"/>\n",
  187. time2pixels(start), time2pixels(end), height, height);
  188. fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\" font-size=\"0.25pt\">%s</text>\n",
  189. time2pixels(start), height+0.9, HzToHuman(freq));
  190. }
  191. void svg_partial_wakeline(u64 start, int row1, int row2)
  192. {
  193. double height;
  194. if (!svgfile)
  195. return;
  196. if (row1 < row2) {
  197. if (row1)
  198. 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",
  199. time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/32);
  200. if (row2)
  201. 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",
  202. time2pixels(start), row2 * SLOT_MULT - SLOT_MULT/32, time2pixels(start), row2 * SLOT_MULT);
  203. } else {
  204. if (row2)
  205. 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",
  206. time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/32);
  207. if (row1)
  208. 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",
  209. time2pixels(start), row1 * SLOT_MULT - SLOT_MULT/32, time2pixels(start), row1 * SLOT_MULT);
  210. }
  211. height = row1 * SLOT_MULT;
  212. if (row2 > row1)
  213. height += SLOT_HEIGHT;
  214. if (row1)
  215. fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(32,255,32)\"/>\n",
  216. time2pixels(start), height);
  217. }
  218. void svg_wakeline(u64 start, int row1, int row2)
  219. {
  220. double height;
  221. if (!svgfile)
  222. return;
  223. if (row1 < row2)
  224. 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",
  225. time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row2 * SLOT_MULT);
  226. else
  227. 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",
  228. time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row1 * SLOT_MULT);
  229. height = row1 * SLOT_MULT;
  230. if (row2 > row1)
  231. height += SLOT_HEIGHT;
  232. fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(32,255,32)\"/>\n",
  233. time2pixels(start), height);
  234. }
  235. void svg_interrupt(u64 start, int row)
  236. {
  237. if (!svgfile)
  238. return;
  239. fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(255,128,128)\"/>\n",
  240. time2pixels(start), row * SLOT_MULT);
  241. fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(255,128,128)\"/>\n",
  242. time2pixels(start), row * SLOT_MULT + SLOT_HEIGHT);
  243. }
  244. void svg_text(int Yslot, u64 start, const char *text)
  245. {
  246. if (!svgfile)
  247. return;
  248. fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\">%s</text>\n",
  249. time2pixels(start), Yslot * SLOT_MULT+SLOT_HEIGHT/2, text);
  250. }
  251. static void svg_legenda_box(int X, const char *text, const char *style)
  252. {
  253. double boxsize;
  254. boxsize = SLOT_HEIGHT / 2;
  255. fprintf(svgfile, "<rect x=\"%i\" width=\"%4.8f\" y=\"0\" height=\"%4.1f\" class=\"%s\"/>\n",
  256. X, boxsize, boxsize, style);
  257. fprintf(svgfile, "<text transform=\"translate(%4.8f, %4.8f)\" font-size=\"%4.4fpt\">%s</text>\n",
  258. X + boxsize + 5, boxsize, 0.8 * boxsize, text);
  259. }
  260. void svg_legenda(void)
  261. {
  262. if (!svgfile)
  263. return;
  264. svg_legenda_box(0, "Running", "sample");
  265. svg_legenda_box(100, "Idle","rect.c1");
  266. svg_legenda_box(200, "Deeper Idle", "rect.c3");
  267. svg_legenda_box(350, "Deepest Idle", "rect.c6");
  268. svg_legenda_box(550, "Sleeping", "process2");
  269. svg_legenda_box(650, "Waiting for cpu", "waiting");
  270. svg_legenda_box(800, "Blocked on IO", "blocked");
  271. }
  272. void svg_time_grid(u64 start, u64 end)
  273. {
  274. u64 i;
  275. first_time = start;
  276. last_time = end;
  277. first_time = first_time / 100000000 * 100000000;
  278. if (!svgfile)
  279. return;
  280. i = first_time;
  281. while (i < last_time) {
  282. int color = 220;
  283. double thickness = 0.075;
  284. if ((i % 100000000) == 0) {
  285. thickness = 0.5;
  286. color = 192;
  287. }
  288. if ((i % 1000000000) == 0) {
  289. thickness = 2.0;
  290. color = 128;
  291. }
  292. 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",
  293. time2pixels(i), SLOT_MULT/2, time2pixels(i), total_height, color, color, color, thickness);
  294. i += 10000000;
  295. }
  296. }
  297. void svg_close(void)
  298. {
  299. if (svgfile) {
  300. fprintf(svgfile, "</svg>\n");
  301. fclose(svgfile);
  302. svgfile = NULL;
  303. }
  304. }