Нет описания

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