svghelper.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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 <linux/bitmap.h>
  20. #include "perf.h"
  21. #include "svghelper.h"
  22. #include "util.h"
  23. #include "cpumap.h"
  24. static u64 first_time, last_time;
  25. static u64 turbo_frequency, max_freq;
  26. #define SLOT_MULT 30.0
  27. #define SLOT_HEIGHT 25.0
  28. int svg_page_width = 1000;
  29. u64 svg_highlight;
  30. const char *svg_highlight_name;
  31. #define MIN_TEXT_SIZE 0.01
  32. static u64 total_height;
  33. static FILE *svgfile;
  34. static double cpu2slot(int cpu)
  35. {
  36. return 2 * cpu + 1;
  37. }
  38. static int *topology_map;
  39. static double cpu2y(int cpu)
  40. {
  41. if (topology_map)
  42. return cpu2slot(topology_map[cpu]) * SLOT_MULT;
  43. else
  44. return cpu2slot(cpu) * SLOT_MULT;
  45. }
  46. static double time2pixels(u64 __time)
  47. {
  48. double X;
  49. X = 1.0 * svg_page_width * (__time - first_time) / (last_time - first_time);
  50. return X;
  51. }
  52. /*
  53. * Round text sizes so that the svg viewer only needs a discrete
  54. * number of renderings of the font
  55. */
  56. static double round_text_size(double size)
  57. {
  58. int loop = 100;
  59. double target = 10.0;
  60. if (size >= 10.0)
  61. return size;
  62. while (loop--) {
  63. if (size >= target)
  64. return target;
  65. target = target / 2.0;
  66. }
  67. return size;
  68. }
  69. void open_svg(const char *filename, int cpus, int rows, u64 start, u64 end)
  70. {
  71. int new_width;
  72. svgfile = fopen(filename, "w");
  73. if (!svgfile) {
  74. fprintf(stderr, "Cannot open %s for output\n", filename);
  75. return;
  76. }
  77. first_time = start;
  78. first_time = first_time / 100000000 * 100000000;
  79. last_time = end;
  80. /*
  81. * if the recording is short, we default to a width of 1000, but
  82. * for longer recordings we want at least 200 units of width per second
  83. */
  84. new_width = (last_time - first_time) / 5000000;
  85. if (new_width > svg_page_width)
  86. svg_page_width = new_width;
  87. total_height = (1 + rows + cpu2slot(cpus)) * SLOT_MULT;
  88. fprintf(svgfile, "<?xml version=\"1.0\" standalone=\"no\"?> \n");
  89. fprintf(svgfile, "<!DOCTYPE svg SYSTEM \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n");
  90. fprintf(svgfile, "<svg width=\"%i\" height=\"%" PRIu64 "\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">\n", svg_page_width, total_height);
  91. fprintf(svgfile, "<defs>\n <style type=\"text/css\">\n <![CDATA[\n");
  92. fprintf(svgfile, " rect { stroke-width: 1; }\n");
  93. fprintf(svgfile, " rect.process { fill:rgb(180,180,180); fill-opacity:0.9; stroke-width:1; stroke:rgb( 0, 0, 0); } \n");
  94. fprintf(svgfile, " rect.process2 { fill:rgb(180,180,180); fill-opacity:0.9; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
  95. fprintf(svgfile, " rect.sample { fill:rgb( 0, 0,255); fill-opacity:0.8; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
  96. fprintf(svgfile, " rect.sample_hi{ fill:rgb(255,128, 0); fill-opacity:0.8; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
  97. fprintf(svgfile, " rect.blocked { fill:rgb(255, 0, 0); fill-opacity:0.5; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
  98. fprintf(svgfile, " rect.waiting { fill:rgb(224,214, 0); fill-opacity:0.8; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
  99. fprintf(svgfile, " rect.WAITING { fill:rgb(255,214, 48); fill-opacity:0.6; stroke-width:0; stroke:rgb( 0, 0, 0); } \n");
  100. fprintf(svgfile, " rect.cpu { fill:rgb(192,192,192); fill-opacity:0.2; stroke-width:0.5; stroke:rgb(128,128,128); } \n");
  101. fprintf(svgfile, " rect.pstate { fill:rgb(128,128,128); fill-opacity:0.8; stroke-width:0; } \n");
  102. fprintf(svgfile, " rect.c1 { fill:rgb(255,214,214); fill-opacity:0.5; stroke-width:0; } \n");
  103. fprintf(svgfile, " rect.c2 { fill:rgb(255,172,172); fill-opacity:0.5; stroke-width:0; } \n");
  104. fprintf(svgfile, " rect.c3 { fill:rgb(255,130,130); fill-opacity:0.5; stroke-width:0; } \n");
  105. fprintf(svgfile, " rect.c4 { fill:rgb(255, 88, 88); fill-opacity:0.5; stroke-width:0; } \n");
  106. fprintf(svgfile, " rect.c5 { fill:rgb(255, 44, 44); fill-opacity:0.5; stroke-width:0; } \n");
  107. fprintf(svgfile, " rect.c6 { fill:rgb(255, 0, 0); fill-opacity:0.5; stroke-width:0; } \n");
  108. fprintf(svgfile, " line.pstate { stroke:rgb(255,255, 0); stroke-opacity:0.8; stroke-width:2; } \n");
  109. fprintf(svgfile, " ]]>\n </style>\n</defs>\n");
  110. }
  111. void svg_box(int Yslot, u64 start, u64 end, const char *type)
  112. {
  113. if (!svgfile)
  114. return;
  115. fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"%s\"/>\n",
  116. time2pixels(start), time2pixels(end)-time2pixels(start), Yslot * SLOT_MULT, SLOT_HEIGHT, type);
  117. }
  118. static char *time_to_string(u64 duration);
  119. void svg_blocked(int Yslot, int cpu, u64 start, u64 end, const char *backtrace)
  120. {
  121. if (!svgfile)
  122. return;
  123. fprintf(svgfile, "<g>\n");
  124. fprintf(svgfile, "<title>#%d blocked %s</title>\n", cpu,
  125. time_to_string(end - start));
  126. if (backtrace)
  127. fprintf(svgfile, "<desc>Blocked on:\n%s</desc>\n", backtrace);
  128. svg_box(Yslot, start, end, "blocked");
  129. fprintf(svgfile, "</g>\n");
  130. }
  131. void svg_running(int Yslot, int cpu, u64 start, u64 end, const char *backtrace)
  132. {
  133. double text_size;
  134. const char *type;
  135. if (!svgfile)
  136. return;
  137. if (svg_highlight && end - start > svg_highlight)
  138. type = "sample_hi";
  139. else
  140. type = "sample";
  141. fprintf(svgfile, "<g>\n");
  142. fprintf(svgfile, "<title>#%d running %s</title>\n",
  143. cpu, time_to_string(end - start));
  144. if (backtrace)
  145. fprintf(svgfile, "<desc>Switched because:\n%s</desc>\n", backtrace);
  146. fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"%s\"/>\n",
  147. time2pixels(start), time2pixels(end)-time2pixels(start), Yslot * SLOT_MULT, SLOT_HEIGHT,
  148. type);
  149. text_size = (time2pixels(end)-time2pixels(start));
  150. if (cpu > 9)
  151. text_size = text_size/2;
  152. if (text_size > 1.25)
  153. text_size = 1.25;
  154. text_size = round_text_size(text_size);
  155. if (text_size > MIN_TEXT_SIZE)
  156. fprintf(svgfile, "<text x=\"%1.8f\" y=\"%1.8f\" font-size=\"%1.8fpt\">%i</text>\n",
  157. time2pixels(start), Yslot * SLOT_MULT + SLOT_HEIGHT - 1, text_size, cpu + 1);
  158. fprintf(svgfile, "</g>\n");
  159. }
  160. static char *time_to_string(u64 duration)
  161. {
  162. static char text[80];
  163. text[0] = 0;
  164. if (duration < 1000) /* less than 1 usec */
  165. return text;
  166. if (duration < 1000 * 1000) { /* less than 1 msec */
  167. sprintf(text, "%4.1f us", duration / 1000.0);
  168. return text;
  169. }
  170. sprintf(text, "%4.1f ms", duration / 1000.0 / 1000);
  171. return text;
  172. }
  173. void svg_waiting(int Yslot, int cpu, u64 start, u64 end, const char *backtrace)
  174. {
  175. char *text;
  176. const char *style;
  177. double font_size;
  178. if (!svgfile)
  179. return;
  180. style = "waiting";
  181. if (end-start > 10 * 1000000) /* 10 msec */
  182. style = "WAITING";
  183. text = time_to_string(end-start);
  184. font_size = 1.0 * (time2pixels(end)-time2pixels(start));
  185. if (font_size > 3)
  186. font_size = 3;
  187. font_size = round_text_size(font_size);
  188. fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\">\n", time2pixels(start), Yslot * SLOT_MULT);
  189. fprintf(svgfile, "<title>#%d waiting %s</title>\n", cpu, time_to_string(end - start));
  190. if (backtrace)
  191. fprintf(svgfile, "<desc>Waiting on:\n%s</desc>\n", backtrace);
  192. fprintf(svgfile, "<rect x=\"0\" width=\"%4.8f\" y=\"0\" height=\"%4.1f\" class=\"%s\"/>\n",
  193. time2pixels(end)-time2pixels(start), SLOT_HEIGHT, style);
  194. if (font_size > MIN_TEXT_SIZE)
  195. fprintf(svgfile, "<text transform=\"rotate(90)\" font-size=\"%1.8fpt\"> %s</text>\n",
  196. font_size, text);
  197. fprintf(svgfile, "</g>\n");
  198. }
  199. static char *cpu_model(void)
  200. {
  201. static char cpu_m[255];
  202. char buf[256];
  203. FILE *file;
  204. cpu_m[0] = 0;
  205. /* CPU type */
  206. file = fopen("/proc/cpuinfo", "r");
  207. if (file) {
  208. while (fgets(buf, 255, file)) {
  209. if (strstr(buf, "model name")) {
  210. strncpy(cpu_m, &buf[13], 255);
  211. break;
  212. }
  213. }
  214. fclose(file);
  215. }
  216. /* CPU type */
  217. file = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies", "r");
  218. if (file) {
  219. while (fgets(buf, 255, file)) {
  220. unsigned int freq;
  221. freq = strtoull(buf, NULL, 10);
  222. if (freq > max_freq)
  223. max_freq = freq;
  224. }
  225. fclose(file);
  226. }
  227. return cpu_m;
  228. }
  229. void svg_cpu_box(int cpu, u64 __max_freq, u64 __turbo_freq)
  230. {
  231. char cpu_string[80];
  232. if (!svgfile)
  233. return;
  234. max_freq = __max_freq;
  235. turbo_frequency = __turbo_freq;
  236. fprintf(svgfile, "<g>\n");
  237. fprintf(svgfile, "<rect x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\" class=\"cpu\"/>\n",
  238. time2pixels(first_time),
  239. time2pixels(last_time)-time2pixels(first_time),
  240. cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT);
  241. sprintf(cpu_string, "CPU %i", (int)cpu);
  242. fprintf(svgfile, "<text x=\"%4.8f\" y=\"%4.8f\">%s</text>\n",
  243. 10+time2pixels(first_time), cpu2y(cpu) + SLOT_HEIGHT/2, cpu_string);
  244. fprintf(svgfile, "<text transform=\"translate(%4.8f,%4.8f)\" font-size=\"1.25pt\">%s</text>\n",
  245. 10+time2pixels(first_time), cpu2y(cpu) + SLOT_MULT + SLOT_HEIGHT - 4, cpu_model());
  246. fprintf(svgfile, "</g>\n");
  247. }
  248. void svg_process(int cpu, u64 start, u64 end, int pid, const char *name, const char *backtrace)
  249. {
  250. double width;
  251. const char *type;
  252. if (!svgfile)
  253. return;
  254. if (svg_highlight && end - start >= svg_highlight)
  255. type = "sample_hi";
  256. else if (svg_highlight_name && strstr(name, svg_highlight_name))
  257. type = "sample_hi";
  258. else
  259. type = "sample";
  260. fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\">\n", time2pixels(start), cpu2y(cpu));
  261. fprintf(svgfile, "<title>%d %s running %s</title>\n", pid, name, time_to_string(end - start));
  262. if (backtrace)
  263. fprintf(svgfile, "<desc>Switched because:\n%s</desc>\n", backtrace);
  264. fprintf(svgfile, "<rect x=\"0\" width=\"%4.8f\" y=\"0\" height=\"%4.1f\" class=\"%s\"/>\n",
  265. time2pixels(end)-time2pixels(start), SLOT_MULT+SLOT_HEIGHT, type);
  266. width = time2pixels(end)-time2pixels(start);
  267. if (width > 6)
  268. width = 6;
  269. width = round_text_size(width);
  270. if (width > MIN_TEXT_SIZE)
  271. fprintf(svgfile, "<text transform=\"rotate(90)\" font-size=\"%3.8fpt\">%s</text>\n",
  272. width, name);
  273. fprintf(svgfile, "</g>\n");
  274. }
  275. void svg_cstate(int cpu, u64 start, u64 end, int type)
  276. {
  277. double width;
  278. char style[128];
  279. if (!svgfile)
  280. return;
  281. fprintf(svgfile, "<g>\n");
  282. if (type > 6)
  283. type = 6;
  284. sprintf(style, "c%i", type);
  285. fprintf(svgfile, "<rect class=\"%s\" x=\"%4.8f\" width=\"%4.8f\" y=\"%4.1f\" height=\"%4.1f\"/>\n",
  286. style,
  287. time2pixels(start), time2pixels(end)-time2pixels(start),
  288. cpu2y(cpu), SLOT_MULT+SLOT_HEIGHT);
  289. width = (time2pixels(end)-time2pixels(start))/2.0;
  290. if (width > 6)
  291. width = 6;
  292. width = round_text_size(width);
  293. if (width > MIN_TEXT_SIZE)
  294. fprintf(svgfile, "<text x=\"%4.8f\" y=\"%4.8f\" font-size=\"%3.8fpt\">C%i</text>\n",
  295. time2pixels(start), cpu2y(cpu)+width, width, type);
  296. fprintf(svgfile, "</g>\n");
  297. }
  298. static char *HzToHuman(unsigned long hz)
  299. {
  300. static char buffer[1024];
  301. unsigned long long Hz;
  302. memset(buffer, 0, 1024);
  303. Hz = hz;
  304. /* default: just put the Number in */
  305. sprintf(buffer, "%9lli", Hz);
  306. if (Hz > 1000)
  307. sprintf(buffer, " %6lli Mhz", (Hz+500)/1000);
  308. if (Hz > 1500000)
  309. sprintf(buffer, " %6.2f Ghz", (Hz+5000.0)/1000000);
  310. if (Hz == turbo_frequency)
  311. sprintf(buffer, "Turbo");
  312. return buffer;
  313. }
  314. void svg_pstate(int cpu, u64 start, u64 end, u64 freq)
  315. {
  316. double height = 0;
  317. if (!svgfile)
  318. return;
  319. fprintf(svgfile, "<g>\n");
  320. if (max_freq)
  321. height = freq * 1.0 / max_freq * (SLOT_HEIGHT + SLOT_MULT);
  322. height = 1 + cpu2y(cpu) + SLOT_MULT + SLOT_HEIGHT - height;
  323. fprintf(svgfile, "<line x1=\"%4.8f\" x2=\"%4.8f\" y1=\"%4.1f\" y2=\"%4.1f\" class=\"pstate\"/>\n",
  324. time2pixels(start), time2pixels(end), height, height);
  325. fprintf(svgfile, "<text x=\"%4.8f\" y=\"%4.8f\" font-size=\"0.25pt\">%s</text>\n",
  326. time2pixels(start), height+0.9, HzToHuman(freq));
  327. fprintf(svgfile, "</g>\n");
  328. }
  329. void svg_partial_wakeline(u64 start, int row1, char *desc1, int row2, char *desc2, const char *backtrace)
  330. {
  331. double height;
  332. if (!svgfile)
  333. return;
  334. fprintf(svgfile, "<g>\n");
  335. fprintf(svgfile, "<title>%s wakes up %s</title>\n",
  336. desc1 ? desc1 : "?",
  337. desc2 ? desc2 : "?");
  338. if (backtrace)
  339. fprintf(svgfile, "<desc>%s</desc>\n", backtrace);
  340. if (row1 < row2) {
  341. if (row1) {
  342. 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",
  343. time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/32);
  344. if (desc2)
  345. fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s &gt;</text></g>\n",
  346. time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT + SLOT_HEIGHT/48, desc2);
  347. }
  348. if (row2) {
  349. 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",
  350. time2pixels(start), row2 * SLOT_MULT - SLOT_MULT/32, time2pixels(start), row2 * SLOT_MULT);
  351. if (desc1)
  352. fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s &gt;</text></g>\n",
  353. time2pixels(start), row2 * SLOT_MULT - SLOT_MULT/32, desc1);
  354. }
  355. } else {
  356. if (row2) {
  357. 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",
  358. time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/32);
  359. if (desc1)
  360. fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s &lt;</text></g>\n",
  361. time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT + SLOT_MULT/48, desc1);
  362. }
  363. if (row1) {
  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_MULT/32, time2pixels(start), row1 * SLOT_MULT);
  366. if (desc2)
  367. fprintf(svgfile, "<g transform=\"translate(%4.8f,%4.8f)\"><text transform=\"rotate(90)\" font-size=\"0.02pt\">%s &lt;</text></g>\n",
  368. time2pixels(start), row1 * SLOT_MULT - SLOT_HEIGHT/32, desc2);
  369. }
  370. }
  371. height = row1 * SLOT_MULT;
  372. if (row2 > row1)
  373. height += SLOT_HEIGHT;
  374. if (row1)
  375. fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(32,255,32)\"/>\n",
  376. time2pixels(start), height);
  377. fprintf(svgfile, "</g>\n");
  378. }
  379. void svg_wakeline(u64 start, int row1, int row2, const char *backtrace)
  380. {
  381. double height;
  382. if (!svgfile)
  383. return;
  384. fprintf(svgfile, "<g>\n");
  385. if (backtrace)
  386. fprintf(svgfile, "<desc>%s</desc>\n", backtrace);
  387. if (row1 < row2)
  388. 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",
  389. time2pixels(start), row1 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row2 * SLOT_MULT);
  390. else
  391. 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",
  392. time2pixels(start), row2 * SLOT_MULT + SLOT_HEIGHT, time2pixels(start), row1 * SLOT_MULT);
  393. height = row1 * SLOT_MULT;
  394. if (row2 > row1)
  395. height += SLOT_HEIGHT;
  396. fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(32,255,32)\"/>\n",
  397. time2pixels(start), height);
  398. fprintf(svgfile, "</g>\n");
  399. }
  400. void svg_interrupt(u64 start, int row, const char *backtrace)
  401. {
  402. if (!svgfile)
  403. return;
  404. fprintf(svgfile, "<g>\n");
  405. fprintf(svgfile, "<title>Wakeup from interrupt</title>\n");
  406. if (backtrace)
  407. fprintf(svgfile, "<desc>%s</desc>\n", backtrace);
  408. fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(255,128,128)\"/>\n",
  409. time2pixels(start), row * SLOT_MULT);
  410. fprintf(svgfile, "<circle cx=\"%4.8f\" cy=\"%4.2f\" r = \"0.01\" style=\"fill:rgb(255,128,128)\"/>\n",
  411. time2pixels(start), row * SLOT_MULT + SLOT_HEIGHT);
  412. fprintf(svgfile, "</g>\n");
  413. }
  414. void svg_text(int Yslot, u64 start, const char *text)
  415. {
  416. if (!svgfile)
  417. return;
  418. fprintf(svgfile, "<text x=\"%4.8f\" y=\"%4.8f\">%s</text>\n",
  419. time2pixels(start), Yslot * SLOT_MULT+SLOT_HEIGHT/2, text);
  420. }
  421. static void svg_legenda_box(int X, const char *text, const char *style)
  422. {
  423. double boxsize;
  424. boxsize = SLOT_HEIGHT / 2;
  425. fprintf(svgfile, "<rect x=\"%i\" width=\"%4.8f\" y=\"0\" height=\"%4.1f\" class=\"%s\"/>\n",
  426. X, boxsize, boxsize, style);
  427. fprintf(svgfile, "<text transform=\"translate(%4.8f, %4.8f)\" font-size=\"%4.8fpt\">%s</text>\n",
  428. X + boxsize + 5, boxsize, 0.8 * boxsize, text);
  429. }
  430. void svg_legenda(void)
  431. {
  432. if (!svgfile)
  433. return;
  434. fprintf(svgfile, "<g>\n");
  435. svg_legenda_box(0, "Running", "sample");
  436. svg_legenda_box(100, "Idle","c1");
  437. svg_legenda_box(200, "Deeper Idle", "c3");
  438. svg_legenda_box(350, "Deepest Idle", "c6");
  439. svg_legenda_box(550, "Sleeping", "process2");
  440. svg_legenda_box(650, "Waiting for cpu", "waiting");
  441. svg_legenda_box(800, "Blocked on IO", "blocked");
  442. fprintf(svgfile, "</g>\n");
  443. }
  444. void svg_time_grid(void)
  445. {
  446. u64 i;
  447. if (!svgfile)
  448. return;
  449. i = first_time;
  450. while (i < last_time) {
  451. int color = 220;
  452. double thickness = 0.075;
  453. if ((i % 100000000) == 0) {
  454. thickness = 0.5;
  455. color = 192;
  456. }
  457. if ((i % 1000000000) == 0) {
  458. thickness = 2.0;
  459. color = 128;
  460. }
  461. 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",
  462. time2pixels(i), SLOT_MULT/2, time2pixels(i), total_height, color, color, color, thickness);
  463. i += 10000000;
  464. }
  465. }
  466. void svg_close(void)
  467. {
  468. if (svgfile) {
  469. fprintf(svgfile, "</svg>\n");
  470. fclose(svgfile);
  471. svgfile = NULL;
  472. }
  473. }
  474. #define cpumask_bits(maskp) ((maskp)->bits)
  475. typedef struct { DECLARE_BITMAP(bits, MAX_NR_CPUS); } cpumask_t;
  476. struct topology {
  477. cpumask_t *sib_core;
  478. int sib_core_nr;
  479. cpumask_t *sib_thr;
  480. int sib_thr_nr;
  481. };
  482. static void scan_thread_topology(int *map, struct topology *t, int cpu, int *pos)
  483. {
  484. int i;
  485. int thr;
  486. for (i = 0; i < t->sib_thr_nr; i++) {
  487. if (!test_bit(cpu, cpumask_bits(&t->sib_thr[i])))
  488. continue;
  489. for_each_set_bit(thr,
  490. cpumask_bits(&t->sib_thr[i]),
  491. MAX_NR_CPUS)
  492. if (map[thr] == -1)
  493. map[thr] = (*pos)++;
  494. }
  495. }
  496. static void scan_core_topology(int *map, struct topology *t)
  497. {
  498. int pos = 0;
  499. int i;
  500. int cpu;
  501. for (i = 0; i < t->sib_core_nr; i++)
  502. for_each_set_bit(cpu,
  503. cpumask_bits(&t->sib_core[i]),
  504. MAX_NR_CPUS)
  505. scan_thread_topology(map, t, cpu, &pos);
  506. }
  507. static int str_to_bitmap(char *s, cpumask_t *b)
  508. {
  509. int i;
  510. int ret = 0;
  511. struct cpu_map *m;
  512. int c;
  513. m = cpu_map__new(s);
  514. if (!m)
  515. return -1;
  516. for (i = 0; i < m->nr; i++) {
  517. c = m->map[i];
  518. if (c >= MAX_NR_CPUS) {
  519. ret = -1;
  520. break;
  521. }
  522. set_bit(c, cpumask_bits(b));
  523. }
  524. cpu_map__delete(m);
  525. return ret;
  526. }
  527. int svg_build_topology_map(char *sib_core, int sib_core_nr,
  528. char *sib_thr, int sib_thr_nr)
  529. {
  530. int i;
  531. struct topology t;
  532. t.sib_core_nr = sib_core_nr;
  533. t.sib_thr_nr = sib_thr_nr;
  534. t.sib_core = calloc(sib_core_nr, sizeof(cpumask_t));
  535. t.sib_thr = calloc(sib_thr_nr, sizeof(cpumask_t));
  536. if (!t.sib_core || !t.sib_thr) {
  537. fprintf(stderr, "topology: no memory\n");
  538. goto exit;
  539. }
  540. for (i = 0; i < sib_core_nr; i++) {
  541. if (str_to_bitmap(sib_core, &t.sib_core[i])) {
  542. fprintf(stderr, "topology: can't parse siblings map\n");
  543. goto exit;
  544. }
  545. sib_core += strlen(sib_core) + 1;
  546. }
  547. for (i = 0; i < sib_thr_nr; i++) {
  548. if (str_to_bitmap(sib_thr, &t.sib_thr[i])) {
  549. fprintf(stderr, "topology: can't parse siblings map\n");
  550. goto exit;
  551. }
  552. sib_thr += strlen(sib_thr) + 1;
  553. }
  554. topology_map = malloc(sizeof(int) * MAX_NR_CPUS);
  555. if (!topology_map) {
  556. fprintf(stderr, "topology: no memory\n");
  557. goto exit;
  558. }
  559. for (i = 0; i < MAX_NR_CPUS; i++)
  560. topology_map[i] = -1;
  561. scan_core_topology(topology_map, &t);
  562. return 0;
  563. exit:
  564. zfree(&t.sib_core);
  565. zfree(&t.sib_thr);
  566. return -1;
  567. }