-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectable_profiler.c
204 lines (162 loc) · 6.69 KB
/
selectable_profiler.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* ========================================================================
(C) Copyright 2023 by Molly Rocket, Inc., All Rights Reserved.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Please see https://computerenhance.com for more information
======================================================================== */
/* ========================================================================
LISTING 91
======================================================================== */
#include "platform_metrics.c"
#ifndef PROFILER
#define PROFILER 0
#endif
#define ArrayCount(Array) (sizeof(Array) / sizeof((Array)[0]))
typedef struct {
char const *Label;
u64 OldTSCElapsedInclusive;
u64 StartTSC;
u32 ParentIndex;
u32 AnchorIndex;
} profile_block;
#if PROFILER
struct profile_anchor
{
u64 TSCElapsedExclusive; // NOTE(casey): Does NOT include children
u64 TSCElapsedInclusive; // NOTE(casey): DOES include children
u64 HitCount;
char const *Label;
u8 IsCounted;
u64 InnerCount;
};
typedef struct profile_anchor profile_anchor;
static profile_anchor GlobalProfilerAnchors[4096];
static u32 GlobalProfilerParent;
profile_block new_profile_block(profile_block *blk, char const *Label_, u32 AnchorIndex_) {
//printf("init!\n");
blk->ParentIndex = GlobalProfilerParent;
blk->AnchorIndex = AnchorIndex_;
blk->Label = Label_;
profile_anchor *Anchor = GlobalProfilerAnchors + blk->AnchorIndex;
blk->OldTSCElapsedInclusive = Anchor->TSCElapsedInclusive;
GlobalProfilerParent = blk->AnchorIndex;
blk->StartTSC = ReadCPUTimer();
}
void end_profile_block(profile_block blk) {
//printf("exit!\n");
u64 Elapsed = ReadCPUTimer() - blk.StartTSC;
GlobalProfilerParent = blk.ParentIndex;
profile_anchor *Parent = GlobalProfilerAnchors + blk.ParentIndex;
profile_anchor *Anchor = GlobalProfilerAnchors + blk.AnchorIndex;
Parent->TSCElapsedExclusive -= Elapsed;
Anchor->TSCElapsedExclusive += Elapsed;
Anchor->TSCElapsedInclusive = blk.OldTSCElapsedInclusive + Elapsed;
Anchor->IsCounted = 0;
++Anchor->HitCount;
/* NOTE(casey): This write happens every time solely because there is no
straightforward way in C++ to have the same ease-of-use. In a better programming
language, it would be simple to have the anchor points gathered and labeled at compile
time, and this repetative write would be eliminated. */
Anchor->Label = blk.Label;
}
void end_counted_profile_block(profile_block blk, int count) {
//printf("exit!\n");
u64 Elapsed = ReadCPUTimer() - blk.StartTSC;
GlobalProfilerParent = blk.ParentIndex;
profile_anchor *Parent = GlobalProfilerAnchors + blk.ParentIndex;
profile_anchor *Anchor = GlobalProfilerAnchors + blk.AnchorIndex;
Parent->TSCElapsedExclusive -= Elapsed;
Anchor->TSCElapsedExclusive += Elapsed;
Anchor->TSCElapsedInclusive = blk.OldTSCElapsedInclusive + Elapsed;
Anchor->IsCounted = 1;
Anchor->InnerCount += count;
++Anchor->HitCount;
/* NOTE(casey): This write happens every time solely because there is no
straightforward way in C++ to have the same ease-of-use. In a better programming
language, it would be simple to have the anchor points gathered and labeled at compile
time, and this repetative write would be eliminated. */
Anchor->Label = blk.Label;
}
#define NameConcat2(A, B) A##B
#define NameConcat(A, B) NameConcat2(A, B)
#define TimeBlock(blk, Name) new_profile_block(&blk, Name, __COUNTER__ + 1);
#define EndTimeBlock(blk) end_profile_block( blk );
#define EndCountedTimeBlock(blk, count) end_counted_profile_block(blk, count);
#define ProfilerEndOfCompilationUnit static_assert(__COUNTER__ < ArrayCount(GlobalProfilerAnchors), "Number of profile points exceeds size of profiler::Anchors array")
static void PrintTimeElapsed(u64 TotalTSCElapsed, profile_anchor *Anchor)
{
f64 Percent = 100.0 * ((f64)Anchor->TSCElapsedExclusive / (f64)TotalTSCElapsed);
printf(" %s[%llu]: %llu cycles (%.2f%%", Anchor->Label, Anchor->HitCount, Anchor->TSCElapsedExclusive, Percent);
if(Anchor->TSCElapsedInclusive != Anchor->TSCElapsedExclusive)
{
f64 PercentWithChildren = 100.0 * ((f64)Anchor->TSCElapsedInclusive / (f64)TotalTSCElapsed);
printf(", %.2f%% w/children", PercentWithChildren);
}
if(Anchor->IsCounted) {
printf(", %llu per iteration", Anchor->TSCElapsedInclusive/Anchor->InnerCount);
}
printf(")\n");
}
static void PrintAnchorData(u64 TotalCPUElapsed)
{
for(u32 AnchorIndex = 0; AnchorIndex < ArrayCount(GlobalProfilerAnchors); ++AnchorIndex)
{
profile_anchor *Anchor = GlobalProfilerAnchors + AnchorIndex;
if(Anchor->TSCElapsedInclusive)
{
PrintTimeElapsed(TotalCPUElapsed, Anchor);
}
}
}
struct profiler
{
u64 StartTSC;
u64 EndTSC;
};
typedef struct profiler profiler;
static profiler GlobalProfiler;
#define TimeFunction TimeBlock(__func__)
static void BeginProfile(void)
{
GlobalProfiler.StartTSC = ReadCPUTimer();
}
static void EndAndPrintProfile()
{
GlobalProfiler.EndTSC = ReadCPUTimer();
u64 CPUFreq = EstimateCPUTimerFreq();
u64 TotalCPUElapsed = GlobalProfiler.EndTSC - GlobalProfiler.StartTSC;
if(CPUFreq)
{
printf("\nTotal time: %0.4fms (CPU freq %llu)\n", 1000.0 * (f64)TotalCPUElapsed / (f64)CPUFreq, CPUFreq);
}
PrintAnchorData(TotalCPUElapsed);
}
static void PrintBlockData(profile_block* blk) {
u64 TotalTSCElapsed = GlobalProfiler.EndTSC - GlobalProfiler.StartTSC;
profile_anchor *Anchor = GlobalProfilerAnchors + blk->AnchorIndex;
if(Anchor->TSCElapsedInclusive) {
f64 Percent = 100.0 * ((f64)Anchor->TSCElapsedExclusive / (f64)TotalTSCElapsed);
printf(" %s[%llu]: %llu (%.2f%%", Anchor->Label, Anchor->HitCount, Anchor->TSCElapsedExclusive, Percent);
if(Anchor->TSCElapsedInclusive != Anchor->TSCElapsedExclusive)
{
f64 PercentWithChildren = 100.0 * ((f64)Anchor->TSCElapsedInclusive / (f64)TotalTSCElapsed);
printf(", %.2f%% w/children", PercentWithChildren);
}
if(Anchor->IsCounted) {
printf(", %llu per iteration", Anchor->TSCElapsedInclusive/Anchor->InnerCount);
}
printf(")\n");
}
}
#else
#define TimeBlock(...) do { } while(0)
#define PrintAnchorData(...)
#define ProfilerEndOfCompilationUnit
#define EndTimeBlock(blk)
#define EndCountedTimeBlock(blk, count)
void end_profile_block(profile_block blk) {
}
static void EndAndPrintProfile() {
}
#endif