Public: anyone can read this; the crew can change it.

A spark profile decodes to a summary: call tree, slowest ticks, per-mod time, GC and memory #18144

opened this

Merged 8h 15m ago, live in production

This adds src/games/sparkprofile.ts, a pure decoder from a gzipped spark profile to a summary (#18139, part of #18058). I could not check spark's field numbers against the real protos, so please do that before this lands.

Notes

opened this

This adds src/games/spark_profile.ts, a pure decoder from a gzipped spark profile to a summary (#18139, part of #18058). I could not check spark's field numbers against the real protos, so please do that before this lands.

What it does: decodeSparkProfile(bytes) returns {ok: true, summary} or {ok: false, error: {name, message}}; it never throws. The summary holds:

  • Call tree: each thread's tree, cut to 12 deep and the 8 slowest children at each level.
  • Slowest ticks: the 10 slowest time windows by max mspt.
  • Time per mod: each node's self time (its time less its children's), added to the mod or plugin that owns its class.
  • GC and memory: per-collector GC (gc) and heap, non-heap and physical memory.
  • Platform and mods: the platform name and versions, and the mod list.

Errors: too-large (body over 32 MB, or expands past 256 MB: checked while inflating, so a zip bomb is refused rather than inflated), not-gzip, truncated (gzip or protobuf cut short) and malformed (bad wire type, non-UTF-8 text, a node refers to one that isn't there, nesting deeper than 256, over 2M nodes).

No dependency. It uses node:zlib and a small protobuf wire reader of about 60 lines.

What I couldn't check: the sandbox can't reach spark's repository, so the field numbers are written from memory into one table, F, at the top of the file. Please check them against spark_sampler.proto and spark.proto; if one is wrong, F is the only place to fix.

Tests: test/games-spark-profile.test.ts builds a small profile with a protobuf writer in the test, so the fixture is only as right as F is. It checks the decoded summary, a truncated gzip refused as truncated, a protobuf cut mid-field, a bad wire type, non-gzip and empty bodies, an oversized body, a 300 MB bomb, and an empty profile. The new tests, the dead-code test and tsc pass; bun scripts/gate.ts exits 0 and still prints a similarity warning between threadTree and its inner build.

What I chose not to do: there is no caller yet. The store, the viewer page and the terminal and docs doors are the later steps of #18058, so the export is listed in scripts/unused-exports-allow.json until the store calls it. I added no page or route, so no docs/features change was needed. I reused nothing from the repo; the nearest protobuf-style readers (parquet.ts, db_size.ts) read different formats.

Try it: bun test test/games-spark-profile.test.ts

Closes #18139

6 earlier comments · 1 event
commented

This adds src/games/spark_profile.ts, a pure decoder from a gzipped spark profile to a summary (#18139, part of #18058). I could not check spark's field numbers against the real protos, so please do that before this lands.

What it does: decodeSparkProfile(bytes) returns {ok: true, summary} or {ok: false, error: {name, message}}; it never throws. The summary holds:

  • Call tree: each thread's tree, cut to 12 deep and the 8 slowest children at each level.
  • Slowest ticks: the 10 slowest time windows by max mspt.
  • Time per mod: each node's self time (its time less its children's), added to the mod or plugin that owns its class.
  • GC and memory: per-collector GC (gc) and heap, non-heap and physical memory.
  • Platform and mods: the platform name and versions, and the mod list.

Errors: too-large (body over 32 MB, or expands past 256 MB: checked while inflating, so a zip bomb is refused rather than inflated), not-gzip, truncated (gzip or protobuf cut short) and malformed (bad wire type, non-UTF-8 text, a node refers to one that isn't there, nesting deeper than 256, over 2M nodes).

No dependency. It uses node:zlib and a small protobuf wire reader of about 60 lines.

What I couldn't check: the sandbox can't reach spark's repository, so the field numbers are written from memory into one table, F, at the top of the file. Please check them against spark_sampler.proto and spark.proto; if one is wrong, F is the only place to fix.

Tests: test/games-spark-profile.test.ts builds a small profile with a protobuf writer in the test, so the fixture is only as right as F is. It checks the decoded summary, a truncated gzip refused as truncated, a protobuf cut mid-field, a bad wire type, non-gzip and empty bodies, an oversized body, a 300 MB bomb, and an empty profile. The new tests, the dead-code test and tsc pass; bun scripts/gate.ts exits 0 and still prints a similarity warning between threadTree and its inner build.

What I chose not to do: there is no caller yet. The store, the viewer page and the terminal and docs doors are the later steps of #18058, so the export is listed in scripts/unused-exports-allow.json until the store calls it. I added no page or route, so no docs/features change was needed. I reused nothing from the repo; the nearest protobuf-style readers (parquet.ts, db_size.ts) read different formats.

Try it: bun test test/games-spark-profile.test.ts

commented

I merged main into the branch and pushed it as e3ef041f0. The only conflict was in scripts/unused-exports-allow.json.

Both sides had appended to the end of that list, and I kept both meanings:

  • Kept: the src/maintain/lockfiles.ts lockfileDependencies entry, which was on both sides, and this pull's src/games/spark_profile.ts decodeSparkProfile entry.
  • Dropped: src/core/grants_view.ts everyGrant. main had removed it because src/actions/grants_view.ts now imports everyGrant, so it is no longer unused.

The file still parses as JSON, and bun x tsc --noEmit is clean. The three spark test files pass (12 tests, 1 skipped, 0 failed). I did not run the full suite or scripts/gate.ts; the forge runs the full check on this push.

commented

The check failed because src/games/spark_profile.ts is imported by nothing under src/, and test/unreached.test.ts refuses that unless the file is on the allow list. I added it to scripts/unreached-allow.json and pushed as f32214477.

test/unreached.test.ts now passes (4 tests), but I did not re-run the full suite or scripts/gate.ts; the forge's check does that on this push.

I allow-listed the decoder instead of wiring it into the app because it is one step of #18058, with no page or route calling it yet. The test says a listed file that later becomes reached has to leave the list, so whoever adds the caller must remove this entry. The same applies to the src/games/spark_profile.ts decodeSparkProfile entry in scripts/unused-exports-allow.json. If you would rather this pull include a caller, say so and I will add one.

commented

This pull adds src/games/spark_profile.ts, a decoder that turns a spark profile body into a summary. The summary holds the call tree, the slowest ticks, per-mod time, and GC and memory. A truncated or oversized body is refused by name. test/games-spark-profile.test.ts covers it, and I didn't change either file this turn.

Nothing new was pushed this turn: the branch was already up to date with origin. The last push already carries the commit that allows the decoder in the dead-code lists.

For the four things the check flagged:

  • Dead-code check: the decoder has no caller yet, because its page and route are the next step of #18058. It is allowed in scripts/unused-exports-allow.json and scripts/unreached-allow.json, and I don't intend to add a caller in this pull. The unused-exports test passes. It does print a "no longer dead" note, but only for other files, none of them mine.
  • Both doors: bun scripts/gate.ts --explain passes this rule. No page, action, API route or MCP tool changes here.
  • Name the issue: this rule passes too, and the commits name #18139.
  • Process-wide state: test/module-state.test.ts passes. The only module-level array is PLATFORM_TYPES, which is never written to.

The gate has one note, which I left: threadTree and build in the decoder are about 0.58 alike, and the gate asks whether they could be one function. They walk different structures, so I kept them separate.

Checks run:

  • The gate, bun x tsc --noEmit, and the four test files that cover this change all pass.
  • I did not run the full suite, since the forge runs that on push.

To try it: bun test test/games-spark-profile.test.ts.

commented

This pull adds src/games/spark_profile.ts, which decodes a gzipped spark profile to a summary. The summary holds the call tree, the slowest ticks, per-mod time, GC and memory, the platform and the mod list. A body that isn't a profile comes back as a named error and never throws. This push fixes sentinel's findings 1 and 2 and lowers the unpack cap for finding 3. Finding 3 is only partly met: the spark field numbers in F are still unchecked.

Shared-node blow-up (finding 1)

  • addSelf now visits each shared node once, so a node's self time reaches perMod once.
  • prune takes a budget of 2,000 emitted nodes per thread. Shared nodes are counted each time they are shown, so output size no longer depends on how nodes refer to each other.
  • A new test builds 24 levels of diamonds, each node referring to the next level four times over. It finishes in under a second, reports each node's time once in perMod, and stays within the node budget.

Roots when a thread has no refs (finding 2)

  • The roots are now the flat nodes that no other node refers to, including references from nested children. Previously every flat node was a root, so referenced nodes were counted twice.
  • A new test covers this: one root with one referenced child gives one root, and the child's time counts once.

Unpack cap (finding 3)

  • MAX_UNPACKED is now 64 MB, down from 256 MB, and the code comment says why. The 300 MB bomb test still returns too-large.
  • Field numbers, not done: the numbers in F are still written from memory, and my sandbox can't reach spark's proto files. I'm leaving that to a person: check F against spark's spark.proto and spark_sampler.proto at a named commit. The test fixture is built from the same numbers, so it can't catch a wrong one.

Not done: there is still no caller, page or terminal door, so the decoder stays in scripts/unused-exports-allow.json and scripts/unreached-allow.json until the next step of #18058. The threadTree/build similarity note from the gate is also still open.

Checks: bun x tsc --noEmit is clean, the gate has no failures, and the spark-profile, unused-exports and module-state tests pass. To try it, run bun test test/games-spark-profile.test.ts.

commented

I merged main into the branch and pushed it (e8177ffd5).

  • Conflict: scripts/unused-exports-allow.json was the only one. Both sides had appended an entry to the end of the list, so I kept both: src/games/spark_profile.ts decodeSparkProfile and src/trace/import.ts readImported. The JSON still parses.
  • Checks: bun x tsc --noEmit is clean. The spark tests pass (20 tests across 4 files, 1 skipped). I ran bun scripts/gate.ts --fix and it found nothing to rewrap. I did not run the full suite; the forge runs it on this push.
  • Not changed: the pull's content is the same as before the merge. The description I gave earlier still applies.
the crew's run was interrupted: restart, resumes on the next pass — cancelled
commented

This adds src/games/spark_profile.ts, which turns a gzipped spark profile into a summary for #18139 (part of #18058). The summary holds the call tree, the slowest ticks, time per mod, GC and memory, the platform and the mod list. A body that isn't a profile comes back as a named error and the decoder never throws. Nothing calls it yet. The page, the terminal screen and the how-to are the next steps of #18058, so for now it is listed in scripts/unused-exports-allow.json and scripts/unreached-allow.json.

Field numbers, checked against spark's protos. The numbers in F come from lucko/spark at commit 3e8745a76eaabe9a4f70aad6283876fb518d2ae8, files spark-common/src/main/proto/spark/spark.proto and spark_sampler.proto. My sandbox can't reach GitHub, so I read two copies of those files bundled in npm packages. dsh-spark-insights is pinned to that commit, and spark-profiler-mcp 0.1.1 is a separate copy. They agree on every number the decoder reads.

  • Thread fields: ThreadNode is name = 1, reserved 2 (the old time), children = 3, times = 4, children_refs = 5. Your list had times: 2 and children_refs: 4; in these files 2 is the reserved data version 1 field. The table had times and children swapped before, which broke every version 2 profile. Commit 9841f5b80 fixed that.
  • Time windows: in SamplerData, time_windows = 6 is the bare window ids and time_window_statistics = 7 is the map of per-window stats. The decoder reads 7 on purpose, and the comment above F now says so.
  • Old profiles: a node's nested children (field 2) and the single time fields are data version 1. They are read as legacy, so older profiles still decode.

Tests. test/games-spark-profile.test.ts includes SPARK_V2, a profile encoded by protobufjs straight from spark's .proto files rather than from F, so a wrong number fails the test. It is not a capture from a real spark server, though. My sandbox reaches only the forge and the package registry, and neither package ships one. Please attach a real .sparkprofile and I'll add it as a fixture. Other tests cover truncated and non-gzip input, an oversized body, a 300 MB zip bomb, and a profile where nodes share children.

Checks: bun scripts/gate.ts --explain passes all 33 rules and bun x tsc --noEmit is clean. The spark-profile, unused-exports, module-state and unreached tests pass (20 tests). This push only changed a comment, so I didn't run the 158 files scripts/fast.ts named; the forge's check runs them.

To try it: bun test test/games-spark-profile.test.ts.

🤖 Generated with Claude Code

Sign in to join this conversation.

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

Keyboard shortcuts

Docs