From 5811c8d5bba05df321cf636678018ec22e0f3340 Mon Sep 17 00:00:00 2001 From: 5hif7y <44300833+5hif7y@users.noreply.github.com> Date: Sat, 14 Nov 2026 15:08:35 -0300 Subject: [PATCH 1/7] Give each GPU pass of a profile to the frame that submitted it 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 --- .../mirai-agent-api/references/methods.md | 4 + .agents/skills/mirai-core/references/host.md | 15 ++-- .../mirai-rendering/references/frame-graph.md | 3 + editor/api_profile.odin | 41 +++++++-- editor/profile.odin | 36 ++++++-- editor/profile_test.odin | 7 +- host/profile.odin | 85 ++++++++++++++----- host/profile_test.odin | 39 +++++++++ plugins/profiler/editor/profiler.odin | 33 +++---- render/frame.odin | 45 +++++++++- render/renderer.odin | 3 +- rhi/metal_device.odin | 15 +++- rhi/tests/rhi_test.odin | 6 +- rhi/vulkan_device.odin | 14 ++- 14 files changed, 281 insertions(+), 65 deletions(-) diff --git a/.agents/skills/mirai-agent-api/references/methods.md b/.agents/skills/mirai-agent-api/references/methods.md index ed2af3827..b72e56630 100644 --- a/.agents/skills/mirai-agent-api/references/methods.md +++ b/.agents/skills/mirai-agent-api/references/methods.md @@ -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`, diff --git a/.agents/skills/mirai-core/references/host.md b/.agents/skills/mirai-core/references/host.md index 247c36ec3..9d48dde8d 100644 --- a/.agents/skills/mirai-core/references/host.md +++ b/.agents/skills/mirai-core/references/host.md @@ -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, diff --git a/.agents/skills/mirai-rendering/references/frame-graph.md b/.agents/skills/mirai-rendering/references/frame-graph.md index 390becdf7..51ba55004 100644 --- a/.agents/skills/mirai-rendering/references/frame-graph.md +++ b/.agents/skills/mirai-rendering/references/frame-graph.md @@ -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 diff --git a/editor/api_profile.odin b/editor/api_profile.odin index 1bdb7f57e..efa078bb0 100644 --- a/editor/api_profile.odin +++ b/editor/api_profile.odin @@ -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"`, @@ -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 { @@ -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, @@ -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 (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 @@ -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)) @@ -527,6 +533,9 @@ profile_notes :: proc(c:^Profile_Capture,out:Api_Profile_Summary) -> []string { append(¬es,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(¬es,"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_frames0 {append(¬es,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(¬es,fmt.tprintf("%d zones were lost: record fewer frames for an exact count",c.lost))} @@ -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) @@ -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 @@ -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) @@ -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 @@ -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":[`) @@ -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) diff --git a/editor/profile.odin b/editor/profile.odin index 3e7cd50db..e96dda6c6 100644 --- a/editor/profile.odin +++ b/editor/profile.odin @@ -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 @@ -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, @@ -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) @@ -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[:] +} diff --git a/editor/profile_test.odin b/editor/profile_test.odin index 28130f6ca..4b215ff52 100644 --- a/editor/profile_test.odin +++ b/editor/profile_test.odin @@ -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} } @@ -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") diff --git a/host/profile.odin b/host/profile.odin index 34c6ddce5..4e4bc624b 100644 --- a/host/profile.odin +++ b/host/profile.odin @@ -11,10 +11,10 @@ import "mirai:std/jobs" // methods of the editor): on the main thread a zone for the frame and one for // each of its parts, and one for each function of the loops, which the world // opens with profile_step_begin; on the ring of each worker of the job -// system its jobs; and per frame what the -// zones do not carry, the times of the -// renderer and of the GPU. Rings are drained at the end of every frame, so a -// ring only has to hold one frame. While nothing records a zone costs a +// system its jobs; and per frame what the zones do not carry, the times of +// the renderer and the GPU passes of every submission of the frame, which +// come back rhi.MAX_FRAMES_IN_FLIGHT submissions later. Rings are drained at +// the end of every frame, so a ring only has to hold one frame. While nothing records a zone costs a // branch and the GPU reports only its whole frame. The first profile_start // reserves the memory; a recording allocates nothing. Whoever reads the zones // as they happen (a profiler plugin) asks for them every frame with @@ -27,6 +27,7 @@ PROFILE_PASSES :: 1<<17 // GPU pass times of a whole recording PROFILE_PASS_NAMES :: 128 // the last one names every pass that did not fit PROFILE_PASS_NAME_BYTES :: 32 PROFILE_MAIN :: jobs.OWNER // the ring of the main thread, worker 0 of the job system +PROFILE_GPU_LATE :: rhi.MAX_FRAMES_IN_FLIGHT+2 // frames a recording runs past its last one for the GPU times of that one Profile_Frame :: struct { number:u32, // the frame of base.Profile that the zones of this frame carry @@ -35,7 +36,9 @@ Profile_Frame :: struct { render_wait_ms:f32, // the wait for a frame slot of the GPU (and for the display with vsync) render_record_ms:f32, render_end_ms:f32, - gpu_ms:f32, // the last frame the GPU finished + gpu_ms:f32, // the sum of its own passes + device_first,device_end:u64, // the rhi frames it submitted, [first,end) + gpu_submissions:u32, // of them, those whose passes came back render_width,render_height:u32, generated:bool, // it drew an image shown before its own (frame generation) presented:u32, // images given to the display since the frame before @@ -47,7 +50,12 @@ Profile_Frame :: struct { Profile_Pass :: struct { name:int, // into the names of the pass of the Profiler - ms:f32, + start_ns,end_ns:i64, // on the clock of the zones +} + +// Whether every submission of the frame gave back its GPU passes. +profile_gpu_measured :: proc "contextless" (f:Profile_Frame) -> bool { + return f.device_end>f.device_first && u64(f.gpu_submissions)==f.device_end-f.device_first } Profiler :: struct { @@ -55,6 +63,7 @@ Profiler :: struct { rings_ready:bool, // the base.Profile rings, which profile_request needs without the recording buffers reserved:bool, // the recording buffers below recording:bool, + late:int, // >0: the frames asked for were recorded and the recording waits for the GPU times of the last ones, this many frames at most requested:u8, // frames profile_request keeps the zones and the GPU passes on wanted:int, // frames the recording ends after frames:[]Profile_Frame, @@ -65,6 +74,8 @@ Profiler :: struct { cursors:[]u64, // per ring: the next zone to drain passes:[]Profile_Pass, pass_count:int, + gpu_next:int, // the first frame that may still get GPU passes + device_first:u64, // rhi.frame_number when the frame being recorded began pass_names:[][PROFILE_PASS_NAME_BYTES]u8, // the GPU reuses the memory of its names every frame pass_name_lengths:[]u8, pass_name_count:int, @@ -90,7 +101,7 @@ profile_start :: proc(app:^App,frames:int) -> bool { if p.recording || frames<=0 {return false} if !p.reserved && !profile_reserve(app) {return false} p.wanted=min(frames,PROFILE_FRAMES_MAX) - p.frame_count,p.zone_count,p.pass_count,p.lost=0,0,0,0 + p.frame_count,p.zone_count,p.pass_count,p.lost,p.gpu_next,p.late=0,0,0,0,0,0 p.frame_zone={} if app.device!=nil {p.presented=rhi.present_stats(app.device)} out:[0]base.Profile_Zone @@ -105,7 +116,7 @@ profile_stop :: proc(app:^App) { p:=&app.profiler if !p.recording {return} base.profile_set_enabled(&p.profile,false) - p.recording=false + p.recording,p.late=false,0 } profile_recording :: proc(app:^App) -> Profile_Recording { @@ -129,7 +140,7 @@ profile_request :: proc(app:^App) -> bool { // Whether the zones and the GPU passes are being recorded. profile_active :: proc "contextless" (app:^App) -> bool { p:=&app.profiler - return p.rings_ready && (p.recording || p.requested>0) + return p.rings_ready && ((p.recording && p.late==0) || p.requested>0) } // The rings: one per thread, the main one first. @@ -182,6 +193,7 @@ profile_frame_open :: proc(app:^App) { active:=profile_active(app) if p.rings_ready {base.profile_set_enabled(&p.profile,active)} if !active {return} + if app.device!=nil {p.device_first=rhi.frame_number(app.device)} base.profile_frame(&p.profile) p.frame_zone=base.profile_begin(&p.profile,PROFILE_MAIN,"frame") } @@ -207,11 +219,15 @@ profile_frame_end :: proc(app:^App,drawn:bool) { opened:=p.frame_zone.profile!=nil if opened {profile_frame_close(app)} if p.requested>0 {p.requested-=1} - if !p.recording {return} - if opened && p.frame_count=len(p.passes) {break} - p.passes[p.pass_count]={profile_pass_intern(p,timing.name),timing.ms} - p.pass_count+=1 - } } } - frame.pass_count=p.pass_count-frame.pass_first p.frame_count+=1 } + if r:=app.renderer;r!=nil { + epoch:=profile_epoch_ns(app) + for submission in render.renderer_gpu_submissions(r) {profile_gpu_add(p,submission.frame,submission.timings,epoch)} + render.renderer_gpu_clear(r) + } for &cursor,thread in p.cursors { + if p.late>0 { + out:[0]base.Profile_Zone + _,cursor,_=base.profile_read_since(&p.profile,thread,0,out[:]) + continue + } count,next,lost:=base.profile_read_since(&p.profile,thread,cursor,p.zones[p.zone_count:]) p.zone_count+=count p.lost+=lost cursor=next } - if p.frame_count>=p.wanted {profile_stop(app)} + if p.frame_countlast.device_first && !profile_gpu_measured(last) && p.gpu_next=PROFILE_GPU_LATE {profile_stop(app);return} + p.late+=1 +} + +// Gives the GPU timings of the rhi frame to the recorded frame that +// submitted it. Batches come in the order they were submitted, so the passes +// of a frame stay together; those of a submission between frames, or of +// one before the recording, belong to no frame and are left out. +profile_gpu_add :: proc(p:^Profiler,frame:u64,timings:[]rhi.Gpu_Timing,epoch_ns:i64) { + for p.gpu_next=f.device_end {p.gpu_next+=1;continue} + if f.gpu_submissions==0 {f.pass_first=p.pass_count} + for timing in timings { + if p.pass_count>=len(p.passes) {break} + p.passes[p.pass_count]={profile_pass_intern(p,timing.name),timing.start_ns-epoch_ns,timing.end_ns-epoch_ns} + p.pass_count+=1 + f.gpu_ms+=timing.ms + } + f.pass_count=p.pass_count-f.pass_first + f.gpu_submissions+=1 + return + } } @(private) diff --git a/host/profile_test.odin b/host/profile_test.odin index ebbdf97e3..507a4cbec 100644 --- a/host/profile_test.odin +++ b/host/profile_test.odin @@ -1,6 +1,9 @@ package host import "core:testing" +import render "mirai:render" +import "mirai:rhi" +import "mirai:rhi/shared_device" import "mirai:std/base" // A request reserves only the rings, not the ~13 MB of buffers that only an @@ -48,3 +51,39 @@ a_recording_adds_its_buffers_to_ready_rings :: proc(t:^testing.T) { testing.expect_value(t, len(app.profiler.passes), PROFILE_PASSES) testing.expect_value(t, len(app.profiler.cursors), 1) } + +// A frame that submits twice (an offscreen capture, then its window) gets +// the GPU passes of both submissions, though they come back in later frames. +@(test) +a_recorded_frame_keeps_the_gpu_passes_of_every_submission :: proc(t: ^testing.T) { + shared_device.hold(t) + app, error := app_create({gpu = true, gpu_validation = true, window_desc = {size = {64, 48}}, fixed_dt = 1.0 / 60, threads = 1}) + if app == nil { + testing.expectf(t, rhi.gpu_skip_allowed(), "no application with a GPU: %v (set MIRAI_ALLOW_GPU_SKIP=1 to skip)", error) + return + } + defer app_destroy(app) + FRAMES :: 4 + if !testing.expect(t, profile_start(app, FRAMES)) {return} + for _ in 0 ..< FRAMES + PROFILE_GPU_LATE { + if !app.profiler.recording {break} + if !testing.expect(t, app_frame_begin(app)) {return} + if app.profiler.late == 0 {testing.expect(t, render.renderer_frame(app.renderer, {offscreen = true, ui_only = true, time = app.time}), app.renderer.error)} + app_frame_end(app) + } + recording := profile_recording(app) + testing.expect(t, recording.complete, "the recording waited for the GPU times of its last frame") + testing.expect_value(t, len(recording.frames), FRAMES) + for frame, i in recording.frames { + testing.expectf(t, frame.drawn, "frame %d drew", i) + testing.expect_value(t, frame.device_end - frame.device_first, 2) + testing.expectf(t, profile_gpu_measured(frame), "frame %d got %d of its 2 submissions", i, frame.gpu_submissions) + total: f64 + for pass in recording.passes[frame.pass_first:][:frame.pass_count] { + testing.expectf(t, pass.end_ns >= pass.start_ns, "frame %d: a pass ends before it starts", i) + total += f64(pass.end_ns - pass.start_ns) / 1e6 + } + testing.expectf(t, abs(total - f64(frame.gpu_ms)) < 0.01, "frame %d: gpu_ms %.3f is not the sum of its passes %.3f", i, frame.gpu_ms, total) + } + testing.expectf(t, rhi.device_validation_errors(app.device) == 0, "validation: %s", rhi.device_validation_message(app.device)) +} diff --git a/plugins/profiler/editor/profiler.odin b/plugins/profiler/editor/profiler.odin index 5fb10a824..97dc9f0f1 100644 --- a/plugins/profiler/editor/profiler.odin +++ b/plugins/profiler/editor/profiler.odin @@ -130,12 +130,12 @@ profiler_header :: proc(s:^editor.Session,p:^Profiler_Panel,app:^host.App) { @(private="file") profiler_show :: proc(p:^Profiler_Panel,c:^editor.Profile_Capture) { p.shown,p.frame,p.workers,p.depth,p.thread_count,p.top_count=c.id,0,0,1,0,0 - frame,gpu:=make([]f64,len(c.frames),context.temp_allocator),make([]f64,len(c.frames),context.temp_allocator) + frame:=make([]f64,len(c.frames),context.temp_allocator) for f,i in c.frames { p.workers+=f.zone_count - frame[i],gpu[i]=editor.profile_frame_ms(f),f64(f.gpu_ms) + frame[i]=editor.profile_frame_ms(f) } - spread,gpu_spread:=editor.profile_spread(frame),editor.profile_spread(gpu) + spread,gpu_spread:=editor.profile_spread(frame),editor.profile_spread(editor.profile_gpu_ms(c)) p.graph_ms=max(spread.p95,gpu_spread.p95,0.001)*1.5 nearest:=max(f64) for ms,i in frame { @@ -172,9 +172,9 @@ profiler_show :: proc(p:^Profiler_Panel,c:^editor.Profile_Capture) { @(private="file") profiler_summary :: proc(p:^Profiler_Panel,c:^editor.Profile_Capture) -> string { - frame,gpu:=make([]f64,len(c.frames),context.temp_allocator),make([]f64,len(c.frames),context.temp_allocator) - for f,i in c.frames {frame[i],gpu[i]=editor.profile_frame_ms(f),f64(f.gpu_ms)} - spread,gpu_spread:=editor.profile_spread(frame),editor.profile_spread(gpu) + frame:=make([]f64,len(c.frames),context.temp_allocator) + for f,i in c.frames {frame[i]=editor.profile_frame_ms(f)} + spread,gpu_spread:=editor.profile_spread(frame),editor.profile_spread(editor.profile_gpu_ms(c)) return fmt.tprintf("Capture %d · %d frames · frame %.2f ms mean, %.2f ms p95 · GPU %.2f ms · %s %s", c.id,len(c.frames),spread.mean,spread.p95,gpu_spread.mean,c.conditions.backend,c.conditions.device) } @@ -184,8 +184,9 @@ profiler_frame_text :: proc(p:^Profiler_Panel,c:^editor.Profile_Capture) -> stri if p.frame>=len(c.frames) {return ""} f:=c.frames[p.frame] bounds:=[editor.Profile_Bound]string{.Cpu="CPU",.Gpu="GPU",.Paced="pacing"} - return fmt.tprintf("Frame %d · %.2f ms · CPU %.2f ms · GPU %.2f ms · waits for %s", - p.frame+1,editor.profile_frame_ms(f),editor.profile_frame_cpu_ms(f),f.gpu_ms,bounds[editor.profile_frame_bound(f)]) + gpu:=fmt.tprintf("%.2f ms",f.gpu_ms) if f.gpu_measured else "not measured" + return fmt.tprintf("Frame %d · %.2f ms · CPU %.2f ms · GPU %s · waits for %s", + p.frame+1,editor.profile_frame_ms(f),editor.profile_frame_cpu_ms(f),gpu,bounds[editor.profile_frame_bound(f)]) } // A color of its own for each name, among muted hues. @@ -217,6 +218,7 @@ frames_draw :: proc(ctx:^ui.Context,rect:ui.Rect,user:rawptr) { x:=rect.x+f32(i)*step ui.draw_rect(ctx,{x+step*0.12,rect.y+rect.h-2-h,max(step*0.76,1),h},color) if ms>top {ui.draw_rect(ctx,{x,rect.y+2,step,3},ui.tone(ctx,"danger"))} + if !f.gpu_measured {continue} gpu:=f32(min(f64(f.gpu_ms)/top,1))*(rect.h-4) ui.draw_rect(ctx,{x,rect.y+rect.h-2-gpu,step,1.5},ui.tone(ctx,"text")) } @@ -228,15 +230,16 @@ frames_draw :: proc(ctx:^ui.Context,rect:ui.Rect,user:rawptr) { } // The frame chosen: the zones of the main thread by depth, those of the -// other threads, and the GPU passes one after another, on one time axis. +// other threads, and the GPU passes where they ran, on one time axis. @(private="file") timeline_draw :: proc(ctx:^ui.Context,rect:ui.Rect,user:rawptr) { p:=(^Profiler_Panel)(user) c:=p.capture if c==nil || p.frame>=len(c.frames) {return} f:=c.frames[p.frame] - gpu_ns:=i64(f64(f.gpu_ms)*1e6) - span:=max(f.ns+f.between_ns,gpu_ns,1) + passes:=c.passes[f.pass_first:][:f.pass_count] + span:=max(f.ns+f.between_ns,1) + for pass in passes {span=max(span,pass.start_ns+pass.ns-f.start_ns)} label:=f32(64) x0:=rect.x+label w:=rect.w-label @@ -264,12 +267,10 @@ timeline_draw :: proc(ctx:^ui.Context,rect:ui.Rect,user:rawptr) { row_y+=ROW+2 } ui.draw_text(ctx,"GPU",rect.x,row_y+2,ui.metric(ctx,"font_small"),ui.tone(ctx,"text_muted")) - at:=f32(0) - for pass in c.passes[f.pass_first:][:f.pass_count] { - width:=f32(f64(pass.ms)*1e6)*scale - bar(ctx,x0+at,row_y,width,c.pass_names[pass.name],f64(pass.ms)) - at+=width + for pass in passes { + bar(ctx,x0+f32(max(pass.start_ns-f.start_ns,0))*scale,row_y,f32(pass.ns)*scale,c.pass_names[pass.name],f64(pass.ns)/1e6) } + if !f.gpu_measured {ui.draw_text(ctx,"not measured",x0+4,row_y+2,ui.metric(ctx,"font_small"),ui.tone(ctx,"text_muted"))} ruler:=row_y+ROW+4 for i in 0..=4 { x:=x0+w*f32(i)/4 diff --git a/render/frame.odin b/render/frame.odin index 954a33de1..27305bb83 100644 --- a/render/frame.odin +++ b/render/frame.odin @@ -157,7 +157,7 @@ read_stats :: proc(r: ^Renderer, slot: int) { r.stats.occluded_instances = stats.occluded_instances r.stats.probes_traced = stats.probes_traced } - r.stats.timings = rhi.frame_timings(r.device) + r.stats.timings, _, _ = rhi.frame_timings(r.device) total: f32 for t in r.stats.timings {total += t.ms} r.stats.gpu_ms = total @@ -464,6 +464,7 @@ renderer_frame :: proc(r:^Renderer,frame:Frame_Desc) -> bool { begun:=time.tick_now() slot:=int(rhi.frame_number(d)%rhi.MAX_FRAMES_IN_FLIGHT) read_stats(r,slot) + gpu_submission_keep(r) points_read(r,slot) pages_update(r,slot) if !frame.ui_only {shadow_movers_update(r)} @@ -1765,6 +1766,48 @@ update_stats :: proc(r: ^Renderer) { renderer_stats :: proc(r: ^Renderer) -> ^Stats {return &r.stats} +RENDER_GPU_SUBMISSIONS :: 8 // whose timings wait between two renderer_gpu_clear +RENDER_GPU_TIMINGS :: 1024 + +// The GPU timings of one submission of the device, copied out of it, which +// reuses its own at the next frame. +Gpu_Submission :: struct { + frame: u64, // the rhi.frame_number it was + timings: []rhi.Gpu_Timing, +} + +Gpu_Submissions :: struct { + list: [RENDER_GPU_SUBMISSIONS]Gpu_Submission, + count: int, + timings: [RENDER_GPU_TIMINGS]rhi.Gpu_Timing, + names: [RENDER_GPU_TIMINGS][rhi.MAX_TIMESTAMP_NAME_BYTES]u8, + timing_count: int, +} + +// The submissions whose GPU timings came back since renderer_gpu_clear, in +// the order they were submitted. A host frame may submit several (captures, +// thumbnails); whoever accounts for each one takes them every frame and +// clears them. A submission that does not fit is left out. +renderer_gpu_submissions :: proc(r: ^Renderer) -> []Gpu_Submission {return r.submissions.list[:r.submissions.count]} + +renderer_gpu_clear :: proc(r: ^Renderer) {r.submissions.count, r.submissions.timing_count = 0, 0} + +@(private) +gpu_submission_keep :: proc(r: ^Renderer) { + timings, frame, measured := rhi.frame_timings(r.device) + g := &r.submissions + if !measured || g.count >= len(g.list) || g.timing_count + len(timings) > len(g.timings) {return} + first := g.timing_count + for timing, i in timings { + k := first + i + g.timings[k] = timing + g.timings[k].name = string(g.names[k][:copy(g.names[k][:], timing.name)]) + } + g.timing_count += len(timings) + g.list[g.count] = {frame, g.timings[first:g.timing_count]} + g.count += 1 +} + // Stats with the counters of the last frame, which it reads back by the time // a later frame needs its slot: for whoever has just read an image of that // frame (renderer_capture, view_texture_capture), so the GPU is idle. diff --git a/render/renderer.odin b/render/renderer.odin index 0b62d6f1f..4b7bfe0de 100644 --- a/render/renderer.odin +++ b/render/renderer.odin @@ -266,7 +266,7 @@ Stats :: struct { probes_per_frame: u32, probes_traced: u32, // in the frame two frames ago rays_per_frame: u64, - gpu_ms: f32, + gpu_ms: f32, // of the last frame the GPU finished ev100: f32, // the main view is exposed at, before its compensation: what its meter took, as read back, or what its camera gives timings: []rhi.Gpu_Timing, // Time of the calling thread in the parts of the last frame, in ms. @@ -669,6 +669,7 @@ Renderer :: struct { pixel_density: f32, // device pixels per native window coordinate frame: u64, stats: Stats, + submissions: Gpu_Submissions, error_buffer: [512]u8, error: string, problem_buffer: [1024]u8, // of Stats.problem diff --git a/rhi/metal_device.odin b/rhi/metal_device.odin index d580f9d25..65acbe84b 100644 --- a/rhi/metal_device.odin +++ b/rhi/metal_device.odin @@ -103,6 +103,8 @@ Mtl_Frame :: struct { query_bytes: [MAX_TIMESTAMPS][MAX_TIMESTAMP_NAME_BYTES]u8, encoder_count: u32, encoder_marks: [MAX_TIMESTAMPS]u16, // of each sampled encoder: the mark it runs after + number: u64, // the frame_number it committed + submitted: bool, // until frame_begin collects its timings paced: bool, // the frame draws into a pair of the presenter begun_ns: i64, submitted_ns: i64, @@ -238,6 +240,8 @@ Device :: struct { timings: [MAX_TIMESTAMPS]Gpu_Timing, timing_bytes: [MAX_TIMESTAMPS][MAX_TIMESTAMP_NAME_BYTES]u8, timing_count: int, + timing_frame: u64, // the frame_number the timings measured + timing_measured: bool, // the timings are of a committed frame timestamps_wanted: bool, clock_cpu, clock_gpu: MTL.Timestamp, // sampled together when the device was made: the rate of the GPU clock error_buffer: [512]u8, @@ -798,7 +802,12 @@ mtl_flush_ranges :: proc(d: ^Device, f: ^Mtl_Frame) { // ---------------------------------------------------------------- frames -frame_timings :: proc(d: ^Device) -> []Gpu_Timing {return d.timings[:d.timing_count]} +// The timings the last frame_begin collected and the frame_number of the +// frame they measured, MAX_FRAMES_IN_FLIGHT before it; measured is false when +// that slot had committed nothing since its timings were last collected. +frame_timings :: proc(d: ^Device) -> (timings: []Gpu_Timing, frame: u64, measured: bool) { + return d.timings[:d.timing_count], d.timing_frame, d.timing_measured +} // Asks for one timing per cmd_timestamp interval in the frames that begin // from now on; without it a frame reports only its whole GPU time. @@ -878,8 +887,11 @@ frame_begin :: proc(d: ^Device, present := true) -> (cmd: ^Command_List, ok: boo if d.in_frame {set_error(d, "frame_begin while a frame is being recorded: end it with frame_end first"); return nil, false} f := &d.frames[d.slot] d.timing_count = 0 + d.timing_frame, d.timing_measured = f.number, false if f.cmd != nil { if mtl_wait(d, f.cmd, "frame") {mtl_collect_timestamps(d, f)} + d.timing_measured = f.submitted + f.submitted = false mtl_release(f.cmd) f.cmd = nil } @@ -961,6 +973,7 @@ frame_end :: proc(d: ^Device, cmd: ^Command_List, generated := false) -> bool { } MTL.CommandBuffer_commit(f.cmd) f.submitted_ns = present_now_ns() + f.number, f.submitted = d.frame_number, true if presented {present_count_direct(d)} mtl_release_drawable(d) if f.paced { diff --git a/rhi/tests/rhi_test.odin b/rhi/tests/rhi_test.odin index 4d5458e24..4c524dfc0 100644 --- a/rhi/tests/rhi_test.odin +++ b/rhi/tests/rhi_test.odin @@ -802,7 +802,11 @@ rhi_timestamps_report_ordered_nanoseconds :: proc(t: ^testing.T) { cmd, ok := rhi.frame_begin(d, false) if !testing.expect(t, ok) {return nil} testing.expect(t, rhi.frame_end(d, cmd)) - return rhi.frame_timings(d) + timings, frame, measured := rhi.frame_timings(d) + // The last frame_begin collected the frame its slot submitted before it. + testing.expect(t, measured, "the slot had submitted a frame") + testing.expect_value(t, frame, rhi.frame_number(d) - 1 - rhi.MAX_FRAMES_IN_FLIGHT) + return timings } now := time.duration_nanoseconds(time.tick_diff({}, time.tick_now())) frame := timings_of(t, d, buffer, false) diff --git a/rhi/vulkan_device.odin b/rhi/vulkan_device.odin index ae947e0f0..a24594293 100644 --- a/rhi/vulkan_device.odin +++ b/rhi/vulkan_device.odin @@ -128,6 +128,8 @@ Vk_Frame :: struct { acquired: bool, paced: bool, // the frame draws into a pair of the presenter begun_ns: i64, + number: u64, // the frame_number it submitted + submitted: bool, // until frame_begin collects its timings submitted_ns: i64, marks: Vk_Marks, // where the GPU went (vulkan_marks.odin) } @@ -247,6 +249,8 @@ Device :: struct { timings: [MAX_TIMESTAMPS]Gpu_Timing, timing_bytes: [MAX_TIMESTAMPS][MAX_TIMESTAMP_NAME_BYTES]u8, // the names of timings, kept until the next ones timing_count: int, + timing_frame: u64, // the frame_number the timings measured + timing_measured: bool, // the timings are of a submitted frame timestamps_wanted: bool, timestamp_bits: u32, // timestampValidBits of the queue: a GPU timestamp wraps past them // VK_KHR or VK_EXT_calibrated_timestamps with the device and host domains; nil without them. @@ -1168,6 +1172,8 @@ vk_collect_timestamps :: proc(d: ^Device, f: ^Vk_Frame) { count := f.query_count f.query_count = 0 d.timing_count = 0 + d.timing_frame, d.timing_measured = f.number, f.submitted + f.submitted = false if f.queries == 0 || count < 2 {return} values: [MAX_TIMESTAMPS]u64 // The slot's submission already completed, so WAIT returns immediately. @@ -1197,7 +1203,12 @@ vk_collect_timestamps :: proc(d: ^Device, f: ^Vk_Frame) { } } -frame_timings :: proc(d: ^Device) -> []Gpu_Timing {return d.timings[:d.timing_count]} +// The timings the last frame_begin collected and the frame_number of the +// frame they measured, MAX_FRAMES_IN_FLIGHT before it; measured is false when +// that slot had submitted nothing since its timings were last collected. +frame_timings :: proc(d: ^Device) -> (timings: []Gpu_Timing, frame: u64, measured: bool) { + return d.timings[:d.timing_count], d.timing_frame, d.timing_measured +} // Begins recording a frame. Without present it takes no image of the // swapchain: the frame draws only into textures, for a capture. @@ -1299,6 +1310,7 @@ frame_end :: proc(d: ^Device, cmd: ^Command_List, generated := false) -> bool { f.marks.sent = submitted == .SUCCESS ok := vk_check(d, submitted, "vkQueueSubmit2") f.signal_value = d.timeline_value + f.number, f.submitted = d.frame_number, submitted == .SUCCESS f.submitted_ns = present_now_ns() if ok && f.acquired { present := vk.PresentInfoKHR{ From 5f91b720a845c73b66698f9fcb681ce34ff0ec52 Mon Sep 17 00:00:00 2001 From: 5hif7y <44300833+5hif7y@users.noreply.github.com> Date: Sat, 14 Nov 2026 19:35:22 -0300 Subject: [PATCH 2/7] 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. Co-Authored-By: Claude Opus 5.5 --- .../mirai-agent-api/references/methods.md | 3 + .agents/skills/mirai-core/references/host.md | 8 +- .../mirai-plugins/references/profiling.md | 6 +- core/world_data.odin | 91 +++++++++ core/world_memory_test.odin | 27 +++ editor/api_profile.odin | 37 +++- editor/profile.odin | 11 +- host/frame.odin | 2 + host/profile.odin | 42 ++++- host/profile_test.odin | 27 +++ platform/process_memory.odin | 9 + platform/process_memory_darwin.odin | 26 +++ platform/process_memory_linux.odin | 39 ++++ platform/process_memory_posix.odin | 24 --- platform/process_memory_test.odin | 16 ++ platform/process_memory_windows.odin | 8 +- plugins/playtest/editor/qa.odin | 4 +- plugins/profiler/editor/profiler.odin | 178 +++++++++++++++--- 18 files changed, 495 insertions(+), 63 deletions(-) create mode 100644 core/world_memory_test.odin create mode 100644 platform/process_memory.odin create mode 100644 platform/process_memory_darwin.odin create mode 100644 platform/process_memory_linux.odin delete mode 100644 platform/process_memory_posix.odin create mode 100644 platform/process_memory_test.odin diff --git a/.agents/skills/mirai-agent-api/references/methods.md b/.agents/skills/mirai-agent-api/references/methods.md index b72e56630..c2546a123 100644 --- a/.agents/skills/mirai-agent-api/references/methods.md +++ b/.agents/skills/mirai-agent-api/references/methods.md @@ -324,6 +324,9 @@ Reading a capture: 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`). +- **Memory.** `profile.capture` with `memory:true` samples it at the end of each frame, as the part + `memory` of the frame, and the summary gives `memory`: the World's tables and storages (its peak + over the capture and its rise), the process and the GPU. Leave it off to measure cost. - **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`, diff --git a/.agents/skills/mirai-core/references/host.md b/.agents/skills/mirai-core/references/host.md index 9d48dde8d..1cc64e88d 100644 --- a/.agents/skills/mirai-core/references/host.md +++ b/.agents/skills/mirai-core/references/host.md @@ -198,7 +198,13 @@ a few frames past its last one, keeping no zones, until they do. The first `prof 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 +request, the device writes no per-pass timestamps. A recording that asks for memory +(`profile_start(app, frames, memory)`, off by default) samples it at the end of each frame, inside +the frame as its part `memory`: what the tables and storages of the World that ran last hold +(`core.world_memory`, the game loop's World, else the editor's), the private and resident memory of +the process (`platform.process_memory`) and the device memory. `core.world_memory` adds up the +capacity of the arrays, maps and slices of the tables and storages, without stepping into flat +values; what game code allocates outside its storages is not in it. 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, diff --git a/.agents/skills/mirai-plugins/references/profiling.md b/.agents/skills/mirai-plugins/references/profiling.md index 4a29537c2..b5140b695 100644 --- a/.agents/skills/mirai-plugins/references/profiling.md +++ b/.agents/skills/mirai-plugins/references/profiling.md @@ -13,8 +13,10 @@ View > Profiler panel. **Record** records frames into a agent see the same captures, and the panel shows the last finished one. - It shows the capture summary, a chart with one bar per frame colored by what it waited on (CPU, - GPU or pacing), the timeline of the chosen frame (main thread zones, one row per worker and the - GPU passes) and the most expensive zones per frame. + GPU or pacing), and a tab per track for the chosen frame: CPU (main thread zones, one row per + worker, the most expensive zones per frame), GPU (its passes where they ran, the most expensive + passes) and Memory (the World, the process and the GPU over the frames). Its recordings sample + memory; the tabs keep the height of the tallest, so the panel does not move under the pointer. - It draws with [`editor.drawing`](../../mirai-editor/references/extensions.md#a-panel); what it computes from a capture it computes when it starts showing it. diff --git a/core/world_data.odin b/core/world_data.odin index ae8332619..c25a5c339 100644 --- a/core/world_data.odin +++ b/core/world_data.odin @@ -302,3 +302,94 @@ world_detach :: proc(info:^World_Info,world,shared:rawptr) { } } } + +// The bytes the tables and storages of a World hold behind their pointers: +// the capacity of the arrays, maps and slices of each table and storage of +// the place World, and of its entities. It reads capacities, not the +// allocator, so what game code allocates outside its storages is not in it. +// elements: the values it had to step into because they hold memory of +// their own (rows of a table whose component is not flat, items of an array +// of such values), which is what makes it cost more than one read per +// container. +world_memory :: proc(info:^World_Info,data:rawptr) -> (bytes,elements:int) { + if info==nil || data==nil {return} + add :: proc(bytes,elements:^int,b,e:int) {bytes^+=b;elements^+=e} + b,e:=value_bytes(entities_of(info,data),type_info_of(Entities)) + add(&bytes,&elements,b,e) + for &t in info.tables { + if t.place!=.World {continue} + b,e=table_bytes(table_of(data,&t),t.info,t.flat) + add(&bytes,&elements,b,e) + } + for &s in info.storages { + if s.place!=.World || s.by_world || s.flat {continue} + b,e=value_bytes(storage_of(data,&s),s.info) + add(&bytes,&elements,b,e) + } + return +} + +@(private="file") +table_bytes :: proc(t:^Raw_Table,info:^runtime.Type_Info,flat:bool) -> (bytes,elements:int) { + size:=info.size + bytes=(t.values.cap+t.added.cap+t.edited.cap)*size + bytes+=cap(t.entities)*size_of(Entity)+cap(t.stamps)*size_of(u64)+cap(t.page_stamps)*size_of(u64) + bytes+=cap(t.off)*size_of(bool)+cap(t.turns)*size_of(u64)+cap(t.pending)*size_of(Pending) + bytes+=cap(t.edits)*size_of(Edit)+cap(t.gone.items)*size_of(Entity)+cap(t.owners)*size_of(Row_Owner) + bytes+=len(t.locks)*size_of(Lock)+cap(t.rows)*size_of([]i32) + for page in t.rows {bytes+=len(page)*size_of(i32)} + when CHECK_WRITES {bytes+=cap(t.settled.values)} + if flat {return} + for i in 0.. (bytes,elements:int) { + base:=runtime.type_info_base(info) + #partial switch v in base.variant { + case runtime.Type_Info_Struct: + for i in 0..=empty+100*size_of(Entity),"100 rows and their entities add to it: %d -> %d",empty,rows) + testing.expect_value(t,elements,0) + for i in 0..<1000 {append(&w.list.items,i32(i))} + items,_:=world_memory(&info,&w) + testing.expect_value(t,items-rows,cap(w.list.items)*size_of(i32)) +} diff --git a/editor/api_profile.odin b/editor/api_profile.odin index efa078bb0..6a6249293 100644 --- a/editor/api_profile.odin +++ b/editor/api_profile.odin @@ -29,6 +29,7 @@ Api_Profile_Capture_Params :: struct { label:[32]u8 `mirai:"optional,text,label=Name to tell the capture by"`, top:int `mirai:"optional,min=1,max=64,clamp,label=Zones and GPU passes in the summary"`, wait:int `mirai:"optional,min=0,max=900,unit=s,label=With advance: how long to queue for a compilation or another measurement that runs; past it the call fails"`, + memory:bool `mirai:"optional,label=Also sample the memory at the end of each frame, in a zone memory of the frame: what the tables and storages of the World hold (the one Play runs, else the scene's), the process and the GPU"`, allow_limited:bool `mirai:"optional,label=Measure with VSync or a frame limit or graphics validation on; the call refuses otherwise and says how to turn them off. The summary says comparable:false"`, } Api_Profile_Id_Params :: struct { @@ -103,6 +104,18 @@ Api_Profile_Presentation :: struct { latency_mean_ms:f32 `mirai:"label=From the start of a frame to its own image going to the display with its GPU work done; without frame generation the GPU is taken to start each frame once it is submitted and the one before is done"`, latency_p95_ms:f32, } +// In MB. world: what the tables and storages of the World that ran hold +// (core.world_memory); what game code allocates outside its storages is not in it. +Api_Profile_Memory :: struct { + world_mb:f32 `mirai:"label=At the end of the last frame"`, + world_peak_mb:f32 `mirai:"label=The most at the end of a frame of the capture"`, + world_rise_mb:f32 `mirai:"label=From the end of the first frame to that of the last"`, + private_mb:f32 `mirai:"label=Of the process: what only it uses, at the end of the last frame"`, + private_rise_mb:f32 `mirai:"label=From the end of the first frame to that of the last"`, + resident_mb:f32 `mirai:"label=Of the process in physical memory, shared pages included"`, + gpu_mb:f32 `mirai:"label=Of the device memory the process holds"`, +} +Api_Profile_Frame_Memory :: struct {world_mb,private_mb,gpu_mb:f32} Api_Profile_Summary :: struct { id:int `mirai:"label=What the id of the other profile methods takes"`, label:string, @@ -117,6 +130,7 @@ Api_Profile_Summary :: struct { zones:[]Api_Profile_Zone `mirai:"optional,label=The other zones that cost the most by their own time"`, gpu:[]Api_Profile_Stat `mirai:"optional,label=The GPU passes that cost the most"`, workers:Api_Profile_Workers `mirai:"optional"`, + memory:Api_Profile_Memory `mirai:"optional,label=Of a capture with memory"`, lost_zones:u64 `mirai:"optional,label=Zones the recorder had no room for"`, notes:[]string `mirai:"optional,label=What to know before reading the numbers"`, } @@ -148,6 +162,7 @@ Api_Profile_Frame :: struct { zones:[]Api_Profile_Tree `mirai:"label=Of the main thread by start"`, hidden_zones:int `mirai:"label=Deeper or shorter than asked or past the most listed"`, gpu:[]Api_Profile_Pass `mirai:"label=The passes that took the longest"`, + memory:Api_Profile_Frame_Memory `mirai:"optional,label=At its end, in MB, of a capture with memory"`, } Api_Profile_Frames :: struct { id:int, @@ -298,7 +313,7 @@ api_profile_capture :: proc(s:^Session,call:^api.Call) { profile_collect(s) warmup,settled:=0,true if p.advance && s.profiles.recording==0 {warmup,settled=profile_settle(s,p.dt)} - c,error:=profile_begin(s,p.frames,base.text_get(p.label[:])) + c,error:=profile_begin(s,p.frames,base.text_get(p.label[:]),p.memory) if c==nil {api.call_fail(call,api.SERVER_ERROR,error);return} c.warmup,c.unsettled=warmup,!settled if !p.advance { @@ -387,6 +402,7 @@ profile_summary :: proc(c:^Profile_Capture,top:int) -> (out:Api_Profile_Summary) 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)) + if c.memory {out.memory=profile_memory(c)} aggregates:=profile_aggregate(c) parts:=make([dynamic]Api_Profile_Stat) @@ -566,12 +582,26 @@ api_profile_frames :: proc(s:^Session,call:^api.Call) { api.call_result(call,Api_Profile_Frames)^=out } +profile_mb :: proc(bytes:i64) -> f32 {return f32(math.round(f64(bytes)/(1<<20)*100)/100)} + +profile_memory :: proc(c:^Profile_Capture) -> (out:Api_Profile_Memory) { + if len(c.frames)==0 {return} + first,last:=c.frames[0].memory,c.frames[len(c.frames)-1].memory + peak:i64 + for f in c.frames {peak=max(peak,f.memory.world)} + out.world_mb,out.world_peak_mb,out.world_rise_mb=profile_mb(last.world),profile_mb(peak),profile_mb(last.world-first.world) + out.private_mb,out.private_rise_mb=profile_mb(i64(last.process.private)),profile_mb(i64(last.process.private)-i64(first.process.private)) + out.resident_mb,out.gpu_mb=profile_mb(i64(last.process.resident)),profile_mb(i64(last.gpu)) + return +} + profile_frame_out :: proc(c:^Profile_Capture,index,depth:int,min_ms:f64) -> (out:Api_Profile_Frame) { f:=c.frames[index] 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 + if c.memory {out.memory={profile_mb(f.memory.world),profile_mb(i64(f.memory.process.private)),profile_mb(i64(f.memory.gpu))}} 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) @@ -855,6 +885,11 @@ profile_trace :: proc(c:^Profile_Capture) -> (text:string,events:int) { event(&b,&first) fmt.sbprintf(&b,`{{"name":"ms","ph":"C","pid":1,"ts":%.3f,"args":{{"frame":%.3f,"gpu":%.3f,"wait":%.3f}}}}`,f64(f.start_ns)/1e3,profile_frame_ms(f),f64(f.gpu_ms),f64(f.wait_ms)) events+=2 + if c.memory { + event(&b,&first) + fmt.sbprintf(&b,`{{"name":"MB","ph":"C","pid":1,"ts":%.3f,"args":{{"world":%.2f,"private":%.2f,"gpu":%.2f}}}}`,f64(f.start_ns)/1e3,profile_mb(f.memory.world),profile_mb(i64(f.memory.process.private)),profile_mb(i64(f.memory.gpu))) + events+=1 + } } for zone in c.zones { event(&b,&first) diff --git a/editor/profile.odin b/editor/profile.odin index e96dda6c6..6a611b5ae 100644 --- a/editor/profile.odin +++ b/editor/profile.odin @@ -22,7 +22,7 @@ import "mirai:std/jobs" PROFILE_CAPTURES :: 8 PROFILE_DEPTH_MAX :: 64 // The zones of the frame itself, children of "frame" on the main thread. -PROFILE_PARTS :: [?]string{"platform","watch","game","editor","audio","render","pacing"} +PROFILE_PARTS :: [?]string{"platform","watch","game","editor","audio","render","pacing","memory"} // A GPU that idled runs its first frames at a lower clock, in steps of a few // hundred ms, and lowers it again after a second or two without frames: a @@ -73,6 +73,7 @@ Profile_Frame_Record :: struct { pass_first,pass_count:int, worker_ns:i64, // time of the other threads in jobs jobs:int, + memory:host.Profile_Memory, // at its end, of a capture with memory } Profile_Zone_Record :: struct { @@ -91,6 +92,7 @@ Profile_Capture :: struct { label:string, state:Profile_State, requested:int, + memory:bool, // it sampled the memory at the end of each frame conditions:Profile_Conditions, frames:[]Profile_Frame_Record, zones:[]Profile_Zone_Record, // the main thread frame by frame, then the other threads @@ -137,7 +139,7 @@ profile_find :: proc(s:^Session,id:int) -> ^Profile_Capture { // Starts recording frames into a new capture, which replaces the oldest when // all are taken. Fails while another records. -profile_begin :: proc(s:^Session,frames:int,label:string) -> (c:^Profile_Capture,error:string) { +profile_begin :: proc(s:^Session,frames:int,label:string,memory:=false) -> (c:^Profile_Capture,error:string) { profile_collect(s) ps:=&s.profiles if ps.recording!=0 {return nil,fmt.tprintf("Capture %d is still recording; profile.stop ends it",ps.recording)} @@ -149,11 +151,11 @@ profile_begin :: proc(s:^Session,frames:int,label:string) -> (c:^Profile_Capture if vmem.arena_init_growing(&c.arena)!=nil {return nil,"No memory for a capture"} ps.next_id+=1 c.id=ps.next_id - c.requested=frames + c.requested,c.memory=frames,memory context.allocator=vmem.arena_allocator(&c.arena) c.label=strings.clone(label) c.conditions=profile_conditions(s) - if !host.profile_start(s.app,frames) { + if !host.profile_start(s.app,frames,memory) { profile_capture_free(c) return nil,"The recorder could not reserve its memory" } @@ -297,6 +299,7 @@ profile_build :: proc(c:^Profile_Capture,recording:host.Profile_Recording,app:^h record.generated,record.presented,record.intervals=frame.generated,int(frame.presented),int(frame.intervals) record.interval_total_ms,record.interval_square,record.latency_ms=frame.interval_total_ms,frame.interval_square,frame.latency_ms record.pass_first,record.pass_count=frame.pass_first,frame.pass_count + record.memory=frame.memory } // A manual editor waits for the next request between frames: that is not part of a frame. if !c.conditions.manual { diff --git a/host/frame.odin b/host/frame.odin index 7bebbf916..19b5f2472 100644 --- a/host/frame.odin +++ b/host/frame.odin @@ -255,6 +255,7 @@ app_game_frame :: proc(app:^App,world:rawptr,phases:=ALL_PHASES,step:=false,dt:= defer app.stats.game_ms=f32(time.duration_milliseconds(time.tick_since(started))) zone:=profile_part(app,"game") defer base.profile_end(zone) + app.profiler.memory_world=world loop:=core.Loop.Game frame:=platform.input_frame(app.platform) ref:=app_world_ref(app,world) @@ -417,6 +418,7 @@ app_editor_frame :: proc(app:^App,world,editor:rawptr,played:=false) { started:=time.tick_now() defer app.stats.editor_ms=f32(time.duration_milliseconds(time.tick_since(started))) zone:=profile_part(app,"editor") + if !played && app.profiler.memory_world==nil {app.profiler.memory_world=world} defer base.profile_end(zone) loop:=core.Loop.Editor if played { diff --git a/host/profile.odin b/host/profile.odin index 4e4bc624b..34ee75f3a 100644 --- a/host/profile.odin +++ b/host/profile.odin @@ -2,6 +2,8 @@ package host import "base:runtime" import "core:time" +import "mirai:core" +import "mirai:platform" import render "mirai:render" import "mirai:rhi" import "mirai:std/base" @@ -46,6 +48,16 @@ Profile_Frame :: struct { interval_total_ms,interval_square:f64, latency_ms:f32, // from the start of the last frame shown to its image going to the display pass_first,pass_count:int, + memory:Profile_Memory, // of a recording with memory +} + +// The memory at the end of a frame, in bytes. world: what the tables and +// storages of the World that ran last in the frame hold (the game loop's, +// else the editor's), as core.world_memory measures it. +Profile_Memory :: struct { + world:i64, + process:platform.Process_Memory, + gpu:u64, // of the device, as its driver says } Profile_Pass :: struct { @@ -63,6 +75,8 @@ Profiler :: struct { rings_ready:bool, // the base.Profile rings, which profile_request needs without the recording buffers reserved:bool, // the recording buffers below recording:bool, + memory:bool, // the recording samples the memory at the end of each frame, in the zone memory + memory_world:rawptr, // the World of the frame that ran last late:int, // >0: the frames asked for were recorded and the recording waits for the GPU times of the last ones, this many frames at most requested:u8, // frames profile_request keeps the zones and the GPU passes on wanted:int, // frames the recording ends after @@ -94,13 +108,15 @@ Profile_Recording :: struct { complete:bool, // it reached the frames asked for } -// Starts recording the next frames, at most PROFILE_FRAMES_MAX. False while -// another recording runs, or when the memory cannot be reserved. -profile_start :: proc(app:^App,frames:int) -> bool { +// Starts recording the next frames, at most PROFILE_FRAMES_MAX, and with +// memory what they hold at their end. False while another recording runs, +// or when the memory cannot be reserved. +profile_start :: proc(app:^App,frames:int,memory:=false) -> bool { p:=&app.profiler if p.recording || frames<=0 {return false} if !p.reserved && !profile_reserve(app) {return false} p.wanted=min(frames,PROFILE_FRAMES_MAX) + p.memory=memory p.frame_count,p.zone_count,p.pass_count,p.lost,p.gpu_next,p.late=0,0,0,0,0,0 p.frame_zone={} if app.device!=nil {p.presented=rhi.present_stats(app.device)} @@ -192,6 +208,7 @@ profile_frame_open :: proc(app:^App) { p:=&app.profiler active:=profile_active(app) if p.rings_ready {base.profile_set_enabled(&p.profile,active)} + p.memory_world=nil if !active {return} if app.device!=nil {p.device_first=rhi.frame_number(app.device)} base.profile_frame(&p.profile) @@ -217,6 +234,12 @@ profile_frame_end :: proc(app:^App,drawn:bool) { load_profile_frame(app,drawn) p:=&app.profiler opened:=p.frame_zone.profile!=nil + sample:Profile_Memory + if opened && p.recording && p.late==0 && p.memory { + zone:=profile_part(app,"memory") + sample=profile_memory_sample(app) + base.profile_end(zone) + } if opened {profile_frame_close(app)} if p.requested>0 {p.requested-=1} if !p.recording { @@ -225,7 +248,7 @@ profile_frame_end :: proc(app:^App,drawn:bool) { } if opened && p.late==0 && p.frame_count (m:Profile_Memory) { + if world:=app.profiler.memory_world;world!=nil && app.info!=nil { + bytes,_:=core.world_memory(app.info,world) + m.world=i64(bytes) + } + m.process=platform.process_memory() + if app.device!=nil {m.gpu,_=rhi.device_memory(app.device)} + return +} + // Gives the GPU timings of the rhi frame to the recorded frame that // submitted it. Batches come in the order they were submitted, so the passes // of a frame stay together; those of a submission between frames, or of diff --git a/host/profile_test.odin b/host/profile_test.odin index 507a4cbec..a5571403b 100644 --- a/host/profile_test.odin +++ b/host/profile_test.odin @@ -52,6 +52,33 @@ a_recording_adds_its_buffers_to_ready_rings :: proc(t:^testing.T) { testing.expect_value(t, len(app.profiler.cursors), 1) } +// Memory is sampled only by a recording that asks for it, inside the frame: +// its zone memory is a part of the frame, not time between frames. +@(test) +a_recording_samples_the_memory_only_when_asked_inside_the_frame :: proc(t:^testing.T) { + app: App + app.allocator = context.allocator + defer profile_release(&app) + for memory in ([]bool{true, false}) { + if !testing.expect(t, profile_start(&app, 2, memory)) {return} + for _ in 0 ..< 2 { + profile_frame_open(&app) + profile_frame_end(&app, false) + } + recording := profile_recording(&app) + testing.expect_value(t, len(recording.frames), 2) + sampled := false + for zone in recording.zones { + if zone.name == "memory" { + sampled = true + testing.expect_value(t, zone.depth, u16(1)) + } + } + testing.expect_value(t, sampled, memory) + testing.expect_value(t, recording.frames[1].memory.process.private > 0, memory) + } +} + // A frame that submits twice (an offscreen capture, then its window) gets // the GPU passes of both submissions, though they come back in later frames. @(test) diff --git a/platform/process_memory.odin b/platform/process_memory.odin new file mode 100644 index 000000000..6f2477b72 --- /dev/null +++ b/platform/process_memory.odin @@ -0,0 +1,9 @@ +package platform + +// The memory this process holds, in bytes, as its system counts it: resident, +// the pages in physical memory (the working set, with what is shared with +// other processes); private, what only this process uses and the system has +// to back (Windows: the private commit; Linux: the anonymous resident pages +// and the swapped ones; macOS: the physical footprint). Zeros when the system +// does not say. Reading it allocates nothing. +Process_Memory :: struct {resident,private:u64} diff --git a/platform/process_memory_darwin.odin b/platform/process_memory_darwin.odin new file mode 100644 index 000000000..b582afb64 --- /dev/null +++ b/platform/process_memory_darwin.odin @@ -0,0 +1,26 @@ +#+build darwin +package platform + +import "core:sys/darwin" + +// TASK_VM_INFO up to phys_footprint, its first revision, as mach/task_info.h lays it out. +@(private="file") +Task_Vm_Info :: struct { + virtual_size:u64, + region_count,page_size:i32, + resident_size,resident_size_peak:u64, + device,device_peak,internal,internal_peak,external,external_peak:u64, + reusable,reusable_peak:u64, + purgeable_volatile_pmap,purgeable_volatile_resident,purgeable_volatile_virtual:u64, + compressed,compressed_peak,compressed_lifetime:u64, + phys_footprint:u64, +} +@(private="file") TASK_VM_INFO :: 22 + +// The resident size, and the physical footprint. +process_memory :: proc "contextless" () -> Process_Memory { + info:Task_Vm_Info + count:=u32(size_of(info)/size_of(i32)) + if darwin.task_info(darwin.mach_task_self(),TASK_VM_INFO,darwin.task_info_t(&info),&count)!=.Success {return {}} + return {info.resident_size,info.phys_footprint} +} diff --git a/platform/process_memory_linux.odin b/platform/process_memory_linux.odin new file mode 100644 index 000000000..d4f7de82e --- /dev/null +++ b/platform/process_memory_linux.odin @@ -0,0 +1,39 @@ +#+build linux +package platform + +import "core:sys/posix" + +// From the VmRSS, RssAnon and VmSwap lines of /proc/self/status, in kB. +process_memory :: proc "contextless" () -> (m:Process_Memory) { + fd:=posix.open("/proc/self/status",{.CLOEXEC}) + if fd<0 {return} + defer posix.close(fd) + buffer:[8192]u8 + length:=0 + for length0 { + end:=0 + for end='0' && c<='9' {kilobytes=kilobytes*10+u64(c-'0')} else if kilobytes>0 {break} + } + switch line[:colon] { + case "VmRSS": m.resident=kilobytes*1024 + case "RssAnon": anonymous=kilobytes*1024 + case "VmSwap": swapped=kilobytes*1024 + } + } + m.private=anonymous+swapped + return +} diff --git a/platform/process_memory_posix.odin b/platform/process_memory_posix.odin deleted file mode 100644 index 3ed9d44e4..000000000 --- a/platform/process_memory_posix.odin +++ /dev/null @@ -1,24 +0,0 @@ -#+build linux, darwin -package platform - -import "core:c" - -@(private="file") -Rusage :: struct { - user_time,system_time:[2]i64, - max_rss:c.long, // kilobytes on Linux and bytes on macOS - rest:[13]c.long, -} - -foreign import libc "system:c" -@(default_calling_convention="c",private="file") -foreign libc { - getrusage :: proc(who:c.int,usage:^Rusage) -> c.int --- -} - -// The bytes of memory the process holds: the most it has held; 0 when the system does not say. -process_memory_bytes :: proc "contextless" () -> u64 { - usage:Rusage - if getrusage(0,&usage)!=0 {return 0} - when ODIN_OS==.Linux {return u64(usage.max_rss)*1024} else {return u64(usage.max_rss)} -} diff --git a/platform/process_memory_test.odin b/platform/process_memory_test.odin new file mode 100644 index 000000000..0f034586b --- /dev/null +++ b/platform/process_memory_test.odin @@ -0,0 +1,16 @@ +package platform + +import "core:testing" + +// What the system says this process holds grows with memory it writes to. +@(test) +process_memory_counts_what_the_process_touches :: proc(t:^testing.T) { + before:=process_memory() + testing.expect(t,before.resident>0,"the system says how much of the process is resident") + testing.expect(t,before.private>0,"and how much is its own") + block:=make([]u8,64<<20) + defer delete(block) + for i:=0;i=before.private+32<<20,"64 MB written raise the private memory: %d -> %d",before.private,after.private) +} diff --git a/platform/process_memory_windows.odin b/platform/process_memory_windows.odin index 81553e56e..8b9bab14c 100644 --- a/platform/process_memory_windows.odin +++ b/platform/process_memory_windows.odin @@ -15,9 +15,9 @@ foreign kernel32 { K32GetProcessMemoryInfo :: proc(process:win32.HANDLE,counters:^Process_Memory_Counters,size:u32) -> win32.BOOL --- } -// The bytes of memory the process holds: its working set; 0 when the system does not say. -process_memory_bytes :: proc "contextless" () -> u64 { +// The working set, and the private commit (pagefile). +process_memory :: proc "contextless" () -> Process_Memory { counters:=Process_Memory_Counters{cb=size_of(Process_Memory_Counters)} - if !K32GetProcessMemoryInfo(win32.GetCurrentProcess(),&counters,counters.cb) {return 0} - return u64(counters.working_set) + if !K32GetProcessMemoryInfo(win32.GetCurrentProcess(),&counters,counters.cb) {return {}} + return {u64(counters.working_set),u64(counters.pagefile)} } diff --git a/plugins/playtest/editor/qa.odin b/plugins/playtest/editor/qa.odin index a771d4613..52fff658d 100644 --- a/plugins/playtest/editor/qa.odin +++ b/plugins/playtest/editor/qa.odin @@ -192,7 +192,7 @@ qa_arm :: proc(s:^editor.Session,r:^editor.Run,limits:Qa_Limits) -> ^Run_Qa { q.stuck,q.fell=false,false _,body:=editor.play_focused(w) if problems:=host.app_world_storage(s.app,w.data,core.Problems);problems!=nil {q.serial=problems.serial} - q.memory_base=platform.process_memory_bytes() + q.memory_base=platform.process_memory().private q.memory_next=q.ran+QA_MEMORY_EVERY if q.broken=="" { if e:=qa_entry_next(q);e!=nil {e.kind,q.armed_at=.Monitors,q.log_count-1} @@ -387,7 +387,7 @@ qa_tick :: proc(s:^editor.Session,r:^editor.Run) { } if limits.memory_mb>0 && q.ran>=q.memory_next { q.memory_next=q.ran+QA_MEMORY_EVERY - now:=platform.process_memory_bytes() + now:=platform.process_memory().private grown:=now>q.memory_base?f32(now-q.memory_base)/(1024*1024):0 if grown>limits.memory_mb {qa_violation_add(q,{kind=.Memory,severity=.Warning,tick=tick,position=at,value=grown,limit=limits.memory_mb},true)} } diff --git a/plugins/profiler/editor/profiler.odin b/plugins/profiler/editor/profiler.odin index 97dc9f0f1..f273fa56b 100644 --- a/plugins/profiler/editor/profiler.odin +++ b/plugins/profiler/editor/profiler.odin @@ -2,6 +2,7 @@ package profiler_editor import "core:fmt" import "core:hash" +import "mirai:editor/kit" import "mirai:core" import "mirai:editor" import "mirai:host" @@ -10,8 +11,10 @@ import "mirai:ui" // A panel of the editor, under View > Profiler, that records frames into the // captures of the profile.* methods and shows the last one: the time of each -// frame, the zones of the threads and the GPU passes of the frame chosen, and -// the zones that took most. An agent and the panel see the same captures. +// frame, then a tab per track: the zones of the threads of the frame chosen +// and the zones that took most; its GPU passes where they ran and the passes +// that took most; the memory over the frames. An agent and the panel see the +// same captures. @(storage) Profiler_Api :: struct {provider:editor.Api_Provider} @@ -43,6 +46,15 @@ FRAMES_DEFAULT :: 120 ROW :: f32(16) // of a row of the timeline, in points GRAPH :: f32(72) // height of the graph of the frames +TABS :: [?]string{"CPU","GPU","Memory"} +Tab :: enum int {Cpu,Gpu,Memory} +TAB_GAP :: f32(8) + +Top_Pass :: struct { + name: int, // into the pass names of the capture + ms: f64, // per measured frame +} + Top_Zone :: struct { name: int, // into the names of the capture worker: bool, @@ -63,6 +75,10 @@ Profiler_Panel :: struct { graph_ms: f64, // the top of the graph: above it a frame is cut and marked top: [TOP_ZONES]Top_Zone, top_count: int, + passes: [TOP_ZONES]Top_Pass, + pass_count: int, + tab: int, + memory_mb: f64, // the top of the memory graph error: [128]u8, error_length: int, capture: ^editor.Profile_Capture, // the one shown, for the drawings of this frame @@ -97,13 +113,41 @@ profiler_panel :: proc(s:^editor.Session,p:^Profiler_Panel,app:^host.App) { p.frame=clamp(int((ctx.pointer.x-r.x)/r.w*f32(len(c.frames))),0,len(c.frames)-1) } } - ui.label(ctx,profiler_frame_text(p,c),ui.metric(ctx,"font_small"),ui.tone(ctx,"text")) - rows:=f32(p.depth+p.thread_count+1) - editor.drawing(g,ui.id(ctx,"timeline"),ui.GROW,ui.fixed(rows*(ROW+2)+ROW),timeline_draw,p) - editor.drawing(g,ui.id(ctx,"zones"),ui.GROW,ui.fixed(f32(p.top_count)*ROW+ROW),zones_draw,p) + titles:=TABS + kit.tabs(ctx,ui.id(ctx,"tracks"),titles[:],&p.tab,ui.fixed(240),28) + small:=ui.metric(ctx,"font_small") + // Every tab is as tall as the tallest: the panel, held by its bottom, keeps its tabs under the pointer. + if ui.box(ctx,{width=ui.GROW,height=ui.fixed(tabs_height(p,small)),direction=.Column,gap=TAB_GAP}) { + switch Tab(p.tab) { + case .Cpu: + ui.label(ctx,profiler_frame_text(p,c),small,ui.tone(ctx,"text")) + rows:=f32(p.depth+p.thread_count) + editor.drawing(g,ui.id(ctx,"timeline"),ui.GROW,ui.fixed(rows*(ROW+2)+ROW),timeline_draw,p) + editor.drawing(g,ui.id(ctx,"zones"),ui.GROW,ui.fixed(f32(p.top_count)*ROW+ROW),zones_draw,p) + case .Gpu: + ui.label(ctx,gpu_frame_text(p,c),small,ui.tone(ctx,"text")) + editor.drawing(g,ui.id(ctx,"gpu_timeline"),ui.GROW,ui.fixed(ROW+2+ROW),timeline_draw,p) + editor.drawing(g,ui.id(ctx,"passes"),ui.GROW,ui.fixed(f32(p.pass_count)*ROW+ROW),passes_draw,p) + case .Memory: + if !c.memory {ui.label(ctx,"This capture did not sample the memory",small,ui.tone(ctx,"text_muted"));break} + ui.label(ctx,memory_frame_text(p,c),small,ui.tone(ctx,"text")) + editor.drawing(g,ui.id(ctx,"memory"),ui.GROW,ui.fixed(GRAPH),memory_draw,p) + } + } } } +// The height of the tallest tab of the capture shown: a label of size font +// over each one's drawings. +@(private="file") +tabs_height :: proc(p:^Profiler_Panel,font:f32) -> f32 { + label:=font+6 + cpu:=label+TAB_GAP+f32(p.depth+p.thread_count)*(ROW+2)+ROW+TAB_GAP+f32(p.top_count)*ROW+ROW + gpu:=label+TAB_GAP+ROW+2+ROW+TAB_GAP+f32(p.pass_count)*ROW+ROW + memory:=label+TAB_GAP+GRAPH + return max(cpu,gpu,memory) +} + @(private="file") profiler_header :: proc(s:^editor.Session,p:^Profiler_Panel,app:^host.App) { ctx:=s.gui.ctx @@ -119,7 +163,7 @@ profiler_header :: proc(s:^editor.Session,p:^Profiler_Panel,app:^host.App) { ui.number_field(ctx,ui.id(ctx,"frames_asked"),&p.frames,{width=ui.fixed(72),decimals=0,speed=1,has_min=true,min=2,has_max=true,max=f64(host.PROFILE_FRAMES_MAX),disabled=recording}) if ui.button(ctx,ui.id(ctx,"record"),{label="Record",variant="accent",size=ui.metric(ctx,"font_small"),disabled=recording}).clicked { p.error_length=0 - if _,error:=editor.profile_begin(s,int(p.frames),"profiler panel");error!="" {p.error_length=copy(p.error[:],error)} + if _,error:=editor.profile_begin(s,int(p.frames),"profiler panel",memory=true);error!="" {p.error_length=copy(p.error[:],error)} } } } @@ -129,7 +173,23 @@ profiler_header :: proc(s:^editor.Session,p:^Profiler_Panel,app:^host.App) { // zones that took most, and the frame nearest the median as the one chosen. @(private="file") profiler_show :: proc(p:^Profiler_Panel,c:^editor.Profile_Capture) { - p.shown,p.frame,p.workers,p.depth,p.thread_count,p.top_count=c.id,0,0,1,0,0 + p.shown,p.frame,p.workers,p.depth,p.thread_count,p.top_count,p.pass_count,p.memory_mb=c.id,0,0,1,0,0,0,0 + for f in c.frames {p.memory_mb=max(p.memory_mb,mb(f.memory.world),mb(i64(f.memory.process.private)),mb(i64(f.memory.gpu)))} + p.memory_mb=max(p.memory_mb*1.1,1) + for values,name in editor.profile_passes_by_name(c) { + total:f64 + for ms in values {total+=ms} + entry:=Top_Pass{name,total/f64(max(len(values),1))} + at:=p.pass_count + for at>0 && p.passes[at-1].ms stri if p.frame>=len(c.frames) {return ""} f:=c.frames[p.frame] bounds:=[editor.Profile_Bound]string{.Cpu="CPU",.Gpu="GPU",.Paced="pacing"} - gpu:=fmt.tprintf("%.2f ms",f.gpu_ms) if f.gpu_measured else "not measured" - return fmt.tprintf("Frame %d · %.2f ms · CPU %.2f ms · GPU %s · waits for %s", - p.frame+1,editor.profile_frame_ms(f),editor.profile_frame_cpu_ms(f),gpu,bounds[editor.profile_frame_bound(f)]) + return fmt.tprintf("Frame %d · %.2f ms · CPU %.2f ms · waits for %s", + p.frame+1,editor.profile_frame_ms(f),editor.profile_frame_cpu_ms(f),bounds[editor.profile_frame_bound(f)]) } +@(private="file") +gpu_frame_text :: proc(p:^Profiler_Panel,c:^editor.Profile_Capture) -> string { + if p.frame>=len(c.frames) {return ""} + f:=c.frames[p.frame] + if !f.gpu_measured {return fmt.tprintf("Frame %d · GPU not measured: a submission of the frame did not come back",p.frame+1)} + return fmt.tprintf("Frame %d · GPU %.2f ms · %d passes",p.frame+1,f.gpu_ms,f.pass_count) +} + +@(private="file") +memory_frame_text :: proc(p:^Profiler_Panel,c:^editor.Profile_Capture) -> string { + if p.frame>=len(c.frames) {return ""} + m:=c.frames[p.frame].memory + return fmt.tprintf("Frame %d · World %.1f MB · process private %.1f MB, resident %.1f MB · GPU %.1f MB", + p.frame+1,mb(m.world),mb(i64(m.process.private)),mb(i64(m.process.resident)),mb(i64(m.gpu))) +} + +@(private="file") +mb :: proc(bytes:i64) -> f64 {return f64(bytes)/(1<<20)} + // A color of its own for each name, among muted hues. @(private="file") zone_color :: proc(name:string) -> ui.Color { @@ -229,8 +307,9 @@ frames_draw :: proc(ctx:^ui.Context,rect:ui.Rect,user:rawptr) { ui.draw_text(ctx,text,rect.x+rect.w-ui.text_width(&ctx.font,text,ui.metric(ctx,"font_small"))-6,rect.y+6,ui.metric(ctx,"font_small"),ui.tone(ctx,"text_muted")) } -// The frame chosen: the zones of the main thread by depth, those of the -// other threads, and the GPU passes where they ran, on one time axis. +// The frame chosen on one time axis, that of its CPU frame and what ran +// after it: in the CPU tab the zones of the main thread by depth and those +// of the other threads; in the GPU tab its passes where they ran. @(private="file") timeline_draw :: proc(ctx:^ui.Context,rect:ui.Rect,user:rawptr) { p:=(^Profiler_Panel)(user) @@ -252,6 +331,15 @@ timeline_draw :: proc(ctx:^ui.Context,rect:ui.Rect,user:rawptr) { if ui.text_width(&ctx.font,text,ui.metric(ctx,"font_small"))+6 Date: Sat, 14 Nov 2026 20:09:48 -0300 Subject: [PATCH 3/7] 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. Co-Authored-By: Claude Opus 5.5 --- plugins/profiler/editor/profiler.odin | 52 +++++++++++++++------------ 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/plugins/profiler/editor/profiler.odin b/plugins/profiler/editor/profiler.odin index f273fa56b..fec5b8817 100644 --- a/plugins/profiler/editor/profiler.odin +++ b/plugins/profiler/editor/profiler.odin @@ -78,7 +78,7 @@ Profiler_Panel :: struct { passes: [TOP_ZONES]Top_Pass, pass_count: int, tab: int, - memory_mb: f64, // the top of the memory graph + memory_range: [3][2]f64, // of the World, the private memory and the GPU, in MB: the lowest and highest of the capture error: [128]u8, error_length: int, capture: ^editor.Profile_Capture, // the one shown, for the drawings of this frame @@ -173,9 +173,11 @@ profiler_header :: proc(s:^editor.Session,p:^Profiler_Panel,app:^host.App) { // zones that took most, and the frame nearest the median as the one chosen. @(private="file") profiler_show :: proc(p:^Profiler_Panel,c:^editor.Profile_Capture) { - p.shown,p.frame,p.workers,p.depth,p.thread_count,p.top_count,p.pass_count,p.memory_mb=c.id,0,0,1,0,0,0,0 - for f in c.frames {p.memory_mb=max(p.memory_mb,mb(f.memory.world),mb(i64(f.memory.process.private)),mb(i64(f.memory.gpu)))} - p.memory_mb=max(p.memory_mb*1.1,1) + p.shown,p.frame,p.workers,p.depth,p.thread_count,p.top_count,p.pass_count=c.id,0,0,1,0,0,0 + p.memory_range={{max(f64),0},{max(f64),0},{max(f64),0}} + for f in c.frames { + for value,k in memory_values(f) {p.memory_range[k]={min(p.memory_range[k][0],value),max(p.memory_range[k][1],value)}} + } for values,name in editor.profile_passes_by_name(c) { total:f64 for ms in values {total+=ms} @@ -385,34 +387,38 @@ passes_draw :: proc(ctx:^ui.Context,rect:ui.Rect,user:rawptr) { } } -// The memory at the end of each frame: of the World, private of the -// process and of the GPU, a line each; the frame chosen is marked. +// The memory at the end of each frame, a band each for the World, the +// private memory of the process and the GPU, each from its lowest to its +// highest in the capture; the frame chosen is marked. @(private="file") memory_draw :: proc(ctx:^ui.Context,rect:ui.Rect,user:rawptr) { p:=(^Profiler_Panel)(user) c:=p.capture ui.draw_rounded(ctx,rect,ui.metric(ctx,"radius_control"),ui.tone(ctx,"field")) if c==nil || len(c.frames)==0 {return} - step:=rect.w/f32(len(c.frames)) - tones:=[3]string{"accent","text","text_muted"} - for f,i in c.frames { - values:=[3]f64{mb(f.memory.world),mb(i64(f.memory.process.private)),mb(i64(f.memory.gpu))} - x:=rect.x+f32(i)*step - for value,k in values { - y:=rect.y+rect.h-2-f32(min(value/p.memory_mb,1))*(rect.h-4) - ui.draw_rect(ctx,{x,y,max(step,1),1.5},ui.tone(ctx,tones[k])) - } - } - ui.draw_rect(ctx,{rect.x+f32(p.frame)*step,rect.y,1,rect.h},ui.color_alpha(ui.tone(ctx,"text_faint"),120)) small:=ui.metric(ctx,"font_small") - names:=[3]string{"World","private","GPU"} - x:=rect.x+6 + label:=f32(150) + x0,w:=rect.x+label,rect.w-label + band:=rect.h/3 + step:=w/f32(len(c.frames)) + names:=[3]string{"World","Private","GPU"} for name,k in names { - ui.draw_text(ctx,name,x,rect.y+4,small,ui.tone(ctx,tones[k])) - x+=ui.text_width(&ctx.font,name,small)+12 + top:=rect.y+band*f32(k) + low,high:=p.memory_range[k][0],p.memory_range[k][1] + ui.draw_text(ctx,fmt.tprintf("%s %.1f-%.1f MB",name,low,high),rect.x+6,top+band/2-small/2,small,ui.tone(ctx,"text_muted")) + if k>0 {ui.draw_rect(ctx,{rect.x,top,rect.w,1},ui.color_alpha(ui.tone(ctx,"text_faint"),60))} + for f,i in c.frames { + at:=0.5 if high<=low else (memory_values(f)[k]-low)/(high-low) + y:=top+band-4-f32(at)*(band-8) + ui.draw_rect(ctx,{x0+f32(i)*step,y,max(step,1),1.5},ui.tone(ctx,"accent")) + } } - text:=fmt.tprintf("%.0f MB",p.memory_mb) - ui.draw_text(ctx,text,rect.x+rect.w-ui.text_width(&ctx.font,text,small)-6,rect.y+4,small,ui.tone(ctx,"text_muted")) + ui.draw_rect(ctx,{x0+f32(p.frame)*step,rect.y,1,rect.h},ui.color_alpha(ui.tone(ctx,"text_faint"),120)) +} + +@(private="file") +memory_values :: proc(f:editor.Profile_Frame_Record) -> [3]f64 { + return {mb(f.memory.world),mb(i64(f.memory.process.private)),mb(i64(f.memory.gpu))} } // The zones that took most per frame over the capture, the parts of the frame left out. From d26240e320fa223e69182581311294be54793a41 Mon Sep 17 00:00:00 2001 From: 5hif7y <44300833+5hif7y@users.noreply.github.com> Date: Sat, 14 Nov 2026 20:35:35 -0300 Subject: [PATCH 4/7] 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. Co-Authored-By: Claude Opus 5.5 --- .agents/skills/mirai-core/references/host.md | 6 +- core/world_data.odin | 89 +++----------- core/world_memory_test.odin | 119 +++++++++++++++++-- core/world_schema.odin | 93 ++++++++++++++- core/worlds_gen_test.odin | 20 ++++ host/profile.odin | 3 +- 6 files changed, 242 insertions(+), 88 deletions(-) diff --git a/.agents/skills/mirai-core/references/host.md b/.agents/skills/mirai-core/references/host.md index 1cc64e88d..8768a9ede 100644 --- a/.agents/skills/mirai-core/references/host.md +++ b/.agents/skills/mirai-core/references/host.md @@ -203,8 +203,10 @@ request, the device writes no per-pass timestamps. A recording that asks for mem the frame as its part `memory`: what the tables and storages of the World that ran last hold (`core.world_memory`, the game loop's World, else the editor's), the private and resident memory of the process (`platform.process_memory`) and the device memory. `core.world_memory` adds up the -capacity of the arrays, maps and slices of the tables and storages, without stepping into flat -values; what game code allocates outside its storages is not in it. The editor exposes it with +capacity of the arrays, maps and slices of the tables and storages, reading the containers that +`world_info_index` found once in their types (`World_Schema.memory`), so a sample costs one read per +container header, those of every slot of a pool included; what game code allocates outside its +storages is not in it. 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, diff --git a/core/world_data.odin b/core/world_data.odin index c25a5c339..25cd64da9 100644 --- a/core/world_data.odin +++ b/core/world_data.odin @@ -305,33 +305,30 @@ world_detach :: proc(info:^World_Info,world,shared:rawptr) { // The bytes the tables and storages of a World hold behind their pointers: // the capacity of the arrays, maps and slices of each table and storage of -// the place World, and of its entities. It reads capacities, not the -// allocator, so what game code allocates outside its storages is not in it. -// elements: the values it had to step into because they hold memory of -// their own (rows of a table whose component is not flat, items of an array -// of such values), which is what makes it cost more than one read per -// container. -world_memory :: proc(info:^World_Info,data:rawptr) -> (bytes,elements:int) { - if info==nil || data==nil {return} - add :: proc(bytes,elements:^int,b,e:int) {bytes^+=b;elements^+=e} - b,e:=value_bytes(entities_of(info,data),type_info_of(Entities)) - add(&bytes,&elements,b,e) - for &t in info.tables { +// the place World, and of its entities, as the ops world_info_index compiled +// from their types read them. It reads capacities, not the allocator, so +// what game code allocates outside its storages is not in it. 0 for a +// World_Info without schema. +world_memory :: proc(info:^World_Info,data:rawptr) -> (bytes:int) { + if info==nil || data==nil || info.schema==nil {return} + ws:=info.schema + bytes=memory_read(ws.memory,ws.entities,entities_of(info,data)) + for &t,i in info.tables { if t.place!=.World {continue} - b,e=table_bytes(table_of(data,&t),t.info,t.flat) - add(&bytes,&elements,b,e) + table:=table_of(data,&t) + bytes+=table_bytes(table,t.info.size) + held:=ws.tables[i].held + if held[0]==held[1] {continue} + for row in 0.. (bytes,elements:int) { - size:=info.size +table_bytes :: proc(t:^Raw_Table,size:int) -> (bytes:int) { bytes=(t.values.cap+t.added.cap+t.edited.cap)*size bytes+=cap(t.entities)*size_of(Entity)+cap(t.stamps)*size_of(u64)+cap(t.page_stamps)*size_of(u64) bytes+=cap(t.off)*size_of(bool)+cap(t.turns)*size_of(u64)+cap(t.pending)*size_of(Pending) @@ -339,57 +336,5 @@ table_bytes :: proc(t:^Raw_Table,info:^runtime.Type_Info,flat:bool) -> (bytes,el bytes+=len(t.locks)*size_of(Lock)+cap(t.rows)*size_of([]i32) for page in t.rows {bytes+=len(page)*size_of(i32)} when CHECK_WRITES {bytes+=cap(t.settled.values)} - if flat {return} - for i in 0.. (bytes,elements:int) { - base:=runtime.type_info_base(info) - #partial switch v in base.variant { - case runtime.Type_Info_Struct: - for i in 0..=empty+100*size_of(Entity),"100 rows and their entities add to it: %d -> %d",empty,rows) - testing.expect_value(t,elements,0) - for i in 0..<1000 {append(&w.list.items,i32(i))} - items,_:=world_memory(&info,&w) - testing.expect_value(t,items-rows,cap(w.list.items)*size_of(i32)) + append(&w.pools.slots,Mem_Slot{}) + for i in 0..<1000 {append(&w.pools.slots[0].points,[3]f32{f32(i),0,0})} + items:=world_memory(&info,&w) + testing.expect_value(t,items-rows,cap(w.pools.slots)*size_of(Mem_Slot)+cap(w.pools.slots[0].points)*size_of([3]f32)) +} + +// The ops world_info_index compiles read what reading the types at every +// step reads, in arrays of values that hold arrays, fixed or not. +@(test) +the_compiled_memory_ops_read_what_the_types_say :: proc(t:^testing.T) { + info,described:=test_info(t,Mem_World) + if !described {return} + defer test_info_destroy(&info) + w:Mem_World + if !testing.expect(t,world_init(&info,.World,&w,context.allocator)) {return} + defer world_destroy(&info,.World,&w) + defer delete(w.pools.names) // world_destroy frees arrays and slices, not maps + for i in 0..<5 { + append(&w.pools.slots,Mem_Slot{tag=u32(i)}) + for _ in 0..5*size_of(Mem_Slot),"the pools hold something") +} + +@(private="file") +reflected :: proc(data:rawptr,info:^runtime.Type_Info) -> int {bytes,_:=reflected_bytes(data,info);return bytes} + +// What a value holds behind its pointers, read from its type at every step: +// what the compiled ops of world_memory must give. +@(private="file") +reflected_bytes :: proc(data:rawptr,info:^runtime.Type_Info) -> (bytes,elements:int) { + base:=runtime.type_info_base(info) + #partial switch v in base.variant { + case runtime.Type_Info_Struct: + for i in 0.. ^Wor return nil } } + ops:=make([dynamic]Memory_Op,allocator) + entities,ok:=memory_compile(&ops,type_info_of(Entities)) + ws.entities=entities + for t,i in info.tables {ws.tables[i].held,ok=memory_compile(&ops,t.info);if !ok {break}} + for s,i in info.storages {if ok {ws.storages[i].held,ok=memory_compile(&ops,s.info)}} + ws.memory=ops[:] + if !ok { + world_schema_destroy(ws) + return nil + } return ws } +// A container a value holds: where it is in the value and how to read what +// it holds. A fixed array of values that hold containers is an op whose +// children are those of one element; a dynamic array or a slice of them +// has children too, read for each element it has. +Memory_Op :: struct { + offset:uintptr, + kind:Memory_Kind, + count:int, // Array: elements + elem_size:int, + map_info:^runtime.Map_Info, + children:[2]i32, // the ops of one element, from its start +} +Memory_Kind :: enum u8 {Dynamic,Slice,Map,Array} + +// Appends the ops of what a type holds behind pointers: first its own, then +// those of each of their elements. held: the range of its own. +@(private="file") +memory_compile :: proc(ops:^[dynamic]Memory_Op,info:^runtime.Type_Info) -> (held:[2]i32,ok:bool) { + own:=make([dynamic]Memory_Op,context.temp_allocator) + elems:=make([dynamic]^runtime.Type_Info,context.temp_allocator) + memory_collect(&own,&elems,info,0) + held={i32(len(ops)),i32(len(ops)+len(own))} + if _,error:=append(ops,..own[:]);error!=nil {return held,false} + for elem,i in elems { + if elem==nil {continue} + children,compiled:=memory_compile(ops,elem) + if !compiled {return held,false} + ops[int(held[0])+i].children=children + } + return held,true +} + +// The containers of a value at offset in it, not of their elements: elems +// gets, by op, the type of an element that holds containers. +@(private="file") +memory_collect :: proc(own:^[dynamic]Memory_Op,elems:^[dynamic]^runtime.Type_Info,info:^runtime.Type_Info,offset:uintptr) { + if value_flat(info) {return} + add :: proc(own:^[dynamic]Memory_Op,elems:^[dynamic]^runtime.Type_Info,op:Memory_Op,elem:^runtime.Type_Info) { + append(own,op) + append(elems,elem if elem!=nil && !value_flat(elem) else nil) + } + #partial switch v in runtime.type_info_base(info).variant { + case runtime.Type_Info_Struct: + for i in 0.. (bytes:int) { + for op in ops[held[0]:held[1]] { + at:=rawptr(uintptr(data)+op.offset) + count,first:=0,rawptr(nil) + switch op.kind { + case .Dynamic: + a:=cast(^runtime.Raw_Dynamic_Array)at + bytes+=a.cap*op.elem_size + count,first=a.len,a.data + case .Slice: + a:=cast(^runtime.Raw_Slice)at + bytes+=a.len*op.elem_size + count,first=a.len,a.data + case .Map: + m:=cast(^runtime.Raw_Map)at + if m.data!=0 && op.map_info!=nil {bytes+=int(runtime.map_total_allocation_size(uintptr(runtime.map_cap(m^)),op.map_info))} + case .Array: + count,first=op.count,at + } + if op.children[0]==op.children[1] {continue} + for i in 0.. (s:^serial.Schema,index:serial.Type_Index,ok:bool) { @@ -194,7 +285,7 @@ world_schema_destroy :: proc(ws:^World_Schema) { for &v in ws.storages {value_schema_destroy(&v,allocator)} for &v in ws.records {value_schema_destroy(&v,allocator)} delete(ws.tables,allocator);delete(ws.storages,allocator);delete(ws.records,allocator) - delete(ws.table_hashes,allocator) + delete(ws.table_hashes,allocator);delete(ws.memory) delete(ws.choices,allocator);delete(ws.schema.named) serial.schema_destroy(&ws.schema) _=runtime.mem_free(ws,allocator) diff --git a/core/worlds_gen_test.odin b/core/worlds_gen_test.odin index 6844430a2..878af0842 100644 --- a/core/worlds_gen_test.odin +++ b/core/worlds_gen_test.odin @@ -440,6 +440,25 @@ test_info_rewound_world :: proc(allocator:=context.allocator) -> (info:World_Inf return info,true } +@(private="file") +test_info_mem_world :: proc(allocator:=context.allocator) -> (info:World_Info,ok:bool) { + context.allocator=allocator + tables,tables_error:=make([]Table_Info,1) + storages,storages_error:=make([]Storage_Info,1) + if tables_error!=nil || storages_error!=nil { + delete(tables);delete(storages) + return + } + tables[0]={name="values",library="",type=typeid_of(type_of((^Mem_World)(nil).values.values[0])),info=type_info_of(type_of((^Mem_World)(nil).values.values[0])),offset=offset_of(Mem_World,values),place=.World,shared=false,replicated=.None,game=false,default=nil,description="",fields=nil,single=false,far=false,derived=false,was="",base=false,locks=nil} + storages[0]={name="Mem_Pools",library="core",type=typeid_of(Mem_Pools),info=type_info_of(Mem_Pools),offset=offset_of(Mem_World,pools),place=.World,setting=.None,default=nil,by_world=false,for_device=nil,persist=false,keep=false,plugin="",game=false,local=false,procs={}} + info.sizes={.World=size_of(Mem_World),.Shared=1,.Editor=1} + info.aligns={.World=align_of(Mem_World),.Shared=1,.Editor=1} + info.entities,info.revisions=offset_of(Mem_World,entities),offset_of(Mem_World,storage_revisions) + info.tables,info.storages=tables,storages + world_info_index(&info) + return info,true +} + @(private="file") test_info_run_world :: proc(allocator:=context.allocator) -> (info:World_Info,ok:bool) { context.allocator=allocator @@ -544,6 +563,7 @@ test_info :: proc(t:^testing.T,$W:typeid,allocator:=context.allocator,loc:=#call else when W==Rev_World {info,ok=test_info_rev_world(allocator)} else when W==Roll_World {info,ok=test_info_roll_world(allocator)} else when W==Rewound_World {info,ok=test_info_rewound_world(allocator)} + else when W==Mem_World {info,ok=test_info_mem_world(allocator)} else when W==Run_World {info,ok=test_info_run_world(allocator)} else when W==Run_Kept_World {info,ok=test_info_run_kept_world(allocator)} else when W==Indexed_World {info,ok=test_info_indexed_world(allocator)} diff --git a/host/profile.odin b/host/profile.odin index 34ee75f3a..9b90330dc 100644 --- a/host/profile.odin +++ b/host/profile.odin @@ -297,8 +297,7 @@ profile_frame_end :: proc(app:^App,drawn:bool) { @(private) profile_memory_sample :: proc(app:^App) -> (m:Profile_Memory) { if world:=app.profiler.memory_world;world!=nil && app.info!=nil { - bytes,_:=core.world_memory(app.info,world) - m.world=i64(bytes) + m.world=i64(core.world_memory(app.info,world)) } m.process=platform.process_memory() if app.device!=nil {m.gpu,_=rhi.device_memory(app.device)} From 2181aac11bef4462a7571e9e2f002b6ac60c7afe Mon Sep 17 00:00:00 2001 From: tomaszbk-pc Date: Sun, 15 Nov 2026 09:47:24 -0300 Subject: [PATCH 5/7] Leave unmeasured a frame whose GPU times cannot be read, and keep the 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 --- host/profile.odin | 8 ++++++-- host/profile_gpu_test.odin | 42 ++++++++++++++++++++++++++++++++++++++ host/profile_test.odin | 39 ----------------------------------- render/frame.odin | 8 ++++++-- rhi/metal_device.odin | 9 +++++--- rhi/vulkan_device.odin | 7 ++++--- 6 files changed, 64 insertions(+), 49 deletions(-) create mode 100644 host/profile_gpu_test.odin diff --git a/host/profile.odin b/host/profile.odin index 4e4bc624b..62fb412cb 100644 --- a/host/profile.odin +++ b/host/profile.odin @@ -265,8 +265,12 @@ profile_frame_end :: proc(app:^App,drawn:bool) { if p.frame_countlast.device_first && !profile_gpu_measured(last) && p.gpu_nextf.device_first && !profile_gpu_measured(f) {waiting=true;break} + } + } if !waiting || p.late>=PROFILE_GPU_LATE {profile_stop(app);return} p.late+=1 } diff --git a/host/profile_gpu_test.odin b/host/profile_gpu_test.odin new file mode 100644 index 000000000..ebf910f15 --- /dev/null +++ b/host/profile_gpu_test.odin @@ -0,0 +1,42 @@ +package host + +import "core:testing" +import render "mirai:render" +import "mirai:rhi" +import "mirai:rhi/shared_device" + +// A frame that submits twice (an offscreen capture, then its window) gets +// the GPU passes of both submissions, though they come back in later frames. +@(test) +a_recorded_frame_keeps_the_gpu_passes_of_every_submission :: proc(t: ^testing.T) { + shared_device.hold(t) + app, error := app_create({gpu = true, gpu_validation = true, window_desc = {size = {64, 48}}, fixed_dt = 1.0 / 60, threads = 1}) + if app == nil { + testing.expectf(t, rhi.gpu_skip_allowed(), "no application with a GPU: %v (set MIRAI_ALLOW_GPU_SKIP=1 to skip)", error) + return + } + defer app_destroy(app) + FRAMES :: 4 + if !testing.expect(t, profile_start(app, FRAMES)) {return} + for _ in 0 ..< FRAMES + PROFILE_GPU_LATE { + if !app.profiler.recording {break} + if !testing.expect(t, app_frame_begin(app)) {return} + if app.profiler.late == 0 {testing.expect(t, render.renderer_frame(app.renderer, {offscreen = true, ui_only = true, time = app.time}), app.renderer.error)} + app_frame_end(app) + } + recording := profile_recording(app) + testing.expect(t, recording.complete, "the recording waited for the GPU times of its last frame") + testing.expect_value(t, len(recording.frames), FRAMES) + for frame, i in recording.frames { + testing.expectf(t, frame.drawn, "frame %d drew", i) + testing.expect_value(t, frame.device_end - frame.device_first, 2) + testing.expectf(t, profile_gpu_measured(frame), "frame %d got %d of its 2 submissions", i, frame.gpu_submissions) + total: f64 + for pass in recording.passes[frame.pass_first:][:frame.pass_count] { + testing.expectf(t, pass.end_ns >= pass.start_ns, "frame %d: a pass ends before it starts", i) + total += f64(pass.end_ns - pass.start_ns) / 1e6 + } + testing.expectf(t, abs(total - f64(frame.gpu_ms)) < 0.01, "frame %d: gpu_ms %.3f is not the sum of its passes %.3f", i, frame.gpu_ms, total) + } + testing.expectf(t, rhi.device_validation_errors(app.device) == 0, "validation: %s", rhi.device_validation_message(app.device)) +} diff --git a/host/profile_test.odin b/host/profile_test.odin index 507a4cbec..ebbdf97e3 100644 --- a/host/profile_test.odin +++ b/host/profile_test.odin @@ -1,9 +1,6 @@ package host import "core:testing" -import render "mirai:render" -import "mirai:rhi" -import "mirai:rhi/shared_device" import "mirai:std/base" // A request reserves only the rings, not the ~13 MB of buffers that only an @@ -51,39 +48,3 @@ a_recording_adds_its_buffers_to_ready_rings :: proc(t:^testing.T) { testing.expect_value(t, len(app.profiler.passes), PROFILE_PASSES) testing.expect_value(t, len(app.profiler.cursors), 1) } - -// A frame that submits twice (an offscreen capture, then its window) gets -// the GPU passes of both submissions, though they come back in later frames. -@(test) -a_recorded_frame_keeps_the_gpu_passes_of_every_submission :: proc(t: ^testing.T) { - shared_device.hold(t) - app, error := app_create({gpu = true, gpu_validation = true, window_desc = {size = {64, 48}}, fixed_dt = 1.0 / 60, threads = 1}) - if app == nil { - testing.expectf(t, rhi.gpu_skip_allowed(), "no application with a GPU: %v (set MIRAI_ALLOW_GPU_SKIP=1 to skip)", error) - return - } - defer app_destroy(app) - FRAMES :: 4 - if !testing.expect(t, profile_start(app, FRAMES)) {return} - for _ in 0 ..< FRAMES + PROFILE_GPU_LATE { - if !app.profiler.recording {break} - if !testing.expect(t, app_frame_begin(app)) {return} - if app.profiler.late == 0 {testing.expect(t, render.renderer_frame(app.renderer, {offscreen = true, ui_only = true, time = app.time}), app.renderer.error)} - app_frame_end(app) - } - recording := profile_recording(app) - testing.expect(t, recording.complete, "the recording waited for the GPU times of its last frame") - testing.expect_value(t, len(recording.frames), FRAMES) - for frame, i in recording.frames { - testing.expectf(t, frame.drawn, "frame %d drew", i) - testing.expect_value(t, frame.device_end - frame.device_first, 2) - testing.expectf(t, profile_gpu_measured(frame), "frame %d got %d of its 2 submissions", i, frame.gpu_submissions) - total: f64 - for pass in recording.passes[frame.pass_first:][:frame.pass_count] { - testing.expectf(t, pass.end_ns >= pass.start_ns, "frame %d: a pass ends before it starts", i) - total += f64(pass.end_ns - pass.start_ns) / 1e6 - } - testing.expectf(t, abs(total - f64(frame.gpu_ms)) < 0.01, "frame %d: gpu_ms %.3f is not the sum of its passes %.3f", i, frame.gpu_ms, total) - } - testing.expectf(t, rhi.device_validation_errors(app.device) == 0, "validation: %s", rhi.device_validation_message(app.device)) -} diff --git a/render/frame.odin b/render/frame.odin index 27305bb83..0a00cb4f2 100644 --- a/render/frame.odin +++ b/render/frame.odin @@ -457,6 +457,7 @@ renderer_frame :: proc(r:^Renderer,frame:Frame_Desc) -> bool { } prepared:=time.tick_now() cmd,began:=rhi.frame_begin(d,!frame.offscreen) + gpu_submission_keep(r) if !began { set_error(r,"the device gave no frame to draw: %s",rhi.device_error(d)!=""?rhi.device_error(d):"the window is being resized or its image is not ready") return false @@ -464,7 +465,6 @@ renderer_frame :: proc(r:^Renderer,frame:Frame_Desc) -> bool { begun:=time.tick_now() slot:=int(rhi.frame_number(d)%rhi.MAX_FRAMES_IN_FLIGHT) read_stats(r,slot) - gpu_submission_keep(r) points_read(r,slot) pages_update(r,slot) if !frame.ui_only {shadow_movers_update(r)} @@ -1782,6 +1782,7 @@ Gpu_Submissions :: struct { timings: [RENDER_GPU_TIMINGS]rhi.Gpu_Timing, names: [RENDER_GPU_TIMINGS][rhi.MAX_TIMESTAMP_NAME_BYTES]u8, timing_count: int, + kept_end: u64, // one past the rhi frame of the last submission seen } // The submissions whose GPU timings came back since renderer_gpu_clear, in @@ -1796,7 +1797,10 @@ renderer_gpu_clear :: proc(r: ^Renderer) {r.submissions.count, r.submissions.tim gpu_submission_keep :: proc(r: ^Renderer) { timings, frame, measured := rhi.frame_timings(r.device) g := &r.submissions - if !measured || g.count >= len(g.list) || g.timing_count + len(timings) > len(g.timings) {return} + // A frame_begin that fails before the device collects leaves the timings of the one before. + if !measured || frame < g.kept_end {return} + g.kept_end = frame + 1 + if g.count >= len(g.list) || g.timing_count + len(timings) > len(g.timings) {return} first := g.timing_count for timing, i in timings { k := first + i diff --git a/rhi/metal_device.odin b/rhi/metal_device.odin index 65acbe84b..219f29c78 100644 --- a/rhi/metal_device.odin +++ b/rhi/metal_device.odin @@ -804,7 +804,8 @@ mtl_flush_ranges :: proc(d: ^Device, f: ^Mtl_Frame) { // The timings the last frame_begin collected and the frame_number of the // frame they measured, MAX_FRAMES_IN_FLIGHT before it; measured is false when -// that slot had committed nothing since its timings were last collected. +// that slot had committed nothing since its timings were last collected, or +// the device could not read its times. frame_timings :: proc(d: ^Device) -> (timings: []Gpu_Timing, frame: u64, measured: bool) { return d.timings[:d.timing_count], d.timing_frame, d.timing_measured } @@ -889,8 +890,10 @@ frame_begin :: proc(d: ^Device, present := true) -> (cmd: ^Command_List, ok: boo d.timing_count = 0 d.timing_frame, d.timing_measured = f.number, false if f.cmd != nil { - if mtl_wait(d, f.cmd, "frame") {mtl_collect_timestamps(d, f)} - d.timing_measured = f.submitted + if mtl_wait(d, f.cmd, "frame") { + mtl_collect_timestamps(d, f) + d.timing_measured = f.submitted + } f.submitted = false mtl_release(f.cmd) f.cmd = nil diff --git a/rhi/vulkan_device.odin b/rhi/vulkan_device.odin index a24594293..d67b347ff 100644 --- a/rhi/vulkan_device.odin +++ b/rhi/vulkan_device.odin @@ -1172,13 +1172,13 @@ vk_collect_timestamps :: proc(d: ^Device, f: ^Vk_Frame) { count := f.query_count f.query_count = 0 d.timing_count = 0 - d.timing_frame, d.timing_measured = f.number, f.submitted + d.timing_frame, d.timing_measured = f.number, f.submitted && f.queries != 0 f.submitted = false if f.queries == 0 || count < 2 {return} values: [MAX_TIMESTAMPS]u64 // The slot's submission already completed, so WAIT returns immediately. result := d.dvk.GetQueryPoolResults(d.handle, f.queries, 0, count, size_of(values), &values, size_of(u64), {._64, .WAIT}) - if result != .SUCCESS {vk_lost(d, result, "vkGetQueryPoolResults"); return} + if result != .SUCCESS {d.timing_measured = false; vk_lost(d, result, "vkGetQueryPoolResults"); return} gpu, now := values[count - 1], time.duration_nanoseconds(time.tick_diff({}, time.tick_now())) if d.calibrate != nil { infos := [2]vk.CalibratedTimestampInfoKHR{{sType = .CALIBRATED_TIMESTAMP_INFO_KHR, timeDomain = .DEVICE}, {sType = .CALIBRATED_TIMESTAMP_INFO_KHR, timeDomain = VK_HOST_TIME_DOMAIN}} @@ -1205,7 +1205,8 @@ vk_collect_timestamps :: proc(d: ^Device, f: ^Vk_Frame) { // The timings the last frame_begin collected and the frame_number of the // frame they measured, MAX_FRAMES_IN_FLIGHT before it; measured is false when -// that slot had submitted nothing since its timings were last collected. +// that slot had submitted nothing since its timings were last collected, or +// the device could not read its times. frame_timings :: proc(d: ^Device) -> (timings: []Gpu_Timing, frame: u64, measured: bool) { return d.timings[:d.timing_count], d.timing_frame, d.timing_measured } From 76ff1a5fd4cfc523177a3c199fe3692e7a728484 Mon Sep 17 00:00:00 2001 From: tomaszbk-pc Date: Sun, 15 Nov 2026 09:50:07 -0300 Subject: [PATCH 6/7] Count the memory a World holds in map values and in types that hold themselves 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 --- .agents/skills/mirai-core/references/host.md | 9 ++-- core/world_memory_test.odin | 31 +++++++++++-- core/world_schema.odin | 48 ++++++++++++++------ platform/process_memory_test.odin | 11 ++--- 4 files changed, 71 insertions(+), 28 deletions(-) diff --git a/.agents/skills/mirai-core/references/host.md b/.agents/skills/mirai-core/references/host.md index 8768a9ede..640146bc7 100644 --- a/.agents/skills/mirai-core/references/host.md +++ b/.agents/skills/mirai-core/references/host.md @@ -203,10 +203,11 @@ request, the device writes no per-pass timestamps. A recording that asks for mem the frame as its part `memory`: what the tables and storages of the World that ran last hold (`core.world_memory`, the game loop's World, else the editor's), the private and resident memory of the process (`platform.process_memory`) and the device memory. `core.world_memory` adds up the -capacity of the arrays, maps and slices of the tables and storages, reading the containers that -`world_info_index` found once in their types (`World_Schema.memory`), so a sample costs one read per -container header, those of every slot of a pool included; what game code allocates outside its -storages is not in it. The editor exposes it with +capacity of the arrays, maps and slices of the tables and storages and of what their elements and +map values hold, reading the containers that `world_info_index` found once in their types, so a +sample costs one read per container header, those of every slot of a pool included; the bytes of +strings are not in it (a string may be a literal or a view), nor what game code allocates outside +its storages. 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, diff --git a/core/world_memory_test.odin b/core/world_memory_test.odin index 52a8dd7aa..efa9751ea 100644 --- a/core/world_memory_test.odin +++ b/core/world_memory_test.odin @@ -4,10 +4,13 @@ import "base:runtime" import "core:testing" @(private) Mem_Slot :: struct {points:[dynamic][3]f32,tag:u32,bytes:[]u8} +@(private) Mem_Node :: struct {kids:[dynamic]Mem_Node,weight:u32} @(private) Mem_Pools :: struct { slots:[dynamic]Mem_Slot, fixed:[4]Mem_Slot, names:map[u32]u32, + lists:map[u32][dynamic]u32, + tree:[dynamic]Mem_Node, flat:[8]u32, } @@ -44,7 +47,8 @@ the_memory_of_a_world_follows_what_its_tables_and_storages_hold :: proc(t:^testi } // The ops world_info_index compiles read what reading the types at every -// step reads, in arrays of values that hold arrays, fixed or not. +// step reads, in arrays of values that hold arrays, fixed or not, in the +// values of a map and in a type that holds itself. @(test) the_compiled_memory_ops_read_what_the_types_say :: proc(t:^testing.T) { info,described:=test_info(t,Mem_World) @@ -53,7 +57,11 @@ the_compiled_memory_ops_read_what_the_types_say :: proc(t:^testing.T) { w:Mem_World if !testing.expect(t,world_init(&info,.World,&w,context.allocator)) {return} defer world_destroy(&info,.World,&w) - defer delete(w.pools.names) // world_destroy frees arrays and slices, not maps + defer { // world_destroy frees arrays and slices, not maps + delete(w.pools.names) + for _,list in w.pools.lists {delete(list)} + delete(w.pools.lists) + } for i in 0..<5 { append(&w.pools.slots,Mem_Slot{tag=u32(i)}) for _ in 0.. (bytes,elements: bytes+=b;elements+=e+1 } case runtime.Type_Info_Map: - m:=cast(^runtime.Raw_Map)data - if m.data!=0 && v.map_info!=nil {bytes=int(runtime.map_total_allocation_size(uintptr(runtime.map_cap(m^)),v.map_info))} + m:=(cast(^runtime.Raw_Map)data)^ + if m.data==0 {return} + capacity:=uintptr(runtime.map_cap(m)) + bytes=int(runtime.map_total_allocation_size(capacity,v.map_info)) + keys,values,hashes,_,_:=runtime.map_kvh_data_dynamic(m,v.map_info) + for i in 0.. ^Wor } } ops:=make([dynamic]Memory_Op,allocator) - entities,ok:=memory_compile(&ops,type_info_of(Entities)) + compiled:=make(map[typeid][2]i32,context.temp_allocator) + entities,ok:=memory_compile(&ops,&compiled,type_info_of(Entities)) ws.entities=entities - for t,i in info.tables {ws.tables[i].held,ok=memory_compile(&ops,t.info);if !ok {break}} - for s,i in info.storages {if ok {ws.storages[i].held,ok=memory_compile(&ops,s.info)}} + for t,i in info.tables {ws.tables[i].held,ok=memory_compile(&ops,&compiled,t.info);if !ok {break}} + for s,i in info.storages {if ok {ws.storages[i].held,ok=memory_compile(&ops,&compiled,s.info)}} ws.memory=ops[:] if !ok { world_schema_destroy(ws) @@ -136,9 +137,10 @@ world_schema_make :: proc(info:^World_Info,allocator:=context.allocator) -> ^Wor } // A container a value holds: where it is in the value and how to read what -// it holds. A fixed array of values that hold containers is an op whose -// children are those of one element; a dynamic array or a slice of them -// has children too, read for each element it has. +// it holds. Its children are the ops of one element, read for each element +// it has: of a fixed array, a dynamic array or a slice, or a value of a map. +// A key holds no container: a type that does is not comparable. +@(private) Memory_Op :: struct { offset:uintptr, kind:Memory_Kind, @@ -147,21 +149,25 @@ Memory_Op :: struct { map_info:^runtime.Map_Info, children:[2]i32, // the ops of one element, from its start } +@(private) Memory_Kind :: enum u8 {Dynamic,Slice,Map,Array} // Appends the ops of what a type holds behind pointers: first its own, then -// those of each of their elements. held: the range of its own. +// those of each of their elements. held: the range of its own. A type is +// compiled once: its later uses, as an element of itself too, read that range. @(private="file") -memory_compile :: proc(ops:^[dynamic]Memory_Op,info:^runtime.Type_Info) -> (held:[2]i32,ok:bool) { +memory_compile :: proc(ops:^[dynamic]Memory_Op,compiled:^map[typeid][2]i32,info:^runtime.Type_Info) -> (held:[2]i32,ok:bool) { + if known,found:=compiled[info.id];found {return known,true} own:=make([dynamic]Memory_Op,context.temp_allocator) elems:=make([dynamic]^runtime.Type_Info,context.temp_allocator) memory_collect(&own,&elems,info,0) held={i32(len(ops)),i32(len(ops)+len(own))} if _,error:=append(ops,..own[:]);error!=nil {return held,false} + if _,error:=map_insert(compiled,info.id,held);error!=nil {return held,false} for elem,i in elems { if elem==nil {continue} - children,compiled:=memory_compile(ops,elem) - if !compiled {return held,false} + children,done:=memory_compile(ops,compiled,elem) + if !done {return held,false} ops[int(held[0])+i].children=children } return held,true @@ -183,11 +189,12 @@ memory_collect :: proc(own:^[dynamic]Memory_Op,elems:^[dynamic]^runtime.Type_Inf case runtime.Type_Info_Enumerated_Array: add(own,elems,{offset=offset,kind=.Array,count=v.count,elem_size=v.elem_size},v.elem) case runtime.Type_Info_Dynamic_Array: add(own,elems,{offset=offset,kind=.Dynamic,elem_size=v.elem_size},v.elem) case runtime.Type_Info_Slice: add(own,elems,{offset=offset,kind=.Slice,elem_size=v.elem_size},v.elem) - case runtime.Type_Info_Map: add(own,elems,{offset=offset,kind=.Map,map_info=v.map_info},nil) + case runtime.Type_Info_Map: add(own,elems,{offset=offset,kind=.Map,map_info=v.map_info},v.value) } } // The bytes of what the ops in held read at data. +@(private) memory_read :: proc(ops:[]Memory_Op,held:[2]i32,data:rawptr) -> (bytes:int) { for op in ops[held[0]:held[1]] { at:=rawptr(uintptr(data)+op.offset) @@ -202,8 +209,7 @@ memory_read :: proc(ops:[]Memory_Op,held:[2]i32,data:rawptr) -> (bytes:int) { bytes+=a.len*op.elem_size count,first=a.len,a.data case .Map: - m:=cast(^runtime.Raw_Map)at - if m.data!=0 && op.map_info!=nil {bytes+=int(runtime.map_total_allocation_size(uintptr(runtime.map_cap(m^)),op.map_info))} + bytes+=memory_read_map(ops,op,(cast(^runtime.Raw_Map)at)^) case .Array: count,first=op.count,at } @@ -213,6 +219,20 @@ memory_read :: proc(ops:[]Memory_Op,held:[2]i32,data:rawptr) -> (bytes:int) { return } +// The bytes of a map and of what its values hold. +@(private="file") +memory_read_map :: proc(ops:[]Memory_Op,op:Memory_Op,m:runtime.Raw_Map) -> (bytes:int) { + if m.data==0 || op.map_info==nil {return} + capacity:=uintptr(runtime.map_cap(m)) + bytes=int(runtime.map_total_allocation_size(capacity,op.map_info)) + if op.children[0]==op.children[1] {return} + _,values,hashes,_,_:=runtime.map_kvh_data_dynamic(m,op.map_info) + for i in 0.. (s:^serial.Schema,index:serial.Type_Index,ok:bool) { @@ -285,7 +305,7 @@ world_schema_destroy :: proc(ws:^World_Schema) { for &v in ws.storages {value_schema_destroy(&v,allocator)} for &v in ws.records {value_schema_destroy(&v,allocator)} delete(ws.tables,allocator);delete(ws.storages,allocator);delete(ws.records,allocator) - delete(ws.table_hashes,allocator);delete(ws.memory) + delete(ws.table_hashes,allocator);delete(ws.memory,allocator) delete(ws.choices,allocator);delete(ws.schema.named) serial.schema_destroy(&ws.schema) _=runtime.mem_free(ws,allocator) diff --git a/platform/process_memory_test.odin b/platform/process_memory_test.odin index 0f034586b..2c2ca4bc7 100644 --- a/platform/process_memory_test.odin +++ b/platform/process_memory_test.odin @@ -2,15 +2,14 @@ package platform import "core:testing" -// What the system says this process holds grows with memory it writes to. +// What the system says this process holds includes memory it writes to and +// keeps, whatever other threads allocate or free meanwhile. @(test) process_memory_counts_what_the_process_touches :: proc(t:^testing.T) { - before:=process_memory() - testing.expect(t,before.resident>0,"the system says how much of the process is resident") - testing.expect(t,before.private>0,"and how much is its own") block:=make([]u8,64<<20) defer delete(block) for i:=0;i=before.private+32<<20,"64 MB written raise the private memory: %d -> %d",before.private,after.private) + held:=process_memory() + testing.expect(t,held.resident>0,"the system says how much of the process is resident") + testing.expectf(t,held.private>=u64(len(block)),"the private memory holds the %d bytes written: %d",len(block),held.private) } From 3bbfde0a36e3a660a7bcf543664fc17d584b0c75 Mon Sep 17 00:00:00 2001 From: tomaszbk-pc Date: Sun, 15 Nov 2026 09:50:08 -0300 Subject: [PATCH 7/7] Record memory from the profiler panel only from its Memory tab 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 --- plugins/profiler/editor/profiler.odin | 67 +++++++++++++++------------ 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/plugins/profiler/editor/profiler.odin b/plugins/profiler/editor/profiler.odin index fec5b8817..5933073e6 100644 --- a/plugins/profiler/editor/profiler.odin +++ b/plugins/profiler/editor/profiler.odin @@ -116,36 +116,40 @@ profiler_panel :: proc(s:^editor.Session,p:^Profiler_Panel,app:^host.App) { titles:=TABS kit.tabs(ctx,ui.id(ctx,"tracks"),titles[:],&p.tab,ui.fixed(240),28) small:=ui.metric(ctx,"font_small") - // Every tab is as tall as the tallest: the panel, held by its bottom, keeps its tabs under the pointer. - if ui.box(ctx,{width=ui.GROW,height=ui.fixed(tabs_height(p,small)),direction=.Column,gap=TAB_GAP}) { - switch Tab(p.tab) { - case .Cpu: - ui.label(ctx,profiler_frame_text(p,c),small,ui.tone(ctx,"text")) - rows:=f32(p.depth+p.thread_count) - editor.drawing(g,ui.id(ctx,"timeline"),ui.GROW,ui.fixed(rows*(ROW+2)+ROW),timeline_draw,p) - editor.drawing(g,ui.id(ctx,"zones"),ui.GROW,ui.fixed(f32(p.top_count)*ROW+ROW),zones_draw,p) - case .Gpu: - ui.label(ctx,gpu_frame_text(p,c),small,ui.tone(ctx,"text")) - editor.drawing(g,ui.id(ctx,"gpu_timeline"),ui.GROW,ui.fixed(ROW+2+ROW),timeline_draw,p) - editor.drawing(g,ui.id(ctx,"passes"),ui.GROW,ui.fixed(f32(p.pass_count)*ROW+ROW),passes_draw,p) - case .Memory: - if !c.memory {ui.label(ctx,"This capture did not sample the memory",small,ui.tone(ctx,"text_muted"));break} - ui.label(ctx,memory_frame_text(p,c),small,ui.tone(ctx,"text")) - editor.drawing(g,ui.id(ctx,"memory"),ui.GROW,ui.fixed(GRAPH),memory_draw,p) + switch Tab(p.tab) { + case .Cpu: ui.label(ctx,profiler_frame_text(p,c),small,ui.tone(ctx,"text")) + case .Gpu: ui.label(ctx,gpu_frame_text(p,c),small,ui.tone(ctx,"text")) + case .Memory: + if c.memory {ui.label(ctx,memory_frame_text(p,c),small,ui.tone(ctx,"text"))} else {ui.label(ctx,"This capture did not sample the memory",small,ui.tone(ctx,"text_muted"))} + } + // Every tab is as tall as the tallest, the others keeping their place empty: the panel, + // held by its bottom, keeps its tabs under the pointer. + if ui.box(ctx,{width=ui.GROW,direction=.Stack}) { + for tab in Tab { + drawn:=tab==Tab(p.tab) && (tab!=.Memory || c.memory) + if ui.box(ctx,{width=ui.GROW,direction=.Column,gap=TAB_GAP}) { + for part in tab_parts(p,tab) { + if part.height==0 {continue} + if drawn {editor.drawing(g,ui.id(ctx,part.id),ui.GROW,ui.fixed(part.height),part.draw,p)} else if ui.box(ctx,{width=ui.GROW,height=ui.fixed(part.height)}) {} + } + } } } } } -// The height of the tallest tab of the capture shown: a label of size font -// over each one's drawings. @(private="file") -tabs_height :: proc(p:^Profiler_Panel,font:f32) -> f32 { - label:=font+6 - cpu:=label+TAB_GAP+f32(p.depth+p.thread_count)*(ROW+2)+ROW+TAB_GAP+f32(p.top_count)*ROW+ROW - gpu:=label+TAB_GAP+ROW+2+ROW+TAB_GAP+f32(p.pass_count)*ROW+ROW - memory:=label+TAB_GAP+GRAPH - return max(cpu,gpu,memory) +Tab_Part :: struct {id:string,height:f32,draw:editor.Drawing_Proc} + +// The drawings of a tab, one over the other. +@(private="file") +tab_parts :: proc(p:^Profiler_Panel,tab:Tab) -> [2]Tab_Part { + switch tab { + case .Cpu: return {{"timeline",f32(p.depth+p.thread_count)*(ROW+2)+ROW,timeline_draw},{"zones",f32(p.top_count)*ROW+ROW,zones_draw}} + case .Gpu: return {{"gpu_timeline",ROW+2+ROW,timeline_draw},{"passes",f32(p.pass_count)*ROW+ROW,passes_draw}} + case .Memory: return {{"memory",GRAPH,memory_draw},{}} + } + return {} } @(private="file") @@ -163,7 +167,7 @@ profiler_header :: proc(s:^editor.Session,p:^Profiler_Panel,app:^host.App) { ui.number_field(ctx,ui.id(ctx,"frames_asked"),&p.frames,{width=ui.fixed(72),decimals=0,speed=1,has_min=true,min=2,has_max=true,max=f64(host.PROFILE_FRAMES_MAX),disabled=recording}) if ui.button(ctx,ui.id(ctx,"record"),{label="Record",variant="accent",size=ui.metric(ctx,"font_small"),disabled=recording}).clicked { p.error_length=0 - if _,error:=editor.profile_begin(s,int(p.frames),"profiler panel",memory=true);error!="" {p.error_length=copy(p.error[:],error)} + if _,error:=editor.profile_begin(s,int(p.frames),"profiler panel",memory=Tab(p.tab)==.Memory);error!="" {p.error_length=copy(p.error[:],error)} } } } @@ -397,15 +401,20 @@ memory_draw :: proc(ctx:^ui.Context,rect:ui.Rect,user:rawptr) { ui.draw_rounded(ctx,rect,ui.metric(ctx,"radius_control"),ui.tone(ctx,"field")) if c==nil || len(c.frames)==0 {return} small:=ui.metric(ctx,"font_small") - label:=f32(150) + names:=[3]string{"World","Private","GPU"} + texts:[3]string + label:f32 + for name,k in names { + texts[k]=fmt.tprintf("%s %.1f-%.1f MB",name,p.memory_range[k][0],p.memory_range[k][1]) + label=max(label,ui.text_width(&ctx.font,texts[k],small)+12) + } x0,w:=rect.x+label,rect.w-label band:=rect.h/3 step:=w/f32(len(c.frames)) - names:=[3]string{"World","Private","GPU"} - for name,k in names { + for text,k in texts { top:=rect.y+band*f32(k) low,high:=p.memory_range[k][0],p.memory_range[k][1] - ui.draw_text(ctx,fmt.tprintf("%s %.1f-%.1f MB",name,low,high),rect.x+6,top+band/2-small/2,small,ui.tone(ctx,"text_muted")) + ui.draw_text(ctx,text,rect.x+6,top+band/2-small/2,small,ui.tone(ctx,"text_muted")) if k>0 {ui.draw_rect(ctx,{rect.x,top,rect.w,1},ui.color_alpha(ui.tone(ctx,"text_faint"),60))} for f,i in c.frames { at:=0.5 if high<=low else (memory_values(f)[k]-low)/(high-low)