Public: anyone can read this; the crew can change it.
The spark page shows the slowest ticks and the time per mod, from the decoded profile #18237
Merged 3h 39m ago, live in production
torque/forge:torque/18212-the-spark-page-shows-the-slowest-ticks-a → main 0 commits fork
7 files
changed +74
−3
docs/features/game-spark.md +1 −1
| @@ -14,4 +14,4 @@ Installing spark (`mods_install spark`, or as a plugin of a new server) writes s | ||
| 14 | 14 | |
| 15 | 15 | ## The call tree and flame graph (#18208) |
| 16 | 16 | |
| 17 | A listed profile opens `/games/<box>/spark/<key>` (the bytes stay at `/games/<box>/@spark/<key>`): `decodeSparkProfile` reads it per request, and each thread shows its time sampled (milliseconds), a flame graph (an SVG drawn on the server, no script; a box is as wide as its share of the thread, the call and time in its tooltip) and the call tree as text with shares. The terminal shows the same lines: on a box's Profiles screen (`p`), `j`/`k` pick a profile and Enter opens `src/tui/screens/game_spark_view.ts`. Only the 10 heaviest threads are drawn, on both doors; the rest are named as "N more threads not drawn, download the profile". Only a grant on the box reads it; a body that is not a profile says why and links the download. The tree is the decoder's pruned one: 12 deep, 8 wide, at most 2,000 nodes a thread, from a body of at most 8 MiB (64 MiB unpacked). Not done: slowest ticks, time per mod, GC and memory are decoded but not shown; a flame box is not zoomable. Code: `src/games/spark_view.ts` (layout), `src/web/pages/games_spark_view.ts`. | |
| 17 | A listed profile opens `/games/<box>/spark/<key>` (the bytes stay at `/games/<box>/@spark/<key>`): `decodeSparkProfile` reads it per request, and each thread shows its time sampled (milliseconds), a flame graph (an SVG drawn on the server, no script; a box is as wide as its share of the thread, the call and time in its tooltip) and the call tree as text with shares. The terminal shows the same lines: on a box's Profiles screen (`p`), `j`/`k` pick a profile and Enter opens `src/tui/screens/game_spark_view.ts`. Only the 10 heaviest threads are drawn, on both doors; the rest are named as "N more threads not drawn, download the profile". Only a grant on the box reads it; a body that is not a profile says why and links the download. The tree is the decoder's pruned one: 12 deep, 8 wide, at most 2,000 nodes a thread, from a body of at most 8 MiB (64 MiB unpacked). The page also lists the slowest ticks (window, max and median ms per tick, TPS, players) and the time per mod (source, ms, share), tables with no script, drawn by `src/web/pages/games_spark_ticks.ts`; the terminal ends the same screen with the same rows, and a profile with no mod data says so in one line. Not done: GC and memory are decoded but not shown; a flame box is not zoomable. Code: `src/games/spark_view.ts` (layout), `src/web/pages/games_spark_view.ts`. |
src/games/spark_view.ts +16
| @@ -81,3 +81,19 @@ export function sparkHeadLines(s: SparkSummary): string[] { | ||
| 81 | 81 | ...s.threads.map((t) => `${t.name || "(unnamed thread)"}: ${millis(t.ms)} sampled`), |
| 82 | 82 | ]; |
| 83 | 83 | } |
| 84 | ||
| 85 | /** | |
| 86 | * The slowest ticks as rows of cells, in the words both doors use (`ms` per tick, `tps` ticks/s). | |
| 87 | */ | |
| 88 | export function slowTickRows(s: SparkSummary): string[][] { | |
| 89 | return s.slowestTicks.map((t) => [`window ${String(t.window)}`, millis(t.msptMax), millis(t.msptMedian), t.tps.toFixed(1), String(t.players)]); | |
| 90 | } | |
| 91 | export const SLOW_TICK_HEAD = ["Window", "Max per tick", "Median per tick", "TPS", "Players"]; | |
| 92 | ||
| 93 | /** Time per mod as rows: source, ms sampled, share of the time attributed to a mod. */ | |
| 94 | export function modTimeRows(s: SparkSummary): string[][] { | |
| 95 | return s.perMod.map((m) => [m.source, millis(m.ms), percent(m.share)]); | |
| 96 | } | |
| 97 | export const MOD_TIME_HEAD = ["Source", "Time", "Share"]; | |
| 98 | export const NO_TICKS = "This profile holds no per-window tick statistics."; | |
| 99 | export const NO_MODS = "This profile has no mod data: no frame was attributed to a mod."; |
src/tui/screens/game_spark_view.ts +5 −2
| @@ -6,7 +6,7 @@ | ||
| 6 | 6 | |
| 7 | 7 | import { READ_UNPACKED, decodeSparkProfile } from "../../games/spark_profile.ts"; |
| 8 | 8 | import { profileOf } from "../../games/spark.ts"; |
| 9 | import { heaviest, millis, sparkHeadLines, treeLines } from "../../games/spark_view.ts"; | |
| 9 | import { MOD_TIME_HEAD, NO_MODS, NO_TICKS, SLOW_TICK_HEAD, heaviest, millis, modTimeRows, slowTickRows, sparkHeadLines, treeLines } from "../../games/spark_view.ts"; | |
| 10 | 10 | import type { GameBox } from "../../games/store.ts"; |
| 11 | 11 | import type { Ctx, Screen } from "../app/app.ts"; |
| 12 | 12 | import { binding } from "../app/keys.ts"; |
| @@ -14,6 +14,9 @@ import { style } from "../term/style.ts"; | ||
| 14 | 14 | import { truncate } from "../term/width.ts"; |
| 15 | 15 | import type { Session } from "../session.ts"; |
| 16 | 16 | |
| 17 | /** A titled table as lines: the head, then each row, cells apart by two spaces. */ | |
| 18 | const section = (title: string, head: string[], rows: string[][], none: string): string[] => ["", `== ${title}`, ...(rows.length === 0 ? [none] : [head.join(" "), ...rows.map((r) => r.join(" "))])]; | |
| 19 | ||
| 17 | 20 | export function gameSparkViewScreen(f: Session, ctx: Ctx, box: GameBox, key: string): Screen { |
| 18 | 21 | let top = 0; |
| 19 | 22 | let cached: string[] | undefined; |
| @@ -29,7 +32,7 @@ export function gameSparkViewScreen(f: Session, ctx: Ctx, box: GameBox, key: str | ||
| 29 | 32 | if (read === undefined || !read.ok) return ["The forge draws a sampler profile only, and this one is not a readable one: download it from the box's page."]; |
| 30 | 33 | const { drawn, note } = heaviest(read.summary.threads); |
| 31 | 34 | const [head = ""] = sparkHeadLines(read.summary); |
| 32 | return [head, "Times are milliseconds sampled, shares are of the thread; trees keep the heaviest calls.", ...drawn.flatMap((t) => ["", `== ${t.name || "(unnamed thread)"}: ${millis(t.ms)} sampled`, ...(t.tree.length === 0 ? ["Nothing was sampled in this thread."] : treeLines(t.tree, t.ms))]), ...(note === "" ? [] : ["", note])]; | |
| 35 | return [head, "Times are milliseconds sampled, shares are of the thread; trees keep the heaviest calls.", ...drawn.flatMap((t) => ["", `== ${t.name || "(unnamed thread)"}: ${millis(t.ms)} sampled`, ...(t.tree.length === 0 ? ["Nothing was sampled in this thread."] : treeLines(t.tree, t.ms))]), ...(note === "" ? [] : ["", note]), ...section("Slowest ticks", SLOW_TICK_HEAD, slowTickRows(read.summary), NO_TICKS), ...section("Time per mod", MOD_TIME_HEAD, modTimeRows(read.summary), NO_MODS)]; | |
| 33 | 36 | }; |
| 34 | 37 | return { |
| 35 | 38 | title: `${box.name}: profile ${key}`, |
src/web/pages/games_spark_ticks.ts added +23
| @@ -0,0 +1,23 @@ | ||
| 1 | /** | |
| 2 | * The slowest ticks and the time per mod of a decoded spark profile (#18212), server-rendered | |
| 3 | * tables, no script. Kept apart from the call tree (`games_spark_view.ts`) so the two change | |
| 4 | * alone. The terminal shows the same rows (`tui/screens/game_spark_view.ts`). | |
| 5 | */ | |
| 6 | import type { SparkSummary } from "../../games/spark_profile.ts"; | |
| 7 | import { MOD_TIME_HEAD, NO_MODS, NO_TICKS, SLOW_TICK_HEAD, modTimeRows, slowTickRows } from "../../games/spark_view.ts"; | |
| 8 | import { html, type Html } from "../html.ts"; | |
| 9 | ||
| 10 | const grid = (id: string, caption: string, head: string[], rows: string[][]): Html => html`<div class="table-scroll"><table class="table" id="${id}" data-dense> | |
| 11 | <caption class="sr-only">${caption}</caption> | |
| 12 | <thead><tr>${head.map((h, i) => html`<th scope="col"${i > 0 ? html` class="num"` : null}>${h}</th>`)}</tr></thead> | |
| 13 | <tbody>${rows.map((r) => html`<tr>${r.map((c, i) => (i === 0 ? html`<th scope="row">${c}</th>` : html`<td class="num">${c}</td>`))}</tr>`)}</tbody> | |
| 14 | </table></div>`; | |
| 15 | ||
| 16 | export function sparkTicksAndMods(s: SparkSummary): Html { | |
| 17 | const ticks = slowTickRows(s); | |
| 18 | const mods = modTimeRows(s); | |
| 19 | return html`<section class="settings-block"><h2>Slowest ticks</h2> | |
| 20 | ${ticks.length === 0 ? html`<p class="muted small">${NO_TICKS}</p>` : grid("spark-ticks", "Slowest tick windows", SLOW_TICK_HEAD, ticks)}</section> | |
| 21 | <section class="settings-block"><h2>Time per mod</h2> | |
| 22 | ${mods.length === 0 ? html`<p class="muted small">${NO_MODS}</p>` : grid("spark-mods", "Time sampled per mod", MOD_TIME_HEAD, mods)}</section>`; | |
| 23 | } |
src/web/pages/games_spark_view.ts +2
| @@ -11,6 +11,7 @@ import { notFoundPage, pageHead } from "../chrome.ts"; | ||
| 11 | 11 | import { html, type Html } from "../html.ts"; |
| 12 | 12 | import { page } from "../layout.ts"; |
| 13 | 13 | import { boxOf } from "./games.ts"; |
| 14 | import { sparkTicksAndMods } from "./games_spark_ticks.ts"; | |
| 14 | 15 | |
| 15 | 16 | const SAMPLER = "application/x-spark-sampler"; |
| 16 | 17 | const ROW = 18; |
| @@ -53,6 +54,7 @@ export function gameSparkView(ctx: Ctx): Response { | ||
| 53 | 54 | ${t.tree.length === 0 ? html`<p class="muted small">Nothing was sampled in this thread.</p>` : html`${flameSvg(t)} |
| 54 | 55 | <details ${i === 0 ? html`open` : null}><summary>Call tree</summary><pre>${treeLines(t.tree, t.ms).join("\n")}</pre></details>`} |
| 55 | 56 | </section>`)} |
| 57 | ${sparkTicksAndMods(read.summary)} | |
| 56 | 58 | ${shown?.note ? html`<p class="muted small">${shown.note} <a href="${href}">Download it</a>.</p>` : null}`; |
| 57 | 59 | return page({ |
| 58 | 60 | ctx, |
test/games-spark-view.test.ts +23
| @@ -114,3 +114,26 @@ test("only the heaviest threads are drawn, and the rest are counted, on both doo | ||
| 114 | 114 | const { heaviest } = await import("../src/games/spark_view.ts"); |
| 115 | 115 | expect(heaviest([{ ms: 1 }, { ms: 5 }]).note).toBe(""); |
| 116 | 116 | }); |
| 117 | ||
| 118 | test("the page lists the slowest ticks and time per mod, escaping a hostile mod name, and says when there is none (#18212)", async () => { | |
| 119 | const win = (ticks: number, max: number) => [...key(1, 0), ...varint(ticks), ...key(6, 1), ...dbl(max), ...key(5, 1), ...dbl(max / 2), ...key(4, 1), ...dbl(19.5), ...key(7, 0), ...varint(3)]; | |
| 120 | const entry = (n: number, k: number[], v: number[]) => bytes(n, [...k, ...v]); | |
| 121 | const raw = [ | |
| 122 | ...bytes(2, [...text(1, "Server thread"), ...key(2, 1), ...dbl(100), ...bytes(3, node("x.Evil", "run", 40))]), | |
| 123 | ...entry(3, text(1, "x.Evil"), text(2, "<img src=x onerror=alert(1)>")), | |
| 124 | ...entry(7, [...key(1, 0), 1], bytes(2, win(1200, 80))), | |
| 125 | ]; | |
| 126 | const box = boxNamed(app.db, "net")!; | |
| 127 | const keep = (b: Uint8Array) => { const k = keepProfile(app.db, box.id, "application/x-spark-sampler", b); if (!k.ok) throw new Error(k.error); return k.value; }; | |
| 128 | const body = await (await get(`/games/net/spark/${keep(gzipSync(Uint8Array.from(raw)))}`, "bob")).text(); | |
| 129 | expect(body).toContain("Slowest ticks"); | |
| 130 | expect(body).toContain("80.0 ms"); | |
| 131 | expect(body).toContain("40.0 ms"); | |
| 132 | expect(body).toContain("100.0%"); | |
| 133 | expect(body).toContain("Time per mod"); | |
| 134 | expect(body).not.toContain("<img src=x"); | |
| 135 | expect(body).toContain("<img src=x"); | |
| 136 | const none = await (await get(`/games/net/spark/${tree}`, "bob")).text(); | |
| 137 | expect(none).toContain("This profile has no mod data"); | |
| 138 | expect(none.slice(none.indexOf("Slowest ticks"), none.indexOf("</main>"))).not.toContain("<script"); | |
| 139 | }); |
test/tui-games.test.ts +4
| @@ -455,6 +455,10 @@ test("Enter on a stored spark profile opens its call tree in the terminal, as th | ||
| 455 | 455 | await owner.type("\r"); |
| 456 | 456 | await settleUntil(owner.tui, "Main thread"); |
| 457 | 457 | expect(owner.tui.screen.toText()).toContain("100.0% 100.0 ms tick"); |
| 458 | // The terminal ends with the page's two lists, here with no ticks and no mod data (#18212). | |
| 459 | for (let i = 0; i < 40; i++) await owner.type("j"); | |
| 460 | await settleUntil(owner.tui, "Time per mod"); | |
| 461 | expect(owner.tui.screen.toText()).toContain("This profile has no mod data"); | |
| 458 | 462 | } finally { |
| 459 | 463 | owner.tui.stop?.(); |
| 460 | 464 | } |
new above
offline