Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .agents/skills/mirai-agent-api/references/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ Reading a capture:
per second, the mean interval between two and its deviation (how even the pace is) and the
latency from the start of a frame to its image. With generation there are twice as many images as
frames.
- **The GPU of a frame.** A frame's `gpu_ms` and passes are those of its own submissions, at the
time they ran (`start_ms` since the frame began on the CPU). A frame whose submissions did not all
come back says `gpu_measured:false`, and the GPU numbers of the summary and of `profile.compare`
leave it out (`gpu_frames`).
- **Own time.** `self_ms` excludes the zones inside: a function that waits for its tasks has them
as children (`job`, `range`); what the workers run appears with `thread:"workers"`.
- **Conditions.** `notes` warns about what invalidates a reading: engine at `-o:minimal`,
Expand Down
15 changes: 9 additions & 6 deletions .agents/skills/mirai-core/references/host.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,15 @@ entities or persistent data.
always covered, like the editor bars, is not drawn.

**Profiling (`profile.odin`).** `profile_start(app, frames)` records the following frames: zones of
the main thread, one per loop function, the tasks of each worker and what the renderer
measures. The first `profile_start` reserves the memory: recording does
not allocate, without recording a zone costs one branch and what does not fit is counted in `lost`.
Whoever reads the zones as they happen (a profiling plugin) calls `profile_request` in every frame
and reads them with its own cursor; without a recording or a request, the device writes no
per-pass timestamps. The editor exposes it with
the main thread, one per loop function, the tasks of each worker, what the renderer measures and the
GPU passes of every submission of the frame (a capture or a thumbnail besides the window). Those
come back `rhi.MAX_FRAMES_IN_FLIGHT` submissions later, by `rhi` frame number, and go to the frame
that submitted them; a frame is measured once all of its submissions came back, and a recording runs
a few frames past its last one, keeping no zones, until they do. The first `profile_start` reserves
the memory: recording does not allocate, without recording a zone costs one branch and what does not
fit is counted in `lost`. Whoever reads the zones as they happen (a profiling plugin) calls
`profile_request` in every frame and reads them with its own cursor; without a recording or a
request, the device writes no per-pass timestamps. The editor exposes it with
[`profile.*`](../../mirai-agent-api/references/methods.md#profiling).

**Loads (`load_profile.odin`).** `app.load_profile` keeps the last load of each kind (scene, Play,
Expand Down
3 changes: 3 additions & 0 deletions .agents/skills/mirai-rendering/references/frame-graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ or interleave its own passes.
of `tick_now` (QPC on Windows, `CLOCK_MONOTONIC_RAW` on Linux). Without the extension, Vulkan
places the last timestamp of the frame at the moment it collects it: it keeps the durations but
arrives late;
- `renderer_gpu_submissions`: a copy of the timings of each submission since
`renderer_gpu_clear`, with the `rhi` frame it was (`rhi.frame_timings`), for whoever accounts for
every submission of a host frame; `Stats.timings` is only the last one the GPU finished;
- visible, culled and occluded instances, probes and estimated rays per frame;
- what was discarded by capacity ([UI limits](ui.md#capacities));
- materials, textures, their memory, reduced textures and compression
Expand Down
41 changes: 32 additions & 9 deletions editor/api_profile.odin
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ Api_Profile_Frame_Stats :: struct {
wait_ms:f32 `mirai:"label=Mean wait for a frame slot of the GPU"`,
pacing_ms:f32 `mirai:"label=Mean wait for the frame limit or VSync"`,
between_ms:f32 `mirai:"label=Mean from the end of a frame to the start of the next"`,
gpu_ms:f32 `mirai:"label=Mean of the GPU"`,
gpu_ms:f32 `mirai:"label=Mean of the GPU over the frames whose GPU times came back"`,
gpu_frames:int `mirai:"label=Frames whose every submission gave back its GPU times: gpu_ms and gpu count only them"`,
bound:Api_Profile_Bounds `mirai:"label=Frames by what they waited for"`,
spikes:int `mirai:"label=Frames over twice the median"`,
worst:[]int `mirai:"label=The slowest frames by index; the slowest first"`,
Expand Down Expand Up @@ -129,6 +130,7 @@ Api_Profile_Tree :: struct {
}
Api_Profile_Pass :: struct {
name:string,
start_ms:f32 `mirai:"label=When it ran on the GPU, since the frame began on the CPU"`,
ms:f32,
}
Api_Profile_Frame :: struct {
Expand All @@ -138,7 +140,8 @@ Api_Profile_Frame :: struct {
wait_ms:f32 `mirai:"label=For a frame slot of the GPU"`,
pacing_ms:f32 `mirai:"label=For the frame limit or VSync"`,
between_ms:f32 `mirai:"label=From its end to the start of the next"`,
gpu_ms:f32 `mirai:"label=Of the last frame the GPU finished"`,
gpu_ms:f32 `mirai:"label=Of its own submissions"`,
gpu_measured:bool `mirai:"label=Every submission of the frame gave back its GPU times; without them gpu_ms and gpu are partial"`,
bound:Profile_Bound `mirai:"label=What it waited for"`,
workers_busy_ms:f32,
jobs:int,
Expand Down Expand Up @@ -303,6 +306,8 @@ api_profile_capture :: proc(s:^Session,call:^api.Call) {
return
}
api_frames(s,p.frames,p.dt)
// The GPU times of the last frames come back in the frames after them.
for late:=0;s.app.profiler.recording && late<host.PROFILE_GPU_LATE;late+=1 {api_frames(s,1,p.dt)}
host.profile_stop(s.app)
profile_collect(s)
api.call_result(call,Api_Profile_Summary)^=profile_summary(c,p.top)
Expand Down Expand Up @@ -360,10 +365,10 @@ profile_summary :: proc(c:^Profile_Capture,top:int) -> (out:Api_Profile_Summary)
out.notes=notes
return
}
totals,cpu,wait,pacing,between,gpu:=make([]f64,n),make([]f64,n),make([]f64,n),make([]f64,n),make([]f64,n),make([]f64,n)
totals,cpu,wait,pacing,between:=make([]f64,n),make([]f64,n),make([]f64,n),make([]f64,n),make([]f64,n)
for f,i in c.frames {
totals[i],cpu[i],wait[i]=profile_frame_ms(f),profile_frame_cpu_ms(f),f64(f.wait_ms)
pacing[i],between[i],gpu[i]=f64(f.pacing_ns)/1e6,f64(f.between_ns)/1e6,f64(f.gpu_ms)
pacing[i],between[i]=f64(f.pacing_ns)/1e6,f64(f.between_ns)/1e6
switch profile_frame_bound(f) {
case .Cpu: out.frame.bound.cpu+=1
case .Gpu: out.frame.bound.gpu+=1
Expand All @@ -377,7 +382,8 @@ profile_summary :: proc(c:^Profile_Capture,top:int) -> (out:Api_Profile_Summary)
if spread.mean>0 {out.frame.fps=profile_ms(1000/spread.mean)}
out.frame.cpu_ms,out.frame.wait_ms=profile_ms(profile_spread(cpu).mean),profile_ms(profile_spread(wait).mean)
out.frame.pacing_ms,out.frame.between_ms=profile_ms(profile_spread(pacing).mean),profile_ms(profile_spread(between).mean)
out.frame.gpu_ms=profile_ms(profile_spread(gpu).mean)
gpu:=profile_gpu_ms(c)
out.frame.gpu_ms,out.frame.gpu_frames=profile_ms(profile_spread(gpu).mean),len(gpu)
for t in totals {if t>2*spread.p50 {out.frame.spikes+=1}}
out.frame.worst=profile_worst(c,min(3,n))
out.presentation=profile_presentation(c,spread.mean*f64(n))
Expand Down Expand Up @@ -527,6 +533,9 @@ profile_notes :: proc(c:^Profile_Capture,out:Api_Profile_Summary) -> []string {
append(&notes,fmt.tprintf("%d of %d frames wait for the GPU (wait_ms): CPU savings will not shorten them; the gpu passes say where the GPU goes",out.frame.bound.gpu,out.frame.count))
if k.vsync {append(&notes,"vsync is on: the wait for a frame slot includes the display; with VSync off (settings.set) it is only the GPU")}
}
drawn:=0
for f in c.frames {if f.drawn {drawn+=1}}
if k.backend!="" && out.frame.gpu_frames<drawn {append(&notes,fmt.tprintf("%d of %d drawn frames lack GPU times (a submission did not come back): gpu_ms and gpu leave them out",drawn-out.frame.gpu_frames,drawn))}
if len(c.pass_names)==1 && c.pass_names[0]=="frame" {append(&notes,"The GPU of this backend is measured per frame, not per pass: gpu has the whole frame only")}
if out.frame.spikes>0 {append(&notes,fmt.tprintf("%d frames took more than twice the median: profile.frames shows the worst with their zones",out.frame.spikes))}
if c.lost>0 {append(&notes,fmt.tprintf("%d zones were lost: record fewer frames for an exact count",c.lost))}
Expand Down Expand Up @@ -562,6 +571,7 @@ profile_frame_out :: proc(c:^Profile_Capture,index,depth:int,min_ms:f64) -> (out
out.index=index
out.ms,out.cpu_ms,out.wait_ms=profile_ms(profile_frame_ms(f)),profile_ms(profile_frame_cpu_ms(f)),profile_ms(f64(f.wait_ms))
out.pacing_ms,out.between_ms,out.gpu_ms=profile_ms(f64(f.pacing_ns)/1e6),profile_ms(f64(f.between_ns)/1e6),profile_ms(f64(f.gpu_ms))
out.gpu_measured=f.gpu_measured
out.bound=profile_frame_bound(f)
out.workers_busy_ms,out.jobs=profile_ms(f64(f.worker_ns)/1e6),f.jobs
zones:=make([dynamic]Api_Profile_Tree)
Expand All @@ -572,7 +582,7 @@ profile_frame_out :: proc(c:^Profile_Capture,index,depth:int,min_ms:f64) -> (out
}
out.zones=zones[:]
passes:=make([dynamic]Api_Profile_Pass)
for pass in c.passes[f.pass_first:][:f.pass_count] {append(&passes,Api_Profile_Pass{c.pass_names[pass.name],profile_ms(f64(pass.ms))})}
for pass in c.passes[f.pass_first:][:f.pass_count] {append(&passes,Api_Profile_Pass{c.pass_names[pass.name],profile_ms(f64(pass.start_ns-f.start_ns)/1e6),profile_ms(f64(pass.ns)/1e6)})}
slice.sort_by(passes[:],proc(a,b:Api_Profile_Pass) -> bool {return a.ms>b.ms})
out.gpu=passes[:min(len(passes),8)]
return
Expand Down Expand Up @@ -688,7 +698,7 @@ profile_compare :: proc(a,b:^Profile_Capture,top:int) -> (out:Api_Profile_Compar
fa,fb:=profile_spread(frame_values(a,0)),profile_spread(frame_values(b,0))
out.mean_ms,out.p95_ms=delta(fa.mean,fb.mean),delta(fa.p95,fb.p95)
out.cpu_ms=delta(profile_spread(frame_values(a,1)).mean,profile_spread(frame_values(b,1)).mean)
out.gpu_ms=delta(profile_spread(frame_values(a,2)).mean,profile_spread(frame_values(b,2)).mean)
out.gpu_ms=delta(profile_spread(profile_gpu_ms(a)).mean,profile_spread(profile_gpu_ms(b)).mean)

ma,mb:=means(a),means(b)
changes:=make([dynamic]Api_Profile_Change)
Expand Down Expand Up @@ -735,7 +745,6 @@ frame_values :: proc(c:^Profile_Capture,which:int) -> []f64 {
switch which {
case 0: values[i]=profile_frame_ms(f)
case 1: values[i]=profile_frame_cpu_ms(f)
case 2: values[i]=f64(f.gpu_ms)
}
}
return values
Expand Down Expand Up @@ -811,7 +820,8 @@ api_profile_export :: proc(s:^Session,call:^api.Call) {
}

// The capture in the Trace Event Format: a complete event per zone, a thread
// per ring, and counters with the time of each frame and of the GPU.
// per ring, one per GPU pass where it ran, and counters with the time of
// each frame and of the GPU.
profile_trace :: proc(c:^Profile_Capture) -> (text:string,events:int) {
b:=strings.builder_make(context.temp_allocator)
strings.write_string(&b,`{"displayTimeUnit":"ms","traceEvents":[`)
Expand All @@ -826,6 +836,19 @@ profile_trace :: proc(c:^Profile_Capture) -> (text:string,events:int) {
fmt.sbprintf(&b,`{{"name":"thread_name","ph":"M","pid":1,"tid":%d,"args":{{"name":"%s"}}}}`,thread,thread==0?"main":fmt.tprintf("worker %d",thread))
events+=1
}
gpu_thread:=max(c.threads,1)
if len(c.passes)>0 {
event(&b,&first)
fmt.sbprintf(&b,`{{"name":"thread_name","ph":"M","pid":1,"tid":%d,"args":{{"name":"GPU"}}}}`,gpu_thread)
events+=1
}
for pass in c.passes {
event(&b,&first)
strings.write_string(&b,`{"name":`)
strings.write_string(&b,serial.text_quote(c.pass_names[pass.name]))
fmt.sbprintf(&b,`,"ph":"X","pid":1,"tid":%d,"ts":%.3f,"dur":%.3f}}`,gpu_thread,f64(pass.start_ns)/1e3,f64(pass.ns)/1e3)
events+=1
}
for f,i in c.frames {
event(&b,&first)
fmt.sbprintf(&b,`{{"name":"frame %d","ph":"X","pid":1,"tid":0,"ts":%.3f,"dur":%.3f}}`,i,f64(f.start_ns)/1e3,f64(f.ns)/1e3)
Expand Down
36 changes: 28 additions & 8 deletions editor/profile.odin
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ Profile_Frame_Record :: struct {
start_ns,ns:i64, // the frame zone, since the first frame began
between_ns:i64, // from its end to the start of the next one: what runs between frames
pacing_ns:i64,
wait_ms,gpu_ms:f32, // the wait for a frame slot, and the last frame the GPU finished
wait_ms,gpu_ms:f32, // the wait for a frame slot, and the sum of its own GPU passes
gpu_measured:bool, // every submission of the frame gave back its passes
render_prepare_ms,render_record_ms,render_end_ms:f32,
drawn:bool,
generated:bool, // it drew an image shown before its own
Expand All @@ -79,7 +80,10 @@ Profile_Zone_Record :: struct {
start_ns,ns,self_ns:i64,
}

Profile_Pass_Record :: struct {name:int,ms:f32}
Profile_Pass_Record :: struct {
name:int,
start_ns,ns:i64, // where it ran on the GPU, on the clock of the zones
}

Profile_Capture :: struct {
arena:vmem.Arena,
Expand Down Expand Up @@ -280,14 +284,14 @@ profile_build :: proc(c:^Profile_Capture,recording:host.Profile_Recording,app:^h
append(&pass_names,strings.clone(host.profile_pass_name(app,pass.name)))
pass_ids[pass.name]=index
}
c.passes[i]={index,pass.ms}
c.passes[i]={index,pass.start_ns-epoch,max(pass.end_ns-pass.start_ns,0)}
}
c.pass_names=pass_names[:]
for frame,i in recording.frames {
// The size the scene was drawn at is known once a frame drew.
if c.conditions.render=={} && frame.render_width>0 {c.conditions.render={int(frame.render_width),int(frame.render_height)}}
record:=&c.frames[i]
record.wait_ms,record.gpu_ms=frame.render_wait_ms,frame.gpu_ms
record.wait_ms,record.gpu_ms,record.gpu_measured=frame.render_wait_ms,frame.gpu_ms,host.profile_gpu_measured(frame)
record.render_prepare_ms,record.render_record_ms,record.render_end_ms=frame.render_prepare_ms,frame.render_record_ms,frame.render_end_ms
record.drawn=frame.drawn
record.generated,record.presented,record.intervals=frame.generated,int(frame.presented),int(frame.intervals)
Expand Down Expand Up @@ -439,13 +443,29 @@ profile_is_part :: proc(name:string) -> bool {
return false
}

// The per frame time of each pass of the GPU.
// The time of each pass of the GPU in each frame whose GPU times came back.
profile_passes_by_name :: proc(c:^Profile_Capture,allocator:=context.temp_allocator) -> [][]f64 {
context.allocator=allocator
measured:=profile_gpu_frames(c)
values:=make([][]f64,len(c.pass_names))
for &v in values {v=make([]f64,len(c.frames))}
for frame,i in c.frames {
for pass in c.passes[frame.pass_first:][:frame.pass_count] {values[pass.name][i]+=f64(pass.ms)}
for &v in values {v=make([]f64,measured)}
i:=0
for frame in c.frames {
if !frame.gpu_measured {continue}
for pass in c.passes[frame.pass_first:][:frame.pass_count] {values[pass.name][i]+=f64(pass.ns)/1e6}
i+=1
}
return values
}

profile_gpu_frames :: proc(c:^Profile_Capture) -> (measured:int) {
for frame in c.frames {if frame.gpu_measured {measured+=1}}
return
}

// The GPU time of each frame whose GPU times came back.
profile_gpu_ms :: proc(c:^Profile_Capture,allocator:=context.temp_allocator) -> []f64 {
values:=make([dynamic]f64,0,len(c.frames),allocator)
for frame in c.frames {if frame.gpu_measured {append(&values,f64(frame.gpu_ms))}}
return values[:]
}
7 changes: 5 additions & 2 deletions editor/profile_test.odin
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ recording_fixture :: proc(a_ms:i64) -> host.Profile_Recording {
)
}
frames:=make([]host.Profile_Frame,2,context.temp_allocator)
frames[0]={number=5,drawn=true,render_wait_ms=0.5,gpu_ms=3}
frames[1]={number=6,drawn=true,render_wait_ms=0.5,gpu_ms=3}
// The second submitted twice and only one came back.
frames[0]={number=5,drawn=true,render_wait_ms=0.5,gpu_ms=3,device_first=1,device_end=2,gpu_submissions=1}
frames[1]={number=6,drawn=true,render_wait_ms=0.5,gpu_ms=1,device_first=2,device_end=4,gpu_submissions=1}
return {frames=frames,zones=zones[:],threads=2,complete=true}
}

Expand Down Expand Up @@ -102,6 +103,8 @@ the_summary_ranks_zones_by_their_own_time_and_reads_what_bounds_the_frames :: pr
for part in summary.parts {parts[part.name]=true}
testing.expect(t,parts["editor"] && parts["pacing"] && parts["other"] && parts["between"],"the parts of the frame, what they leave and what runs between frames")
testing.expect_value(t,summary.frame.bound.cpu,2)
testing.expect_value(t,summary.frame.gpu_frames,1)
testing.expect_value(t,summary.frame.gpu_ms,3.0) // the partial frame is left out
testing.expect_value(t,summary.workers.threads,1)
testing.expect_value(t,summary.workers.busy_ms,2.0)
testing.expect(t,len(summary.notes)>0,"two frames are too few for percentiles")
Expand Down
Loading
Loading