Public: anyone can read this; the crew can change it.
Spark profile: each slowest-tick row has an id the box page can link to, and a finding carries its profile key and tick window #18281
Closed 23h 33m ago
torque/forge:torque/18265-spark-profile-each-slowest-tick-row-has → main 4 commits fork
7 files
changed +81
−5
docs/features/game-spark.md +2
| @@ -15,3 +15,5 @@ Installing spark (`mods_install spark`, or as a plugin of a new server) writes s | ||
| 15 | 15 | ## The call tree and flame graph (#18208) |
| 16 | 16 | |
| 17 | 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). Under the threads, both doors show (#18140, `summarySections`) the slowest tick windows, time per mod, GC and memory, and the mods listed. Not done: a finding that links to a slow frame; a flame box is not zoomable. Code: `src/games/spark_view.ts` (layout), `src/web/pages/games_spark_view.ts`. |
| 18 | ||
| 19 | A profile whose worst tick window ran past 50 ms (the budget of a tick at 20 TPS) shows one finding above the sections, on the page and in the terminal's profile screen. It names the profile's key and the window; the page links to that window's row, `#tick-<window>` (#18265). |
src/games/findings.ts +6
| @@ -26,6 +26,12 @@ export interface Finding { | ||
| 26 | 26 | /** The newest reason or command this was read from, as the log wrote it. */ |
| 27 | 27 | readonly last: string; |
| 28 | 28 | readonly at: string; |
| 29 | /** | |
| 30 | * Set only on a finding read from a spark profile (`spark_finding.ts`): the stored profile's key | |
| 31 | * on the box and the tick window in it, which the profile page names `tick-<window>`. | |
| 32 | */ | |
| 33 | readonly profileKey?: string; | |
| 34 | readonly tickWindow?: number; | |
| 29 | 35 | } |
| 30 | 36 | |
| 31 | 37 | interface Cause { |
src/games/spark_finding.ts added +39
| @@ -0,0 +1,39 @@ | ||
| 1 | /** | |
| 2 | * A finding read from a spark profile (#18265): the worst of its slowest tick windows when it | |
| 3 | * overran the server's tick budget. It carries the profile's key and the window, so a page can link | |
| 4 | * to the row (`#tick-<window>`, `spark_view.ts`'s `tickId`). | |
| 5 | * | |
| 6 | * Units: `msptMax` is milliseconds per tick. A Minecraft server ticks 20 times a second, so a tick | |
| 7 | * has 1000 / 20 = 50 ms; a tick past that is a lag spike. | |
| 8 | */ | |
| 9 | import type { Database } from "bun:sqlite"; | |
| 10 | import type { Finding } from "./findings.ts"; | |
| 11 | import { profileOf } from "./spark.ts"; | |
| 12 | import type { SparkSummary } from "./spark_profile.ts"; | |
| 13 | ||
| 14 | const TICK_BUDGET_MS = 50; | |
| 15 | ||
| 16 | /** | |
| 17 | * The finding for a profile's worst tick window, or undefined: the key is not a profile of this | |
| 18 | * box (it is looked up in the box's own profiles), or no window went past the tick budget. | |
| 19 | */ | |
| 20 | export function profileFinding(db: Database, boxId: string, key: string, summary: SparkSummary, now = Date.now()): Finding | undefined { | |
| 21 | const stored = profileOf(db, boxId, key, now); | |
| 22 | if (stored === undefined) return undefined; | |
| 23 | const worst = summary.slowestTicks.reduce<SparkSummary["slowestTicks"][number] | undefined>((w, t) => (w === undefined || t.msptMax > w.msptMax ? t : w), undefined); | |
| 24 | if (worst === undefined || !(worst.msptMax > TICK_BUDGET_MS)) return undefined; | |
| 25 | return { | |
| 26 | key: "slow-tick", | |
| 27 | says: `The worst tick took ${String(worst.msptMax)} ms, past the ${String(TICK_BUDGET_MS)} ms a tick has at 20 TPS (window ${String(worst.window)}, ${String(worst.ticks)} ticks, ${String(worst.players)} players).`, | |
| 28 | fix: "Read the call tree and the time per mod below for what ran in it; nothing here is changed for you.", | |
| 29 | count: 1, | |
| 30 | players: [], | |
| 31 | last: "", | |
| 32 | at: stored.at, | |
| 33 | profileKey: key, | |
| 34 | tickWindow: worst.window, | |
| 35 | }; | |
| 36 | } | |
| 37 | ||
| 38 | /** A profile finding as one line for both doors. */ | |
| 39 | export const sparkFindingLine = (f: Finding): string => `${f.says} Fix: ${f.fix}`; |
src/games/spark_view.ts +5 −2
| @@ -82,8 +82,11 @@ export function sparkHeadLines(s: SparkSummary): string[] { | ||
| 82 | 82 | ]; |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | /** The page's anchor for a tick window: `#tick-<window>`. */ | |
| 86 | export const tickId = (window: number): string => `tick-${String(window)}`; | |
| 87 | ||
| 85 | 88 | export interface SparkFact { readonly label: string; readonly value: string } |
| 86 | export interface SparkRow { readonly title: string; readonly facts: readonly SparkFact[] } | |
| 89 | export interface SparkRow { readonly id?: string; readonly title: string; readonly facts: readonly SparkFact[] } | |
| 87 | 90 | export interface SparkSection { readonly id: string; readonly title: string; readonly rows: readonly SparkRow[]; readonly empty: string } |
| 88 | 91 | |
| 89 | 92 | const mib = (bytes: number): string => `${String(Math.round(bytes / 1048576))} MiB`; |
| @@ -99,7 +102,7 @@ export function summarySections(s: SparkSummary): SparkSection[] { | ||
| 99 | 102 | return [ |
| 100 | 103 | { |
| 101 | 104 | id: "ticks", title: "Slowest ticks", empty: "The profile recorded no tick windows.", |
| 102 | rows: s.slowestTicks.map((t, i) => ({ title: `Window ${String(i + 1)}`, facts: [ | |
| 105 | rows: s.slowestTicks.map((t, i) => ({ id: tickId(t.window), title: `Window ${String(i + 1)}`, facts: [ | |
| 103 | 106 | { label: "Worst tick", value: `${String(t.msptMax)} ms` }, { label: "Median", value: `${String(t.msptMedian)} ms` }, |
| 104 | 107 | { label: "TPS", value: String(t.tps) }, { label: "Ticks", value: String(t.ticks) }, { label: "Players", value: String(t.players) }, |
| 105 | 108 | ] })), |
src/tui/screens/game_spark_view.ts +3 −1
| @@ -6,6 +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 { profileFinding, sparkFindingLine } from "../../games/spark_finding.ts"; | |
| 9 | 10 | import { heaviest, millis, sectionLines, sparkHeadLines, summarySections, treeLines } from "../../games/spark_view.ts"; |
| 10 | 11 | import type { GameBox } from "../../games/store.ts"; |
| 11 | 12 | import type { Ctx, Screen } from "../app/app.ts"; |
| @@ -28,8 +29,9 @@ export function gameSparkViewScreen(f: Session, ctx: Ctx, box: GameBox, key: str | ||
| 28 | 29 | const read = stored.type === "application/x-spark-sampler" ? decodeSparkProfile(stored.body, READ_UNPACKED) : undefined; |
| 29 | 30 | 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 | 31 | const { drawn, note } = heaviest(read.summary.threads); |
| 32 | const found = profileFinding(f.app.db, box.id, key, read.summary); | |
| 31 | 33 | 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]), ...summarySections(read.summary).flatMap((x) => ["", `== ${x.title}`, ...sectionLines(x)])]; | |
| 34 | 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]), ...(found === undefined ? [] : ["", sparkFindingLine(found)]), ...summarySections(read.summary).flatMap((x) => ["", `== ${x.title}`, ...sectionLines(x)])]; | |
| 33 | 35 | }; |
| 34 | 36 | return { |
| 35 | 37 | title: `${box.name}: profile ${key}`, |
src/web/pages/games_spark_view.ts +5 −2
| @@ -6,7 +6,8 @@ | ||
| 6 | 6 | import type { Ctx } from "../../http/context.ts"; |
| 7 | 7 | import { READ_UNPACKED, decodeSparkProfile, type SparkSummary } from "../../games/spark_profile.ts"; |
| 8 | 8 | import { profileOf } from "../../games/spark.ts"; |
| 9 | import { FLAME_WIDTH, flameRects, heaviest, millis, sparkHeadLines, summarySections, treeLines } from "../../games/spark_view.ts"; | |
| 9 | import { FLAME_WIDTH, flameRects, heaviest, millis, sparkHeadLines, summarySections, tickId, treeLines } from "../../games/spark_view.ts"; | |
| 10 | import { profileFinding, sparkFindingLine } from "../../games/spark_finding.ts"; | |
| 10 | 11 | import { notFoundPage, pageHead } from "../chrome.ts"; |
| 11 | 12 | import { html, type Html } from "../html.ts"; |
| 12 | 13 | import { page } from "../layout.ts"; |
| @@ -45,6 +46,7 @@ export function gameSparkView(ctx: Ctx): Response { | ||
| 45 | 46 | const read = stored.type === SAMPLER ? decodeSparkProfile(stored.body, READ_UNPACKED) : undefined; |
| 46 | 47 | const href = `/games/${got.box.name}/@spark/${key}`; |
| 47 | 48 | const shown = read !== undefined && read.ok ? heaviest(read.summary.threads) : undefined; |
| 49 | const found = read !== undefined && read.ok ? profileFinding(ctx.app.db, got.box.id, key, read.summary) : undefined; | |
| 48 | 50 | const body: Html = read === undefined || !read.ok |
| 49 | 51 | ? html`<p role="alert">The forge draws a sampler profile only, and this one is not a readable one: download it.</p><p><a href="${href}">Download it</a> and open it in spark's own viewer.</p>` |
| 50 | 52 | : html`<p class="muted small">${sparkHeadLines(read.summary)[0]}. Times are milliseconds sampled, shares are of the thread; trees keep the heaviest calls. <a href="${href}">Download the profile</a>.</p> |
| @@ -55,7 +57,8 @@ export function gameSparkView(ctx: Ctx): Response { | ||
| 55 | 57 | <details ${i === 0 ? html`open` : null}><summary>Call tree</summary><pre>${treeLines(t.tree, t.ms).join("\n")}</pre></details>`} |
| 56 | 58 | </section>`)} |
| 57 | 59 | ${shown?.note ? html`<p class="muted small">${shown.note} <a href="${href}">Download it</a>.</p>` : null} |
| 58 | ${summarySections(read.summary).map((x) => html`<section class="settings-block" id="${x.id}"><h2>${x.title}</h2>${x.rows.length === 0 ? html`<p class="muted small">${x.empty}</p>` : rowList(x.rows.map((r) => row({ title: r.title, facts: r.facts })))}</section>`)}`; | |
| 60 | ${found === undefined ? null : html`<p class="small" id="finding">${sparkFindingLine(found)} <a href="#${tickId(found.tickWindow ?? 0)}">See the window</a>.</p>`} | |
| 61 | ${summarySections(read.summary).map((x) => html`<section class="settings-block" id="${x.id}"><h2>${x.title}</h2>${x.rows.length === 0 ? html`<p class="muted small">${x.empty}</p>` : rowList(x.rows.map((r) => row({ ...(r.id === undefined ? {} : { id: r.id }), title: r.title, facts: r.facts })))}</section>`)}`; | |
| 59 | 62 | return page({ |
| 60 | 63 | ctx, |
| 61 | 64 | title: `Games · ${got.box.name} · profile`, |
test/games-spark-view.test.ts +21
| @@ -154,3 +154,24 @@ test("the page lists the slowest ticks and time per mod, escaping a hostile mod | ||
| 154 | 154 | expect(body).not.toContain("<img src=x"); |
| 155 | 155 | expect(body).toContain("<img src=x"); |
| 156 | 156 | }); |
| 157 | ||
| 158 | test("each slowest-tick row has an id, and a profile's finding names a real one (#18265)", async () => { | |
| 159 | const { sparkFixture } = await import("./helpers/spark_fixture.ts"); | |
| 160 | const { profileFinding } = await import("../src/games/spark_finding.ts"); | |
| 161 | const box = boxNamed(app.db, "net")!; | |
| 162 | const k = keepProfile(app.db, box.id, "application/x-spark-sampler", sparkFixture()); | |
| 163 | if (!k.ok) throw new Error(k.error); | |
| 164 | const r = decodeSparkProfile(sparkFixture()); | |
| 165 | if (!r.ok) throw new Error(r.error.message); | |
| 166 | const body = await (await get(`/games/net/spark/${k.value}`, "bob")).text(); | |
| 167 | for (const t of r.summary.slowestTicks) expect(body).toContain(`id="tick-${String(t.window)}"`); | |
| 168 | const f = profileFinding(app.db, box.id, k.value, r.summary)!; | |
| 169 | expect(f.profileKey).toBe(k.value); | |
| 170 | expect(f.tickWindow).toBe(r.summary.slowestTicks.reduce((w, t) => (t.msptMax > w.msptMax ? t : w)).window); | |
| 171 | expect(body).toContain(`id="tick-${String(f.tickWindow)}"`); | |
| 172 | expect(body).toContain(`href="#tick-${String(f.tickWindow)}"`); | |
| 173 | // A key that is not this box's own profile gives no finding. | |
| 174 | createGameBox(app.db, { name: "other", repo: "alice/o", path: "o/docker-compose.yml" }); | |
| 175 | expect(profileFinding(app.db, boxNamed(app.db, "other")!.id, k.value, r.summary)).toBeUndefined(); | |
| 176 | expect(profileFinding(app.db, box.id, k.value, { ...r.summary, slowestTicks: [] })).toBeUndefined(); | |
| 177 | }); |
new above
offline