Без опису

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