-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathats_timer.c
71 lines (60 loc) · 1.58 KB
/
ats_timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "ats.h"
typedef struct timer_entry timer_entry;
struct timer_entry {
const char* name;
f64 start;
f64 stop;
u32 depth;
};
typedef struct timer_node timer_node;
struct timer_node {
const char* name;
f32 current;
f32 max;
};
static u32 timer_top;
static u32 timer_count;
static timer_entry timer_stack[512];
static timer_entry timer_array[512];
static timer_node timer_table[512];
#define timer_scope(name) scope_guard(timer_start(name), timer_stop())
static void timer_start(const char* name) {
timer_entry* entry = timer_stack + timer_top++;
entry->name = name;
entry->start = platform_get_time();
entry->stop = 0;
entry->depth = timer_top - 1;
}
static timer_node* timer_node_next(timer_node* node) {
node++;
if (node >= (timer_table + 512)) {
node = timer_table;
}
return node;
}
static void timer_stop(void) {
timer_entry* entry = timer_stack + (--timer_top);
{
entry->stop = platform_get_time();
timer_array[timer_count++] = *entry;
}
{
u32 hash = hash_str(entry->name);
u32 index = hash & 511;
timer_node* node = &timer_table[index];
while (node->name && (strcmp(entry->name, node->name) != 0)) {
node = timer_node_next(node);
}
if (!node->name) {
node->name = entry->name;
}
node->current = entry->stop - entry->start;
if (node->max < node->current) {
node->max = node->current;
}
}
}
static void timer_reset_all(void) {
timer_top = 0;
timer_count = 0;
}