Skip to content

Record profiles in CPU, GPU and memory tracks - #12

Merged
tomaszbk merged 8 commits into
mainfrom
profiler/tracks
Nov 15, 2026
Merged

tomaszbk merged 8 commits into
mainfrom
profiler/tracks

Conversation

@5hif7y

@5hif7y 5hif7y commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Closes #6.

Rebuilt on main 8c5a2a6 after the review; the comment below answers it point by point.

Changes

Record the memory of a profile, and show CPU, GPU and memory in tabs

  • platform.process_memory reads the resident and the private memory of
    the process (Windows private commit, Linux RssAnon+VmSwap, macOS
    physical footprint) without allocating. It replaces
    process_memory_bytes, one algorithm; the playtest QA reads the private
    memory with it.
  • core.world_memory: the bytes the tables and storages of a World hold
    behind their pointers, from the capacity of their arrays, maps and
    slices. It reads capacities, not an allocator, so what game code
    allocates outside its storages is not in it.
  • host: profile_start takes memory, off by default. A recording with it
    samples at the end of each frame, inside the frame as its part memory:
    the World that ran last (the game loop's, else the editor's), the
    process and the device memory.
  • editor: profile.capture takes memory; the summary and the frames gain
    an optional memory section, profile.export an MB counter.
  • The profiler panel shows the frame graph, then a CPU, a GPU and a
    Memory tab, as tall as the tallest.

Draw the memory tab as a band per series, from its lowest to its highest

One scale for the World, the private memory and the GPU left the World,
tens of MB beside thousands, flat at the bottom of the graph.

Read the memory of a World from container layouts compiled once

world_memory walked the types of every storage at each sample: about
3 ms on demo, most of it in the pools of physics and navigation, whose
slots hold arrays of their own. world_info_index now compiles, into
World_Schema.memory, where each table value, storage and the Entities
hold arrays, maps and slices, with the layout of the elements that hold
more; a sample reads only container headers. The bytes are those of the
walk by type, which a test checks on a World with nested pools.

Merge order

Merge after #10, which this branch contains: #10, then #12, then #13. Until #10 is in main, this diff shows #10's commit too; with merge commits it shrinks to this PR's own once #10 lands. All six PRs were merged together onto main with no conflict, and the steps of checks.yml pass on that tree (mirai check on 135 packages, 18 test suites without a GPU).

Not tested

🤖 Generated with Claude Code

@5hif7y

5hif7y commented Nov 9, 2026

Copy link
Copy Markdown
Member Author

Needs more testing as a user before merging.

The tests and the sessions listed in the description cover the engine side only. My lead and I will adapt it as needed, and tomorrow I will test it with a separate agent acting as a Mirai user.

@tomaszbk

Copy link
Copy Markdown
Member

Review: rework. This PR builds on code that main has since reshaped. The branch conflicts in many files, among them host/profile.odin, editor/api_profile.odin, tools/mirai/bench.odin and scripts/check_ui_style.py.

  1. The same algorithm exists twice. platform/memory_*.odin duplicates platform.process_memory_bytes (process_memory_*.odin), which main already has and the playtest plugin uses. This PR's private-memory reading is the better of the two. Keep it and replace the one on main, so that only one remains.
  2. Compatibility shim. Bench_Summary_Untracked (tools/mirai/bench.odin:69) stays "until main answers with tracks". The project keeps no backward compatibility: an incompatible change updates all its consumers in the same change.
    • Its release=true for the base is also superseded. On main, bench compare builds both sides the same way: dev by default, release only with --release (b6db65c).
  3. Memory is sampled every frame, and it is on by default. The sampling runs after the frame zone closes (host/profile.odin:240). Every frame it reads /proc or queries the driver. That time lands in between_ns, so it is added to every frame time.
  4. The api_profile.odin rewrite is built on the *_Out structs that main removed in 67b5aca.
  5. Unrelated or stale parts.

5hif7y and others added 4 commits November 14, 2026 15:08
A host frame can submit more than once (a capture, a thumbnail, a UI
texture besides the window), and the renderer kept only the timings of
the last submission the GPU finished: a recorded frame lost the passes of
its other submissions and was still counted as measured.

