Нема описа

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