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

opened this

Merged 3h 39m ago, live in production

torque/forge:torque/18212-the-spark-page-shows-the-slowest-ticks-a → main 0 commits fork

  • Merged. torque/18212-the-spark-page-shows-the-slowest-ticks-a was merged into main as 281fd2f.

Review

Sign in to review this change.
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
1414
1515 ## The call tree and flame graph (#18208)
1616
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[] {
8181 ...s.threads.map((t) => `${t.name || "(unnamed thread)"}: ${millis(t.ms)} sampled`),
8282 ];
8383 }
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 @@
66
77 import { READ_UNPACKED, decodeSparkProfile } from "../../games/spark_profile.ts";
88 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";
1010 import type { GameBox } from "../../games/store.ts";
1111 import type { Ctx, Screen } from "../app/app.ts";
1212 import { binding } from "../app/keys.ts";
@@ -14,6 +14,9 @@ import { style } from "../term/style.ts";
1414 import { truncate } from "../term/width.ts";
1515 import type { Session } from "../session.ts";
1616
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
1720 export function gameSparkViewScreen(f: Session, ctx: Ctx, box: GameBox, key: string): Screen {
1821 let top = 0;
1922 let cached: string[] | undefined;
@@ -29,7 +32,7 @@ export function gameSparkViewScreen(f: Session, ctx: Ctx, box: GameBox, key: str
2932 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."];
3033 const { drawn, note } = heaviest(read.summary.threads);
3134 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)];
3336 };
3437 return {
3538 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";
1111 import { html, type Html } from "../html.ts";
1212 import { page } from "../layout.ts";
1313 import { boxOf } from "./games.ts";
14 import { sparkTicksAndMods } from "./games_spark_ticks.ts";
1415
1516 const SAMPLER = "application/x-spark-sampler";
1617 const ROW = 18;
@@ -53,6 +54,7 @@ export function gameSparkView(ctx: Ctx): Response {
5354 ${t.tree.length === 0 ? html`<p class="muted small">Nothing was sampled in this thread.</p>` : html`${flameSvg(t)}
5455 <details ${i === 0 ? html`open` : null}><summary>Call tree</summary><pre>${treeLines(t.tree, t.ms).join("\n")}</pre></details>`}
5556 </section>`)}
57 ${sparkTicksAndMods(read.summary)}
5658 ${shown?.note ? html`<p class="muted small">${shown.note} <a href="${href}">Download it</a>.</p>` : null}`;
5759 return page({
5860 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
114114 const { heaviest } = await import("../src/games/spark_view.ts");
115115 expect(heaviest([{ ms: 1 }, { ms: 5 }]).note).toBe("");
116116 });
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("&lt;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
455455 await owner.type("\r");
456456 await settleUntil(owner.tui, "Main thread");
457457 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");
458462 } finally {
459463 owner.tui.stop?.();
460464 }

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs