svghelper.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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.01
  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(224,214, 0); fill-opacity:0.8; 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. /* CPU type */
  176. file = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies", "r");
  177. if (file) {
  178. while (fgets(buf, 255, file)) {
  179. unsigned int freq;
  180. freq = strtoull(buf, NULL, 10);
  181. if (freq > max_freq)
  182. max_freq = freq;
  183. }
  184. fclose(file);
  185. }
  186. return cpu_m;
  187. }
  188. void svg_cpu_box(int cpu, u64 __max_freq, u64 __turbo_freq)
  189. {
  190. char cpu_string[80];
  191. if (!svgfile)
  192. return;
  193. max_freq = __max_freq;
  194. turbo_frequency = __turbo_freq;
  195. fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"cpu\"/>\n",
  196. time2pixels(first_time),
  197. time2pixels(last_time)-time2pixels(first_time),
  198. cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT);
  199. sprintf(cpu_string, "CPU %i", (int)cpu+1);
  200. fprintf(svgfile, "<text x=\"%4.8f\" y=\"%4.8f\">%s</text>\n",
  201. 10+time2pixels(first_time), cpu2y(cpu) + SLOT_HEIGHT/2, cpu_string);
  202. fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\" font-size=\"1.25pt\">%s</text>\n",
  203. 10+time2pixels(first_time), cpu2y(cpu) + SLOT_MULT + SLOT_HEIGHT - 4, cpu_model());
  204. }
  205. void svg_process(int cpu, u64 start, u64 end, const char *type, const char *name)
  206. {
  207. double width;
  208. if (!svgfile)
  209. return;
  210. fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\">\n", time2pixels(start), cpu2y(cpu));
  211. fprintf(svgfile, "<rect x=\"0\" width=\"%4.8f\" y=\"0\" height=\"%4.1f\" class=\"%s\"/>\n",
  212. time2pixels(end)-time2pixels(start), SLOT_MULT+SLOT_HEIGHT, type);
  213. width = time2pixels(end)-time2pixels(start);
  214. if (width > 6)
  215. width = 6;
  216. width = round_text_size(width);
  217. if (width > MIN_TEXT_SIZE)
  218. fprintf(svgfile, "<text transform=\"rotate(90)\" font-size=\"%3.8fpt\">%s</text>\n",
  219. width, name);
  220. fprintf(svgfile, "</g>\n");
  221. }
  222. void svg_cstate(int cpu, u64 start, u64 end, int type)
  223. {
  224. double width;
  225. char style[128];
  226. if (!svgfile)
  227. return;
  228. if (type > 6)
  229. type = 6;
  230. sprintf(style, "c%i", type);
  231. fprintf(svgfile, "<rect class=\"%s\" x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\"/>\n",
  232. style,
  233. time2pixels(start), time2pixels(end)-time2pixels(start),
  234. cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT);
  235. width = (time2pixels(end)-time2pixels(start))/2.0;
  236. if (width > 6)
  237. width = 6;
  238. width = round_text_size(width);
  239. if (width > MIN_TEXT_SIZE)
  240. fprintf(svgfile, "<text x=\"%4.8f\" y=\"%4.8f\" font-size=\"%3.8fpt\">C%i</text>\n",
  241. time2pixels(start), cpu2y(cpu)+width, width, type);
  242. }
  243. static char *HzToHuman(unsigned long hz)
  244. {
  245. static char buffer[1024];
  246. unsigned long long Hz;
  247. memset(buffer, 0, 1024);
  248. Hz = hz;
  249. /* default: just put the Number in */
  250. sprintf(buffer, "%9lli", Hz);
  251. if (Hz > 1000)
  252. sprintf(buffer, " %6lli Mhz", (Hz+500)/1000);
  253. if (Hz > 1500000)
  254. sprintf(buffer, " %6.2f Ghz", (Hz+5000.0)/1000000);
  255. if (Hz == turbo_frequency)
  256. sprintf(buffer, "Turbo");
  257. return buffer;
  258. }
  259. void svg_pstate(int cpu, u64 start, u64 end, u64 freq)
  260. {
  261. double height = 0;
  262. if (!svgfile)
  263. return;
  264. if (max_freq)
  265. height = freq * 1.0 / max_freq * (SLOT_HEIGHT + SLOT_MULT);
  266. height = 1 + cpu2y(cpu) + SLOT_MULT + SLOT_HEIGHT - height;
  267. fprintf(svgfile, "<line x1=\"%4.8f\" x2=\"%4.8f\" y1=\"%4.1f\" y2=\"%4.1f\" class=\"pstate\"/>\n",
  268. time2pixels(start), time2pixels(end), height, height);
  269. fprintf(svgfile, "<text x=\"%4.8f\" y=\"%4.8f\" font-size=\"0.25pt\">%s</text>\n",
  270. time2pixels(start), height+0.9, HzToHuman(freq));
  271. }
  272. void svg_partial_wakeline(u64 start, int row1, char *desc1, int row2, char *desc2)
  273. {
  274. double height;
  275. if (!svgfile)
  276. return;
  277. if (row1 < row2) {
  278. if (row1) {
  279. 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",
  280. time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/32);
  281. if (desc2)
  282. fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s &gt;</text></g>\n",
  283. time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT + SLOT_HEIGHT/48, desc2);
  284. }
  285. if (row2) {
  286. 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",
  287. time2pixels(start), row2 * SLOT_MULT - SLOT_MULT/32, time2pixels(start), row2 * SLOT_MULT);
  288. if (desc1)
  289. fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s &gt;</text></g>\n",
  290. time2pixels(start), row2 * SLOT_MULT - SLOT_MULT/32, desc1);
  291. }
  292. } else {
  293. if (row2) {
  294. 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",
  295. time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/32);
  296. if (desc1)
  297. fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s &lt;</text></g>\n",
  298. time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/48, desc1);
  299. }
  300. if (row1) {
  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_MULT/32, time2pixels(start), row1 * SLOT_MULT);
  303. if (desc2)
  304. fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s &lt;</text></g>\n",
  305. time2pixels(start), row1 * SLOT_MULT - SLOT_HEIGHT/32, desc2);
  306. }
  307. }
  308. height = row1 * SLOT_MULT;
  309. if (row2 > row1)
  310. height += SLOT_HEIGHT;
  311. if (row1)
  312. fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(32,255,32)\"/>\n",
  313. time2pixels(start), height);
  314. }
  315. void svg_wakeline(u64 start, int row1, int row2)
  316. {
  317. double height;
  318. if (!svgfile)
  319. return;
  320. if (row1 < row2)
  321. 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",
  322. time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row2 * SLOT_MULT);
  323. else
  324. 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",
  325. time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row1 * SLOT_MULT);
  326. height = row1 * SLOT_MULT;
  327. if (row2 > row1)
  328. height += SLOT_HEIGHT;
  329. fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(32,255,32)\"/>\n",
  330. time2pixels(start), height);
  331. }
  332. void svg_interrupt(u64 start, int row)
  333. {
  334. if (!svgfile)
  335. return;
  336. fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(255,128,128)\"/>\n",
  337. time2pixels(start), row * SLOT_MULT);
  338. fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(255,128,128)\"/>\n",
  339. time2pixels(start), row * SLOT_MULT + SLOT_HEIGHT);
  340. }
  341. void svg_text(int Yslot, u64 start, const char *text)
  342. {
  343. if (!svgfile)
  344. return;
  345. fprintf(svgfile, "<text x=\"%4.8f\" y=\"%4.8f\">%s</text>\n",
  346. time2pixels(start), Yslot * SLOT_MULT+SLOT_HEIGHT/2, text);
  347. }
  348. static void svg_legenda_box(int X, const char *text, const char *style)
  349. {
  350. double boxsize;
  351. boxsize = SLOT_HEIGHT / 2;
  352. fprintf(svgfile, "<rect x=\"%i\" width=\"%4.8f\" y=\"0\" height=\"%4.1f\" class=\"%s\"/>\n",
  353. X, boxsize, boxsize, style);
  354. fprintf(svgfile, "<text transform=\"translate(%4.8f, %4.8f)\" font-size=\"%4.8fpt\">%s</text>\n",
  355. X + boxsize + 5, boxsize, 0.8 * boxsize, text);
  356. }
  357. void svg_legenda(void)
  358. {
  359. if (!svgfile)
  360. return;
  361. svg_legenda_box(0, "Running", "sample");
  362. svg_legenda_box(100, "Idle","rect.c1");
  363. svg_legenda_box(200, "Deeper Idle", "rect.c3");
  364. svg_legenda_box(350, "Deepest Idle", "rect.c6");
  365. svg_legenda_box(550, "Sleeping", "process2");
  366. svg_legenda_box(650, "Waiting for cpu", "waiting");
  367. svg_legenda_box(800, "Blocked on IO", "blocked");
  368. }
  369. void svg_time_grid(void)
  370. {
  371. u64 i;
  372. if (!svgfile)
  373. return;
  374. i = first_time;
  375. while (i < last_time) {
  376. int color = 220;
  377. double thickness = 0.075;
  378. if ((i % 100000000) == 0) {
  379. thickness = 0.5;
  380. color = 192;
  381. }
  382. if ((i % 1000000000) == 0) {
  383. thickness = 2.0;
  384. color = 128;
  385. }
  386. 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",
  387. time2pixels(i), SLOT_MULT/2, time2pixels(i), total_height, color, color, color, thickness);
  388. i += 10000000;
  389. }
  390. }
  391. void svg_close(void)
  392. {
  393. if (svgfile) {
  394. fprintf(svgfile, "</svg>\n");
  395. fclose(svgfile);
  396. svgfile = NULL;
  397. }
  398. }