From b04fd4295ecd179bcddc2c9f5b7b3737028fb185 Mon Sep 17 00:00:00 2001 From: 5hif7y <44300833+5hif7y@users.noreply.github.com> Date: Sun, 15 Nov 2026 11:10:15 -0300 Subject: [PATCH] Say who owns each zone of a profile, and compare against saved baselines - Step_Info gains plugin and game; the generator writes the steps by field name and tells a package of the game with one check (game_package), which types, records and steps share. - Api_Profile_Zone gains owner (Game, Plugin, Engine) and plugin, found once per capture from Step_Info. GPU passes have none: a pass does not know who added it. - profile.save keeps what profile.compare reads of a capture as a baseline of the project, by name, in .mirai/profiles/baselines; profile.compare takes baseline. The file carries a format version and is validated: a corrupt one or one of another version is refused. profile.list names the baselines. - Generator tests find a step's row by name and check only its fields. Co-Authored-By: Claude Opus 5.5 --- .../mirai-agent-api/references/methods.md | 8 +- cook/build/contracts_test.odin | 3 +- cook/build/minds_test.odin | 2 +- cook/build/stages_test.odin | 16 ++- cook/build/world.odin | 14 +- core/world.odin | 2 + editor/ai_context.odin | 2 +- editor/api_profile.odin | 105 ++++++++------- editor/profile.odin | 25 ++++ editor/profile_baseline.odin | 122 ++++++++++++++++++ editor/profile_test.odin | 78 ++++++++++- 11 files changed, 316 insertions(+), 61 deletions(-) create mode 100644 editor/profile_baseline.odin diff --git a/.agents/skills/mirai-agent-api/references/methods.md b/.agents/skills/mirai-agent-api/references/methods.md index c2546a123..e9b1ca3e1 100644 --- a/.agents/skills/mirai-agent-api/references/methods.md +++ b/.agents/skills/mirai-agent-api/references/methods.md @@ -288,7 +288,13 @@ the GPU passes and the conditions of the measurement. The editor keeps the last (`settings.set` or open without `--validation`). `allow_limited:true` measures anyway and the summary says `comparable:false`; a headless editor waits for neither the display nor a limit. - `profile.compare` gives `comparable:false` with a blocking difference in conditions or if either - of the two is not `comparable`. + of the two is not `comparable`. `profile.save` keeps what it reads of a capture as a baseline of + the project, by name, in `.mirai/profiles/baselines` outside git, and `profile.compare` takes it + with `baseline` in a later session. A baseline file has a format version: one of another + version, or that is not one, is refused and is saved again. +- The `owner` of a zone is whoever declares the loop function it measures (`Step_Info.game` and + `plugin`, which the generator writes): `Game`, `Plugin` with its name, or `Engine` for any other + zone. GPU passes have none: a pass does not know who added it. - `profile.load` answers about the last load of the document (`scene.open`, a scene a game asks for), of Play (`play.start`) and of a section, without recording anything first ([loads](../../mirai-core/references/host.md#the-frame)): `ready_ms` until it was the document diff --git a/cook/build/contracts_test.odin b/cook/build/contracts_test.odin index 0b4c91d06..5cd111c29 100644 --- a/cook/build/contracts_test.odin +++ b/cook/build/contracts_test.odin @@ -219,7 +219,8 @@ functions_are_called_from_the_loops_and_from_their_order :: proc(t:^testing.T) { testing.expect(t,strings.contains(text,"game_fixed :: proc(world,shared,editor:rawptr,app_data:rawptr) {\n call_clock_read(world,shared,editor,app_data)\n call_things_move(world,shared,editor,app_data)\n}"),text) testing.expect(t,strings.contains(text,"@(rodata) order_game_fixed:=[2]i32{1,0}"),text) testing.expect(t,strings.contains(text,"info.loops[.Game][.Late],info.order[.Game][.Late]=game_late,order_game_late[:]"),text) - testing.expect(t,strings.contains(text,"steps[0]={\"things_move\",\"core\",{.Fixed,.Late,},{.Game,.Editor,},call_things_move}"),text) + step:=step_written(text,"things_move") + testing.expect(t,strings.contains(step,"phases={.Fixed,.Late,}") && strings.contains(step,"loops={.Game,.Editor,}"),step) } defaulted:=`package core @(storage) Clock :: struct {tick:u64} diff --git a/cook/build/minds_test.odin b/cook/build/minds_test.odin index 393fbbba1..abebf04b0 100644 --- a/cook/build/minds_test.odin +++ b/cook/build/minds_test.odin @@ -70,7 +70,7 @@ Zombie_Act :: enum u8 {Wander,Chase,Circle} call=call[:strings.index(call,"\n}\n")] testing.expect(t,strings.contains(call,"if w.network.role==.Client {return}"),call) testing.expect(t,strings.contains(call,"p_core.table_settle(&w.hunger,&w.entities)"),call) - testing.expect(t,strings.contains(text,`steps[0]={"zombie_think","game",{.Fixed,},{.Game,},call_zombie_think}`),"its step is named after its think") + testing.expect(t,strings.contains(step_written(text,"zombie_think"),"call=call_zombie_think"),"its step is named after its think") } } diff --git a/cook/build/stages_test.odin b/cook/build/stages_test.odin index 9f5909cf4..2311da759 100644 --- a/cook/build/stages_test.odin +++ b/cook/build/stages_test.odin @@ -90,7 +90,11 @@ import "mirai:core" for gone in ([]string{"Step_Edge","info.edges","info.parallel","worker"}) { testing.expectf(t,!strings.contains(text,gone),"the generated world still says %s",gone) } - testing.expect(t,strings.contains(text,"steps[0]={\"core_last\",\"core\",{.Fixed,},{.Game,},call_core_last}"),text) + // A step says who declares it: an engine package, or the game of the project. + last,logic:=step_written(text,"core_last"),step_written(text,"game_logic") + testing.expect(t,strings.contains(last,"phases={.Fixed,}") && strings.contains(last,"call=call_core_last"),last) + testing.expect(t,strings.contains(last,`plugin=""`) && strings.contains(last,"game=false"),last) + testing.expect(t,strings.contains(logic,"game=true"),logic) // What game_spawn made is settled inside its call, before game_count is called. _,_,rest:=strings.partition(text,"call_game_spawn :: proc(world,shared,editor:rawptr,app_data:rawptr) {") body,_,_:=strings.partition(rest,"\n}\n") @@ -295,3 +299,13 @@ import "mirai:core" output=world_generate(&p,context.temp_allocator) testing.expectf(t,errors_have(output.errors,"game_decide is server, and the world has no core.Network"),"%v",output.errors) } + +// The row of info.steps the generated world writes for a function, by +// name: its fields, whatever their order. +@(private) +step_written :: proc(text,name:string) -> string { + at:=strings.index(text,fmt.tprintf("={{name=%q,",name)) + if at<0 {return ""} + row:=text[at:] + return row[:strings.index_byte(row,'\n')] +} diff --git a/cook/build/world.odin b/cook/build/world.odin index 43479a0fd..9724ab86d 100644 --- a/cook/build/world.odin +++ b/cook/build/world.odin @@ -2374,7 +2374,7 @@ world_write :: proc(g:^Generator,editor:bool) -> string { for r,ri in g.records { tag:=fmt.tprintf("%s_%s",snake(r.pkg.name),snake(r.name)) _,has_default:=record_default(r) - game:=strings.has_prefix(r.pkg.path,"project:") && !strings.has_prefix(r.pkg.path,"project:plugins/") + game:=game_package(r.pkg) fmt.sbprintf(w," records[%d]={{\"%s\",\"%s\",typeid_of(%s.%s),type_info_of(%s.%s),typeid_of(%s.Record(%s.%s)),%s,%v}}\n",ri,r.name,r.pkg.name,r.pkg.alias,r.name,r.pkg.alias,r.name,core_alias,r.pkg.alias,r.name,has_default?fmt.tprintf("record_default_%s",tag):"nil",game) } codes:=record_codes(g) @@ -2389,7 +2389,7 @@ world_write :: proc(g:^Generator,editor:bool) -> string { loop_set:=strings.builder_make(context.temp_allocator) if s.loops[0] {strings.write_string(&loop_set,".Game,")} if s.loops[1] {strings.write_string(&loop_set,".Editor,")} - fmt.sbprintf(w," steps[%d]={{\"%s\",\"%s\",{{%s}},{{%s}},call_%s}}\n",si,s.name,s.pkg.name,strings.to_string(phase_set),strings.to_string(loop_set),s.name) + fmt.sbprintf(w," steps[%d]={{name=%q,library=%q,phases={{%s}},loops={{%s}},call=call_%s,plugin=%q,game=%v}}\n",si,s.name,s.pkg.name,strings.to_string(phase_set),strings.to_string(loop_set),s.name,project.import_plugin(s.pkg.path),game_package(s.pkg)) } fmt.sbprintf(w," draws:=make([]%s.Draw_Info,%d)\n",core_alias,len(g.draws)) for d,di in g.draws {fmt.sbprintf(w," draws[%d]={{%q,rawptr(%s.%s)}}\n",di,d.name,d.pkg.alias,d.ident)} @@ -2467,12 +2467,16 @@ type_procs_write :: proc(w:^strings.Builder,t:^Type,c:string) { t.procs=strings.to_string(list) } -// Whether the game of a project declares a type, not the engine or a plugin. +// Whether a package is of the game of a project, not of the engine or a plugin. @(private="file") -of_game :: proc(t:^Type) -> bool { - return t.pkg!=nil && strings.has_prefix(t.pkg.path,"project:") && !strings.has_prefix(t.pkg.path,"project:plugins/") +game_package :: proc(pkg:^Package) -> bool { + return pkg!=nil && strings.has_prefix(pkg.path,"project:") && !strings.has_prefix(pkg.path,"project:plugins/") } +// Whether the game of a project declares a type. +@(private="file") +of_game :: proc(t:^Type) -> bool {return game_package(t.pkg)} + // The row of info.tables of a table of a type, after type_procs_write: name, // what the scene file and the API call it; offset, where it is in the struct // of its place. named: its fields go by name, as those of a test world do: diff --git a/core/world.odin b/core/world.odin index d8e606d7a..cc9bab702 100644 --- a/core/world.odin +++ b/core/world.odin @@ -118,6 +118,8 @@ Step_Info :: struct { phases:Phases, loops:Loops, call:Step_Proc, // the function as its loops call it: its binding, its settle and its time + plugin:string, // the plugin whose package declares it; empty for the engine and the project + game:bool, // the game of the project declares it, not the engine or a plugin } // A procedure @(draw="name") of the game: what draws the Draw nodes of the diff --git a/editor/ai_context.odin b/editor/ai_context.odin index b90ec478a..d25ba2138 100644 --- a/editor/ai_context.odin +++ b/editor/ai_context.odin @@ -389,7 +389,7 @@ ai_prompt_stable :: proc(s:^Session,kind:ai_store.Conversation_Kind) -> string { strings.write_string(&b,": read the declarations there for the types and procedures you use, not the sources of the engine.\n") } strings.write_string(&b,"- You may read files outside the project only to import them, as when the user names a model in Downloads or an image in Pictures: `asset_import` takes the path wherever it is and writes the assets into the project, in `folder` when given; it answers when it ends with what it wrote. Deleting a file asks the user first and moving one does not; delete an asset with `asset_delete`, which moves it to the trash, after `asset_users` says what uses it.\n") - strings.write_string(&b,"- When the game is slow or the user asks about performance, measure instead of guessing: `profile.capture` records frames and answers with what bounds them, the costliest functions, GPU passes and notes on how to read them; `profile.frames` and `profile.zone` explain a spike or a function, and `profile.compare` says whether a change helped. With VSync or a frame limit on it refuses and says how to turn them off; `allow_limited` measures anyway, and then read `cpu_ms` and the functions rather than the frame time.\n") + strings.write_string(&b,"- When the game is slow or the user asks about performance, measure instead of guessing: `profile.capture` records frames and answers with what bounds them, the costliest functions, GPU passes and notes on how to read them; `profile.frames` and `profile.zone` explain a spike or a function, and `profile.compare` says whether a change helped. With VSync or a frame limit on it refuses and says how to turn them off; `allow_limited` measures anyway, and then read `cpu_ms` and the functions rather than the frame time. Each zone says its `owner`: `Game` and the plugins of the project are code the project can change, the others are not. Compare the numbers with the target the user gives; `profile.save` keeps a capture as a baseline that `profile.compare` takes in a later session.\n") strings.write_string(&b,"- Saved games of the project: `save.list` reads their slots and `save.delete` removes one; `save.write` and `save.load` save and load your run of the game as the game asks for it, so start it with `play.start` first.\n") if gameplay:=project.path(engine,".agents/skills/mirai-gameplay/SKILL.md",allocator=context.temp_allocator);engine!="" && os.is_file(gameplay) { fmt.sbprintf(&b,"- Only when you write game code, read %s and the references it links; it is the only engine documentation you need. Editing the scene does not need it.\n",gameplay) diff --git a/editor/api_profile.odin b/editor/api_profile.odin index 6a6249293..cb68621d4 100644 --- a/editor/api_profile.odin +++ b/editor/api_profile.odin @@ -51,6 +51,7 @@ Api_Profile_Zone_Params :: struct { } Api_Profile_Compare_Params :: struct { base:int `mirai:"optional,min=0,label=Capture to compare against; 0 is the one before id"`, + baseline:[PROFILE_BASELINE_NAME_BYTES]u8 `mirai:"optional,text,label=A baseline the project saved (profile.save) to compare against instead of base"`, id:int `mirai:"optional,min=0,label=Capture compared; 0 is the last one"`, top:int `mirai:"optional,min=1,max=64,clamp,label=Zones and GPU passes that changed the most"`, } @@ -82,6 +83,8 @@ Api_Profile_Frame_Stats :: struct { Api_Profile_Zone :: struct { name:string, thread:string `mirai:"label=main or workers"`, + owner:Profile_Owner `mirai:"label=Who declares the loop function it measures: the game of the project or a plugin; Engine for the rest"`, + plugin:string `mirai:"optional,label=Of a Plugin owner"`, calls_per_frame:f32, self_ms:f32 `mirai:"label=Mean per frame without the zones inside"`, total_ms:f32 `mirai:"label=Mean per frame"`, @@ -217,6 +220,7 @@ Api_Profile_Difference :: struct { } Api_Profile_Compare :: struct { base,id:int, + baseline:string `mirai:"optional,label=The baseline base is, instead of a capture"`, comparable:bool `mirai:"label=No blocking difference and neither measured with VSync or a frame limit or graphics validation"`, differences:[]Api_Profile_Difference `mirai:"label=How their conditions differ"`, mean_ms:Api_Profile_Delta `mirai:"label=Of the time of a frame"`, @@ -250,6 +254,14 @@ Api_Profile_List :: struct { recorded:int `mirai:"label=Frames the capture that records recorded so far"`, requested:int `mirai:"label=Frames the capture that records asked for"`, captures:[]Api_Profile_List_Item `mirai:"label=Those that ended; the newest last"`, + baselines:[]string `mirai:"optional,label=The baselines the project saved (profile.save)"`, +} +Api_Profile_Save_Params :: struct { + id:int `mirai:"optional,min=0,label=Capture; 0 is the last one"`, + name:[PROFILE_BASELINE_NAME_BYTES]u8 `mirai:"text,label=Of the baseline: letters, digits, _ and -; one of that name is replaced"`, +} +Api_Profile_Saved :: struct { + path:string `mirai:"label=The file, in .mirai/profiles/baselines of the project, outside git"`, } @(private) @@ -265,6 +277,7 @@ profile_api_register :: proc(s:^Session,st:^Api_State) { api_register(s,st,{name="profile.frames",summary="The slowest frames of a capture, or a range of them, zone by zone with their GPU passes: where a spike went.",params=type_info_of(Api_Profile_Frames_Params),result=type_info_of(Api_Profile_Frames),defaults=&frames_default,flags={.Read_Only},run=api_profile_frames}) api_register(s,st,{name="profile.zone",summary="One zone across a capture: how its time spreads with and without what runs inside it, the zones around and inside it and the frames where it took the longest.",params=type_info_of(Api_Profile_Zone_Params),result=type_info_of(Api_Profile_Zone_Detail),flags={.Read_Only},run=api_profile_zone}) api_register(s,st,{name="profile.compare",summary="Two captures: whether they were measured alike and, zone by zone and pass by pass, what changed the most.",params=type_info_of(Api_Profile_Compare_Params),result=type_info_of(Api_Profile_Compare),defaults=&compare_default,flags={.Read_Only},run=api_profile_compare}) + api_register(s,st,{name="profile.save",summary="Keeps what profile.compare reads of a capture in the project as a baseline of that name, which profile.compare takes in a later session.",params=type_info_of(Api_Profile_Save_Params),result=type_info_of(Api_Profile_Saved),run=api_profile_save}) load_default:=Api_Profile_Load_Params{top=PROFILE_LOAD_TOP} api_register(s,st,{name="profile.load",summary="Where the seconds of the last load of the document, of Play and of a section went: phase by phase, by kind of asset with the slowest, the loop functions meanwhile and what it waited for until a frame drew with all of it resident; Loading until then.",params=type_info_of(Api_Profile_Load_Params),result=type_info_of(Api_Profile_Loads),defaults=&load_default,flags={.Read_Only},run=api_profile_load}) api_register(s,st,{name="profile.export",summary="Writes a capture as a Chrome trace (JSON) for Perfetto, chrome://tracing or Spall.",params=type_info_of(Api_Profile_Export_Params),result=type_info_of(Api_Profile_Export),run=api_profile_export}) @@ -355,6 +368,7 @@ api_profile_list :: proc(s:^Session,call:^api.Call) { } slice.sort_by(list[:],proc(a,b:Api_Profile_List_Item) -> bool {return a.id (out:Api_Profile_Summary) } total,own:=profile_spread(a.total),profile_spread(a.self) append(&zones,Api_Profile_Zone{ - name=name,thread=a.worker?"workers":"main",calls_per_frame=profile_ms(f64(a.calls)/f64(n)), + name=name,thread=a.worker?"workers":"main",owner=c.owners[a.name].owner,plugin=c.owners[a.name].plugin,calls_per_frame=profile_ms(f64(a.calls)/f64(n)), self_ms=profile_ms(own.mean),total_ms=profile_ms(total.mean),p95_ms=profile_ms(total.p95),max_ms=profile_ms(total.max), share=spread.mean>0?profile_ms(own.mean/spread.mean):0, }) @@ -698,59 +712,81 @@ profile_zone_detail :: proc(c:^Profile_Capture,name,thread:string) -> (out:Api_P // ---------------------------------------------------------------- compare +@(private) +api_profile_save :: proc(s:^Session,call:^api.Call) { + p:=api.call_params(call,Api_Profile_Save_Params) + if !s.has_project {api.call_fail(call,api.SERVER_ERROR,"Without a project there is no .mirai folder to keep a baseline in");return} + c:=api_profile_get(s,call,p.id) + if c==nil {return} + path,error:=profile_baseline_save(s.project_root,base.text_get(p.name[:]),profile_digest(c)) + if error!="" {api.call_fail(call,api.INVALID_PARAMS,error);return} + api.call_result(call,Api_Profile_Saved).path=strings.clone(path,call.allocator) +} + @(private) api_profile_compare :: proc(s:^Session,call:^api.Call) { p:=api.call_params(call,Api_Profile_Compare_Params) b:=api_profile_get(s,call,p.id) if b==nil {return} + after:=profile_digest(b) + if name:=base.text_get(p.baseline[:]);name!="" { + before,error:=profile_baseline_load(s.project_root,name,call.allocator) + if error!="" {api.call_fail(call,api.INVALID_PARAMS,error);return} + api.call_result(call,Api_Profile_Compare)^=profile_compare(&before,&after,p.top) + return + } base_id:=p.base if base_id==0 { for &c in s.profiles.captures { if c.id!=0 && c.idbase_id {base_id=c.id} } - if base_id==0 {api.call_failf(call,api.SERVER_ERROR,"Capture %d has none before it: record another, or pass base",b.id);return} + if base_id==0 {api.call_failf(call,api.SERVER_ERROR,"Capture %d has none before it: record another, or pass base or baseline",b.id);return} } a:=api_profile_get(s,call,base_id) if a==nil {return} if a==b {api.call_fail(call,api.INVALID_PARAMS,"base and id are the same capture");return} - api.call_result(call,Api_Profile_Compare)^=profile_compare(a,b,p.top) + before:=profile_digest(a) + api.call_result(call,Api_Profile_Compare)^=profile_compare(&before,&after,p.top) } -profile_compare :: proc(a,b:^Profile_Capture,top:int) -> (out:Api_Profile_Compare) { +profile_compare :: proc(a,b:^Profile_Digest,top:int) -> (out:Api_Profile_Compare) { context.allocator=context.temp_allocator - out.base,out.id=a.id,b.id + out.base,out.id,out.baseline=a.id,b.id,a.baseline out.differences=profile_differences(a.conditions,b.conditions) apart:=false for d in out.differences {if d.blocking {apart=true}} limited:=profile_limits(a.conditions)!={} || profile_limits(b.conditions)!={} out.comparable=!apart && !limited + out.mean_ms,out.p95_ms=delta(a.mean_ms,b.mean_ms),delta(a.p95_ms,b.p95_ms) + out.cpu_ms,out.gpu_ms=delta(a.cpu_ms,b.cpu_ms),delta(a.gpu_ms,b.gpu_ms) - 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(profile_gpu_ms(a)).mean,profile_spread(profile_gpu_ms(b)).mean) - - ma,mb:=means(a),means(b) + Key :: struct {name:string,worker:bool} + mb:=make(map[Key]f64) + for z in b.zones {mb[{z.name,z.worker}]=z.self_ms} + seen:=make(map[Key]bool) changes:=make([dynamic]Api_Profile_Change) only_a,only_b:=make([dynamic]string),make([dynamic]string) - for key,x in ma { + for z in a.zones { + key:=Key{z.name,z.worker} + seen[key]=true y,found:=mb[key] - if !found {append(&only_a,key.name);continue} - d:=delta(x,y) - append(&changes,Api_Profile_Change{key.name,key.worker?"workers":"main",d.base,d.id,d.delta,d.delta_pct}) + if !found {append(&only_a,z.name);continue} + d:=delta(z.self_ms,y) + append(&changes,Api_Profile_Change{z.name,z.worker?"workers":"main",d.base,d.id,d.delta,d.delta_pct}) } - for key,_ in mb {if key not_in ma {append(&only_b,key.name)}} + for z in b.zones {if (Key{z.name,z.worker}) not_in seen {append(&only_b,z.name)}} slice.sort_by(changes[:],proc(x,y:Api_Profile_Change) -> bool {return abs(x.delta_ms)>abs(y.delta_ms)}) out.zones=changes[:min(len(changes),top)] out.only_in_base,out.only_in_id=only_a[:min(len(only_a),top)],only_b[:min(len(only_b),top)] - pa,pb:=pass_means(a),pass_means(b) + pb:=make(map[string]f64) + for pass in b.passes {pb[pass.name]=pass.ms} gpu_changes:=make([dynamic]Api_Profile_Change) - for name,x in pa { - y,found:=pb[name] + for pass in a.passes { + y,found:=pb[pass.name] if !found {continue} - d:=delta(x,y) - append(&gpu_changes,Api_Profile_Change{name,"gpu",d.base,d.id,d.delta,d.delta_pct}) + d:=delta(pass.ms,y) + append(&gpu_changes,Api_Profile_Change{pass.name,"gpu",d.base,d.id,d.delta,d.delta_pct}) } slice.sort_by(gpu_changes[:],proc(x,y:Api_Profile_Change) -> bool {return abs(x.delta_ms)>abs(y.delta_ms)}) out.gpu=gpu_changes[:min(len(gpu_changes),top)] @@ -758,7 +794,7 @@ profile_compare :: proc(a,b:^Profile_Capture,top:int) -> (out:Api_Profile_Compar notes:=make([dynamic]string) if apart {append(¬es,"Measured in different conditions: a change in time can be the conditions and not the code. Record both alike")} if limited {append(¬es,"A capture was measured with VSync, a frame limit or graphics validation (allow_limited): its frame time is not the cost; cpu_ms and the zones still compare")} - if len(a.frames)<30 || len(b.frames)<30 {append(¬es,"Fewer than 30 frames in a capture: differences of a few percent are noise")} + if a.frames<30 || b.frames<30 {append(¬es,"Fewer than 30 frames in a capture: differences of a few percent are noise")} out.notes=notes[:] return } @@ -768,34 +804,9 @@ delta :: proc(x,y:f64) -> Api_Profile_Delta { return {profile_ms(x),profile_ms(y),profile_ms(y-x),f32(x>0?math.round((y-x)/x*1000)/10:0)} } -@(private="file") -frame_values :: proc(c:^Profile_Capture,which:int) -> []f64 { - values:=make([]f64,len(c.frames)) - for f,i in c.frames { - switch which { - case 0: values[i]=profile_frame_ms(f) - case 1: values[i]=profile_frame_cpu_ms(f) - } - } - return values -} -@(private="file") -Key :: struct {name:string,worker:bool} -@(private="file") -means :: proc(c:^Profile_Capture) -> map[Key]f64 { - result:=make(map[Key]f64) - for agg in profile_aggregate(c) {result[{c.names[agg.name],agg.worker}]=profile_spread(agg.self).mean} - return result -} -@(private="file") -pass_means :: proc(c:^Profile_Capture) -> map[string]f64 { - result:=make(map[string]f64) - for values,i in profile_passes_by_name(c) {result[c.pass_names[i]]=profile_spread(values).mean} - return result -} @(private="file") difference_check :: proc(list:^[dynamic]Api_Profile_Difference,field:string,x,y:$T,blocking:bool) { diff --git a/editor/profile.odin b/editor/profile.odin index 6a611b5ae..baedd0f48 100644 --- a/editor/profile.odin +++ b/editor/profile.odin @@ -97,6 +97,7 @@ Profile_Capture :: struct { frames:[]Profile_Frame_Record, zones:[]Profile_Zone_Record, // the main thread frame by frame, then the other threads names:[]string, + owners:[]Profile_Zone_Owner, // by name: who declares the loop function of that name passes:[]Profile_Pass_Record, pass_names:[]string, threads:int, @@ -270,6 +271,14 @@ profile_build :: proc(c:^Profile_Capture,recording:host.Profile_Recording,app:^h copy(c.zones,main[:]) copy(c.zones[len(main):],workers[:]) c.names=names[:] + c.owners=make([]Profile_Zone_Owner,len(names)) + if app!=nil && app.info!=nil { + // The plugin is kept in the capture: the library that named it may be reloaded. + for name,i in names { + c.owners[i]=profile_zone_owner(app.info.steps,name) + c.owners[i].plugin=strings.clone(c.owners[i].plugin) + } + } for zone,i in main { frame:=&c.frames[zone.frame] if frame.zone_count==0 {frame.zone_first=i} @@ -472,3 +481,19 @@ profile_gpu_ms :: proc(c:^Profile_Capture,allocator:=context.temp_allocator) -> for frame in c.frames {if frame.gpu_measured {append(&values,f64(frame.gpu_ms))}} return values[:] } + +// Who declares what a zone measures: a loop function of the game of the +// project or of a plugin; any other zone (a part of the frame, a job, an +// engine function) is the engine's. +Profile_Owner :: enum u8 {Engine,Game,Plugin} +Profile_Zone_Owner :: struct {owner:Profile_Owner,plugin:string} + +profile_zone_owner :: proc(steps:[]core.Step_Info,name:string) -> Profile_Zone_Owner { + for step in steps { + if step.name!=name {continue} + if step.game {return {owner=.Game}} + if step.plugin!="" {return {.Plugin,step.plugin}} + break + } + return {} +} diff --git a/editor/profile_baseline.odin b/editor/profile_baseline.odin new file mode 100644 index 000000000..01e1aa01e --- /dev/null +++ b/editor/profile_baseline.odin @@ -0,0 +1,122 @@ +package editor + +import "core:encoding/json" +import "core:fmt" +import "core:os" +import "core:slice" +import "core:strings" +import "mirai:project" +import "mirai:std/serial" + +// What profile.compare reads of a capture, and what a baseline keeps of one +// in the project: profile.save keeps it by name, and a later session +// compares against it after the captures of this one are gone. + +PROFILE_BASELINE_NAME_BYTES :: 32 +PROFILE_BASELINE_VERSION :: 1 // of the file of a baseline: raised when Profile_Digest changes +PROFILE_BASELINES :: "baselines" // folder of the baselines in the profiles folder of the project + +Profile_Digest_Zone :: struct {name:string,worker:bool,self_ms:f64} +Profile_Digest_Pass :: struct {name:string,ms:f64} + +Profile_Digest :: struct { + version:int, // PROFILE_BASELINE_VERSION in a file + id:int, // of the capture; 0 for a baseline + baseline:string, // its name; empty for a capture + conditions:Profile_Conditions, + frames:int, + mean_ms,p95_ms,cpu_ms,gpu_ms:f64, + zones:[]Profile_Digest_Zone, // own time per frame + passes:[]Profile_Digest_Pass, // per frame whose GPU times came back +} + +// The digest of a capture, in the temp allocator. +profile_digest :: proc(c:^Profile_Capture) -> (d:Profile_Digest) { + context.allocator=context.temp_allocator + d.id,d.conditions,d.frames=c.id,c.conditions,len(c.frames) + totals,cpu:=make([]f64,len(c.frames)),make([]f64,len(c.frames)) + for f,i in c.frames {totals[i],cpu[i]=profile_frame_ms(f),profile_frame_cpu_ms(f)} + frame:=profile_spread(totals) + d.mean_ms,d.p95_ms,d.cpu_ms=frame.mean,frame.p95,profile_spread(cpu).mean + d.gpu_ms=profile_spread(profile_gpu_ms(c)).mean + aggregates:=profile_aggregate(c) + zones:=make([]Profile_Digest_Zone,len(aggregates)) + for a,i in aggregates {zones[i]={c.names[a.name],a.worker,profile_spread(a.self).mean}} + d.zones=zones + by_name:=profile_passes_by_name(c) + passes:=make([]Profile_Digest_Pass,len(by_name)) + for values,i in by_name {passes[i]={c.pass_names[i],profile_spread(values).mean}} + d.passes=passes + return +} + +// A name a baseline may take: what fits a file name everywhere. +profile_baseline_name_valid :: proc(name:string) -> bool { + if name=="" || len(name)>=PROFILE_BASELINE_NAME_BYTES {return false} + for c in transmute([]u8)name { + if !(c>='a' && c<='z' || c>='A' && c<='Z' || c>='0' && c<='9' || c=='_' || c=='-') {return false} + } + return true +} + +@(private="file") +baseline_name_error :: proc(name:string) -> string { + return fmt.tprintf("%q is not a baseline name: up to %d letters, digits, '_' and '-'",name,PROFILE_BASELINE_NAME_BYTES-1) +} + +profile_baseline_path :: proc(project_root,name:string) -> string { + folder:=project.path(project.folder_path(project_root,.Generated,context.temp_allocator),"profiles",PROFILE_BASELINES,allocator=context.temp_allocator) + return project.path(folder,fmt.tprintf("%s.json",name),allocator=context.temp_allocator) +} + +// Writes the digest as the baseline name of the project; the path it +// wrote, or the error. +profile_baseline_save :: proc(project_root,name:string,d:Profile_Digest) -> (path,error:string) { + if !profile_baseline_name_valid(name) {return "",baseline_name_error(name)} + path=profile_baseline_path(project_root,name) + folder,_:=os.split_path(path) + if os.make_directory_all(folder)!=nil && !os.is_dir(folder) {return "",fmt.tprintf("%s could not be made",folder)} + saved:=d + saved.version,saved.id,saved.baseline=PROFILE_BASELINE_VERSION,0,name + bytes,marshaled:=json.marshal(saved,{pretty=true},context.temp_allocator) + if marshaled!=nil {return "","The baseline could not be serialized"} + if serial.file_write_atomic(path,bytes)!=.None {return "",fmt.tprintf("%s could not be written",path)} + return path,"" +} + +// The baseline name of the project, in allocator. +profile_baseline_load :: proc(project_root,name:string,allocator:=context.allocator) -> (d:Profile_Digest,error:string) { + if !profile_baseline_name_valid(name) {return {},baseline_name_error(name)} + path:=profile_baseline_path(project_root,name) + bytes,read:=os.read_entire_file(path,context.temp_allocator) + if read!=nil {return {},fmt.tprintf("No baseline %q (%s): profile.save writes one; profile.list names the saved ones",name,path)} + if json.unmarshal(bytes,&d,allocator=allocator)!=nil || !digest_valid(d) {return {},fmt.tprintf("%s is not a baseline: save it again with profile.save",path)} + if d.version!=PROFILE_BASELINE_VERSION {return {},fmt.tprintf("%s is a baseline of format %d and this editor reads %d: save it again with profile.save",path,d.version,PROFILE_BASELINE_VERSION)} + d.id,d.baseline=0,strings.clone(name,allocator) + return d,"" +} + +// What a file must say to be a digest: frames, and times that are numbers +// of milliseconds; a zone or a pass has a name. +@(private="file") +digest_valid :: proc(d:Profile_Digest) -> bool { + ms :: proc(v:f64) -> bool {return v>=0 && v<1e9} + if d.version<=0 || d.frames<=0 || !ms(d.mean_ms) || !ms(d.p95_ms) || !ms(d.cpu_ms) || !ms(d.gpu_ms) {return false} + for z in d.zones {if z.name=="" || !ms(z.self_ms) {return false}} + for p in d.passes {if p.name=="" || !ms(p.ms) {return false}} + return true +} + +// The names of the baselines the project saved, sorted, in allocator. +profile_baselines :: proc(project_root:string,allocator:=context.allocator) -> []string { + folder,_:=os.split_path(profile_baseline_path(project_root,"x")) + entries,error:=os.read_all_directory_by_path(folder,context.temp_allocator) + if error!=nil {return nil} + names:=make([dynamic]string,allocator) + for entry in entries { + stem,ext:=os.split_filename(entry.name) + if ext=="json" && profile_baseline_name_valid(stem) {append(&names,strings.clone(stem,allocator))} + } + slice.sort(names[:]) + return names[:] +} diff --git a/editor/profile_test.odin b/editor/profile_test.odin index 4b215ff52..e23f4a0fb 100644 --- a/editor/profile_test.odin +++ b/editor/profile_test.odin @@ -1,9 +1,12 @@ package editor +import "core:fmt" import vmem "core:mem/virtual" +import "core:os" import "core:strings" import "core:testing" import "core:time" +import "mirai:core" import "mirai:editor/api" import "mirai:host" import "mirai:std/base" @@ -155,7 +158,7 @@ two_captures_compare_zone_by_zone_and_say_when_they_were_measured_apart :: proc( if !testing.expect(t,capture_fixture(&other,3,4,"Low")) {return} defer profile_capture_free(&other) - result:=profile_compare(&before,&after,8) + result:=compare_of(&before,&after) testing.expect(t,result.comparable,"same conditions") testing.expect_value(t,len(result.differences),0) testing.expect_value(t,result.zones[0].name,"a") @@ -163,7 +166,7 @@ two_captures_compare_zone_by_zone_and_say_when_they_were_measured_apart :: proc( testing.expect_value(t,result.mean_ms.delta,-4.0) testing.expect(t,result.mean_ms.delta_pct<0) - result=profile_compare(&after,&other,8) + result=compare_of(&after,&other) testing.expect(t,!result.comparable,"another preset changes the times by itself") testing.expect_value(t,result.differences[0].field,"preset") testing.expect(t,len(result.notes)>0) @@ -198,11 +201,11 @@ a_capture_measured_with_limits_is_not_comparable :: proc(t:^testing.T) { testing.expect(t,profile_summary(&free,4).comparable) testing.expect(t,!profile_summary(&limited,4).comparable) - result:=profile_compare(&limited,&other,8) + result:=compare_of(&limited,&other) testing.expect_value(t,len(result.differences),0) testing.expect(t,!result.comparable,"both waited for the display") testing.expect(t,strings.contains(strings.join(result.notes,"\n",context.temp_allocator),"allow_limited")) - testing.expect(t,profile_compare(&free,&free,8).comparable) + testing.expect(t,compare_of(&free,&free).comparable) } @(test) @@ -335,3 +338,70 @@ the_answers_of_the_profile_methods_have_the_shape_they_declare :: proc(t:^testin load.kind=.Play profile_answer_expect(t,&s,api_profile_load,&load,Api_Profile_Load_Params,Api_Profile_Loads) } + +@(private="file") +compare_of :: proc(a,b:^Profile_Capture) -> Api_Profile_Compare { + x,y:=profile_digest(a),profile_digest(b) + return profile_compare(&x,&y,8) +} + +// A zone of a loop function belongs to whoever declares it; any other zone +// is the engine's. The summary says so of each zone. +@(test) +a_zone_belongs_to_whoever_declares_its_function :: proc(t:^testing.T) { + steps:=[]core.Step_Info{{name="a",game=true},{name="b",plugin="terrain"},{name="editor"}} + Case :: struct {zone:string,want:Profile_Zone_Owner} + for c in ([]Case{{"a",{owner=.Game}},{"b",{.Plugin,"terrain"}},{"editor",{}},{"job",{}}}) { + got:=profile_zone_owner(steps,c.zone) + testing.expectf(t,got==c.want,"%s: %v",c.zone,got) + } + capture:Profile_Capture + if !testing.expect(t,capture_fixture(&capture,1,12)) {return} + defer profile_capture_free(&capture) + for name,i in capture.names {capture.owners[i]=profile_zone_owner(steps,name)} + for zone in profile_summary(&capture,8).zones { + if zone.name=="b" {testing.expect(t,zone.owner==.Plugin && zone.plugin=="terrain",zone.plugin)} + if zone.name=="a" {testing.expect_value(t,zone.owner,Profile_Owner.Game)} + } +} + +// A baseline keeps what compare reads of a capture: compared after it is +// read again, nothing changed. A file that is not one, or of another format, +// is refused with what to do. +@(test) +a_baseline_keeps_what_compare_reads_and_refuses_another_file :: proc(t:^testing.T) { + root:=base.test_folder(t,"profile-baseline") + if root=="" {return} + c:Profile_Capture + if !testing.expect(t,capture_fixture(&c,1,12)) {return} + defer profile_capture_free(&c) + context.allocator=context.temp_allocator + digest:=profile_digest(&c) + path,error:=profile_baseline_save(root,"before",digest) + if !testing.expect_value(t,error,"") {return} + _,error=profile_baseline_save(root,"../out",digest) + testing.expect(t,error!="","a name that is not a file name is refused") + loaded,load_error:=profile_baseline_load(root,"before") + if !testing.expect_value(t,load_error,"") {return} + result:=profile_compare(&loaded,&digest,8) + testing.expect(t,result.comparable && result.baseline=="before") + testing.expect_value(t,result.mean_ms.delta,0) + testing.expect_value(t,len(result.only_in_base)+len(result.only_in_id),0) + names:=profile_baselines(root) + testing.expect(t,len(names)==1 && names[0]=="before") + + Case :: struct {text,says:string} + cases:=[]Case{ + {`{"version":1,"frames":`,"is not a baseline"}, + {`{"version":1,"frames":0,"mean_ms":5}`,"is not a baseline"}, + {`{"version":1,"frames":2,"mean_ms":-1}`,"is not a baseline"}, + {`{"version":1,"frames":2,"mean_ms":5,"zones":[{"name":"","self_ms":1}]}`,"is not a baseline"}, + {`{"frames":2,"mean_ms":5}`,"is not a baseline"}, + {strings.concatenate({`{"version":`,fmt.tprint(PROFILE_BASELINE_VERSION+1),`,"frames":2,"mean_ms":5}`}),"format"}, + } + for kase in cases { + testing.expect(t,os.write_entire_file(path,transmute([]u8)kase.text)==nil) + _,refused:=profile_baseline_load(root,"before") + testing.expectf(t,strings.contains(refused,kase.says) && strings.contains(refused,"profile.save"),"%s: %q",kase.text,refused) + } +}