Sin descripción

webhtml.go 29KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. // Copyright 2017 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package driver
  15. import "html/template"
  16. import "github.com/google/pprof/third_party/d3"
  17. import "github.com/google/pprof/third_party/d3flamegraph"
  18. // addTemplates adds a set of template definitions to templates.
  19. func addTemplates(templates *template.Template) {
  20. template.Must(templates.Parse(`{{define "d3script"}}` + d3.JSSource + `{{end}}`))
  21. template.Must(templates.Parse(`{{define "d3flamegraphscript"}}` + d3flamegraph.JSSource + `{{end}}`))
  22. template.Must(templates.Parse(`{{define "d3flamegraphcss"}}` + d3flamegraph.CSSSource + `{{end}}`))
  23. template.Must(templates.Parse(`
  24. {{define "css"}}
  25. <style type="text/css">
  26. * {
  27. margin: 0;
  28. padding: 0;
  29. box-sizing: border-box;
  30. }
  31. html, body {
  32. height: 100%;
  33. }
  34. body {
  35. font-family: 'Roboto', -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';
  36. font-size: 13px;
  37. line-height: 1.4;
  38. display: flex;
  39. flex-direction: column;
  40. }
  41. a {
  42. color: #2a66d9;
  43. }
  44. .header {
  45. display: flex;
  46. align-items: center;
  47. height: 44px;
  48. min-height: 44px;
  49. background-color: #eee;
  50. color: #212121;
  51. padding: 0 1rem;
  52. }
  53. .header > div {
  54. margin: 0 0.125em;
  55. }
  56. .header .title h1 {
  57. font-size: 1.75em;
  58. margin-right: 1rem;
  59. }
  60. .header .title a {
  61. color: #212121;
  62. text-decoration: none;
  63. }
  64. .header .title a:hover {
  65. text-decoration: underline;
  66. }
  67. .header .description {
  68. width: 100%;
  69. text-align: right;
  70. white-space: nowrap;
  71. }
  72. @media screen and (max-width: 799px) {
  73. .header input {
  74. display: none;
  75. }
  76. }
  77. #detailsbox {
  78. display: none;
  79. z-index: 1;
  80. position: fixed;
  81. top: 40px;
  82. right: 20px;
  83. background-color: #ffffff;
  84. box-shadow: 0 1px 5px rgba(0,0,0,.3);
  85. line-height: 24px;
  86. padding: 1em;
  87. text-align: left;
  88. }
  89. .header input {
  90. background: white url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' style='pointer-events:none;display:block;width:100%25;height:100%25;fill:#757575'%3E%3Cpath d='M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61.0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z'/%3E%3C/svg%3E") no-repeat 4px center/20px 20px;
  91. border: 1px solid #d1d2d3;
  92. border-radius: 2px 0 0 2px;
  93. padding: 0.25em;
  94. padding-left: 28px;
  95. margin-left: 1em;
  96. font-family: 'Roboto', 'Noto', sans-serif;
  97. font-size: 1em;
  98. line-height: 24px;
  99. color: #212121;
  100. }
  101. .downArrow {
  102. border-top: .36em solid #ccc;
  103. border-left: .36em solid transparent;
  104. border-right: .36em solid transparent;
  105. margin-bottom: .05em;
  106. margin-left: .5em;
  107. transition: border-top-color 200ms;
  108. }
  109. .menu-item {
  110. height: 100%;
  111. text-transform: uppercase;
  112. font-family: 'Roboto Medium', -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';
  113. position: relative;
  114. }
  115. .menu-item .menu-name:hover {
  116. opacity: 0.75;
  117. }
  118. .menu-item .menu-name:hover .downArrow {
  119. border-top-color: #666;
  120. }
  121. .menu-name {
  122. height: 100%;
  123. padding: 0 0.5em;
  124. display: flex;
  125. align-items: center;
  126. justify-content: center;
  127. }
  128. .submenu {
  129. display: none;
  130. z-index: 1;
  131. margin-top: -4px;
  132. min-width: 10em;
  133. position: absolute;
  134. left: 0px;
  135. background-color: white;
  136. box-shadow: 0 1px 5px rgba(0,0,0,.3);
  137. font-size: 100%;
  138. text-transform: none;
  139. }
  140. .menu-item, .submenu {
  141. user-select: none;
  142. -moz-user-select: none;
  143. -ms-user-select: none;
  144. -webkit-user-select: none;
  145. }
  146. .submenu hr {
  147. border: 0;
  148. border-top: 2px solid #eee;
  149. }
  150. .submenu a {
  151. display: block;
  152. padding: .5em 1em;
  153. text-decoration: none;
  154. }
  155. .submenu a:hover, .submenu a.active {
  156. color: white;
  157. background-color: #6b82d6;
  158. }
  159. .submenu a.disabled {
  160. color: gray;
  161. pointer-events: none;
  162. }
  163. #content {
  164. overflow-y: scroll;
  165. padding: 1em;
  166. }
  167. #top {
  168. overflow-y: scroll;
  169. }
  170. #graph {
  171. overflow: hidden;
  172. }
  173. #graph svg {
  174. width: 100%;
  175. height: auto;
  176. padding: 10px;
  177. }
  178. #content.source .filename {
  179. margin-top: 0;
  180. margin-bottom: 1em;
  181. font-size: 120%;
  182. }
  183. #content.source pre {
  184. margin-bottom: 3em;
  185. }
  186. table {
  187. border-spacing: 0px;
  188. width: 100%;
  189. padding-bottom: 1em;
  190. white-space: nowrap;
  191. }
  192. table thead {
  193. font-family: 'Roboto Medium', -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol';
  194. }
  195. table tr th {
  196. background-color: #ddd;
  197. text-align: right;
  198. padding: .3em .5em;
  199. }
  200. table tr td {
  201. padding: .3em .5em;
  202. text-align: right;
  203. }
  204. #top table tr th:nth-child(6),
  205. #top table tr th:nth-child(7),
  206. #top table tr td:nth-child(6),
  207. #top table tr td:nth-child(7) {
  208. text-align: left;
  209. }
  210. #top table tr td:nth-child(6) {
  211. width: 100%;
  212. text-overflow: ellipsis;
  213. overflow: hidden;
  214. white-space: nowrap;
  215. }
  216. #flathdr1, #flathdr2, #cumhdr1, #cumhdr2, #namehdr {
  217. cursor: ns-resize;
  218. }
  219. .hilite {
  220. background-color: #ebf5fb;
  221. font-weight: bold;
  222. }
  223. </style>
  224. {{end}}
  225. {{define "header"}}
  226. <div class="header">
  227. <div class="title">
  228. <h1><a href="./">pprof</a></h1>
  229. </div>
  230. <div id="view" class="menu-item">
  231. <div class="menu-name">
  232. View
  233. <i class="downArrow"></i>
  234. </div>
  235. <div class="submenu">
  236. <a title="{{.Help.top}}" href="./top" id="topbtn">Top</a>
  237. <a title="{{.Help.graph}}" href="./" id="graphbtn">Graph</a>
  238. <a title="{{.Help.flamegraph}}" href="./flamegraph" id="flamegraph">Flame Graph</a>
  239. <a title="{{.Help.peek}}" href="./peek" id="peek">Peek</a>
  240. <a title="{{.Help.list}}" href="./source" id="list">Source</a>
  241. <a title="{{.Help.disasm}}" href="./disasm" id="disasm">Disassemble</a>
  242. </div>
  243. </div>
  244. <div id="refine" class="menu-item">
  245. <div class="menu-name">
  246. Refine
  247. <i class="downArrow"></i>
  248. </div>
  249. <div class="submenu">
  250. <a title="{{.Help.focus}}" href="?" id="focus">Focus</a>
  251. <a title="{{.Help.ignore}}" href="?" id="ignore">Ignore</a>
  252. <a title="{{.Help.hide}}" href="?" id="hide">Hide</a>
  253. <a title="{{.Help.show}}" href="?" id="show">Show</a>
  254. <hr>
  255. <a title="{{.Help.reset}}" href="?">Reset</a>
  256. </div>
  257. </div>
  258. <div>
  259. <input id="search" type="text" placeholder="Search regexp" autocomplete="off" autocapitalize="none" size=40>
  260. </div>
  261. <div class="description">
  262. <a title="{{.Help.details}}" href="#" id="details">{{.Title}}</a>
  263. <div id="detailsbox">
  264. {{range .Legend}}<div>{{.}}</div>{{end}}
  265. </div>
  266. </div>
  267. </div>
  268. <div id="errors">{{range .Errors}}<div>{{.}}</div>{{end}}</div>
  269. {{end}}
  270. {{define "graph" -}}
  271. <!DOCTYPE html>
  272. <html>
  273. <head>
  274. <meta charset="utf-8">
  275. <title>{{.Title}}</title>
  276. {{template "css" .}}
  277. </head>
  278. <body>
  279. {{template "header" .}}
  280. <div id="graph">
  281. {{.HTMLBody}}
  282. </div>
  283. {{template "script" .}}
  284. <script>viewer(new URL(window.location.href), {{.Nodes}});</script>
  285. </body>
  286. </html>
  287. {{end}}
  288. {{define "script"}}
  289. <script>
  290. // Make svg pannable and zoomable.
  291. // Call clickHandler(t) if a click event is caught by the pan event handlers.
  292. function initPanAndZoom(svg, clickHandler) {
  293. 'use strict';
  294. // Current mouse/touch handling mode
  295. const IDLE = 0;
  296. const MOUSEPAN = 1;
  297. const TOUCHPAN = 2;
  298. const TOUCHZOOM = 3;
  299. let mode = IDLE;
  300. // State needed to implement zooming.
  301. let currentScale = 1.0;
  302. const initWidth = svg.viewBox.baseVal.width;
  303. const initHeight = svg.viewBox.baseVal.height;
  304. // State needed to implement panning.
  305. let panLastX = 0; // Last event X coordinate
  306. let panLastY = 0; // Last event Y coordinate
  307. let moved = false; // Have we seen significant movement
  308. let touchid = null; // Current touch identifier
  309. // State needed for pinch zooming
  310. let touchid2 = null; // Second id for pinch zooming
  311. let initGap = 1.0; // Starting gap between two touches
  312. let initScale = 1.0; // currentScale when pinch zoom started
  313. let centerPoint = null; // Center point for scaling
  314. // Convert event coordinates to svg coordinates.
  315. function toSvg(x, y) {
  316. const p = svg.createSVGPoint();
  317. p.x = x;
  318. p.y = y;
  319. let m = svg.getCTM();
  320. if (m == null) m = svg.getScreenCTM(); // Firefox workaround.
  321. return p.matrixTransform(m.inverse());
  322. }
  323. // Change the scaling for the svg to s, keeping the point denoted
  324. // by u (in svg coordinates]) fixed at the same screen location.
  325. function rescale(s, u) {
  326. // Limit to a good range.
  327. if (s < 0.2) s = 0.2;
  328. if (s > 10.0) s = 10.0;
  329. currentScale = s;
  330. // svg.viewBox defines the visible portion of the user coordinate
  331. // system. So to magnify by s, divide the visible portion by s,
  332. // which will then be stretched to fit the viewport.
  333. const vb = svg.viewBox;
  334. const w1 = vb.baseVal.width;
  335. const w2 = initWidth / s;
  336. const h1 = vb.baseVal.height;
  337. const h2 = initHeight / s;
  338. vb.baseVal.width = w2;
  339. vb.baseVal.height = h2;
  340. // We also want to adjust vb.baseVal.x so that u.x remains at same
  341. // screen X coordinate. In other words, want to change it from x1 to x2
  342. // so that:
  343. // (u.x - x1) / w1 = (u.x - x2) / w2
  344. // Simplifying that, we get
  345. // (u.x - x1) * (w2 / w1) = u.x - x2
  346. // x2 = u.x - (u.x - x1) * (w2 / w1)
  347. vb.baseVal.x = u.x - (u.x - vb.baseVal.x) * (w2 / w1);
  348. vb.baseVal.y = u.y - (u.y - vb.baseVal.y) * (h2 / h1);
  349. }
  350. function handleWheel(e) {
  351. if (e.deltaY == 0) return;
  352. // Change scale factor by 1.1 or 1/1.1
  353. rescale(currentScale * (e.deltaY < 0 ? 1.1 : (1/1.1)),
  354. toSvg(e.offsetX, e.offsetY));
  355. }
  356. function setMode(m) {
  357. mode = m;
  358. touchid = null;
  359. touchid2 = null;
  360. }
  361. function panStart(x, y) {
  362. moved = false;
  363. panLastX = x;
  364. panLastY = y;
  365. }
  366. function panMove(x, y) {
  367. let dx = x - panLastX;
  368. let dy = y - panLastY;
  369. if (Math.abs(dx) <= 2 && Math.abs(dy) <= 2) return; // Ignore tiny moves
  370. moved = true;
  371. panLastX = x;
  372. panLastY = y;
  373. // Firefox workaround: get dimensions from parentNode.
  374. const swidth = svg.clientWidth || svg.parentNode.clientWidth;
  375. const sheight = svg.clientHeight || svg.parentNode.clientHeight;
  376. // Convert deltas from screen space to svg space.
  377. dx *= (svg.viewBox.baseVal.width / swidth);
  378. dy *= (svg.viewBox.baseVal.height / sheight);
  379. svg.viewBox.baseVal.x -= dx;
  380. svg.viewBox.baseVal.y -= dy;
  381. }
  382. function handleScanStart(e) {
  383. if (e.button != 0) return; // Do not catch right-clicks etc.
  384. setMode(MOUSEPAN);
  385. panStart(e.clientX, e.clientY);
  386. e.preventDefault();
  387. svg.addEventListener('mousemove', handleScanMove);
  388. }
  389. function handleScanMove(e) {
  390. if (e.buttons == 0) {
  391. // Missed an end event, perhaps because mouse moved outside window.
  392. setMode(IDLE);
  393. svg.removeEventListener('mousemove', handleScanMove);
  394. return;
  395. }
  396. if (mode == MOUSEPAN) panMove(e.clientX, e.clientY);
  397. }
  398. function handleScanEnd(e) {
  399. if (mode == MOUSEPAN) panMove(e.clientX, e.clientY);
  400. setMode(IDLE);
  401. svg.removeEventListener('mousemove', handleScanMove);
  402. if (!moved) clickHandler(e.target);
  403. }
  404. // Find touch object with specified identifier.
  405. function findTouch(tlist, id) {
  406. for (const t of tlist) {
  407. if (t.identifier == id) return t;
  408. }
  409. return null;
  410. }
  411. // Return distance between two touch points
  412. function touchGap(t1, t2) {
  413. const dx = t1.clientX - t2.clientX;
  414. const dy = t1.clientY - t2.clientY;
  415. return Math.hypot(dx, dy);
  416. }
  417. function handleTouchStart(e) {
  418. if (mode == IDLE && e.changedTouches.length == 1) {
  419. // Start touch based panning
  420. const t = e.changedTouches[0];
  421. setMode(TOUCHPAN);
  422. touchid = t.identifier;
  423. panStart(t.clientX, t.clientY);
  424. e.preventDefault();
  425. } else if (mode == TOUCHPAN && e.touches.length == 2) {
  426. // Start pinch zooming
  427. setMode(TOUCHZOOM);
  428. const t1 = e.touches[0];
  429. const t2 = e.touches[1];
  430. touchid = t1.identifier;
  431. touchid2 = t2.identifier;
  432. initScale = currentScale;
  433. initGap = touchGap(t1, t2);
  434. centerPoint = toSvg((t1.clientX + t2.clientX) / 2,
  435. (t1.clientY + t2.clientY) / 2);
  436. e.preventDefault();
  437. }
  438. }
  439. function handleTouchMove(e) {
  440. if (mode == TOUCHPAN) {
  441. const t = findTouch(e.changedTouches, touchid);
  442. if (t == null) return;
  443. if (e.touches.length != 1) {
  444. setMode(IDLE);
  445. return;
  446. }
  447. panMove(t.clientX, t.clientY);
  448. e.preventDefault();
  449. } else if (mode == TOUCHZOOM) {
  450. // Get two touches; new gap; rescale to ratio.
  451. const t1 = findTouch(e.touches, touchid);
  452. const t2 = findTouch(e.touches, touchid2);
  453. if (t1 == null || t2 == null) return;
  454. const gap = touchGap(t1, t2);
  455. rescale(initScale * gap / initGap, centerPoint);
  456. e.preventDefault();
  457. }
  458. }
  459. function handleTouchEnd(e) {
  460. if (mode == TOUCHPAN) {
  461. const t = findTouch(e.changedTouches, touchid);
  462. if (t == null) return;
  463. panMove(t.clientX, t.clientY);
  464. setMode(IDLE);
  465. e.preventDefault();
  466. if (!moved) clickHandler(t.target);
  467. } else if (mode == TOUCHZOOM) {
  468. setMode(IDLE);
  469. e.preventDefault();
  470. }
  471. }
  472. svg.addEventListener('mousedown', handleScanStart);
  473. svg.addEventListener('mouseup', handleScanEnd);
  474. svg.addEventListener('touchstart', handleTouchStart);
  475. svg.addEventListener('touchmove', handleTouchMove);
  476. svg.addEventListener('touchend', handleTouchEnd);
  477. svg.addEventListener('wheel', handleWheel, true);
  478. }
  479. function initMenus() {
  480. 'use strict';
  481. let activeMenu = null;
  482. let activeMenuHdr = null;
  483. function cancelActiveMenu() {
  484. if (activeMenu == null) return;
  485. activeMenu.style.display = 'none';
  486. activeMenu = null;
  487. activeMenuHdr = null;
  488. }
  489. // Set click handlers on every menu header.
  490. for (const menu of document.getElementsByClassName('submenu')) {
  491. const hdr = menu.parentElement;
  492. if (hdr == null) return;
  493. if (hdr.classList.contains('disabled')) return;
  494. function showMenu(e) {
  495. // menu is a child of hdr, so this event can fire for clicks
  496. // inside menu. Ignore such clicks.
  497. if (e.target.parentElement != hdr) return;
  498. activeMenu = menu;
  499. activeMenuHdr = hdr;
  500. menu.style.display = 'block';
  501. }
  502. hdr.addEventListener('mousedown', showMenu);
  503. hdr.addEventListener('touchstart', showMenu);
  504. }
  505. // If there is an active menu and a down event outside, retract the menu.
  506. for (const t of ['mousedown', 'touchstart']) {
  507. document.addEventListener(t, (e) => {
  508. // Note: to avoid unnecessary flicker, if the down event is inside
  509. // the active menu header, do not retract the menu.
  510. if (activeMenuHdr != e.target.closest('.menu-item')) {
  511. cancelActiveMenu();
  512. }
  513. }, { passive: true, capture: true });
  514. }
  515. // If there is an active menu and an up event inside, retract the menu.
  516. document.addEventListener('mouseup', (e) => {
  517. if (activeMenu == e.target.closest('.submenu')) {
  518. cancelActiveMenu();
  519. }
  520. }, { passive: true, capture: true });
  521. }
  522. function viewer(baseUrl, nodes) {
  523. 'use strict';
  524. // Elements
  525. const search = document.getElementById('search');
  526. const graph0 = document.getElementById('graph0');
  527. const svg = (graph0 == null ? null : graph0.parentElement);
  528. const toptable = document.getElementById('toptable');
  529. let regexpActive = false;
  530. let selected = new Map();
  531. let origFill = new Map();
  532. let searchAlarm = null;
  533. let buttonsEnabled = true;
  534. function handleDetails(e) {
  535. e.preventDefault();
  536. const detailsText = document.getElementById('detailsbox');
  537. if (detailsText != null) {
  538. if (detailsText.style.display === 'block') {
  539. detailsText.style.display = 'none';
  540. } else {
  541. detailsText.style.display = 'block';
  542. }
  543. }
  544. }
  545. function handleKey(e) {
  546. if (e.keyCode != 13) return;
  547. window.location.href =
  548. updateUrl(new URL(window.location.href), 'f');
  549. e.preventDefault();
  550. }
  551. function handleSearch() {
  552. // Delay expensive processing so a flurry of key strokes is handled once.
  553. if (searchAlarm != null) {
  554. clearTimeout(searchAlarm);
  555. }
  556. searchAlarm = setTimeout(selectMatching, 300);
  557. regexpActive = true;
  558. updateButtons();
  559. }
  560. function selectMatching() {
  561. searchAlarm = null;
  562. let re = null;
  563. if (search.value != '') {
  564. try {
  565. re = new RegExp(search.value);
  566. } catch (e) {
  567. // TODO: Display error state in search box
  568. return;
  569. }
  570. }
  571. function match(text) {
  572. return re != null && re.test(text);
  573. }
  574. // drop currently selected items that do not match re.
  575. selected.forEach(function(v, n) {
  576. if (!match(nodes[n])) {
  577. unselect(n, document.getElementById('node' + n));
  578. }
  579. })
  580. // add matching items that are not currently selected.
  581. for (let n = 0; n < nodes.length; n++) {
  582. if (!selected.has(n) && match(nodes[n])) {
  583. select(n, document.getElementById('node' + n));
  584. }
  585. }
  586. updateButtons();
  587. }
  588. function toggleSvgSelect(elem) {
  589. // Walk up to immediate child of graph0
  590. while (elem != null && elem.parentElement != graph0) {
  591. elem = elem.parentElement;
  592. }
  593. if (!elem) return;
  594. // Disable regexp mode.
  595. regexpActive = false;
  596. const n = nodeId(elem);
  597. if (n < 0) return;
  598. if (selected.has(n)) {
  599. unselect(n, elem);
  600. } else {
  601. select(n, elem);
  602. }
  603. updateButtons();
  604. }
  605. function unselect(n, elem) {
  606. if (elem == null) return;
  607. selected.delete(n);
  608. setBackground(elem, false);
  609. }
  610. function select(n, elem) {
  611. if (elem == null) return;
  612. selected.set(n, true);
  613. setBackground(elem, true);
  614. }
  615. function nodeId(elem) {
  616. const id = elem.id;
  617. if (!id) return -1;
  618. if (!id.startsWith('node')) return -1;
  619. const n = parseInt(id.slice(4), 10);
  620. if (isNaN(n)) return -1;
  621. if (n < 0 || n >= nodes.length) return -1;
  622. return n;
  623. }
  624. function setBackground(elem, set) {
  625. // Handle table row highlighting.
  626. if (elem.nodeName == 'TR') {
  627. elem.classList.toggle('hilite', set);
  628. return;
  629. }
  630. // Handle svg element highlighting.
  631. const p = findPolygon(elem);
  632. if (p != null) {
  633. if (set) {
  634. origFill.set(p, p.style.fill);
  635. p.style.fill = '#ccccff';
  636. } else if (origFill.has(p)) {
  637. p.style.fill = origFill.get(p);
  638. }
  639. }
  640. }
  641. function findPolygon(elem) {
  642. if (elem.localName == 'polygon') return elem;
  643. for (const c of elem.children) {
  644. const p = findPolygon(c);
  645. if (p != null) return p;
  646. }
  647. return null;
  648. }
  649. // convert a string to a regexp that matches that string.
  650. function quotemeta(str) {
  651. return str.replace(/([\\\.?+*\[\](){}|^$])/g, '\\$1');
  652. }
  653. // Update id's href to reflect current selection whenever it is
  654. // liable to be followed.
  655. function makeLinkDynamic(id) {
  656. const elem = document.getElementById(id);
  657. if (elem == null) return;
  658. // Most links copy current selection into the 'f' parameter,
  659. // but Refine menu links are different.
  660. let param = 'f';
  661. if (id == 'ignore') param = 'i';
  662. if (id == 'hide') param = 'h';
  663. if (id == 'show') param = 's';
  664. // We update on mouseenter so middle-click/right-click work properly.
  665. elem.addEventListener('mouseenter', updater);
  666. elem.addEventListener('touchstart', updater);
  667. function updater() {
  668. elem.href = updateUrl(new URL(elem.href), param);
  669. }
  670. }
  671. // Update URL to reflect current selection.
  672. function updateUrl(url, param) {
  673. url.hash = '';
  674. // The selection can be in one of two modes: regexp-based or
  675. // list-based. Construct regular expression depending on mode.
  676. let re = regexpActive
  677. ? search.value
  678. : Array.from(selected.keys()).map(key => quotemeta(nodes[key])).join('|');
  679. // Copy params from this page's URL.
  680. const params = url.searchParams;
  681. for (const p of new URLSearchParams(window.location.search)) {
  682. params.set(p[0], p[1]);
  683. }
  684. if (re != '') {
  685. // For focus/show, forget old parameter. For others, add to re.
  686. if (param != 'f' && param != 's' && params.has(param)) {
  687. const old = params.get(param);
  688. if (old != '') {
  689. re += '|' + old;
  690. }
  691. }
  692. params.set(param, re);
  693. } else {
  694. params.delete(param);
  695. }
  696. return url.toString();
  697. }
  698. function handleTopClick(e) {
  699. // Walk back until we find TR and then get the Name column (index 5)
  700. let elem = e.target;
  701. while (elem != null && elem.nodeName != 'TR') {
  702. elem = elem.parentElement;
  703. }
  704. if (elem == null || elem.children.length < 6) return;
  705. e.preventDefault();
  706. const tr = elem;
  707. const td = elem.children[5];
  708. if (td.nodeName != 'TD') return;
  709. const name = td.innerText;
  710. const index = nodes.indexOf(name);
  711. if (index < 0) return;
  712. // Disable regexp mode.
  713. regexpActive = false;
  714. if (selected.has(index)) {
  715. unselect(index, elem);
  716. } else {
  717. select(index, elem);
  718. }
  719. updateButtons();
  720. }
  721. function updateButtons() {
  722. const enable = (search.value != '' || selected.size != 0);
  723. if (buttonsEnabled == enable) return;
  724. buttonsEnabled = enable;
  725. for (const id of ['focus', 'ignore', 'hide', 'show']) {
  726. const link = document.getElementById(id);
  727. if (link != null) {
  728. link.classList.toggle('disabled', !enable);
  729. }
  730. }
  731. }
  732. // Initialize button states
  733. updateButtons();
  734. // Setup event handlers
  735. initMenus();
  736. if (svg != null) {
  737. initPanAndZoom(svg, toggleSvgSelect);
  738. }
  739. if (toptable != null) {
  740. toptable.addEventListener('mousedown', handleTopClick);
  741. toptable.addEventListener('touchstart', handleTopClick);
  742. }
  743. const ids = ['topbtn', 'graphbtn', 'peek', 'list', 'disasm',
  744. 'focus', 'ignore', 'hide', 'show'];
  745. ids.forEach(makeLinkDynamic);
  746. // Bind action to button with specified id.
  747. function addAction(id, action) {
  748. const btn = document.getElementById(id);
  749. if (btn != null) {
  750. btn.addEventListener('click', action);
  751. btn.addEventListener('touchstart', action);
  752. }
  753. }
  754. addAction('details', handleDetails);
  755. search.addEventListener('input', handleSearch);
  756. search.addEventListener('keydown', handleKey);
  757. // Give initial focus to main container so it can be scrolled using keys.
  758. const main = document.getElementById('bodycontainer');
  759. if (main) {
  760. main.focus();
  761. }
  762. }
  763. </script>
  764. {{end}}
  765. {{define "top" -}}
  766. <!DOCTYPE html>
  767. <html>
  768. <head>
  769. <meta charset="utf-8">
  770. <title>{{.Title}}</title>
  771. {{template "css" .}}
  772. <style type="text/css">
  773. </style>
  774. </head>
  775. <body>
  776. {{template "header" .}}
  777. <div id="top">
  778. <table id="toptable">
  779. <thead>
  780. <tr>
  781. <th id="flathdr1">Flat</th>
  782. <th id="flathdr2">Flat%</th>
  783. <th>Sum%</th>
  784. <th id="cumhdr1">Cum</th>
  785. <th id="cumhdr2">Cum%</th>
  786. <th id="namehdr">Name</th>
  787. <th>Inlined?</th>
  788. </tr>
  789. </thead>
  790. <tbody id="rows"></tbody>
  791. </table>
  792. </div>
  793. {{template "script" .}}
  794. <script>
  795. function makeTopTable(total, entries) {
  796. const rows = document.getElementById('rows');
  797. if (rows == null) return;
  798. // Store initial index in each entry so we have stable node ids for selection.
  799. for (let i = 0; i < entries.length; i++) {
  800. entries[i].Id = 'node' + i;
  801. }
  802. // Which column are we currently sorted by and in what order?
  803. let currentColumn = '';
  804. let descending = false;
  805. sortBy('Flat');
  806. function sortBy(column) {
  807. // Update sort criteria
  808. if (column == currentColumn) {
  809. descending = !descending; // Reverse order
  810. } else {
  811. currentColumn = column;
  812. descending = (column != 'Name');
  813. }
  814. // Sort according to current criteria.
  815. function cmp(a, b) {
  816. const av = a[currentColumn];
  817. const bv = b[currentColumn];
  818. if (av < bv) return -1;
  819. if (av > bv) return +1;
  820. return 0;
  821. }
  822. entries.sort(cmp);
  823. if (descending) entries.reverse();
  824. function addCell(tr, val) {
  825. const td = document.createElement('td');
  826. td.textContent = val;
  827. tr.appendChild(td);
  828. }
  829. function percent(v) {
  830. return (v * 100.0 / total).toFixed(2) + '%';
  831. }
  832. // Generate rows
  833. const fragment = document.createDocumentFragment();
  834. let sum = 0;
  835. for (const row of entries) {
  836. const tr = document.createElement('tr');
  837. tr.id = row.Id;
  838. sum += row.Flat;
  839. addCell(tr, row.FlatFormat);
  840. addCell(tr, percent(row.Flat));
  841. addCell(tr, percent(sum));
  842. addCell(tr, row.CumFormat);
  843. addCell(tr, percent(row.Cum));
  844. addCell(tr, row.Name);
  845. addCell(tr, row.InlineLabel);
  846. fragment.appendChild(tr);
  847. }
  848. rows.textContent = ''; // Remove old rows
  849. rows.appendChild(fragment);
  850. }
  851. // Make different column headers trigger sorting.
  852. function bindSort(id, column) {
  853. const hdr = document.getElementById(id);
  854. if (hdr == null) return;
  855. const fn = function() { sortBy(column) };
  856. hdr.addEventListener('click', fn);
  857. hdr.addEventListener('touch', fn);
  858. }
  859. bindSort('flathdr1', 'Flat');
  860. bindSort('flathdr2', 'Flat');
  861. bindSort('cumhdr1', 'Cum');
  862. bindSort('cumhdr2', 'Cum');
  863. bindSort('namehdr', 'Name');
  864. }
  865. viewer(new URL(window.location.href), {{.Nodes}});
  866. makeTopTable({{.Total}}, {{.Top}});
  867. </script>
  868. </body>
  869. </html>
  870. {{end}}
  871. {{define "sourcelisting" -}}
  872. <!DOCTYPE html>
  873. <html>
  874. <head>
  875. <meta charset="utf-8">
  876. <title>{{.Title}}</title>
  877. {{template "css" .}}
  878. {{template "weblistcss" .}}
  879. {{template "weblistjs" .}}
  880. </head>
  881. <body>
  882. {{template "header" .}}
  883. <div id="content" class="source">
  884. {{.HTMLBody}}
  885. </div>
  886. {{template "script" .}}
  887. <script>viewer(new URL(window.location.href), null);</script>
  888. </body>
  889. </html>
  890. {{end}}
  891. {{define "plaintext" -}}
  892. <!DOCTYPE html>
  893. <html>
  894. <head>
  895. <meta charset="utf-8">
  896. <title>{{.Title}}</title>
  897. {{template "css" .}}
  898. </head>
  899. <body>
  900. {{template "header" .}}
  901. <div id="content">
  902. <pre>
  903. {{.TextBody}}
  904. </pre>
  905. </div>
  906. {{template "script" .}}
  907. <script>viewer(new URL(window.location.href), null);</script>
  908. </body>
  909. </html>
  910. {{end}}
  911. {{define "flamegraph" -}}
  912. <!DOCTYPE html>
  913. <html>
  914. <head>
  915. <meta charset="utf-8">
  916. <title>{{.Title}}</title>
  917. {{template "css" .}}
  918. <style type="text/css">{{template "d3flamegraphcss" .}}</style>
  919. <style type="text/css">
  920. .flamegraph-content {
  921. width: 90%;
  922. min-width: 80%;
  923. margin-left: 5%;
  924. }
  925. .flamegraph-details {
  926. height: 1.2em;
  927. width: 90%;
  928. min-width: 90%;
  929. margin-left: 5%;
  930. padding: 15px 0 35px;
  931. }
  932. </style>
  933. </head>
  934. <body>
  935. {{template "header" .}}
  936. <div id="bodycontainer">
  937. <div id="flamegraphdetails" class="flamegraph-details"></div>
  938. <div class="flamegraph-content">
  939. <div id="chart"></div>
  940. </div>
  941. </div>
  942. {{template "script" .}}
  943. <script>viewer(new URL(window.location.href), {{.Nodes}});</script>
  944. <script>{{template "d3script" .}}</script>
  945. <script>{{template "d3flamegraphscript" .}}</script>
  946. <script>
  947. var data = {{.FlameGraph}};
  948. var width = document.getElementById('chart').clientWidth;
  949. var flameGraph = d3.flamegraph()
  950. .width(width)
  951. .cellHeight(18)
  952. .minFrameSize(1)
  953. .transitionDuration(750)
  954. .transitionEase(d3.easeCubic)
  955. .inverted(true)
  956. .title('')
  957. .tooltip(false)
  958. .details(document.getElementById('flamegraphdetails'));
  959. // <full name> (percentage, value)
  960. flameGraph.label((d) => d.data.f + ' (' + d.data.p + ', ' + d.data.l + ')');
  961. (function(flameGraph) {
  962. var oldColorMapper = flameGraph.color();
  963. function colorMapper(d) {
  964. // Hack to force default color mapper to use 'warm' color scheme by not passing libtype
  965. const { data, highlight } = d;
  966. return oldColorMapper({ data: { n: data.n }, highlight });
  967. }
  968. flameGraph.color(colorMapper);
  969. }(flameGraph));
  970. d3.select('#chart')
  971. .datum(data)
  972. .call(flameGraph);
  973. function clear() {
  974. flameGraph.clear();
  975. }
  976. function resetZoom() {
  977. flameGraph.resetZoom();
  978. }
  979. window.addEventListener('resize', function() {
  980. var width = document.getElementById('chart').clientWidth;
  981. var graphs = document.getElementsByClassName('d3-flame-graph');
  982. if (graphs.length > 0) {
  983. graphs[0].setAttribute('width', width);
  984. }
  985. flameGraph.width(width);
  986. flameGraph.resetZoom();
  987. }, true);
  988. var search = document.getElementById('search');
  989. var searchAlarm = null;
  990. function selectMatching() {
  991. searchAlarm = null;
  992. if (search.value != '') {
  993. flameGraph.search(search.value);
  994. } else {
  995. flameGraph.clear();
  996. }
  997. }
  998. function handleSearch() {
  999. // Delay expensive processing so a flurry of key strokes is handled once.
  1000. if (searchAlarm != null) {
  1001. clearTimeout(searchAlarm);
  1002. }
  1003. searchAlarm = setTimeout(selectMatching, 300);
  1004. }
  1005. search.addEventListener('input', handleSearch);
  1006. </script>
  1007. </body>
  1008. </html>
  1009. {{end}}
  1010. `))
  1011. }