svghelper.c 23 KB

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