- rhi.frame_timings also returns the rhi frame those timings measured
  (Vulkan and Metal), and whether the slot had submitted one.
- render copies the timings of every submission into a list
  (renderer_gpu_submissions) that the host takes and clears each frame.
- host gives each submission to the recorded frame whose range of rhi
  frames holds it. A frame is measured once all of its submissions came
  back; a recording runs up to PROFILE_GPU_LATE frames past its last one,
  keeping no zones, until they do. Passes keep where they ran, on the
  clock of the zones.
- editor: the result structs of main gain gpu_measured, gpu_frames and
  the start_ms of a pass; GPU numbers leave out unmeasured frames.
  profile.export writes a GPU thread. The profiler panel draws passes
  where they ran and says when a frame was not measured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- platform.process_memory reads the resident and the private memory of
  the process (Windows private commit, Linux RssAnon+VmSwap, macOS
  physical footprint) without allocating. It replaces
  process_memory_bytes, one algorithm; the playtest QA reads the private
  memory with it.
- core.world_memory: the bytes the tables and storages of a World hold
  behind their pointers, from the capacity of their arrays, maps and
  slices. It reads capacities, not an allocator, so what game code
  allocates outside its storages is not in it.
- host: profile_start takes memory, off by default. A recording with it
  samples at the end of each frame, inside the frame as its part memory:
  the World that ran last (the game loop's, else the editor's), the
  process and the device memory.
- editor: profile.capture takes memory; the summary and the frames gain
  an optional memory section, profile.export an MB counter.
- The profiler panel shows the frame graph, then a CPU, a GPU and a
  Memory tab, as tall as the tallest.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One scale for the World, the private memory and the GPU left the World,
tens of MB beside thousands, flat at the bottom of the graph.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
world_memory walked the types of every storage at each sample: about
3 ms on demo, most of it in the pools of physics and navigation, whose
slots hold arrays of their own. world_info_index now compiles, into
World_Schema.memory, where each table value, storage and the Entities
hold arrays, maps and slices, with the layout of the elements that hold
more; a sample reads only container headers. The bytes are those of the
walk by type, which a test checks on a World with nested pools.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@5hif7y
5hif7y force-pushed the profiler/gpu-pass-frames branch from 3603803 to 5811c8d Compare November 15, 2026 01:24
@5hif7y
5hif7y changed the base branch from profiler/gpu-pass-frames to main November 15, 2026 01:24
@5hif7y

5hif7y commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

Rebuilt on top of the new #10, as 5f91b720, 7dcc65b5 and d26240e3, force-pushed to profiler/tracks. It merges into main cleanly.

  1. The same algorithm twice. One is left.
    • platform.process_memory reads resident and private memory without allocating: Windows private commit, Linux RssAnon+VmSwap, macOS physical footprint.
    • It replaces process_memory_bytes. process_memory_posix.odin is removed, and the playtest QA (plugins/playtest/editor/qa.odin) moves to it in the same change.
  2. Compatibility shim. Bench_Summary_Untracked and the release=true of the base are gone. bench uses the structs of main.
  3. Memory sampled every frame, on by default. Now off by default.
    • profile_start / profile.capture take memory.
    • With it, the sample runs inside the frame as its own zone, memory, so it no longer lands in between_ns.
    • Test: a_recording_samples_the_memory_only_when_asked_inside_the_frame.
  4. Built on the removed *_Out structs. Rebuilt on the result structs and call_result. The memory section is mirai:"optional", so it is omitted when memory was not asked for.
  5. Unrelated or stale parts. git_bash_find is gone; Find Git Bash through git --exec-path in mirai.cmd and the tool alike #11 handles it. The host.md text about worker rings is gone.

New: the game's memory.

  • core.world_memory reports the bytes the tables and storages of a World hold behind their pointers, from the capacities of their arrays, maps and slices.
  • world_info_index compiles where those containers sit once (World_Schema.memory), so a sample reads only container headers.
  • It reads capacities, not an allocator. A per-World counting allocator was ruled out: Worlds swap arrays (play history, rollback, scene.write), which would make it a use-after-free.
  • What game code allocates outside its storages is not counted.
  • Test: the_compiled_memory_ops_read_what_the_types_say checks that the compiled layout gives the same bytes as a walk by type, on a World with nested pools.

