svghelper.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. int svg_page_width = 1000;
  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 = 1.0 * svg_page_width * (time - first_time) / (last_time - first_time);
  39. return X;
  40. }
  41. /*
  42. * Round text sizes so that the svg viewer only needs a discrete
  43. * number of renderings of the font
  44. */
  45. static double round_text_size(double size)
  46. {
  47. int loop = 100;
  48. double target = 10.0;
  49. if (size >= 10.0)
  50. return size;
  51. while (loop--) {
  52. if (size >= target)
  53. return target;
  54. target = target / 2.0;
  55. }
  56. return size;
  57. }
  58. void open_svg(const char *filename, int cpus, int rows, u64 start, u64 end)
  59. {
  60. int new_width;
  61. svgfile = fopen(filename, "w");
  62. if (!svgfile) {
  63. fprintf(stderr, "Cannot open %s for output\n", filename);
  64. return;
  65. }
  66. first_time = start;
  67. first_time = first_time / 100000000 * 100000000;
  68. last_time = end;
  69. /*
  70. * if the recording is short, we default to a width of 1000, but
  71. * for longer recordings we want at least 200 units of width per second
  72. */
  73. new_width = (last_time - first_time) / 5000000;
  74. if (new_width > svg_page_width)
  75. svg_page_width = new_width;
  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. fprintf(svgfile, "<defs>\n <style type=\"text/css\">\n <![CDATA[\n");
  80. fprintf(svgfile, " rect { stroke-width: 1; }\n");
  81. fprintf(svgfile, " rect.process { fill:rgb(180,180,180); fill-opacity:0.9; stroke-width:1; stroke:rgb( 0, 0, 0); } \n");
  82. fprintf(svgfile, " rect.process2 { fill:rgb(180,180,180); fill-opacity:0.9; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
  83. fprintf(svgfile, " rect.sample { fill:rgb( 0, 0,255); fill-opacity:0.8; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
  84. fprintf(svgfile, " rect.blocked { fill:rgb(255, 0, 0); fill-opacity:0.5; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
  85. fprintf(svgfile, " rect.waiting { fill:rgb(214,214, 0); fill-opacity:0.3; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
  86. fprintf(svgfile, " rect.WAITING { fill:rgb(255,214, 48); fill-opacity:0.6; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
  87. fprintf(svgfile, " rect.cpu { fill:rgb(192,192,192); fill-opacity:0.2; stroke-width:0.5; stroke:rgb(128,128,128); } \n");
  88. fprintf(svgfile, " rect.pstate { fill:rgb(128,128,128); fill-opacity:0.8; stroke-width:0; } \n");
  89. fprintf(svgfile, " rect.c1 { fill:rgb(255,214,214); fill-opacity:0.5; stroke-width:0; } \n");
  90. fprintf(svgfile, " rect.c2 { fill:rgb(255,172,172); fill-opacity:0.5; stroke-width:0; } \n");
  91. fprintf(svgfile, " rect.c3 { fill:rgb(255,130,130); fill-opacity:0.5; stroke-width:0; } \n");
  92. fprintf(svgfile, " rect.c4 { fill:rgb(255, 88, 88); fill-opacity:0.5; stroke-width:0; } \n");
  93. fprintf(svgfile, " rect.c5 { fill:rgb(255, 44, 44); fill-opacity:0.5; stroke-width:0; } \n");
  94. fprintf(svgfile, " rect.c6 { fill:rgb(255, 0, 0); fill-opacity:0.5; stroke-width:0; } \n");
  95. fprintf(svgfile, " line.pstate { stroke:rgb(255,255, 0); stroke-opacity:0.8; stroke-width:2; } \n");
  96. fprintf(svgfile, " ]]>\n </style>\n</defs>\n");
  97. }
  98. void svg_box(int Yslot, u64 start, u64 end, const char *type)
  99. {
  100. if (!svgfile)
  101. return;
  102. fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"%s\"/>\n",
  103. time2pixels(start), time2pixels(end)-time2pixels(start), Yslot * SLOT_MULT, SLOT_HEIGHT, type);
  104. }
  105. void svg_sample(int Yslot, int cpu, u64 start, u64 end)
  106. {
  107. double text_size;
  108. if (!svgfile)
  109. return;
  110. fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"sample\"/>\n",
  111. time2pixels(start), time2pixels(end)-time2pixels(start), Yslot * SLOT_MULT, SLOT_HEIGHT);
  112. text_size = (time2pixels(end)-time2pixels(start));
  113. if (cpu > 9)
  114. text_size = text_size/2;
  115. if (text_size > 1.25)
  116. text_size = 1.25;
  117. text_size = round_text_size(text_size);
  118. if (text_size > MIN_TEXT_SIZE)
  119. fprintf(svgfile, "<text x=\"%1.8f\" y=\"%1.8f\" font-size=\"%1.8fpt\">%i</text>\n",
  120. time2pixels(start), Yslot * SLOT_MULT + SLOT_HEIGHT - 1, text_size, cpu + 1);
  121. }
  122. static char *time_to_string(u64 duration)
  123. {
  124. static char text[80];
  125. text[0] = 0;
  126. if (duration < 1000) /* less than 1 usec */
  127. return text;
  128. if (duration < 1000 * 1000) { /* less than 1 msec */
  129. sprintf(text, "%4.1f us", duration / 1000.0);
  130. return text;
  131. }
  132. sprintf(text, "%4.1f ms", duration / 1000.0 / 1000);
  133. return text;
  134. }
  135. void svg_waiting(int Yslot, u64 start, u64 end)
  136. {
  137. char *text;
  138. const char *style;
  139. double font_size;
  140. if (!svgfile)
  141. return;
  142. style = "waiting";
  143. if (end-start > 10 * 1000000) /* 10 msec */
  144. style = "WAITING";
  145. text = time_to_string(end-start);
  146. font_size = 1.0 * (time2pixels(end)-time2pixels(start));
  147. if (font_size > 3)
  148. font_size = 3;
  149. font_size = round_text_size(font_size);
  150. fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\">\n", time2pixels(start), Yslot * SLOT_MULT);
  151. fprintf(svgfile, "<rect x=\"0\" width=\"%4.8f\" y=\"0\" height=\"%4.1f\" class=\"%s\"/>\n",
  152. time2pixels(end)-time2pixels(start), SLOT_HEIGHT, style);
  153. if (font_size > MIN_TEXT_SIZE)
  154. fprintf(svgfile, "<text transform=\"rotate(90)\" font-size=\"%1.8fpt\"> %s</text>\n",
  155. font_size, text);
  156. fprintf(svgfile, "</g>\n");
  157. }
  158. static char *cpu_model(void)
  159. {
  160. static char cpu_m[255];
  161. char buf[256];
  162. FILE *file;
  163. cpu_m[0] = 0;
  164. /* CPU type */
  165. file = fopen("/proc/cpuinfo", "r");
  166. if (file) {
  167. while (fgets(buf, 255, file)) {
  168. if (strstr(buf, "model name")) {
  169. strncpy(cpu_m, &buf[13], 255);
  170. break;
  171. }
  172. }
  173. fclose(file);
  174. }
  175. return cpu_m;
  176. }
  177. void svg_cpu_box(int cpu, u64 __max_freq, u64 __turbo_freq)
  178. {
  179. char cpu_string[80];
  180. if (!svgfile)
  181. return;
  182. max_freq = __max_freq;
  183. turbo_frequency = __turbo_freq;
  184. fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"cpu\"/>\n",
  185. time2pixels(first_time),
  186. time2pixels(last_time)-time2pixels(first_time),
  187. cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT);
  188. sprintf(cpu_string, "CPU %i", (int)cpu+1);
  189. fprintf(svgfile, "<text x=\"%4.8f\" y=\"%4.8f\">%s</text>\n",
  190. 10+time2pixels(first_time), cpu2y(cpu) + SLOT_HEIGHT/2, cpu_string);
  191. fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\" font-size=\"1.25pt\">%s</text>\n",
  192. 10+time2pixels(first_time), cpu2y(cpu) + SLOT_MULT + SLOT_HEIGHT - 4, cpu_model());
  193. }
  194. void svg_process(int cpu, u64 start, u64 end, const char *type, const char *name)
  195. {
  196. double width;
  197. if (!svgfile)
  198. return;
  199. fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\">\n", time2pixels(start), cpu2y(cpu));
  200. fprintf(svgfile, "<rect x=\"0\" width=\"%4.8f\" y=\"0\" height=\"%4.1f\" class=\"%s\"/>\n",
  201. time2pixels(end)-time2pixels(start), SLOT_MULT+SLOT_HEIGHT, type);
  202. width = time2pixels(end)-time2pixels(start);
  203. if (width > 6)
  204. width = 6;
  205. width = round_text_size(width);
  206. if (width > MIN_TEXT_SIZE)
  207. fprintf(svgfile, "<text transform=\"rotate(90)\" font-size=\"%3.8fpt\">%s</text>\n",
  208. width, name);
  209. fprintf(svgfile, "</g>\n");
  210. }
  211. void svg_cstate(int cpu, u64 start, u64 end, int type)
  212. {
  213. double width;
  214. char style[128];
  215. if (!svgfile)
  216. return;
  217. if (type > 6)
  218. type = 6;
  219. sprintf(style, "c%i", type);
  220. fprintf(svgfile, "<rect class=\"%s\" x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\"/>\n",
  221. style,
  222. time2pixels(start), time2pixels(end)-time2pixels(start),
  223. cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT);
  224. width = (time2pixels(end)-time2pixels(start))/2.0;
  225. if (width > 6)
  226. width = 6;
  227. width = round_text_size(width);
  228. if (width > MIN_TEXT_SIZE)
  229. fprintf(svgfile, "<text x=\"%4.8f\" y=\"%4.8f\" font-size=\"%3.8fpt\">C%i</text>\n",
  230. time2pixels(start), cpu2y(cpu)+width, width, type);
  231. }
  232. static char *HzToHuman(unsigned long hz)
  233. {
  234. static char buffer[1024];
  235. unsigned long long Hz;
  236. memset(buffer, 0, 1024);
  237. Hz = hz;
  238. /* default: just put the Number in */
  239. sprintf(buffer, "%9lli", Hz);
  240. if (Hz > 1000)
  241. sprintf(buffer, " %6lli Mhz", (Hz+500)/1000);
  242. if (Hz > 1500000)
  243. sprintf(buffer, " %6.2f Ghz", (Hz+5000.0)/1000000);
  244. if (Hz == turbo_frequency)
  245. sprintf(buffer, "Turbo");
  246. return buffer;
  247. }
  248. void svg_pstate(int cpu, u64 start, u64 end, u64 freq)
  249. {
  250. double height = 0;
  251. if (!svgfile)
  252. return;
  253. if (max_freq)
  254. height = freq * 1.0 / max_freq * (SLOT_HEIGHT + SLOT_MULT);
  255. height = 1 + cpu2y(cpu) + SLOT_MULT + SLOT_HEIGHT - height;
  256. fprintf(svgfile, "<line x1=\"%4.8f\" x2=\"%4.8f\" y1=\"%4.1f\" y2=\"%4.1f\" class=\"pstate\"/>\n",
  257. time2pixels(start), time2pixels(end), height, height);
  258. fprintf(svgfile, "<text x=\"%4.8f\" y=\"%4.8f\" font-size=\"0.25pt\">%s</text>\n",
  259. time2pixels(start), height+0.9, HzToHuman(freq));
  260. }
  261. void svg_partial_wakeline(u64 start, int row1, char *desc1, int row2, char *desc2)
  262. {
  263. double height;
  264. if (!svgfile)
  265. return;
  266. if (row1 < row2) {
  267. if (row1) {
  268. 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",
  269. time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/32);
  270. if (desc2)
  271. fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s &gt;</text></g>\n",
  272. time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT + SLOT_HEIGHT/48, desc2);
  273. }
  274. if (row2) {
  275. 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",
  276. time2pixels(start), row2 * SLOT_MULT - SLOT_MULT/32, time2pixels(start), row2 * SLOT_MULT);
  277. if (desc1)
  278. fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s &gt;</text></g>\n",
  279. time2pixels(start), row2 * SLOT_MULT - SLOT_MULT/32, desc1);
  280. }
  281. } else {
  282. if (row2) {
  283. 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",
  284. time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/32);
  285. if (desc1)
  286. fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s &lt;</text></g>\n",
  287. time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/48, desc1);
  288. }
  289. if (row1) {
  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), row1 * SLOT_MULT - SLOT_MULT/32, time2pixels(start), row1 * SLOT_MULT);
  292. if (desc2)
  293. fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s &lt;</text></g>\n",
  294. time2pixels(start), row1 * SLOT_MULT - SLOT_HEIGHT/32, desc2);
  295. }
  296. }
  297. height = row1 * SLOT_MULT;
  298. if (row2 > row1)
  299. height += SLOT_HEIGHT;
  300. if (row1)
  301. fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(32,255,32)\"/>\n",
  302. time2pixels(start), height);
  303. }
  304. void svg_wakeline(u64 start, int row1, int row2)
  305. {
  306. double height;
  307. if (!svgfile)
  308. return;
  309. if (row1 < row2)
  310. 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",
  311. time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row2 * SLOT_MULT);
  312. else
  313. 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",
  314. time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row1 * SLOT_MULT);
  315. height = row1 * SLOT_MULT;
  316. if (row2 > row1)
  317. height += SLOT_HEIGHT;
  318. fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(32,255,32)\"/>\n",
  319. time2pixels(start), height);
  320. }
  321. void svg_interrupt(u64 start, int row)
  322. {
  323. if (!svgfile)
  324. return;
  325. fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(255,128,128)\"/>\n",
  326. time2pixels(start), row * SLOT_MULT);
  327. fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(255,128,128)\"/>\n",
  328. time2pixels(start), row * SLOT_MULT + SLOT_HEIGHT);
  329. }
  330. void svg_text(int Yslot, u64 start, const char *text)
  331. {
  332. if (!svgfile)
  333. return;
  334. fprintf(svgfile, "<text x=\"%4.8f\" y=\"%4.8f\">%s</text>\n",
  335. time2pixels(start), Yslot * SLOT_MULT+SLOT_HEIGHT/2, text);
  336. }
  337. static void svg_legenda_box(int X, const char *text, const char *style)
  338. {
  339. double boxsize;
  340. boxsize = SLOT_HEIGHT / 2;
  341. fprintf(svgfile, "<rect x=\"%i\" width=\"%4.8f\" y=\"0\" height=\"%4.1f\" class=\"%s\"/>\n",
  342. X, boxsize, boxsize, style);
  343. fprintf(svgfile, "<text transform=\"translate(%4.8f, %4.8f)\" font-size=\"%4.8fpt\">%s</text>\n",
  344. X + boxsize + 5, boxsize, 0.8 * boxsize, text);
  345. }
  346. void svg_legenda(void)
  347. {
  348. if (!svgfile)
  349. return;
  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. void svg_time_grid(void)
  359. {
  360. u64 i;
  361. if (!svgfile)
  362. return;
  363. i = first_time;
  364. while (i < last_time) {
  365. int color = 220;
  366. double thickness = 0.075;
  367. if ((i % 100000000) == 0) {
  368. thickness = 0.5;
  369. color = 192;
  370. }
  371. if ((i % 1000000000) == 0) {
  372. thickness = 2.0;
  373. color = 128;
  374. }
  375. 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",
  376. time2pixels(i), SLOT_MULT/2, time2pixels(i), total_height, color, color, color, thickness);
  377. i += 10000000;
  378. }
  379. }
  380. void svg_close(void)
  381. {
  382. if (svgfile) {
  383. fprintf(svgfile, "</svg>\n");
  384. fclose(svgfile);
  385. svgfile = NULL;
  386. }
  387. }