svghelper.c 23 KB

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