Profiling: recording a run and reading where its time and memory went
Recording a run
One call starts a recording at a level, one stops it:
profiler.startCapture("bench", { level = "comprehensive" })
engine.mode = "play" -- in the same call, so no edit-mode frame is recorded
-- ... play the benchmark: a camera path, a scripted fight, a load ...
profiler.stopCapture({ frames = false }) -- the header only; frames stay stored
A recording holds every frame from its start, whatever mode the editor is
in, so a play benchmark starts the recording and enters play in the same
execute. It records the frames of the client it runs in: a headless
engine and an agent's own sub-client present no frames and draw nothing, so
a benchmark of
what a player sees runs in the client that presents them (execute with
client = "user"), and its files are that client's
/zero/runtime/profiler/. The profiler toolbox's record tool takes the
same levels (record start bench comprehensive).
| Level | What it keeps |
|---|---|
frames (default) | Every frame: wall time, the engine thread's CPU time and its wait for a core, the engine's GPU time, the time of every profiler key (each schedule, system and script block, the Luau GC step, each VM's tick) and its counts (entities, draws, instances, Luau bytes allocated on the engine thread). |
memory | The frames, and a memory sample every memoryIntervalMs (default 100): the process's private and resident bytes, the allocator's live heap, every source memory.report() names (system and device: textures, meshes, geometry, shadow pool, compute, VFS store...) and each Luau VM's own heap, live and allocated. |
comprehensive | The memory level, every GPU frame the timer resolves with each pass label's time, every job the worker pools ran (asset decodes and imports, shader compiles, module loads, encodes: each with its pool, kind, worker, start and length), and the user VM's Luau call stacks sampled at stackHz (default 1000). |
Options: { level, memoryIntervalMs (>= 10), stackHz (1..10000), budgetBytes }.
The recording is one slot shared by every caller of the engine:
startCapture answers false while another recording holds it, and
profiler.recordingHold() names who. A recording holds at most budgetBytes
(32 MiB) of frame records, about 15 minutes at 60 fps of a busy scene; past
it the oldest frames leave first and the capture counts them in
dropped_for_budget. Each series holds at most 65536 samples, and the memory,
GPU and job series hold at most 32 MiB together: past it the series holding
the most bytes lets its oldest samples go and counts them (memory_dropped,
gpu_dropped, jobs_dropped), so a recording left open for an hour holds
at most 32 MiB of frames and 32 MiB of series, its newest span of each.
seconds stops a recording on its own at the first frame past that many
seconds, stored under its label as stopCapture stores it, so the span it
covers does not wait on the call that would stop it:
profiler.startCapture("bench", { level = "comprehensive", seconds = 10 })
engine.mode = "play"
task.wait(10.5) -- or poll profiler.isCapturing()
local info = profiler.captureInfo("bench")
What it costs
Every stopped recording states what recording its own frames cost them, as
info.recording_cost: { frames, mean_ms, max_ms, memory_samples, memory_sample_mean_ms, memory_off_frame }, measured on the engine thread
inside each frame. Measured on a native engine (a dev build, windowed, 400
dynamic physics boxes in play mode at 60 fps, 8 rounds of 2 s per level in
rotating order, on a host running other engines):
| Level | Engine-thread cost per frame (mean) | Beside the frames |
|---|---|---|
frames | 0.050 ms (0.3% of a 16.7 ms frame) | nothing |
memory | 0.058 ms (0.35%) | one memory sample per interval, 4 to 8 ms each, read on a thread of its own |
comprehensive | 0.080 ms (0.5%), the costliest round 0.12 ms | the memory samples, the stack sampler's thread, and one event kept per worker-pool job |
The engine thread's CPU time per frame (profiler.frameCost) over the same
rounds had a median of 7.86 ms recording nothing, 7.62 ms at frames, 8.19
ms at memory and 8.10 ms at comprehensive: every level inside the spread
of the rounds that recorded nothing (6.7 to 8.2 ms), and the frame rate held
at 60 in each. The first recording of an engine measured its costliest frame
at 0.17 to 0.22 ms on two later engines and at 57 ms once; recording_cost.max_ms
states it for every capture, so a capture whose max_ms stands far above its
mean_ms is the one to record again.
A browser engine runs no sampler thread, so its frames hand each memory
sample to the off-frame pool's worker and read none themselves. Measured on a
browser engine (30 fps, 3 rounds of 2 s per level), the
engine-thread cost per frame was 0.20 to 0.43 ms at frames, 0.21 to 0.32 ms
at memory and 0.21 to 0.47 ms at comprehensive, under 1.5% of its 33 ms
frame; each memory sample took 1.5 to 3.2 ms on the worker. Where the page
has no worker for the pool, the frames read the samples and the capture
states memory_off_frame = false.
Platforms
Native and Android record every level in full. A browser engine records the
frames, memory, GPU and job series and the engine's timed scopes; its Luau VM
runs on a worker with no sampler thread beside it, so a comprehensive
recording there states series.stacks.available = false with the reason,
and the scopes flame graph is the one that shows where its frames went. On
every platform the engine thread's own time reads as its timed scopes (the
scopes file), and the sampled stacks are the Luau VM's.
Reading it back
local info = profiler.captureInfo("bench") -- header: summary, hitches, series counts, export paths
local rows = profiler.captureFrames("bench", 1, 500) -- frames, a page at a time
local mem = profiler.captureSeries("bench", "memory", 1, 100) -- { total, items }
local gpu = profiler.captureSeries("bench", "gpu")
local jobs = profiler.captureSeries("bench", "jobs") -- the load and compile timeline
local hitches = profiler.captureSeries("bench", "hitches")
info.hitches states the median frame, the bar a hitch crossed (twice the
median and at least 8 ms above it) and each hitch's frame with the profiler
keys that took the most of it and, at comprehensive, the worker pools' jobs
that ran across it (jobs = {{ kind, ms }}), so a hitch that landed with a
shader compile or a texture decode names it. A job's kind is the name it
gave itself or the place in the engine's source that queued it, as
<file>:<line>: a kind under shader_compiles.rs is a shader compile, one
under model_import.rs a model import, one under texture_encode.rs a
texture encode. info.frame_cost is what the frames cost the engine itself
(CPU, wait for a core, GPU), the report profiler.frameCostReport gives.
Exporting: files for scripts, people and trace viewers
Every stored capture reads out as files of /zero/runtime/profiler/,
generated when they are read; info.exports names each file the capture
holds, and listing the directory names every stored capture's:
| Format | File | Opens in |
|---|---|---|
json | <label>.json | Anything: the whole capture (header, frames, series, hitches) |
csv | <label>.csv | A spreadsheet or pandas: one row per frame with its times, counts, memory, each schedule's time and each profiler count the frame tallied (physics.contacts, physics.constraints, ...) |
trace | <label>.trace.json | Perfetto (ui.perfetto.dev) and chrome://tracing: frames as spans; schedules, memory, Luau heaps and GPU passes as counter tracks; each pool worker's jobs as spans on a track of its own; hitches as markers |
stacks | <label>.stacks.folded | speedscope, inferno, flamegraph.pl: the sampled Luau stacks, frame;frame;... microseconds, root first (a capture that sampled them: comprehensive, on an engine with a sampler thread) |
scopes | <label>.scopes.folded | The same tools: the engine's timed scopes (schedule, system, script block), each weighed by its own time in microseconds |
stacksSvg | <label>.stacks.svg | A browser: the sampled Luau stacks drawn as a flame graph, each frame as wide as its time, its time and share in its tooltip |
scopesSvg | <label>.scopes.svg | A browser: the engine's timed scopes drawn the same way |
The scopes fold by the dots of their keys, each under the scope that holds
it: the physics step's stages sit under physics_step.pipeline (the whole
rapier step), the broad and narrow phase under collision_detection. The
profiler's counts (the physics step's contacts, contact pairs and
constraints, its workers) are counts, not time: each frame carries them as
tallies (captureFrames, the JSON's frames, a CSV column each, a
profiler counts track of the trace), and no flame graph or hitch reads them.
local where = profiler.exportCapture("bench", "trace") -- { path, format, bytes }
A file is read from outside the engine like any VFS file, so a long capture
never passes through a call's answer: the proxy's download_file with the
path saves it to disk, and GET /engine/vfs/zero/runtime/profiler/<file>
serves it over HTTP.
Measuring a benchmark the same way every time
Record the same span each run (a fixed camera path, a fixed number of
frames), at the same level, with the same window size, and compare runs by
their frame_cost and summary rather than by a single frame. A frame that
drew a render a caller asked for (an agent's capture) or ran a caller's
queued Luau is marked on the frame (capture, agent_ms) and left out of
frame_cost and of the hitches' effective time, so a session driving the
benchmark does not count against it.
A benchmark world's runner records each scenario in the same shape:
-- One scenario: warm up, record a fixed span, stop, and hand back the files.
local function runScenario(name: string, seconds: number)
task.wait(2) -- let loads and compiles settle first
if not profiler.startCapture(name, { level = "comprehensive" }) then
error("another recording holds the slot: " .. tostring(profiler.recordingHold()))
end
task.wait(seconds) -- the scenario plays: a camera path, a fight
profiler.stopCapture({ frames = false })
local info = assert(profiler.captureInfo(name))
return {
frames = info.frame_count,
frameCost = info.frame_cost, -- wall, cpu, gpu, waitingForCore: mean and percentiles
hitches = info.hitches.count,
cost = info.recording_cost, -- what recording took, to state beside the result
files = info.exports, -- csv, trace, scopesSvg, stacksSvg, scopes, stacks, json under /zero/runtime/profiler/
}
end
- Frame times for every frame: the
csvfile (one row per frame) orprofiler.captureFrames(name, first, count)a page at a time. - Memory over the run:
profiler.captureSeries(name, "memory"), or the memory columns of the CSV (the newest sample at each frame). - Where the time went: the
scopesSvgfile is the engine thread's flame graph by timed scope and thestacksSvgfile the Luau VM's sampled stacks, each an SVG a browser opens (scopesandstacksare the same folded, for speedscope orflamegraph.pl); thetracefile opens in Perfetto with every frame, the GPU passes, the memory tracks and each worker's jobs on one timeline. - What hitched and why:
info.hitchesandcaptureSeries(name, "hitches"), each hitch with the keys and the jobs (shader compiles, asset decodes) that ran in it.
A runner in the runtime profile (a published benchmark world played) records
the same way: the recording runs in the engine there too. Taking the files off
the machine is the caller's step (download_file with each path in
info.exports); a stored capture stays readable until the stored captures'
bound lets it go (profiler.storedCaptures()), so read the files once each
scenario stops.