| 1 |
// stackViewer displays a flame-graph like view (extended to show callers). |
| 2 |
// stacks - report.StackSet |
| 3 |
// nodes - List of names for each source in report.StackSet |
| 4 |
function stackViewer(stacks, nodes) { |
| 5 |
'use strict'; |
| 6 |
|
| 7 |
// Constants used in rendering. |
| 8 |
const ROW = 20; |
| 9 |
const PADDING = 2; |
| 10 |
const MIN_WIDTH = 4; |
| 11 |
const MIN_TEXT_WIDTH = 16; |
| 12 |
const TEXT_MARGIN = 2; |
| 13 |
const FONT_SIZE = 12; |
| 14 |
const MIN_FONT_SIZE = 8; |
| 15 |
|
| 16 |
// Fields |
| 17 |
let pivots = []; // Indices of currently selected data.Sources entries. |
| 18 |
let matches = new Set(); // Indices of sources that match search |
| 19 |
let elems = new Map(); // Mapping from source index to display elements |
| 20 |
let displayList = []; // List of boxes to display. |
| 21 |
let actionMenuOn = false; // Is action menu visible? |
| 22 |
let actionTarget = null; // Box on which action menu is operating. |
| 23 |
let diff = false; // Are we displaying a diff? |
| 24 |
let shown = 0; // How many profile values are being displayed? |
| 25 |
|
| 26 |
for (const stack of stacks.Stacks) { |
| 27 |
if (stack.Value < 0) { |
| 28 |
diff = true; |
| 29 |
break; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
// Setup to allow measuring text width. |
| 34 |
const textSizer = document.createElement('canvas'); |
| 35 |
textSizer.id = 'textsizer'; |
| 36 |
const textContext = textSizer.getContext('2d'); |
| 37 |
|
| 38 |
// Get DOM elements. |
| 39 |
const chart = find('stack-chart'); |
| 40 |
const search = find('search'); |
| 41 |
const actions = find('action-menu'); |
| 42 |
const actionTitle = find('action-title'); |
| 43 |
const leftDetailBox = find('current-details-left'); |
| 44 |
const rightDetailBox = find('current-details-right'); |
| 45 |
|
| 46 |
window.addEventListener('resize', render); |
| 47 |
window.addEventListener('popstate', render); |
| 48 |
search.addEventListener('keydown', handleSearchKey); |
| 49 |
|
| 50 |
// Withdraw action menu when clicking outside, or when item selected. |
| 51 |
document.addEventListener('mousedown', (e) => { |
| 52 |
if (!actions.contains(e.target)) { |
| 53 |
hideActionMenu(); |
| 54 |
} |
| 55 |
}); |
| 56 |
actions.addEventListener('click', hideActionMenu); |
| 57 |
|
| 58 |
// Initialize menus and other general UI elements. |
| 59 |
viewer(new URL(window.location.href), nodes, { |
| 60 |
hiliter: (n, on) => { return hilite(n, on); }, |
| 61 |
current: () => { |
| 62 |
let r = new Map(); |
| 63 |
if (pivots.length == 1 && pivots[0] == 0) { |
| 64 |
// Not pivoting |
| 65 |
} else { |
| 66 |
for (let p of pivots) { |
| 67 |
r.set(p, true); |
| 68 |
} |
| 69 |
} |
| 70 |
return r; |
| 71 |
}}); |
| 72 |
|
| 73 |
render(); |
| 74 |
clearDetails(); |
| 75 |
|
| 76 |
// Helper functions follow: |
| 77 |
|
| 78 |
// hilite changes the highlighting of elements corresponding to specified src. |
| 79 |
function hilite(src, on) { |
| 80 |
if (on) { |
| 81 |
matches.add(src); |
| 82 |
} else { |
| 83 |
matches.delete(src); |
| 84 |
} |
| 85 |
toggleClass(src, 'hilite', on); |
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
// Display action menu (triggered by right-click on a frame) |
| 90 |
function showActionMenu(e, box) { |
| 91 |
if (box.src == 0) return; // No action menu for root |
| 92 |
e.preventDefault(); // Disable browser context menu |
| 93 |
const src = stacks.Sources[box.src]; |
| 94 |
actionTitle.innerText = src.Display[src.Display.length-1]; |
| 95 |
const menu = actions; |
| 96 |
menu.style.display = 'block'; |
| 97 |
// Compute position so menu stays visible and near the mouse. |
| 98 |
const x = Math.min(e.clientX - 10, document.body.clientWidth - menu.clientWidth); |
| 99 |
const y = Math.min(e.clientY - 10, document.body.clientHeight - menu.clientHeight); |
| 100 |
menu.style.left = x + 'px'; |
| 101 |
menu.style.top = y + 'px'; |
| 102 |
// Set menu links to operate on clicked box. |
| 103 |
setHrefParam('action-source', 'f', box.src); |
| 104 |
setHrefParam('action-source-tab', 'f', box.src); |
| 105 |
setHrefParam('action-focus', 'f', box.src); |
| 106 |
setHrefParam('action-ignore', 'i', box.src); |
| 107 |
setHrefParam('action-hide', 'h', box.src); |
| 108 |
setHrefParam('action-showfrom', 'sf', box.src); |
| 109 |
toggleClass(box.src, 'hilite2', true); |
| 110 |
actionTarget = box; |
| 111 |
actionMenuOn = true; |
| 112 |
} |
| 113 |
|
| 114 |
function hideActionMenu() { |
| 115 |
actions.style.display = 'none'; |
| 116 |
actionMenuOn = false; |
| 117 |
if (actionTarget != null) { |
| 118 |
toggleClass(actionTarget.src, 'hilite2', false); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// setHrefParam updates the specified parameter in the href of an <a> |
| 123 |
// element to make it operate on the specified src. |
| 124 |
function setHrefParam(id, param, src) { |
| 125 |
const elem = document.getElementById(id); |
| 126 |
if (!elem) return; |
| 127 |
|
| 128 |
let url = new URL(elem.href); |
| 129 |
url.hash = ''; |
| 130 |
|
| 131 |
// Copy params from this page's URL. |
| 132 |
const params = url.searchParams; |
| 133 |
for (const p of new URLSearchParams(window.location.search)) { |
| 134 |
params.set(p[0], p[1]); |
| 135 |
} |
| 136 |
|
| 137 |
// Update params to include src. |
| 138 |
// When `pprof` is invoked with `-lines`, FullName will be suffixed with `:<line>`, |
| 139 |
// which we need to remove. |
| 140 |
let v = pprofQuoteMeta(stacks.Sources[src].FullName.replace(/:[0-9]+$/, '')); |
| 141 |
if (param != 'f' && param != 'sf') { // old f,sf values are overwritten |
| 142 |
// Add new source to current parameter value. |
| 143 |
const old = params.get(param); |
| 144 |
if (old && old != '') { |
| 145 |
v += '|' + old; |
| 146 |
} |
| 147 |
} |
| 148 |
params.set(param, v); |
| 149 |
|
| 150 |
elem.href = url.toString(); |
| 151 |
} |
| 152 |
|
| 153 |
// Capture Enter key in the search box to make it pivot instead of focus. |
| 154 |
function handleSearchKey(e) { |
| 155 |
if (e.key != 'Enter') return; |
| 156 |
e.stopImmediatePropagation(); // Disable normal enter key handling |
| 157 |
const val = search.value; |
| 158 |
try { |
| 159 |
new RegExp(search.value); |
| 160 |
} catch (error) { |
| 161 |
return; // TODO: Display error state in search box |
| 162 |
} |
| 163 |
switchPivots(val); |
| 164 |
} |
| 165 |
|
| 166 |
function switchPivots(regexp) { |
| 167 |
// Switch URL without hitting the server. |
| 168 |
const url = new URL(document.URL); |
| 169 |
if (regexp === '' || regexp === '^$') { |
| 170 |
url.searchParams.delete('p'); // Not pivoting |
| 171 |
} else { |
| 172 |
url.searchParams.set('p', regexp); |
| 173 |
} |
| 174 |
history.pushState('', '', url.toString()); // Makes back-button work |
| 175 |
matches = new Set(); |
| 176 |
search.value = ''; |
| 177 |
render(); |
| 178 |
} |
| 179 |
|
| 180 |
function handleEnter(box, div) { |
| 181 |
if (actionMenuOn) return; |
| 182 |
const src = stacks.Sources[box.src]; |
| 183 |
div.title = details(box) + ' │ ' + src.FullName + (src.Inlined ? "\n(inlined)" : ""); |
| 184 |
leftDetailBox.innerText = src.FullName + (src.Inlined ? " (inlined)" : ""); |
| 185 |
let timing = summary(box.sumpos, box.sumneg); |
| 186 |
if (box.self != 0) { |
| 187 |
timing = "self " + unitText(box.self) + " │ " + timing; |
| 188 |
} |
| 189 |
rightDetailBox.innerText = timing; |
| 190 |
// Highlight all boxes that have the same source as box. |
| 191 |
toggleClass(box.src, 'hilite2', true); |
| 192 |
} |
| 193 |
|
| 194 |
function handleLeave(box) { |
| 195 |
if (actionMenuOn) return; |
| 196 |
clearDetails(); |
| 197 |
toggleClass(box.src, 'hilite2', false); |
| 198 |
} |
| 199 |
|
| 200 |
function clearDetails() { |
| 201 |
leftDetailBox.innerText = ''; |
| 202 |
rightDetailBox.innerText = percentText(shown); |
| 203 |
} |
| 204 |
|
| 205 |
// Return list of sources that match the regexp given by the 'p' URL parameter. |
| 206 |
function urlPivots() { |
| 207 |
const pivots = []; |
| 208 |
const params = (new URL(document.URL)).searchParams; |
| 209 |
const val = params.get('p'); |
| 210 |
if (val !== null && val != '') { |
| 211 |
try { |
| 212 |
const re = new RegExp(val); |
| 213 |
for (let i = 0; i < stacks.Sources.length; i++) { |
| 214 |
const src = stacks.Sources[i]; |
| 215 |
if (re.test(src.UniqueName) || re.test(src.FileName)) { |
| 216 |
pivots.push(i); |
| 217 |
} |
| 218 |
} |
| 219 |
} catch (error) {} |
| 220 |
} |
| 221 |
if (pivots.length == 0) { |
| 222 |
pivots.push(0); |
| 223 |
} |
| 224 |
return pivots; |
| 225 |
} |
| 226 |
|
| 227 |
// render re-generates the stack display. |
| 228 |
function render() { |
| 229 |
pivots = urlPivots(); |
| 230 |
|
| 231 |
// Get places where pivots occur. |
| 232 |
let places = []; |
| 233 |
for (let pivot of pivots) { |
| 234 |
const src = stacks.Sources[pivot]; |
| 235 |
for (let p of src.Places) { |
| 236 |
places.push(p); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
const width = chart.clientWidth; |
| 241 |
elems.clear(); |
| 242 |
actionTarget = null; |
| 243 |
const [pos, neg] = totalValue(places); |
| 244 |
const total = pos + neg; |
| 245 |
const xscale = (width-2*PADDING) / total; // Converts from profile value to X pixels |
| 246 |
const x = PADDING; |
| 247 |
const y = 0; |
| 248 |
|
| 249 |
// Show summary for pivots if we are actually pivoting. |
| 250 |
const showPivotSummary = !(pivots.length == 1 && pivots[0] == 0); |
| 251 |
|
| 252 |
shown = pos + neg; |
| 253 |
displayList.length = 0; |
| 254 |
renderStacks(0, xscale, x, y, places, +1); // Callees |
| 255 |
renderStacks(0, xscale, x, y-ROW, places, -1); // Callers (ROW left for separator) |
| 256 |
display(xscale, pos, neg, displayList, showPivotSummary); |
| 257 |
} |
| 258 |
|
| 259 |
// renderStacks creates boxes with top-left at x,y with children drawn as |
| 260 |
// nested stacks (below or above based on the sign of direction). |
| 261 |
// Returns the largest y coordinate filled. |
| 262 |
function renderStacks(depth, xscale, x, y, places, direction) { |
| 263 |
// Example: suppose we are drawing the following stacks: |
| 264 |
// a->b->c |
| 265 |
// a->b->d |
| 266 |
// a->e->f |
| 267 |
// After rendering a, we will call renderStacks, with places pointing to |
| 268 |
// the preceding stacks. |
| 269 |
// |
| 270 |
// We first group all places with the same leading entry. In this example |
| 271 |
// we get [b->c, b->d] and [e->f]. We render the two groups side-by-side. |
| 272 |
const groups = partitionPlaces(places); |
| 273 |
for (const g of groups) { |
| 274 |
renderGroup(depth, xscale, x, y, g, direction); |
| 275 |
x += groupWidth(xscale, g); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
// Some of the types used below: |
| 280 |
// |
| 281 |
// // Group represents a displayed (sub)tree. |
| 282 |
// interface Group { |
| 283 |
// name: string; // Full name of source |
| 284 |
// src: number; // Index in stacks.Sources |
| 285 |
// self: number; // Contribution as leaf (may be < 0 for diffs) |
| 286 |
// sumpos: number; // Sum of |self| of positive nodes in tree (>= 0) |
| 287 |
// sumneg: number; // Sum of |self| of negative nodes in tree (>= 0) |
| 288 |
// places: Place[]; // Stack slots that contributed to this group |
| 289 |
// } |
| 290 |
// |
| 291 |
// // Box is a rendered item. |
| 292 |
// interface Box { |
| 293 |
// x: number; // X coordinate of top-left |
| 294 |
// y: number; // Y coordinate of top-left |
| 295 |
// width: number; // Width of box to display |
| 296 |
// src: number; // Index in stacks.Sources |
| 297 |
// sumpos: number; // From corresponding Group |
| 298 |
// sumneg: number; // From corresponding Group |
| 299 |
// self: number; // From corresponding Group |
| 300 |
// }; |
| 301 |
|
| 302 |
function groupWidth(xscale, g) { |
| 303 |
return xscale * (g.sumpos + g.sumneg); |
| 304 |
} |
| 305 |
|
| 306 |
function renderGroup(depth, xscale, x, y, g, direction) { |
| 307 |
// Skip if not wide enough. |
| 308 |
const width = groupWidth(xscale, g); |
| 309 |
if (width < MIN_WIDTH) return; |
| 310 |
|
| 311 |
// Draw the box for g.src (except for selected element in upwards direction |
| 312 |
// since that duplicates the box we added in downwards direction). |
| 313 |
if (depth != 0 || direction > 0) { |
| 314 |
const box = { |
| 315 |
x: x, |
| 316 |
y: y, |
| 317 |
width: width, |
| 318 |
src: g.src, |
| 319 |
sumpos: g.sumpos, |
| 320 |
sumneg: g.sumneg, |
| 321 |
self: g.self, |
| 322 |
}; |
| 323 |
displayList.push(box); |
| 324 |
if (direction > 0) { |
| 325 |
// Leave gap on left hand side to indicate self contribution. |
| 326 |
x += xscale*Math.abs(g.self); |
| 327 |
} |
| 328 |
} |
| 329 |
y += direction * ROW; |
| 330 |
|
| 331 |
// Find child or parent stacks. |
| 332 |
const next = []; |
| 333 |
for (const place of g.places) { |
| 334 |
const stack = stacks.Stacks[place.Stack]; |
| 335 |
const nextSlot = place.Pos + direction; |
| 336 |
if (nextSlot >= 0 && nextSlot < stack.Sources.length) { |
| 337 |
next.push({Stack: place.Stack, Pos: nextSlot}); |
| 338 |
} |
| 339 |
} |
| 340 |
renderStacks(depth+1, xscale, x, y, next, direction); |
| 341 |
} |
| 342 |
|
| 343 |
// partitionPlaces partitions a set of places into groups where each group |
| 344 |
// contains places with the same source. If a stack occurs multiple times |
| 345 |
// in places, only the outer-most occurrence is kept. |
| 346 |
function partitionPlaces(places) { |
| 347 |
// Find outer-most slot per stack (used later to elide duplicate stacks). |
| 348 |
const stackMap = new Map(); // Map from stack index to outer-most slot# |
| 349 |
for (const place of places) { |
| 350 |
const prevSlot = stackMap.get(place.Stack); |
| 351 |
if (prevSlot && prevSlot <= place.Pos) { |
| 352 |
// We already have a higher slot in this stack. |
| 353 |
} else { |
| 354 |
stackMap.set(place.Stack, place.Pos); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
// Now partition the stacks. |
| 359 |
const groups = []; // Array of Group {name, src, sum, self, places} |
| 360 |
const groupMap = new Map(); // Map from Source to Group |
| 361 |
for (const place of places) { |
| 362 |
if (stackMap.get(place.Stack) != place.Pos) { |
| 363 |
continue; |
| 364 |
} |
| 365 |
|
| 366 |
const stack = stacks.Stacks[place.Stack]; |
| 367 |
const src = stack.Sources[place.Pos]; |
| 368 |
let group = groupMap.get(src); |
| 369 |
if (!group) { |
| 370 |
const name = stacks.Sources[src].FullName; |
| 371 |
group = {name: name, src: src, sumpos: 0, sumneg: 0, self: 0, places: []}; |
| 372 |
groupMap.set(src, group); |
| 373 |
groups.push(group); |
| 374 |
} |
| 375 |
if (stack.Value < 0) { |
| 376 |
group.sumneg += -stack.Value; |
| 377 |
} else { |
| 378 |
group.sumpos += stack.Value; |
| 379 |
} |
| 380 |
group.self += (place.Pos == stack.Sources.length-1) ? stack.Value : 0; |
| 381 |
group.places.push(place); |
| 382 |
} |
| 383 |
|
| 384 |
// Order by decreasing cost (makes it easier to spot heavy functions). |
| 385 |
// Though alphabetical ordering is a potential alternative that will make |
| 386 |
// profile comparisons easier. |
| 387 |
groups.sort(function(a, b) { |
| 388 |
return (b.sumpos + b.sumneg) - (a.sumpos + a.sumneg); |
| 389 |
}); |
| 390 |
|
| 391 |
return groups; |
| 392 |
} |
| 393 |
|
| 394 |
function display(xscale, posTotal, negTotal, list, showPivotSummary) { |
| 395 |
// Sort boxes so that text selection follows a predictable order. |
| 396 |
list.sort(function(a, b) { |
| 397 |
if (a.y != b.y) return a.y - b.y; |
| 398 |
return a.x - b.x; |
| 399 |
}); |
| 400 |
|
| 401 |
// Adjust Y coordinates so that zero is at top. |
| 402 |
let adjust = (list.length > 0) ? list[0].y : 0; |
| 403 |
|
| 404 |
const divs = []; |
| 405 |
for (const box of list) { |
| 406 |
box.y -= adjust; |
| 407 |
divs.push(drawBox(xscale, box)); |
| 408 |
} |
| 409 |
if (showPivotSummary) { |
| 410 |
divs.push(drawSep(-adjust, posTotal, negTotal)); |
| 411 |
} |
| 412 |
|
| 413 |
const h = (list.length > 0 ? list[list.length-1].y : 0) + 4*ROW; |
| 414 |
chart.style.height = h+'px'; |
| 415 |
chart.replaceChildren(...divs); |
| 416 |
} |
| 417 |
|
| 418 |
function drawBox(xscale, box) { |
| 419 |
const srcIndex = box.src; |
| 420 |
const src = stacks.Sources[srcIndex]; |
| 421 |
|
| 422 |
function makeRect(cl, x, y, w, h) { |
| 423 |
const r = document.createElement('div'); |
| 424 |
r.style.left = x+'px'; |
| 425 |
r.style.top = y+'px'; |
| 426 |
r.style.width = w+'px'; |
| 427 |
r.style.height = h+'px'; |
| 428 |
r.classList.add(cl); |
| 429 |
return r; |
| 430 |
} |
| 431 |
|
| 432 |
// Background |
| 433 |
const w = box.width - 1; // Leave 1px gap |
| 434 |
const r = makeRect('boxbg', box.x, box.y, w, ROW); |
| 435 |
if (!diff) r.style.background = makeColor(src.Color); |
| 436 |
addElem(srcIndex, r); |
| 437 |
if (!src.Inlined) { |
| 438 |
r.classList.add('not-inlined'); |
| 439 |
} |
| 440 |
|
| 441 |
// Positive/negative indicator for diff mode. |
| 442 |
if (diff) { |
| 443 |
const delta = box.sumpos - box.sumneg; |
| 444 |
const partWidth = xscale * Math.abs(delta); |
| 445 |
if (partWidth >= MIN_WIDTH) { |
| 446 |
r.appendChild(makeRect((delta < 0 ? 'negative' : 'positive'), |
| 447 |
0, 0, partWidth, ROW-1)); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
// Label |
| 452 |
if (box.width >= MIN_TEXT_WIDTH) { |
| 453 |
const t = document.createElement('div'); |
| 454 |
t.classList.add('boxtext'); |
| 455 |
fitText(t, box.width-2*TEXT_MARGIN, src.Display); |
| 456 |
r.appendChild(t); |
| 457 |
} |
| 458 |
|
| 459 |
onClick(r, () => { switchPivots(pprofQuoteMeta(src.UniqueName)); }); |
| 460 |
r.addEventListener('mouseenter', () => { handleEnter(box, r); }); |
| 461 |
r.addEventListener('mouseleave', () => { handleLeave(box); }); |
| 462 |
r.addEventListener('contextmenu', (e) => { showActionMenu(e, box); }); |
| 463 |
return r; |
| 464 |
} |
| 465 |
|
| 466 |
// Handle clicks, but only if the mouse did not move during the click. |
| 467 |
function onClick(target, handler) { |
| 468 |
// Disable click if mouse moves more than threshold pixels since mousedown. |
| 469 |
const threshold = 3; |
| 470 |
let [x, y] = [-1, -1]; |
| 471 |
target.addEventListener('mousedown', (e) => { |
| 472 |
[x, y] = [e.clientX, e.clientY]; |
| 473 |
}); |
| 474 |
target.addEventListener('click', (e) => { |
| 475 |
if (Math.abs(e.clientX - x) <= threshold && |
| 476 |
Math.abs(e.clientY - y) <= threshold) { |
| 477 |
handler(); |
| 478 |
} |
| 479 |
}); |
| 480 |
} |
| 481 |
|
| 482 |
function drawSep(y, posTotal, negTotal) { |
| 483 |
const m = document.createElement('div'); |
| 484 |
m.innerText = summary(posTotal, negTotal); |
| 485 |
m.style.top = (y-ROW) + 'px'; |
| 486 |
m.style.left = PADDING + 'px'; |
| 487 |
m.style.width = (chart.clientWidth - PADDING*2) + 'px'; |
| 488 |
m.classList.add('separator'); |
| 489 |
return m; |
| 490 |
} |
| 491 |
|
| 492 |
// addElem registers an element that belongs to the specified src. |
| 493 |
function addElem(src, elem) { |
| 494 |
let list = elems.get(src); |
| 495 |
if (!list) { |
| 496 |
list = []; |
| 497 |
elems.set(src, list); |
| 498 |
} |
| 499 |
list.push(elem); |
| 500 |
elem.classList.toggle('hilite', matches.has(src)); |
| 501 |
} |
| 502 |
|
| 503 |
// Adds or removes cl from classList of all elements for the specified source. |
| 504 |
function toggleClass(src, cl, value) { |
| 505 |
const list = elems.get(src); |
| 506 |
if (list) { |
| 507 |
for (const elem of list) { |
| 508 |
elem.classList.toggle(cl, value); |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
// fitText sets text and font-size clipped to the specified width w. |
| 514 |
function fitText(t, avail, textList) { |
| 515 |
// Find first entry in textList that fits. |
| 516 |
let width = avail; |
| 517 |
textContext.font = FONT_SIZE + 'pt Arial'; |
| 518 |
for (let i = 0; i < textList.length; i++) { |
| 519 |
let text = textList[i]; |
| 520 |
width = textContext.measureText(text).width; |
| 521 |
if (width <= avail) { |
| 522 |
t.innerText = text; |
| 523 |
return; |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
// Try to fit by dropping font size. |
| 528 |
let text = textList[textList.length-1]; |
| 529 |
const fs = Math.max(MIN_FONT_SIZE, FONT_SIZE * (avail / width)); |
| 530 |
t.style.fontSize = fs + 'pt'; |
| 531 |
t.innerText = text; |
| 532 |
} |
| 533 |
|
| 534 |
// totalValue returns the positive and negative sums of the Values of stacks |
| 535 |
// listed in places. |
| 536 |
function totalValue(places) { |
| 537 |
const seen = new Set(); |
| 538 |
let pos = 0; |
| 539 |
let neg = 0; |
| 540 |
for (const place of places) { |
| 541 |
if (seen.has(place.Stack)) continue; // Do not double-count stacks |
| 542 |
seen.add(place.Stack); |
| 543 |
const stack = stacks.Stacks[place.Stack]; |
| 544 |
if (stack.Value < 0) { |
| 545 |
neg += -stack.Value; |
| 546 |
} else { |
| 547 |
pos += stack.Value; |
| 548 |
} |
| 549 |
} |
| 550 |
return [pos, neg]; |
| 551 |
} |
| 552 |
|
| 553 |
function summary(pos, neg) { |
| 554 |
// Examples: |
| 555 |
// 6s (10%) |
| 556 |
// 12s (20%) 🠆 18s (30%) |
| 557 |
return diff ? diffText(neg, pos) : percentText(pos); |
| 558 |
} |
| 559 |
|
| 560 |
function details(box) { |
| 561 |
// Examples: |
| 562 |
// 6s (10%) |
| 563 |
// 6s (10%) │ self 3s (5%) |
| 564 |
// 6s (10%) │ 12s (20%) 🠆 18s (30%) |
| 565 |
let result = percentText(box.sumpos - box.sumneg); |
| 566 |
if (box.self != 0) { |
| 567 |
result += " │ self " + unitText(box.self); |
| 568 |
} |
| 569 |
if (diff && box.sumpos > 0 && box.sumneg > 0) { |
| 570 |
result += " │ " + diffText(box.sumneg, box.sumpos); |
| 571 |
} |
| 572 |
return result; |
| 573 |
} |
| 574 |
|
| 575 |
// diffText returns text that displays from and to alongside their percentages. |
| 576 |
// E.g., 9s (45%) 🠆 10s (50%) |
| 577 |
function diffText(from, to) { |
| 578 |
return percentText(from) + " 🠆 " + percentText(to); |
| 579 |
} |
| 580 |
|
| 581 |
// percentText returns text that displays v in appropriate units alongside its |
| 582 |
// percentage. |
| 583 |
function percentText(v) { |
| 584 |
function percent(v, total) { |
| 585 |
return Number(((100.0 * v) / total).toFixed(1)) + '%'; |
| 586 |
} |
| 587 |
return unitText(v) + " (" + percent(v, stacks.Total) + ")"; |
| 588 |
} |
| 589 |
|
| 590 |
// unitText returns a formatted string to display for value. |
| 591 |
function unitText(value) { |
| 592 |
return pprofUnitText(value*stacks.Scale, stacks.Unit); |
| 593 |
} |
| 594 |
|
| 595 |
function find(name) { |
| 596 |
const elem = document.getElementById(name); |
| 597 |
if (!elem) { |
| 598 |
throw 'element not found: ' + name |
| 599 |
} |
| 600 |
return elem; |
| 601 |
} |
| 602 |
|
| 603 |
function makeColor(index) { |
| 604 |
// Rotate hue around a circle. Multiple by phi to spread things |
| 605 |
// out better. Use 50% saturation to make subdued colors, and |
| 606 |
// 80% lightness to have good contrast with black foreground text. |
| 607 |
const PHI = 1.618033988; |
| 608 |
const hue = (index+1) * PHI * 2 * Math.PI; // +1 to avoid 0 |
| 609 |
const hsl = `hsl(${hue}rad 50% 80%)`; |
| 610 |
return hsl; |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
// pprofUnitText returns a formatted string to display for value in the specified unit. |
| 615 |
function pprofUnitText(value, unit) { |
| 616 |
const sign = (value < 0) ? "-" : ""; |
| 617 |
let v = Math.abs(value); |
| 618 |
// Rescale to appropriate display unit. |
| 619 |
let list = null; |
| 620 |
for (const def of pprofUnitDefs) { |
| 621 |
if (def.DefaultUnit.CanonicalName == unit) { |
| 622 |
list = def.Units; |
| 623 |
v *= def.DefaultUnit.Factor; |
| 624 |
break; |
| 625 |
} |
| 626 |
} |
| 627 |
if (list) { |
| 628 |
// Stop just before entry that is too large. |
| 629 |
for (let i = 0; i < list.length; i++) { |
| 630 |
if (i == list.length-1 || list[i+1].Factor > v) { |
| 631 |
v /= list[i].Factor; |
| 632 |
unit = list[i].CanonicalName; |
| 633 |
break; |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
return sign + Number(v.toFixed(2)) + unit; |
| 638 |
} |