-
Notifications
You must be signed in to change notification settings - Fork 13
/
profiler.cc
102 lines (84 loc) · 2.18 KB
/
profiler.cc
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#if OPPAI_PROFILING
#include <time.h>
#include <map>
#include <string>
// profiling is linux-only for now since it's mainly for myself
internalfn
f64 time_now()
{
struct timespec t;
memset(&t, 0, sizeof(struct timespec));
if (clock_gettime(CLOCK_MONOTONIC, &t) < 0) {
perror(0);
exit(1);
}
return t.tv_sec + t.tv_nsec * (f64)1e-9;
}
#define MAX_PROFILERS 3
globvar char const* profile_last_name[MAX_PROFILERS];
globvar f64 profile_last[MAX_PROFILERS];
struct iterations
{
u32 n;
f64 sum;
iterations() : n(0), sum(0) {}
};
// TODO: don't use map
globvar std::map<std::string, iterations> profile_iterations[MAX_PROFILERS];
internalfn
void profile_init() {
memset(profile_last_name, 0, sizeof(profile_last_name));
memset(profile_last, 0, sizeof(profile_last));
}
internalfn
void profile(int i, char const* name)
{
if (i > MAX_PROFILERS - 1) {
fprintf(stderr, "bruh fix your profilers\n");
exit(1);
}
f64 now = time_now();
if (profile_last_name[i])
{
iterations& it = profile_iterations[i][profile_last_name[i]];
++it.n;
it.sum += now - profile_last[i];
}
profile_last[i] = now;
profile_last_name[i] = name;
}
internalfn
void profile_end()
{
for (int i = 0; i < MAX_PROFILERS; ++i)
{
f64 total_time = 0;
for (std::map<std::string, iterations>::iterator pair =
profile_iterations[i].begin();
pair != profile_iterations[i].end();
++pair)
{
total_time += pair->second.sum;
}
for (std::map<std::string, iterations>::iterator pair =
profile_iterations[i].begin();
pair != profile_iterations[i].end();
++pair)
{
for (int j = 0; j < i; ++j) {
fprintf(stderr, " ");
}
fprintf(
stderr,
"PROFILER%d|%s: %gs (%g%%)\n", i,
pair->first.c_str(), pair->second.sum / pair->second.n,
pair->second.sum / total_time * 100.0
);
}
}
}
#else
#define profile_init()
#define profile(a, b)
#define profile_end()
#endif