Compare commits

..

No commits in common. "8e1776577410523854774a235e591c9d2c009640" and "419a7c853404e35e4ca6aa05768c238e41a0905b" have entirely different histories.

4 changed files with 24 additions and 50 deletions

View File

@ -5,4 +5,3 @@ pairs.json
main main
genhavr genhavr
prochavr prochavr
timer_test*

View File

@ -30,13 +30,6 @@
"args": [ "args": [
"${workspaceFolder}/pairs.json" "${workspaceFolder}/pairs.json"
] ]
},
{
"name": "Debug timer test",
"type": "cppdbg",
"request": "launch",
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/timer_test"
} }
] ]
} }

View File

@ -27,10 +27,9 @@ typedef struct sample profiler_sample_t;
struct sample { struct sample {
const char *title; const char *title;
u64 first_start;
u64 start; u64 start;
u64 exclusive_time; u64 duration;
u64 children_time; u64 children_duration;
u64 hit_count; u64 hit_count;
profiler_sample_t *parent; profiler_sample_t *parent;
}; };

View File

@ -1,6 +1,5 @@
#include "profiler/timer.h" #include "profiler/timer.h"
#include "aliases.h" #include "aliases.h"
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
@ -106,17 +105,21 @@ void profile_end() {
continue; continue;
} }
u64 duration = 0;
if (sample->duration >= sample->children_duration) {
duration = sample->duration - sample->children_duration;
}
printf("%*s (hits: %*lld): %*lld (%*.*f %%", (i32)profiler.max_title_length, printf("%*s (hits: %*lld): %*lld (%*.*f %%", (i32)profiler.max_title_length,
sample->title, hits_char_count, sample->title, hits_char_count,
(unsigned long long)sample->hit_count, duration_char_count, (unsigned long long)sample->hit_count, duration_char_count,
(unsigned long long)sample->exclusive_time, percentage_char_count, (unsigned long long)duration, percentage_char_count,
percentage_precision, (f64)(sample->exclusive_time) / total * 100.0); percentage_precision, (f64)duration / total * 100.0);
if (sample->children_time > 0) { if (sample->children_duration > 0) {
printf(", w/ children: %*.*f %%", percentage_char_count, printf(", w/ children: %*.*f %%", percentage_char_count,
percentage_precision, percentage_precision, (f64)sample->duration / total * 100.0);
(f64)(sample->exclusive_time + sample->children_time) / total *
100.0);
} }
printf(")\n"); printf(")\n");
@ -132,10 +135,9 @@ void sample_start(u64 id, const char *title) {
if (!(sample->title) || strcmp(title, sample->title) != 0) { if (!(sample->title) || strcmp(title, sample->title) != 0) {
sample->title = title; sample->title = title;
sample->first_start = 0;
sample->start = 0; sample->start = 0;
sample->exclusive_time = 0; sample->duration = 0;
sample->children_time = 0; sample->children_duration = 0;
sample->hit_count = 0; sample->hit_count = 0;
sample->parent = NULL; sample->parent = NULL;
@ -147,20 +149,9 @@ void sample_start(u64 id, const char *title) {
} }
sample->start = read_cpu_timer(); sample->start = read_cpu_timer();
if (sample->hit_count == 0) {
sample->first_start = sample->start;
}
++(sample->hit_count); ++(sample->hit_count);
if (profiler.active) { if (sample != profiler.active) {
u64 duration = sample->start - (profiler.active->start);
profiler.active->exclusive_time += duration;
}
if (!(profiler.active) || sample != profiler.active) {
// This handles recursive functions by changing the parent only when a // This handles recursive functions by changing the parent only when a
// function isn't calling itself // function isn't calling itself
sample->parent = profiler.active; sample->parent = profiler.active;
@ -178,24 +169,16 @@ void sample_end(u64 id) {
u64 duration = read_cpu_timer() - sample->start; u64 duration = read_cpu_timer() - sample->start;
sample->exclusive_time += duration; if (!(sample->parent) || sample != sample->parent) {
// This handles recursive functions by adding to the duration only when a
// function isn't calling itself
sample->duration += duration;
}
u64 now = read_cpu_timer(); if (sample->parent && sample != sample->parent) {
// This handles recursive functions by adding to the children_duration only
// Reset the start time at the end of the sample to handle recursion // when a function isn't calling itself
sample->start = now; sample->parent->children_duration += duration;
profiler_sample_t *parent = sample->parent;
if (parent) {
sample->parent->start = now;
// Add sample duration to all parents. This handles deep call stacks
while (parent) {
parent->children_time += duration;
parent = parent->parent;
}
} }
profiler.active = sample->parent; profiler.active = sample->parent;