From 3b8362143d8f98376b6a4a422cd0481618bbfc09 Mon Sep 17 00:00:00 2001 From: jacob Date: Tue, 31 Mar 2026 20:43:23 -0500 Subject: [PATCH] collect persistent profiler samples --- src/base/base_prof.c | 5 +- src/base/base_prof.h | 2 +- src/pp/pp_sim/pp_sim_core.c | 4 +- src/pp/pp_vis/pp_vis_core.c | 110 +++++++++++++++++++++++++++++------- src/pp/pp_vis/pp_vis_core.h | 21 ++++++- 5 files changed, 116 insertions(+), 26 deletions(-) diff --git a/src/base/base_prof.c b/src/base/base_prof.c index 6f479bb9..e5d22f70 100644 --- a/src/base/base_prof.c +++ b/src/base/base_prof.c @@ -45,8 +45,9 @@ void BeginProfZone(char *name_cstr_lit) } LockTicketMutex(&Base.prof.register_track_tm); { - Base_tl.prof.track_idx = Atomic32Fetch(&Base.prof.tracks_count); - Base.prof.tracks[Base_tl.prof.track_idx] = track; + u64 track_idx = Atomic32Fetch(&Base.prof.tracks_count); + Base.prof.tracks[track_idx] = track; + track->id = track_idx; Atomic32FetchAdd(&Base.prof.tracks_count, 1); } UnlockTicketMutex(&Base.prof.register_track_tm); diff --git a/src/base/base_prof.h b/src/base/base_prof.h index efa8d354..36f14619 100644 --- a/src/base/base_prof.h +++ b/src/base/base_prof.h @@ -20,6 +20,7 @@ Struct(ProfSample) Struct(ProfTrack) { + u64 id; Atomic64 top_sample_seq; ProfSample samples[ProfTrackSamplesCap]; }; @@ -43,7 +44,6 @@ Struct(ProfCtx) Struct(ThreadLocalProfCtx) { - u32 track_idx; ProfTrack *thread_track; }; diff --git a/src/pp/pp_sim/pp_sim_core.c b/src/pp/pp_sim/pp_sim_core.c index 3bc6ec62..e5fb4179 100644 --- a/src/pp/pp_sim/pp_sim_core.c +++ b/src/pp/pp_sim/pp_sim_core.c @@ -85,7 +85,9 @@ void S_TickForever(WaveLaneCtx *lane) //- Sim loop b32 shutdown = 0; - while (!shutdown) ProfZoneDF("Sim tick") + + while (!shutdown) + ProfZoneDF("Sim tick") { shutdown = Atomic32Fetch(&S.shutdown); P_tl.debug_draw_enabled = TweakBool("Simulation debug draw", 0); diff --git a/src/pp/pp_vis/pp_vis_core.c b/src/pp/pp_vis/pp_vis_core.c index b41e2a3a..88d01515 100644 --- a/src/pp/pp_vis/pp_vis_core.c +++ b/src/pp/pp_vis/pp_vis_core.c @@ -872,7 +872,9 @@ void V_TickForever(WaveLaneCtx *lane) //- Begin vis loop b32 shutdown = 0; - while (!shutdown) ProfZoneDF("Vis tick") + + while (!shutdown) + ProfZoneDF("Vis tick") { shutdown = Atomic32Fetch(&V.shutdown); P_tl.debug_draw_enabled = TweakBool("Vis debug draw", 0); @@ -5372,6 +5374,55 @@ void V_TickForever(WaveLaneCtx *lane) + ////////////////////////////// + //- Collect profiler samples + + ProfZoneDF("Collect profiler samples") + if (PROFILING_ENABLED) + { + RegisteredProfTracksArray prof_tracks = FetchRegisteredProfTracks(frame->arena); + for (u64 track_idx = 0; track_idx < prof_tracks.count; ++track_idx) + { + ProfTrack *prof_track = prof_tracks.tracks[track_idx]; + + //- Fetch collection track + V_ProfilerTrackCollection *col_track = 0; + { + for (col_track = V.first_profiler_track_collection; col_track; col_track = col_track->next) + { + if (col_track->prof_track->id == prof_track->id) + { + break; + } + } + if (!col_track) + { + col_track = PushStruct(perm, V_ProfilerTrackCollection); + col_track->samples_arena = AcquireArena(Gibi(64)); + col_track->prof_track = prof_track; + col_track->samples = ArenaFirst(col_track->samples_arena, ProfSample); + SllQueuePush(V.first_profiler_track_collection, V.last_profiler_track_collection, col_track); + } + } + + //- Copy new samples + u64 copy_sample_start_seq = col_track->next_collection_seq; + u64 copy_sample_end_seq = Atomic64Fetch(&prof_track->top_sample_seq); + u64 copy_samples_count = copy_sample_end_seq - copy_sample_start_seq; + ProfSample *copy_samples_dst = PushStructsNoZero(col_track->samples_arena, ProfSample, copy_samples_count); + for (u64 src_sample_seq = copy_sample_start_seq; src_sample_seq < copy_sample_end_seq; ++src_sample_seq) + { + u64 src_idx = src_sample_seq % ProfTrackSamplesCap; + u64 dst_idx = src_sample_seq - copy_sample_start_seq; + copy_samples_dst[dst_idx] = prof_track->samples[src_idx]; + } + col_track->next_collection_seq = copy_sample_end_seq; + col_track->samples_count += copy_samples_count; + } + } + + + @@ -5648,13 +5699,12 @@ void V_TickForever(WaveLaneCtx *lane) UI_BuildSpacer(window_padding, Axis_X); UI_SetDF(Parent, UI_BuildColumn()) { - //- Collect tracks + //- Build zone tracks i64 highest_sample_ns = 0; V_ZoneTrack *first_zone_track = 0; V_ZoneTrack *last_zone_track = 0; { - RegisteredProfTracksArray prof_tracks = FetchRegisteredProfTracks(frame->arena); - for (u64 track_idx = 0; track_idx < prof_tracks.count; ++track_idx) + for (V_ProfilerTrackCollection *col_track = V.first_profiler_track_collection; col_track; col_track = col_track->next) { // FIXME: Remove this // if (track_idx == 0) continue; @@ -5662,19 +5712,34 @@ void V_TickForever(WaveLaneCtx *lane) V_ZoneTrack *zone_track = PushStruct(frame->arena, V_ZoneTrack); DllQueuePush(first_zone_track, last_zone_track, zone_track); - ProfTrack *prof_track = prof_tracks.tracks[track_idx]; // FIXME: Atomic - u64 track_top_sample_seq = Atomic64Fetch(&prof_track->top_sample_seq); - ProfSample *samples = prof_track->samples; - u64 samples_start_seq = track_top_sample_seq - MinU64(track_top_sample_seq, ProfTrackSamplesCap); - u64 samples_end_seq = track_top_sample_seq; + u64 samples_count = col_track->samples_count; + ProfSample *samples = col_track->samples; + + + u64 start_sample_idx = 0; + samples_count = MinU64(samples_count, 512); - samples_end_seq = MinU64(samples_end_seq, samples_start_seq + 512); + // u64 samples_start_seq = track_top_sample_seq - MinU64(track_top_sample_seq, ProfTrackSamplesCap); + // u64 samples_end_seq = track_top_sample_seq; + + + + // for (u64 sample_seq = samples_start_seq; sample_seq < samples_end_seq + + + + + + + + + // samples_end_seq = MinU64(samples_end_seq, samples_start_seq + 512); // samples_end_seq = MinU64(samples_end_seq, samples_start_seq + 64); - // samples_end_seq = MinU64(samples_end_seq, samples_start_seq + Kibi(16)); + // // samples_end_seq = MinU64(samples_end_seq, samples_start_seq + Kibi(16)); @@ -5689,9 +5754,9 @@ void V_TickForever(WaveLaneCtx *lane) // u64 zone_idx = 0; u64 depth = 0; - for (u64 sample_seq = samples_start_seq; sample_seq < samples_end_seq; ++sample_seq) + for (u64 sample_idx = start_sample_idx; sample_idx < samples_count; ++sample_idx) { - ProfSample *sample = &samples[sample_seq % ProfTrackSamplesCap]; + ProfSample *sample = &samples[sample_idx]; // u64 elapsed_ns = NsFromTsc @@ -5708,7 +5773,7 @@ void V_TickForever(WaveLaneCtx *lane) zone = zone->prev; } zone->end_ns = time_ns; - zone->end_sample_seq = sample_seq; + // zone->end_sample_seq = sample_seq; highest_sample_ns = MaxI64(highest_sample_ns, time_ns); } @@ -5726,15 +5791,20 @@ void V_TickForever(WaveLaneCtx *lane) // u64 seed = MixU64((u64)name_cstr); f32 h = (Norm16(seed >> 0) * 1) * 360; - f32 s = TweakFloat("Profiler zone saturation", 0.85, 0, 1); - f32 v = TweakFloat("Profiler zone brightness", 0.50, 0, 1); + // f32 s = TweakFloat("Profiler zone saturation", 0.85, 0, 1); + // f32 v = TweakFloat("Profiler zone brightness", 0.50, 0, 1); + f32 s = TweakFloat("Profiler zone saturation", 0.50, 0, 1); + f32 v = TweakFloat("Profiler zone brightness", 1.00, 0, 1); zone->color = SrgbFromHsv(h, s, v); - zone->start_sample_seq = sample_seq; + // zone->start_sample_seq = sample_seq; zone->name = name; zone->depth = depth; + // TODO: Real zone ID + zone->id = MixU64s(sample_idx, col_track->prof_track->id); + depth += 1; zone_track->zone_rows_count = MaxU64(depth, zone_track->zone_rows_count); } @@ -5882,7 +5952,7 @@ void V_TickForever(WaveLaneCtx *lane) { if (zone->end_ns > zone->start_ns) { - UI_Key zone_box = UI_KeyF("Zone %F", FmtUint(zone->start_sample_seq)); + UI_Key zone_box = UI_KeyF("Zone %F", FmtUint(zone->id)); if (UI_HotAbsolute(zone_box)) { hovered_zone_box = zone_box; @@ -7114,7 +7184,7 @@ void V_TickForever(WaveLaneCtx *lane) frame->backdrop_src = sprite.tex; } - // Tiles + // Tile sprites { for (P_TileKind tile_kind = 0; tile_kind < P_TileKind_COUNT; ++tile_kind) { @@ -7135,7 +7205,7 @@ void V_TickForever(WaveLaneCtx *lane) } } - // Upload tiles + // Tile map if (frame->tiles_dirty) { // LogDebugF("Uploading tiles to gpu"); diff --git a/src/pp/pp_vis/pp_vis_core.h b/src/pp/pp_vis/pp_vis_core.h index 2a842676..71e81fb8 100644 --- a/src/pp/pp_vis/pp_vis_core.h +++ b/src/pp/pp_vis/pp_vis_core.h @@ -249,8 +249,7 @@ Struct(V_ZoneDesc) V_ZoneDesc *next_in_row; String name; - u64 start_sample_seq; - u64 end_sample_seq; + u64 id; u32 depth; @@ -387,6 +386,18 @@ Struct(V_ObservationBin) #define V_MaxInterpRatio 4 +Struct(V_ProfilerTrackCollection) +{ + V_ProfilerTrackCollection *next; + + u64 samples_count; + ProfSample *samples; + Arena *samples_arena; + + ProfTrack *prof_track; + u64 next_collection_seq; +}; + Struct(V_Frame) { Arena *arena; @@ -435,6 +446,12 @@ Struct(V_Ctx) Arena *transient_markers_arena; Arena *persistent_markers_arena; + V_ProfilerTrackCollection *first_profiler_track_collection; + V_ProfilerTrackCollection *last_profiler_track_collection; + + u64 profiler_samples_count; + ProfSample *profile_samples; + i64 panels_count; i64 windows_count; V_Panel *root_panel;