Cost of the memory zone (demo, 39 entities, 120 frames, warm session):

p50 p95 mean max
Edit 0.377 ms 0.612 ms 0.572 ms 16.4 ms
Play 0.376 ms 0.833 ms 0.582 ms 13.1 ms
  • Before the compiled layout, a sample took ~3 ms. The process and GPU reads take ~0.02 ms of it.
  • The bytes match the walk by type: 37.05 MB in Edit, 37.85 MB in Play.
  • The cost is O(fixed pool slots): about 6,400 headers, mostly the 1024-slot pools of physics and navigation.
  • Open: the single 13–16 ms max shows up mid-run, at frames 58, 77, 103 and 116. It sits in the walk, which takes no lock, does not allocate and makes no system call. In frame 103, render spiked too. 1,320 more samples stayed under 0.68 ms, so the cause is not confirmed: the thread being pre-empted, or page faults on pool memory touched rarely.

Panel: CPU, GPU and Memory tabs. Memory draws one band per series, because the World's tens of MB sat flat next to the process's thousands on one scale.

Not tested: bench compare --base main (a cold main does not open on this machine, see #10), Linux and macOS (type-checked only).

@5hif7y
5hif7y requested a review from tomaszbk November 15, 2026 01:26
@5hif7y 5hif7y closed this Nov 15, 2026
@5hif7y 5hif7y reopened this Nov 15, 2026
@5hif7y

5hif7y commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

Follow-up: the 13–16 ms max of the memory zone is not caused by the walk. Run on all six PRs merged onto main 8c5a2a6, demo, a headless --copy session: 1,200 frames with memory:true, exported, every main-thread zone compared.

Zone p50 max Samples > 5×p50 and > 3 ms
memory 0.375 ms 12.98 ms 3
editor_panels 0.485 ms 34.09 ms 12
editor_end 0.625 ms 39.20 ms 9
editor 1.584 ms 59.47 ms 17
render 1.876 ms 62.90 ms 12
  • Every main-thread zone has the same kind of outliers, at the same rate or a higher one.
  • The slow memory samples contain no nested zone.
  • So they are stalls of the thread itself, on a 4-core machine with 3 job workers, not a cost of the walk.
  • The memory zone's p99 is 0.88 ms. I am closing the open item.

@5hif7y

5hif7y commented Nov 15, 2026

Copy link
Copy Markdown
Member Author

Follow-up: bench compare now runs on this machine (with #84), and this is what it measures.

Setup:

Run frame p50 Δ Δ per pair GPU Δ Δ per pair CPU main Δ
head vs base, 1 +0.056 ms (+1.2 %) +0.055 to +0.095 +0.050 ms +0.045 to +0.080 −0.062 (pairs disagree)
head vs base, 2 +0.055 ms (+1.2 %) +0.040 to +0.068 +0.053 ms +0.038 to +0.068 +0.024 (pairs disagree)
base vs base +0.024 ms (+0.5 %) −0.425 to +0.027 +0.023 ms −0.126 to +0.035 −0.020 (pairs disagree)

How I read it:

tomaszbk and others added 4 commits November 15, 2026 09:47
… ones a failed begin collected

- rhi: a slot with no query pool, or whose times the device could not read,
  is not measured (Vulkan and Metal), instead of measured with no passes.
- render keeps the timings a frame_begin collected before it failed, once per
  rhi frame.
- host waits for the GPU times of any recorded frame still missing them, not
  only of the last one.
- The GPU test of the profile has its own file, so --no-gpu runs the others.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…hemselves

Each element type is compiled to memory ops once, so a type that holds
itself reuses its own range instead of recursing without end; a map reads
what its values hold. The ops are private to core and freed with the
allocator that made them. The process memory test checks that private
memory holds what it wrote, whatever other tests do meanwhile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The tabs keep the height of the tallest by stacking the drawings of all of
them, and the memory labels take the width of their text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rofiler/tracks

# Conflicts:
#	host/profile_test.odin
@tomaszbk
tomaszbk merged commit b33d9a2 into main Nov 15, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Split the profiler into CPU, GPU and memory tracks [Related to #5]

2 participants