설명 없음

webhtml.go 30KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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 (
  16. "html/template"
  17. "github.com/google/pprof/third_party/d3"
  18. "github.com/google/pprof/third_party/d3flamegraph"
  19. )
  20. // addTemplates adds a set of template definitions to templates.
  21. func addTemplates(templates *template.Template) {
  22. template.Must(templates.Parse(`{{define "d3script"}}` + d3.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:%23757575'%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. {{$sampleLen := len .SampleTypes}}
  247. {{if gt $sampleLen 1}}
  248. <div id="sample" class="menu-item">
  249. <div class="menu-name">
  250. Sample
  251. <i class="downArrow"></i>
  252. </div>
  253. <div class="submenu">
  254. {{range .SampleTypes}}
  255. <a href="?si={{.}}" id="{{.}}">{{.}}</a>
  256. {{end}}
  257. </div>
  258. </div>
  259. {{end}}
  260. <div id="refine" class="menu-item">
  261. <div class="menu-name">
  262. Refine
  263. <i class="downArrow"></i>
  264. </div>
  265. <div class="submenu">
  266. <a title="{{.Help.focus}}" href="?" id="focus">Focus</a>
  267. <a title="{{.Help.ignore}}" href="?" id="ignore">Ignore</a>
  268. <a title="{{.Help.hide}}" href="?" id="hide">Hide</a>
  269. <a title="{{.Help.show}}" href="?" id="show">Show</a>
  270. <a title="{{.Help.show_from}}" href="?" id="show-from">Show from</a>
  271. <hr>
  272. <a title="{{.Help.reset}}" href="?">Reset</a>
  273. </div>
  274. </div>
  275. <div>
  276. <input id="search" type="text" placeholder="Search regexp" autocomplete="off" autocapitalize="none" size=40>
  277. </div>
  278. <div class="description">
  279. <a title="{{.Help.details}}" href="#" id="details">{{.Title}}</a>
  280. <div id="detailsbox">
  281. {{range .Legend}}<div>{{.}}</div>{{end}}
  282. </div>
  283. </div>
  284. </div>
  285. <div id="errors">{{range .Errors}}<div>{{.}}</div>{{end}}</div>
  286. {{end}}
  287. {{define "graph" -}}
  288. <!DOCTYPE html>
  289. <html>
  290. <head>
  291. <meta charset="utf-8">
  292. <title>{{.Title}}</title>
  293. {{template "css" .}}
  294. </head>
  295. <body>
  296. {{template "header" .}}
  297. <div id="graph">
  298. {{.HTMLBody}}
  299. </div>
  300. {{template "script" .}}
  301. <script>viewer(new URL(window.location.href), {{.Nodes}});</script>
  302. </body>
  303. </html>
  304. {{end}}
  305. {{define "script"}}
  306. <script>
  307. // Make svg pannable and zoomable.
  308. // Call clickHandler(t) if a click event is caught by the pan event handlers.
  309. function initPanAndZoom(svg, clickHandler) {
  310. 'use strict';
  311. // Current mouse/touch handling mode
  312. const IDLE = 0;
  313. const MOUSEPAN = 1;
  314. const TOUCHPAN = 2;
  315. const TOUCHZOOM = 3;
  316. let mode = IDLE;
  317. // State needed to implement zooming.
  318. let currentScale = 1.0;
  319. const initWidth = svg.viewBox.baseVal.width;
  320. const initHeight = svg.viewBox.baseVal.height;
  321. // State needed to implement panning.
  322. let panLastX = 0; // Last event X coordinate
  323. let panLastY = 0; // Last event Y coordinate
  324. let moved = false; // Have we seen significant movement
  325. let touchid = null; // Current touch identifier
  326. // State needed for pinch zooming
  327. let touchid2 = null; // Second id for pinch zooming
  328. let initGap = 1.0; // Starting gap between two touches
  329. let initScale = 1.0; // currentScale when pinch zoom started
  330. let centerPoint = null; // Center point for scaling
  331. // Convert event coordinates to svg coordinates.
  332. function toSvg(x, y) {
  333. const p = svg.createSVGPoint();
  334. p.x = x;
  335. p.y = y;
  336. let m = svg.getCTM();
  337. if (m == null) m = svg.getScreenCTM(); // Firefox workaround.
  338. return p.matrixTransform(m.inverse());
  339. }
  340. // Change the scaling for the svg to s, keeping the point denoted
  341. // by u (in svg coordinates]) fixed at the same screen location.
  342. function rescale(s, u) {
  343. // Limit to a good range.
  344. if (s < 0.2) s = 0.2;
  345. if (s > 10.0) s = 10.0;
  346. currentScale = s;
  347. // svg.viewBox defines the visible portion of the user coordinate
  348. // system. So to magnify by s, divide the visible portion by s,
  349. // which will then be stretched to fit the viewport.
  350. const vb = svg.viewBox;
  351. const w1 = vb.baseVal.width;
  352. const w2 = initWidth / s;
  353. const h1 = vb.baseVal.height;
  354. const h2 = initHeight / s;
  355. vb.baseVal.width = w2;
  356. vb.baseVal.height = h2;
  357. // We also want to adjust vb.baseVal.x so that u.x remains at same
  358. // screen X coordinate. In other words, want to change it from x1 to x2
  359. // so that:
  360. // (u.x - x1) / w1 = (u.x - x2) / w2
  361. // Simplifying that, we get
  362. // (u.x - x1) * (w2 / w1) = u.x - x2
  363. // x2 = u.x - (u.x - x1) * (w2 / w1)
  364. vb.baseVal.x = u.x - (u.x - vb.baseVal.x) * (w2 / w1);
  365. vb.baseVal.y = u.y - (u.y - vb.baseVal.y) * (h2 / h1);
  366. }
  367. function handleWheel(e) {
  368. if (e.deltaY == 0) return;
  369. // Change scale factor by 1.1 or 1/1.1
  370. rescale(currentScale * (e.deltaY < 0 ? 1.1 : (1/1.1)),
  371. toSvg(e.offsetX, e.offsetY));
  372. }
  373. function setMode(m) {
  374. mode = m;
  375. touchid = null;
  376. touchid2 = null;
  377. }
  378. function panStart(x, y) {
  379. moved = false;
  380. panLastX = x;
  381. panLastY = y;
  382. }
  383. function panMove(x, y) {
  384. let dx = x - panLastX;
  385. let dy = y - panLastY;
  386. if (Math.abs(dx) <= 2 && Math.abs(dy) <= 2) return; // Ignore tiny moves
  387. moved = true;
  388. panLastX = x;
  389. panLastY = y;
  390. // Firefox workaround: get dimensions from parentNode.
  391. const swidth = svg.clientWidth || svg.parentNode.clientWidth;
  392. const sheight = svg.clientHeight || svg.parentNode.clientHeight;
  393. // Convert deltas from screen space to svg space.
  394. dx *= (svg.viewBox.baseVal.width / swidth);
  395. dy *= (svg.viewBox.baseVal.height / sheight);
  396. svg.viewBox.baseVal.x -= dx;
  397. svg.viewBox.baseVal.y -= dy;
  398. }
  399. function handleScanStart(e) {
  400. if (e.button != 0) return; // Do not catch right-clicks etc.
  401. setMode(MOUSEPAN);
  402. panStart(e.clientX, e.clientY);
  403. e.preventDefault();
  404. svg.addEventListener('mousemove', handleScanMove);
  405. }
  406. function handleScanMove(e) {
  407. if (e.buttons == 0) {
  408. // Missed an end event, perhaps because mouse moved outside window.
  409. setMode(IDLE);
  410. svg.removeEventListener('mousemove', handleScanMove);
  411. return;
  412. }
  413. if (mode == MOUSEPAN) panMove(e.clientX, e.clientY);
  414. }
  415. function handleScanEnd(e) {
  416. if (mode == MOUSEPAN) panMove(e.clientX, e.clientY);
  417. setMode(IDLE);
  418. svg.removeEventListener('mousemove', handleScanMove);
  419. if (!moved) clickHandler(e.target);
  420. }
  421. // Find touch object with specified identifier.
  422. function findTouch(tlist, id) {
  423. for (const t of tlist) {
  424. if (t.identifier == id) return t;
  425. }
  426. return null;
  427. }
  428. // Return distance between two touch points
  429. function touchGap(t1, t2) {
  430. const dx = t1.clientX - t2.clientX;
  431. const dy = t1.clientY - t2.clientY;
  432. return Math.hypot(dx, dy);
  433. }
  434. function handleTouchStart(e) {
  435. if (mode == IDLE && e.changedTouches.length == 1) {
  436. // Start touch based panning
  437. const t = e.changedTouches[0];
  438. setMode(TOUCHPAN);
  439. touchid = t.identifier;
  440. panStart(t.clientX, t.clientY);
  441. e.preventDefault();
  442. } else if (mode == TOUCHPAN && e.touches.length == 2) {
  443. // Start pinch zooming
  444. setMode(TOUCHZOOM);
  445. const t1 = e.touches[0];
  446. const t2 = e.touches[1];
  447. touchid = t1.identifier;
  448. touchid2 = t2.identifier;
  449. initScale = currentScale;
  450. initGap = touchGap(t1, t2);
  451. centerPoint = toSvg((t1.clientX + t2.clientX) / 2,
  452. (t1.clientY + t2.clientY) / 2);
  453. e.preventDefault();
  454. }
  455. }
  456. function handleTouchMove(e) {
  457. if (mode == TOUCHPAN) {
  458. const t = findTouch(e.changedTouches, touchid);
  459. if (t == null) return;
  460. if (e.touches.length != 1) {
  461. setMode(IDLE);
  462. return;
  463. }
  464. panMove(t.clientX, t.clientY);
  465. e.preventDefault();
  466. } else if (mode == TOUCHZOOM) {
  467. // Get two touches; new gap; rescale to ratio.
  468. const t1 = findTouch(e.touches, touchid);
  469. const t2 = findTouch(e.touches, touchid2);
  470. if (t1 == null || t2 == null) return;
  471. const gap = touchGap(t1, t2);
  472. rescale(initScale * gap / initGap, centerPoint);
  473. e.preventDefault();
  474. }
  475. }
  476. function handleTouchEnd(e) {
  477. if (mode == TOUCHPAN) {
  478. const t = findTouch(e.changedTouches, touchid);
  479. if (t == null) return;
  480. panMove(t.clientX, t.clientY);
  481. setMode(IDLE);
  482. e.preventDefault();
  483. if (!moved) clickHandler(t.target);
  484. } else if (mode == TOUCHZOOM) {
  485. setMode(IDLE);
  486. e.preventDefault();
  487. }
  488. }
  489. svg.addEventListener('mousedown', handleScanStart);
  490. svg.addEventListener('mouseup', handleScanEnd);
  491. svg.addEventListener('touchstart', handleTouchStart);
  492. svg.addEventListener('touchmove', handleTouchMove);
  493. svg.addEventListener('touchend', handleTouchEnd);
  494. svg.addEventListener('wheel', handleWheel, true);
  495. }
  496. function initMenus() {
  497. 'use strict';
  498. let activeMenu = null;
  499. let activeMenuHdr = null;
  500. function cancelActiveMenu() {
  501. if (activeMenu == null) return;
  502. activeMenu.style.display = 'none';
  503. activeMenu = null;
  504. activeMenuHdr = null;
  505. }
  506. // Set click handlers on every menu header.
  507. for (const menu of document.getElementsByClassName('submenu')) {
  508. const hdr = menu.parentElement;
  509. if (hdr == null) return;
  510. if (hdr.classList.contains('disabled')) return;
  511. function showMenu(e) {
  512. // menu is a child of hdr, so this event can fire for clicks
  513. // inside menu. Ignore such clicks.
  514. if (e.target.parentElement != hdr) return;
  515. activeMenu = menu;
  516. activeMenuHdr = hdr;
  517. menu.style.display = 'block';
  518. }
  519. hdr.addEventListener('mousedown', showMenu);
  520. hdr.addEventListener('touchstart', showMenu);
  521. }
  522. // If there is an active menu and a down event outside, retract the menu.
  523. for (const t of ['mousedown', 'touchstart']) {
  524. document.addEventListener(t, (e) => {
  525. // Note: to avoid unnecessary flicker, if the down event is inside
  526. // the active menu header, do not retract the menu.
  527. if (activeMenuHdr != e.target.closest('.menu-item')) {
  528. cancelActiveMenu();
  529. }
  530. }, { passive: true, capture: true });
  531. }
  532. // If there is an active menu and an up event inside, retract the menu.
  533. document.addEventListener('mouseup', (e) => {
  534. if (activeMenu == e.target.closest('.submenu')) {
  535. cancelActiveMenu();
  536. }
  537. }, { passive: true, capture: true });
  538. }
  539. function viewer(baseUrl, nodes) {
  540. 'use strict';
  541. // Elements
  542. const search = document.getElementById('search');
  543. const graph0 = document.getElementById('graph0');
  544. const svg = (graph0 == null ? null : graph0.parentElement);
  545. const toptable = document.getElementById('toptable');
  546. let regexpActive = false;
  547. let selected = new Map();
  548. let origFill = new Map();
  549. let searchAlarm = null;
  550. let buttonsEnabled = true;
  551. function handleDetails(e) {
  552. e.preventDefault();
  553. const detailsText = document.getElementById('detailsbox');
  554. if (detailsText != null) {
  555. if (detailsText.style.display === 'block') {
  556. detailsText.style.display = 'none';
  557. } else {
  558. detailsText.style.display = 'block';
  559. }
  560. }
  561. }
  562. function handleKey(e) {
  563. if (e.keyCode != 13) return;
  564. setHrefParams(window.location, function (params) {
  565. params.set('f', search.value);
  566. });
  567. e.preventDefault();
  568. }
  569. function handleSearch() {
  570. // Delay expensive processing so a flurry of key strokes is handled once.
  571. if (searchAlarm != null) {
  572. clearTimeout(searchAlarm);
  573. }
  574. searchAlarm = setTimeout(selectMatching, 300);
  575. regexpActive = true;
  576. updateButtons();
  577. }
  578. function selectMatching() {
  579. searchAlarm = null;
  580. let re = null;
  581. if (search.value != '') {
  582. try {
  583. re = new RegExp(search.value);
  584. } catch (e) {
  585. // TODO: Display error state in search box
  586. return;
  587. }
  588. }
  589. function match(text) {
  590. return re != null && re.test(text);
  591. }
  592. // drop currently selected items that do not match re.
  593. selected.forEach(function(v, n) {
  594. if (!match(nodes[n])) {
  595. unselect(n, document.getElementById('node' + n));
  596. }
  597. })
  598. // add matching items that are not currently selected.
  599. if (nodes) {
  600. for (let n = 0; n < nodes.length; n++) {
  601. if (!selected.has(n) && match(nodes[n])) {
  602. select(n, document.getElementById('node' + n));
  603. }
  604. }
  605. }
  606. updateButtons();
  607. }
  608. function toggleSvgSelect(elem) {
  609. // Walk up to immediate child of graph0
  610. while (elem != null && elem.parentElement != graph0) {
  611. elem = elem.parentElement;
  612. }
  613. if (!elem) return;
  614. // Disable regexp mode.
  615. regexpActive = false;
  616. const n = nodeId(elem);
  617. if (n < 0) return;
  618. if (selected.has(n)) {
  619. unselect(n, elem);
  620. } else {
  621. select(n, elem);
  622. }
  623. updateButtons();
  624. }
  625. function unselect(n, elem) {
  626. if (elem == null) return;
  627. selected.delete(n);
  628. setBackground(elem, false);
  629. }
  630. function select(n, elem) {
  631. if (elem == null) return;
  632. selected.set(n, true);
  633. setBackground(elem, true);
  634. }
  635. function nodeId(elem) {
  636. const id = elem.id;
  637. if (!id) return -1;
  638. if (!id.startsWith('node')) return -1;
  639. const n = parseInt(id.slice(4), 10);
  640. if (isNaN(n)) return -1;
  641. if (n < 0 || n >= nodes.length) return -1;
  642. return n;
  643. }
  644. function setBackground(elem, set) {
  645. // Handle table row highlighting.
  646. if (elem.nodeName == 'TR') {
  647. elem.classList.toggle('hilite', set);
  648. return;
  649. }
  650. // Handle svg element highlighting.
  651. const p = findPolygon(elem);
  652. if (p != null) {
  653. if (set) {
  654. origFill.set(p, p.style.fill);
  655. p.style.fill = '#ccccff';
  656. } else if (origFill.has(p)) {
  657. p.style.fill = origFill.get(p);
  658. }
  659. }
  660. }
  661. function findPolygon(elem) {
  662. if (elem.localName == 'polygon') return elem;
  663. for (const c of elem.children) {
  664. const p = findPolygon(c);
  665. if (p != null) return p;
  666. }
  667. return null;
  668. }
  669. // convert a string to a regexp that matches that string.
  670. function quotemeta(str) {
  671. return str.replace(/([\\\.?+*\[\](){}|^$])/g, '\\$1');
  672. }
  673. function setSampleIndexLink(id) {
  674. const elem = document.getElementById(id);
  675. if (elem != null) {
  676. setHrefParams(elem, function (params) {
  677. params.set("si", id);
  678. });
  679. }
  680. }
  681. // Update id's href to reflect current selection whenever it is
  682. // liable to be followed.
  683. function makeSearchLinkDynamic(id) {
  684. const elem = document.getElementById(id);
  685. if (elem == null) return;
  686. // Most links copy current selection into the 'f' parameter,
  687. // but Refine menu links are different.
  688. let param = 'f';
  689. if (id == 'ignore') param = 'i';
  690. if (id == 'hide') param = 'h';
  691. if (id == 'show') param = 's';
  692. if (id == 'show-from') param = 'sf';
  693. // We update on mouseenter so middle-click/right-click work properly.
  694. elem.addEventListener('mouseenter', updater);
  695. elem.addEventListener('touchstart', updater);
  696. function updater() {
  697. // The selection can be in one of two modes: regexp-based or
  698. // list-based. Construct regular expression depending on mode.
  699. let re = regexpActive
  700. ? search.value
  701. : Array.from(selected.keys()).map(key => quotemeta(nodes[key])).join('|');
  702. setHrefParams(elem, function (params) {
  703. if (re != '') {
  704. // For focus/show/show-from, forget old parameter. For others, add to re.
  705. if (param != 'f' && param != 's' && param != 'sf' && params.has(param)) {
  706. const old = params.get(param);
  707. if (old != '') {
  708. re += '|' + old;
  709. }
  710. }
  711. params.set(param, re);
  712. } else {
  713. params.delete(param);
  714. }
  715. });
  716. }
  717. }
  718. function setHrefParams(elem, paramSetter) {
  719. let url = new URL(elem.href);
  720. url.hash = '';
  721. // Copy params from this page's URL.
  722. const params = url.searchParams;
  723. for (const p of new URLSearchParams(window.location.search)) {
  724. params.set(p[0], p[1]);
  725. }
  726. // Give the params to the setter to modify.
  727. paramSetter(params);
  728. elem.href = url.toString();
  729. }
  730. function handleTopClick(e) {
  731. // Walk back until we find TR and then get the Name column (index 5)
  732. let elem = e.target;
  733. while (elem != null && elem.nodeName != 'TR') {
  734. elem = elem.parentElement;
  735. }
  736. if (elem == null || elem.children.length < 6) return;
  737. e.preventDefault();
  738. const tr = elem;
  739. const td = elem.children[5];
  740. if (td.nodeName != 'TD') return;
  741. const name = td.innerText;
  742. const index = nodes.indexOf(name);
  743. if (index < 0) return;
  744. // Disable regexp mode.
  745. regexpActive = false;
  746. if (selected.has(index)) {
  747. unselect(index, elem);
  748. } else {
  749. select(index, elem);
  750. }
  751. updateButtons();
  752. }
  753. function updateButtons() {
  754. const enable = (search.value != '' || selected.size != 0);
  755. if (buttonsEnabled == enable) return;
  756. buttonsEnabled = enable;
  757. for (const id of ['focus', 'ignore', 'hide', 'show', 'show-from']) {
  758. const link = document.getElementById(id);
  759. if (link != null) {
  760. link.classList.toggle('disabled', !enable);
  761. }
  762. }
  763. }
  764. // Initialize button states
  765. updateButtons();
  766. // Setup event handlers
  767. initMenus();
  768. if (svg != null) {
  769. initPanAndZoom(svg, toggleSvgSelect);
  770. }
  771. if (toptable != null) {
  772. toptable.addEventListener('mousedown', handleTopClick);
  773. toptable.addEventListener('touchstart', handleTopClick);
  774. }
  775. const ids = ['topbtn', 'graphbtn', 'flamegraph', 'peek', 'list', 'disasm',
  776. 'focus', 'ignore', 'hide', 'show', 'show-from'];
  777. ids.forEach(makeSearchLinkDynamic);
  778. const sampleIDs = [{{range .SampleTypes}}'{{.}}', {{end}}];
  779. sampleIDs.forEach(setSampleIndexLink);
  780. // Bind action to button with specified id.
  781. function addAction(id, action) {
  782. const btn = document.getElementById(id);
  783. if (btn != null) {
  784. btn.addEventListener('click', action);
  785. btn.addEventListener('touchstart', action);
  786. }
  787. }
  788. addAction('details', handleDetails);
  789. search.addEventListener('input', handleSearch);
  790. search.addEventListener('keydown', handleKey);
  791. // Give initial focus to main container so it can be scrolled using keys.
  792. const main = document.getElementById('bodycontainer');
  793. if (main) {
  794. main.focus();
  795. }
  796. }
  797. </script>
  798. {{end}}
  799. {{define "top" -}}
  800. <!DOCTYPE html>
  801. <html>
  802. <head>
  803. <meta charset="utf-8">
  804. <title>{{.Title}}</title>
  805. {{template "css" .}}
  806. <style type="text/css">
  807. </style>
  808. </head>
  809. <body>
  810. {{template "header" .}}
  811. <div id="top">
  812. <table id="toptable">
  813. <thead>
  814. <tr>
  815. <th id="flathdr1">Flat</th>
  816. <th id="flathdr2">Flat%</th>
  817. <th>Sum%</th>
  818. <th id="cumhdr1">Cum</th>
  819. <th id="cumhdr2">Cum%</th>
  820. <th id="namehdr">Name</th>
  821. <th>Inlined?</th>
  822. </tr>
  823. </thead>
  824. <tbody id="rows"></tbody>
  825. </table>
  826. </div>
  827. {{template "script" .}}
  828. <script>
  829. function makeTopTable(total, entries) {
  830. const rows = document.getElementById('rows');
  831. if (rows == null) return;
  832. // Store initial index in each entry so we have stable node ids for selection.
  833. for (let i = 0; i < entries.length; i++) {
  834. entries[i].Id = 'node' + i;
  835. }
  836. // Which column are we currently sorted by and in what order?
  837. let currentColumn = '';
  838. let descending = false;
  839. sortBy('Flat');
  840. function sortBy(column) {
  841. // Update sort criteria
  842. if (column == currentColumn) {
  843. descending = !descending; // Reverse order
  844. } else {
  845. currentColumn = column;
  846. descending = (column != 'Name');
  847. }
  848. // Sort according to current criteria.
  849. function cmp(a, b) {
  850. const av = a[currentColumn];
  851. const bv = b[currentColumn];
  852. if (av < bv) return -1;
  853. if (av > bv) return +1;
  854. return 0;
  855. }
  856. entries.sort(cmp);
  857. if (descending) entries.reverse();
  858. function addCell(tr, val) {
  859. const td = document.createElement('td');
  860. td.textContent = val;
  861. tr.appendChild(td);
  862. }
  863. function percent(v) {
  864. return (v * 100.0 / total).toFixed(2) + '%';
  865. }
  866. // Generate rows
  867. const fragment = document.createDocumentFragment();
  868. let sum = 0;
  869. for (const row of entries) {
  870. const tr = document.createElement('tr');
  871. tr.id = row.Id;
  872. sum += row.Flat;
  873. addCell(tr, row.FlatFormat);
  874. addCell(tr, percent(row.Flat));
  875. addCell(tr, percent(sum));
  876. addCell(tr, row.CumFormat);
  877. addCell(tr, percent(row.Cum));
  878. addCell(tr, row.Name);
  879. addCell(tr, row.InlineLabel);
  880. fragment.appendChild(tr);
  881. }
  882. rows.textContent = ''; // Remove old rows
  883. rows.appendChild(fragment);
  884. }
  885. // Make different column headers trigger sorting.
  886. function bindSort(id, column) {
  887. const hdr = document.getElementById(id);
  888. if (hdr == null) return;
  889. const fn = function() { sortBy(column) };
  890. hdr.addEventListener('click', fn);
  891. hdr.addEventListener('touch', fn);
  892. }
  893. bindSort('flathdr1', 'Flat');
  894. bindSort('flathdr2', 'Flat');
  895. bindSort('cumhdr1', 'Cum');
  896. bindSort('cumhdr2', 'Cum');
  897. bindSort('namehdr', 'Name');
  898. }
  899. viewer(new URL(window.location.href), {{.Nodes}});
  900. makeTopTable({{.Total}}, {{.Top}});
  901. </script>
  902. </body>
  903. </html>
  904. {{end}}
  905. {{define "sourcelisting" -}}
  906. <!DOCTYPE html>
  907. <html>
  908. <head>
  909. <meta charset="utf-8">
  910. <title>{{.Title}}</title>
  911. {{template "css" .}}
  912. {{template "weblistcss" .}}
  913. {{template "weblistjs" .}}
  914. </head>
  915. <body>
  916. {{template "header" .}}
  917. <div id="content" class="source">
  918. {{.HTMLBody}}
  919. </div>
  920. {{template "script" .}}
  921. <script>viewer(new URL(window.location.href), null);</script>
  922. </body>
  923. </html>
  924. {{end}}
  925. {{define "plaintext" -}}
  926. <!DOCTYPE html>
  927. <html>
  928. <head>
  929. <meta charset="utf-8">
  930. <title>{{.Title}}</title>
  931. {{template "css" .}}
  932. </head>
  933. <body>
  934. {{template "header" .}}
  935. <div id="content">
  936. <pre>
  937. {{.TextBody}}
  938. </pre>
  939. </div>
  940. {{template "script" .}}
  941. <script>viewer(new URL(window.location.href), null);</script>
  942. </body>
  943. </html>
  944. {{end}}
  945. {{define "flamegraph" -}}
  946. <!DOCTYPE html>
  947. <html>
  948. <head>
  949. <meta charset="utf-8">
  950. <title>{{.Title}}</title>
  951. {{template "css" .}}
  952. <style type="text/css">{{template "d3flamegraphcss" .}}</style>
  953. <style type="text/css">
  954. .flamegraph-content {
  955. width: 90%;
  956. min-width: 80%;
  957. margin-left: 5%;
  958. }
  959. .flamegraph-details {
  960. height: 1.2em;
  961. width: 90%;
  962. min-width: 90%;
  963. margin-left: 5%;
  964. padding: 15px 0 35px;
  965. }
  966. </style>
  967. </head>
  968. <body>
  969. {{template "header" .}}
  970. <div id="bodycontainer">
  971. <div id="flamegraphdetails" class="flamegraph-details"></div>
  972. <div class="flamegraph-content">
  973. <div id="chart"></div>
  974. </div>
  975. </div>
  976. {{template "script" .}}
  977. <script>viewer(new URL(window.location.href), {{.Nodes}});</script>
  978. <script>{{template "d3script" .}}</script>
  979. <script>{{template "d3flamegraphscript" .}}</script>
  980. <script>
  981. var data = {{.FlameGraph}};
  982. var width = document.getElementById('chart').clientWidth;
  983. var flameGraph = d3.flamegraph()
  984. .width(width)
  985. .cellHeight(18)
  986. .minFrameSize(1)
  987. .transitionDuration(750)
  988. .transitionEase(d3.easeCubic)
  989. .inverted(true)
  990. .sort(true)
  991. .title('')
  992. .tooltip(false)
  993. .details(document.getElementById('flamegraphdetails'));
  994. // <full name> (percentage, value)
  995. flameGraph.label((d) => d.data.f + ' (' + d.data.p + ', ' + d.data.l + ')');
  996. (function(flameGraph) {
  997. var oldColorMapper = flameGraph.color();
  998. function colorMapper(d) {
  999. // Hack to force default color mapper to use 'warm' color scheme by not passing libtype
  1000. const { data, highlight } = d;
  1001. return oldColorMapper({ data: { n: data.n }, highlight });
  1002. }
  1003. flameGraph.color(colorMapper);
  1004. }(flameGraph));
  1005. d3.select('#chart')
  1006. .datum(data)
  1007. .call(flameGraph);
  1008. function clear() {
  1009. flameGraph.clear();
  1010. }
  1011. function resetZoom() {
  1012. flameGraph.resetZoom();
  1013. }
  1014. window.addEventListener('resize', function() {
  1015. var width = document.getElementById('chart').clientWidth;
  1016. var graphs = document.getElementsByClassName('d3-flame-graph');
  1017. if (graphs.length > 0) {
  1018. graphs[0].setAttribute('width', width);
  1019. }
  1020. flameGraph.width(width);
  1021. flameGraph.resetZoom();
  1022. }, true);
  1023. var search = document.getElementById('search');
  1024. var searchAlarm = null;
  1025. function selectMatching() {
  1026. searchAlarm = null;
  1027. if (search.value != '') {
  1028. flameGraph.search(search.value);
  1029. } else {
  1030. flameGraph.clear();
  1031. }
  1032. }
  1033. function handleSearch() {
  1034. // Delay expensive processing so a flurry of key strokes is handled once.
  1035. if (searchAlarm != null) {
  1036. clearTimeout(searchAlarm);
  1037. }
  1038. searchAlarm = setTimeout(selectMatching, 300);
  1039. }
  1040. search.addEventListener('input', handleSearch);
  1041. </script>
  1042. </body>
  1043. </html>
  1044. {{end}}
  1045. `))
  1046. }