Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
Loading...
Searching...
No Matches
perf_timer.h
Go to the documentation of this file.
1
10#ifndef PERF_TIMER_H
11#define PERF_TIMER_H
12
13#include "gl_common.h"
14#include <stdbool.h>
15#include <time.h>
16
24typedef struct {
25 struct timespec start;
26 struct timespec end;
27} PerfTimer;
28
35typedef struct {
36 GLuint query_start;
37 GLuint query_end;
38 bool active;
39} GPUTimer;
40
41#ifdef TRACY_ENABLE
42#include <tracy/TracyC.h>
43#endif
44
49typedef struct {
52#ifdef TRACY_ENABLE
53 TracyCZoneCtx tracy_ctx;
54 TracyCZoneCtx host_ctx;
55#endif
57
58/* ========================================================================= */
59/* CPU Timer API */
60/* ========================================================================= */
61
66void perf_timer_start(PerfTimer* timer);
67
73double perf_timer_elapsed_ms(PerfTimer* timer);
74
80double perf_timer_elapsed_us(PerfTimer* timer);
81
87double perf_timer_elapsed_s(PerfTimer* timer);
88
89/* ========================================================================= */
90/* GPU Timer API */
91/* ========================================================================= */
92
97void gpu_timer_start(GPUTimer* timer);
98
104
113double gpu_timer_elapsed_ms(GPUTimer* timer, int wait_for_result);
114
119void gpu_timer_cleanup(GPUTimer* timer);
120
121/* ========================================================================= */
122/* Hybrid Timer API */
123/* ========================================================================= */
124
130
136void perf_hybrid_stop(HybridTimer* timer, const char* label);
137
148double perf_hybrid_stop_debug(HybridTimer* timer, const char* label);
149
150/* ========================================================================= */
151/* Macro Helpers */
152/* ========================================================================= */
153
165#define PERF_MEASURE_MS(var_name) \
166 double var_name = 0.0; \
167 for (PerfTimer \
168 _timer##var_name = {0}, \
169 *_run = (perf_timer_start(&_timer##var_name), (PerfTimer*)1); \
170 _run; \
171 var_name = perf_timer_elapsed_ms(&_timer##var_name), _run = NULL)
172
176#define PERF_MEASURE_LOG(label) \
177 for (PerfTimer _timer = {0}, \
178 *_run = (perf_timer_start(&_timer), (PerfTimer*)1); \
179 _run; LOG_INFO("perf", "%s: %.2f ms", label, \
180 perf_timer_elapsed_ms(&_timer)), \
181 _run = NULL)
182
186#define GPU_MEASURE_MS(var_name) \
187 double var_name = 0.0; \
188 for (GPUTimer _gpu_timer##var_name = {0}, \
189 *_gpu_run = (gpu_timer_start(&_gpu_timer##var_name), \
190 (GPUTimer*)1); \
191 _gpu_run; \
192 var_name = gpu_timer_elapsed_ms(&_gpu_timer##var_name, 1), \
193 gpu_timer_cleanup(&_gpu_timer##var_name), \
194 _gpu_run = NULL)
195
199#define GPU_MEASURE_LOG(label) \
200 for (GPUTimer \
201 _gpu_timer = {0}, \
202 *_gpu_run = (gpu_timer_start(&_gpu_timer), (GPUTimer*)1); \
203 _gpu_run; LOG_INFO("perf.gpu", "%s: %.2f ms", label, \
204 gpu_timer_elapsed_ms(&_gpu_timer, 1)), \
205 gpu_timer_cleanup(&_gpu_timer), _gpu_run = NULL)
206
211#define HYBRID_MEASURE_LOG(label) \
212 for (HybridTimer _h = perf_hybrid_start(), *_h_run = (HybridTimer*)1; \
213 _h_run; perf_hybrid_stop(&_h, label), _h_run = NULL)
214
229#define HYBRID_MEASURE_DEBUG_MS(var_name, label) \
230 double var_name = 0.0; \
231 for (HybridTimer _h##var_name = perf_hybrid_start(), \
232 *_h_run = (HybridTimer*)1; \
233 _h_run; var_name = perf_hybrid_stop_debug(&_h##var_name, label), \
234 _h_run = NULL)
235
240typedef struct {
242 const char* label;
244
246static inline void hybrid_timer_cleanup_raii(HybridTimerRAII* timer_raii)
247{
248 perf_hybrid_stop(&timer_raii->timer, timer_raii->label);
249}
250
262#define HYBRID_FUNC_TIMER(label) \
263 HybridTimerRAII _h_raii \
264 __attribute__((cleanup(hybrid_timer_cleanup_raii))) \
265 __attribute__((unused)) = {perf_hybrid_start(), label}
266
267#endif /* PERF_TIMER_H */
Common OpenGL definitions, RAII helpers, and utilities.
double perf_timer_elapsed_ms(PerfTimer *timer)
Stops the timer and returns elapsed time in milliseconds.
Definition perf_timer.c:39
void perf_hybrid_stop(HybridTimer *timer, const char *label)
Stops both measurements and logs the results to the console.
Definition perf_timer.c:288
double perf_timer_elapsed_s(PerfTimer *timer)
Stops the timer and returns elapsed time in seconds.
Definition perf_timer.c:69
double perf_timer_elapsed_us(PerfTimer *timer)
Stops the timer and returns elapsed time in microseconds.
Definition perf_timer.c:54
double gpu_timer_elapsed_ms(GPUTimer *timer, int wait_for_result)
Stops the GPU timer and retrieves the result.
Definition perf_timer.c:107
void perf_timer_start(PerfTimer *timer)
Starts the CPU timer.
Definition perf_timer.c:31
HybridTimer perf_hybrid_start(void)
Starts both CPU and GPU measurement simultaneously.
Definition perf_timer.c:210
void gpu_timer_stop(GPUTimer *timer)
Explicitly stops the GPU timer (records end timestamp).
static void hybrid_timer_cleanup_raii(HybridTimerRAII *timer_raii)
Internal cleanup function for HybridTimerRAII.
Definition perf_timer.h:246
void gpu_timer_start(GPUTimer *timer)
Initializes and starts a GPU measurement.
Definition perf_timer.c:88
void gpu_timer_cleanup(GPUTimer *timer)
Releases OpenGL resources associated with the GPU timer.
Definition perf_timer.c:161
double perf_hybrid_stop_debug(HybridTimer *timer, const char *label)
Like perf_hybrid_stop, but logs at DEBUG level instead of INFO.
Definition perf_timer.c:297
GPU performance timer using OpenGL Query Objects.
Definition perf_timer.h:35
bool active
Definition perf_timer.h:38
GLuint query_end
Definition perf_timer.h:37
GLuint query_start
Definition perf_timer.h:36
Internal helper for scope-based timing.
Definition perf_timer.h:240
const char * label
Definition perf_timer.h:242
HybridTimer timer
Definition perf_timer.h:241
Hybrid timer combining CPU and GPU measurements.
Definition perf_timer.h:49
PerfTimer cpu
Definition perf_timer.h:50
GPUTimer gpu
Definition perf_timer.h:51
CPU high-precision timer.
Definition perf_timer.h:24