Use sample_start and sample_end pairs instead of the PROFILER_SAMPLE

macro
This commit is contained in:
2023-07-09 01:52:16 +01:00
parent 0073114723
commit c053d20a8f
3 changed files with 100 additions and 90 deletions

View File

@@ -8,11 +8,6 @@
#define MAX_PROFILE_SAMPLES 1024
typedef struct {
const char *title;
u64 duration;
} profiler_sample_t;
typedef struct {
profiler_sample_t samples[MAX_PROFILE_SAMPLES];
u64 cpu_freq;
@@ -101,26 +96,35 @@ void profile_end() {
for (u64 i = 0; i < profiler.size; ++i) {
sample = &(profiler.samples[i]);
u64 duration = sample->end - sample->start;
printf("%*s: %*lld (%*.*f %%)\n", (i32)profiler.max_title_length,
sample->title, duration_char_count,
(unsigned long long)sample->duration, percentage_char_count,
percentage_precision, (f64)sample->duration / total * 100.0);
sample->title, duration_char_count, (unsigned long long)duration,
percentage_char_count, percentage_precision,
(f64)duration / total * 100.0);
}
}
void add_sample(const char *title, u64 duration) {
profiler_sample_t sample_start(const char *title) {
return (profiler_sample_t){
.title = title,
.start = read_cpu_timer(),
.end = 0,
};
}
void sample_end(profiler_sample_t *sample) {
if (profiler.size == MAX_PROFILE_SAMPLES) {
return;
}
u64 length = strlen(title);
u64 length = strlen(sample->title);
if (length > profiler.max_title_length) {
profiler.max_title_length = length;
}
profiler.samples[(profiler.size)++] = (profiler_sample_t){
.title = title,
.duration = duration,
};
sample->end = read_cpu_timer();
profiler.samples[(profiler.size)++] = *sample;
}