-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofiler.h
54 lines (42 loc) · 1.08 KB
/
profiler.h
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
#pragma once
#include <time.h>
#define PROFILER_SIZE 16
static uint64_t profiler_last;
static uint64_t profiler_points[PROFILER_SIZE] = {};
static const char *profiler_names[PROFILER_SIZE] = {};
static inline uint64_t profiler_now(void)
{
struct timespec ts = {};
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000000000 + ts.tv_nsec;
}
static inline double profiler_to_sec(uint64_t t)
{
return (double)t / 1000000000;
}
static inline void profiler_start(void)
{
profiler_last = profiler_now();
}
static inline void profiler_record(size_t pos, const char *name)
{
uint64_t now;
assert(pos < PROFILER_SIZE);
now = profiler_now();
profiler_points[pos] += now - profiler_last;
profiler_names[pos] = name;
profiler_last = now;
}
static inline void profiler_report(void)
{
uint64_t total = 0;
for (size_t i = 0; i < ARRAY_SIZE(profiler_points); i++) {
uint64_t val;
if (profiler_points[i] == 0)
continue;
val = profiler_points[i];
printf("%.9fs %s\n", profiler_to_sec(val), profiler_names[i]);
total += val;
}
printf("total=%fs\n", profiler_to_sec(total));
}