svghelper.c 22 KB

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