PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/perf/util/svghelper.c

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