Нема описа

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