95 lines
2.8 KiB
C
95 lines
2.8 KiB
C
////////////////////////////////////////////////////////////
|
|
//~ Profile zone
|
|
|
|
// void PushProfZoneEx(char *name_cstr_lit, b32 end_zone)
|
|
// {
|
|
// ProfTrack *track = Base_tl.prof.thread_track;
|
|
// if (!track)
|
|
// {
|
|
// Arena *perm = PermArena();
|
|
// {
|
|
// track = PushStruct(perm, ProfTrack);
|
|
// Base_tl.prof.thread_track = track;
|
|
// }
|
|
// 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;
|
|
// Atomic32FetchAdd(&Base.prof.tracks_count, 1);
|
|
// }
|
|
// UnlockTicketMutex(&Base.prof.register_track_tm);
|
|
// }
|
|
|
|
// u64 sample_seq = track->top_sample_seq;
|
|
// ProfSample *sample = &track->samples[sample_seq % ProfTrackSamplesCap];
|
|
// {
|
|
// // TODO: Wrap rdtsc for cpu-relative <-> program-relative timestamp conversion
|
|
// sample->stamp = __rdtsc() | ((u64)(!!end_zone) << 63);
|
|
// // sample->stamp = TimeNs() | ((u64)(!!end_zone) << 63);
|
|
// sample->name_cstr_lit = name_cstr_lit;
|
|
// }
|
|
// track->top_sample_seq = sample_seq + 1;
|
|
// }
|
|
|
|
|
|
|
|
void BeginProfZone(char *name_cstr_lit)
|
|
{
|
|
ProfTrack *track = Base_tl.prof.thread_track;
|
|
if (!track)
|
|
{
|
|
Arena *perm = PermArena();
|
|
{
|
|
track = PushStruct(perm, ProfTrack);
|
|
Base_tl.prof.thread_track = track;
|
|
}
|
|
LockTicketMutex(&Base.prof.register_track_tm);
|
|
{
|
|
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);
|
|
}
|
|
u64 sample_seq = *NonAtomic(&track->top_sample_seq);
|
|
ProfSample *sample = &track->samples[sample_seq % ProfTrackSamplesCap];
|
|
{
|
|
sample->flags = 0;
|
|
sample->name_cstr_lit = name_cstr_lit;
|
|
sample->tsc = BeginTsc();
|
|
}
|
|
Atomic64Set(&track->top_sample_seq, sample_seq + 1);
|
|
}
|
|
|
|
void EndProfZone(void)
|
|
{
|
|
i64 tsc = EndTsc();
|
|
ProfTrack *track = Base_tl.prof.thread_track;
|
|
u64 sample_seq = *NonAtomic(&track->top_sample_seq);
|
|
ProfSample *sample = &track->samples[sample_seq % ProfTrackSamplesCap];
|
|
{
|
|
sample->flags = ProfSampleFlag_EndZone;
|
|
sample->tsc = tsc;
|
|
sample->name_cstr_lit = 0;
|
|
}
|
|
Atomic64Set(&track->top_sample_seq, sample_seq + 1);
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Retrieve tracks
|
|
|
|
RegisteredProfTracksArray FetchRegisteredProfTracks(Arena *arena)
|
|
{
|
|
RegisteredProfTracksArray result = Zi;
|
|
{
|
|
result.count = Atomic32Fetch(&Base.prof.tracks_count);
|
|
result.tracks = PushStructsNoZero(arena, ProfTrack *, result.count);
|
|
for (u64 track_idx = 0; track_idx < result.count; ++track_idx)
|
|
{
|
|
result.tracks[track_idx] = Base.prof.tracks[track_idx];
|
|
}
|
|
}
|
|
return result;
|
|
}
|