profile_chart.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. var canvas;
  2. var ctx;
  3. var tooltip;
  4. var min_scale;
  5. var scale;
  6. var offset_x = 0;
  7. var offset_y = 0;
  8. var size_y;
  9. var dragging = false;
  10. var previous_x = 0;
  11. var previous_y = 0;
  12. var bar_height = 15;
  13. var line_height = bar_height + 2;
  14. var thread_separation = 6;
  15. var thread_font_size = 12;
  16. var thread_font = thread_font_size + "px Arial";
  17. var bar_font_size = 10;
  18. var bar_font = bar_font_size + "px Arial";
  19. var end_cycle = 0;
  20. function drawChart()
  21. {
  22. ctx.clearRect(0, 0, canvas.width, canvas.height);
  23. ctx.lineWidth = 1;
  24. var y = offset_y;
  25. for (var t = 0; t < threads.length; t++)
  26. {
  27. // Check if thread has samples
  28. var thread = threads[t];
  29. if (thread.start.length == 0)
  30. continue;
  31. // Draw thread name
  32. y += thread_font_size;
  33. ctx.font = thread_font;
  34. ctx.fillStyle = "#000000";
  35. ctx.fillText(thread.thread_name, 0, y);
  36. y += thread_separation;
  37. // Draw outlines for each bar of samples
  38. ctx.fillStyle = "#c0c0c0";
  39. for (var d = 0; d <= thread.max_depth; d++)
  40. ctx.fillRect(0, y + d * line_height, canvas.width, bar_height);
  41. // Draw samples
  42. ctx.font = bar_font;
  43. for (var s = 0; s < thread.start.length; s++)
  44. {
  45. // Cull bar
  46. var rx = scale * (offset_x + thread.start[s]);
  47. if (rx > canvas.width) // right of canvas
  48. break;
  49. var rw = scale * thread.cycles[s];
  50. if (rw < 0.5) // less than half pixel, skip
  51. continue;
  52. if (rx + rw < 0) // left of canvas
  53. continue;
  54. // Draw bar
  55. var ry = y + line_height * thread.depth[s];
  56. ctx.fillStyle = thread.color[s];
  57. ctx.fillRect(rx, ry, rw, bar_height);
  58. ctx.strokeStyle = thread.darkened_color[s];
  59. ctx.strokeRect(rx, ry, rw, bar_height);
  60. // Get index in aggregated list
  61. var a = thread.aggregator[s];
  62. // Draw text
  63. if (rw > aggregated.name_width[a])
  64. {
  65. ctx.fillStyle = "#000000";
  66. ctx.fillText(aggregated.name[a], rx + (rw - aggregated.name_width[a]) / 2, ry + bar_height - 4);
  67. }
  68. }
  69. // Next line
  70. y += line_height * (1 + thread.max_depth) + thread_separation;
  71. }
  72. // Update size
  73. size_y = y - offset_y;
  74. }
  75. function drawTooltip(mouse_x, mouse_y)
  76. {
  77. var y = offset_y;
  78. for (var t = 0; t < threads.length; t++)
  79. {
  80. // Check if thread has samples
  81. var thread = threads[t];
  82. if (thread.start.length == 0)
  83. continue;
  84. // Thead name
  85. y += thread_font_size + thread_separation;
  86. // Draw samples
  87. for (var s = 0; s < thread.start.length; s++)
  88. {
  89. // Cull bar
  90. var rx = scale * (offset_x + thread.start[s]);
  91. if (rx > mouse_x)
  92. break;
  93. var rw = scale * thread.cycles[s];
  94. if (rx + rw < mouse_x)
  95. continue;
  96. var ry = y + line_height * thread.depth[s];
  97. if (mouse_y >= ry && mouse_y < ry + bar_height)
  98. {
  99. // Get index into aggregated list
  100. var a = thread.aggregator[s];
  101. // Found bar, fill in tooltip
  102. tooltip.style.left = (canvas.offsetLeft + mouse_x) + "px";
  103. tooltip.style.top = (canvas.offsetTop + mouse_y) + "px";
  104. tooltip.style.visibility = "visible";
  105. tooltip.innerHTML = aggregated.name[a] + "<br>"
  106. + "<table>"
  107. + "<tr><td>Time:</td><td class=\"stat\">" + (1000000 * thread.cycles[s] / cycles_per_second).toFixed(2) + " &micro;s</td></tr>"
  108. + "<tr><td>Start:</td><td class=\"stat\">" + (1000000 * thread.start[s] / cycles_per_second).toFixed(2) + " &micro;s</td></tr>"
  109. + "<tr><td>End:</td><td class=\"stat\">" + (1000000 * (thread.start[s] + thread.cycles[s]) / cycles_per_second).toFixed(2) + " &micro;s</td></tr>"
  110. + "<tr><td>Avg. Time:</td><td class=\"stat\">" + (1000000 * aggregated.cycles_per_frame[a] / cycles_per_second / aggregated.calls[a]).toFixed(2) + " &micro;s</td></tr>"
  111. + "<tr><td>Min Time:</td><td class=\"stat\">" + (1000000 * aggregated.min_cycles[a] / cycles_per_second).toFixed(2) + " &micro;s</td></tr>"
  112. + "<tr><td>Max Time:</td><td class=\"stat\">" + (1000000 * aggregated.max_cycles[a] / cycles_per_second).toFixed(2) + " &micro;s</td></tr>"
  113. + "<tr><td>Time / Frame:</td><td class=\"stat\">" + (1000000 * aggregated.cycles_per_frame[a] / cycles_per_second).toFixed(2) + " &micro;s</td></tr>"
  114. + "<tr><td>Calls:</td><td class=\"stat\">" + aggregated.calls[a] + "</td></tr>"
  115. + "</table>";
  116. return;
  117. }
  118. }
  119. // Next line
  120. y += line_height * (1 + thread.max_depth) + thread_separation;
  121. }
  122. // No bar found, hide tooltip
  123. tooltip.style.visibility = "hidden";
  124. }
  125. function onMouseDown(evt)
  126. {
  127. dragging = true;
  128. previous_x = evt.clientX, previous_y = evt.clientY;
  129. tooltip.style.visibility = "hidden";
  130. }
  131. function onMouseUp(evt)
  132. {
  133. dragging = false;
  134. }
  135. function clampMotion()
  136. {
  137. // Clamp horizontally
  138. var min_offset_x = canvas.width / scale - end_cycle;
  139. if (offset_x < min_offset_x)
  140. offset_x = min_offset_x;
  141. if (offset_x > 0)
  142. offset_x = 0;
  143. // Clamp vertically
  144. var min_offset_y = canvas.height - size_y;
  145. if (offset_y < min_offset_y)
  146. offset_y = min_offset_y;
  147. if (offset_y > 0)
  148. offset_y = 0;
  149. // Clamp scale
  150. if (scale < min_scale)
  151. scale = min_scale;
  152. var max_scale = 1000 * min_scale;
  153. if (scale > max_scale)
  154. scale = max_scale;
  155. }
  156. function onMouseMove(evt)
  157. {
  158. if (dragging)
  159. {
  160. // Calculate new offset
  161. offset_x += (evt.clientX - previous_x) / scale;
  162. offset_y += evt.clientY - previous_y;
  163. clampMotion();
  164. drawChart();
  165. }
  166. else
  167. drawTooltip(evt.clientX - canvas.offsetLeft, evt.clientY - canvas.offsetTop);
  168. previous_x = evt.clientX, previous_y = evt.clientY;
  169. }
  170. function onScroll(evt)
  171. {
  172. tooltip.style.visibility = "hidden";
  173. var old_scale = scale;
  174. if (evt.deltaY > 0)
  175. scale /= 1.1;
  176. else
  177. scale *= 1.1;
  178. clampMotion();
  179. // Ensure that event under mouse stays under mouse
  180. var x = previous_x - canvas.offsetLeft;
  181. offset_x += x / scale - x / old_scale;
  182. clampMotion();
  183. drawChart();
  184. }
  185. function darkenColor(color)
  186. {
  187. var i = parseInt(color.slice(1), 16);
  188. var r = i >> 16;
  189. var g = (i >> 8) & 0xff;
  190. var b = i & 0xff;
  191. r = Math.round(0.8 * r);
  192. g = Math.round(0.8 * g);
  193. b = Math.round(0.8 * b);
  194. i = (r << 16) + (g << 8) + b;
  195. return "#" + i.toString(16);
  196. }
  197. function startChart()
  198. {
  199. // Fetch elements
  200. canvas = document.getElementById('canvas');
  201. ctx = canvas.getContext("2d");
  202. tooltip = document.getElementById('tooltip');
  203. // Resize canvas to fill screen
  204. canvas.width = document.body.offsetWidth - 20;
  205. canvas.height = document.body.offsetHeight - 20;
  206. // Register mouse handlers
  207. canvas.onmousedown = onMouseDown;
  208. canvas.onmouseup = onMouseUp;
  209. canvas.onmouseout = onMouseUp;
  210. canvas.onmousemove = onMouseMove;
  211. canvas.onwheel = onScroll;
  212. for (var t = 0; t < threads.length; t++)
  213. {
  214. var thread = threads[t];
  215. // Calculate darkened colors
  216. thread.darkened_color = new Array(thread.color.length);
  217. for (var s = 0; s < thread.color.length; s++)
  218. thread.darkened_color[s] = darkenColor(thread.color[s]);
  219. // Calculate max depth and end cycle
  220. thread.max_depth = 0;
  221. for (var s = 0; s < thread.start.length; s++)
  222. {
  223. thread.max_depth = Math.max(thread.max_depth, thread.depth[s]);
  224. end_cycle = Math.max(end_cycle, thread.start[s] + thread.cycles[s]);
  225. }
  226. }
  227. // Calculate width of name strings
  228. ctx.font = bar_font;
  229. aggregated.name_width = new Array(aggregated.name.length);
  230. for (var a = 0; a < aggregated.name.length; a++)
  231. aggregated.name_width[a] = ctx.measureText(aggregated.name[a]).width;
  232. // Store scale properties
  233. min_scale = canvas.width / end_cycle;
  234. scale = min_scale;
  235. drawChart();
  236. }