$darkmode
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
perf_timer.h File Reference

High-precision performance measurement (CPU and GPU). More...

#include "gl_common.h"
#include <time.h>
Include dependency graph for perf_timer.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PerfTimer
 CPU high-precision timer. More...
 
struct  GPUTimer
 GPU performance timer using OpenGL Query Objects. More...
 
struct  HybridTimer
 Hybrid timer combining CPU and GPU measurements. More...
 
struct  HybridTimerRAII
 Internal helper for scope-based timing. More...
 

Macros

#define PERF_MEASURE_MS(var_name)
 Automatically measures a code block and stores result in var_name. More...
 
#define PERF_MEASURE_LOG(label)
 Automatically measures and logs a code block to the "perf" category. More...
 
#define GPU_MEASURE_MS(var_name)
 Automatically measures a GPU block and stores result in var_name. More...
 
#define GPU_MEASURE_LOG(label)
 Automatically measures and logs a GPU block to "perf.gpu". More...
 
#define HYBRID_MEASURE_LOG(label)
 Hybrid measurement macro. Useful for identifying driver vs GPU bottlenecks. More...
 
#define HYBRID_MEASURE_DEBUG_MS(var_name, label)
 Hybrid measurement with DEBUG-level console logging. More...
 
#define HYBRID_FUNC_TIMER(label)
 Scoped hybrid timer. Automatically logs on scope exit. More...
 

Functions

void perf_timer_start (PerfTimer *timer)
 Starts the CPU timer. More...
 
double perf_timer_elapsed_ms (PerfTimer *timer)
 Stops the timer and returns elapsed time in milliseconds. More...
 
double perf_timer_elapsed_us (PerfTimer *timer)
 Stops the timer and returns elapsed time in microseconds. More...
 
double perf_timer_elapsed_s (PerfTimer *timer)
 Stops the timer and returns elapsed time in seconds. More...
 
void gpu_timer_start (GPUTimer *timer)
 Initializes and starts a GPU measurement. More...
 
void gpu_timer_stop (GPUTimer *timer)
 Explicitly stops the GPU timer (records end timestamp). More...
 
double gpu_timer_elapsed_ms (GPUTimer *timer, int wait_for_result)
 Stops the GPU timer and retrieves the result. More...
 
void gpu_timer_cleanup (GPUTimer *timer)
 Releases OpenGL resources associated with the GPU timer. More...
 
HybridTimer perf_hybrid_start (void)
 Starts both CPU and GPU measurement simultaneously. More...
 
void perf_hybrid_stop (HybridTimer *timer, const char *label)
 Stops both measurements and logs the results to the console. More...
 
double perf_hybrid_stop_debug (HybridTimer *timer, const char *label)
 Like perf_hybrid_stop, but logs at DEBUG level instead of INFO. More...
 
static void hybrid_timer_cleanup_raii (HybridTimerRAII *timer_raii)
 Internal cleanup function for HybridTimerRAII. More...
 

Detailed Description

High-precision performance measurement (CPU and GPU).

This module provides timers for measuring CPU wall-clock time using clock_gettime and GPU execution time using OpenGL timer queries. It also includes RAII-style macros for automatic profiling.

Macro Definition Documentation

◆ GPU_MEASURE_LOG

#define GPU_MEASURE_LOG (   label)
Value:
for (GPUTimer \
_gpu_timer = {0}, \
*_gpu_run = (gpu_timer_start(&_gpu_timer), (GPUTimer*)1); \
_gpu_run; LOG_INFO("perf.gpu", "%s: %.2f ms", label, \
gpu_timer_elapsed_ms(&_gpu_timer, 1)), \
gpu_timer_cleanup(&_gpu_timer), _gpu_run = NULL)
#define LOG_INFO(tag,...)
Log an info message.
Definition: log.h:73
double gpu_timer_elapsed_ms(GPUTimer *timer, int wait_for_result)
Stops the GPU timer and retrieves the result.
Definition: perf_timer.c:110
void gpu_timer_start(GPUTimer *timer)
Initializes and starts a GPU measurement.
Definition: perf_timer.c:91
GPU performance timer using OpenGL Query Objects.
Definition: perf_timer.h:34

Automatically measures and logs a GPU block to "perf.gpu".

◆ GPU_MEASURE_MS

#define GPU_MEASURE_MS (   var_name)
Value:
double var_name = 0.0; \
for (GPUTimer _gpu_timer##var_name = {0}, \
*_gpu_run = (gpu_timer_start(&_gpu_timer##var_name), \
(GPUTimer*)1); \
_gpu_run; \
var_name = gpu_timer_elapsed_ms(&_gpu_timer##var_name, 1), \
gpu_timer_cleanup(&_gpu_timer##var_name), \
_gpu_run = NULL)

Automatically measures a GPU block and stores result in var_name.

◆ HYBRID_FUNC_TIMER

#define HYBRID_FUNC_TIMER (   label)
Value:
HybridTimerRAII _h_raii \
__attribute__((cleanup(hybrid_timer_cleanup_raii))) \
__attribute__((unused)) = {perf_hybrid_start(), label}
HybridTimer perf_hybrid_start(void)
Starts both CPU and GPU measurement simultaneously.
Definition: perf_timer.c:212
static void hybrid_timer_cleanup_raii(HybridTimerRAII *timer_raii)
Internal cleanup function for HybridTimerRAII.
Definition: perf_timer.h:245
Internal helper for scope-based timing.
Definition: perf_timer.h:239

Scoped hybrid timer. Automatically logs on scope exit.

Usage:

void complex_function() {
HYBRID_FUNC_TIMER("IBL: Specular Map");
// ... function body ...
}
#define HYBRID_FUNC_TIMER(label)
Scoped hybrid timer. Automatically logs on scope exit.
Definition: perf_timer.h:261

◆ HYBRID_MEASURE_DEBUG_MS

#define HYBRID_MEASURE_DEBUG_MS (   var_name,
  label 
)
Value:
double var_name = 0.0; \
for (HybridTimer _h##var_name = perf_hybrid_start(), \
*_h_run = (HybridTimer*)1; \
_h_run; var_name = perf_hybrid_stop_debug(&_h##var_name, label), \
_h_run = NULL)
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:298
Hybrid timer combining CPU and GPU measurements.
Definition: perf_timer.h:48

Hybrid measurement with DEBUG-level console logging.

Like HYBRID_MEASURE_LOG but demotes console output to LOG_DEBUG. Tracy zones are still emitted. Captures GPU elapsed ms into var_name.

Usage:

HYBRID_MEASURE_DEBUG_MS(gpu_ms, "Progressive IBL: Specular Mip 0 Slice
1/24") { pbr_prefilter_mip(...);
}
// gpu_ms now contains the GPU time in milliseconds
void pbr_prefilter_mip(GLuint shader, const PBRSpecUniforms *uniforms, GLuint env_hdr_tex, GLuint dest_tex, int width, int height, int level, int total_levels, int slice_index, int total_slices, float threshold)
Computes a single slice or mip-level for progressive pre-filtering.
Definition: pbr.c:63
#define HYBRID_MEASURE_DEBUG_MS(var_name, label)
Hybrid measurement with DEBUG-level console logging.
Definition: perf_timer.h:228

◆ HYBRID_MEASURE_LOG

#define HYBRID_MEASURE_LOG (   label)
Value:
for (HybridTimer _h = perf_hybrid_start(), *_h_run = (HybridTimer*)1; \
_h_run; perf_hybrid_stop(&_h, label), _h_run = NULL)
void perf_hybrid_stop(HybridTimer *timer, const char *label)
Stops both measurements and logs the results to the console.
Definition: perf_timer.c:289

Hybrid measurement macro. Useful for identifying driver vs GPU bottlenecks.

◆ PERF_MEASURE_LOG

#define PERF_MEASURE_LOG (   label)
Value:
for (PerfTimer _timer = {0}, \
*_run = (perf_timer_start(&_timer), (PerfTimer*)1); \
_run; LOG_INFO("perf", "%s: %.2f ms", label, \
perf_timer_elapsed_ms(&_timer)), \
_run = NULL)
double perf_timer_elapsed_ms(PerfTimer *timer)
Stops the timer and returns elapsed time in milliseconds.
Definition: perf_timer.c:39
void perf_timer_start(PerfTimer *timer)
Starts the CPU timer.
Definition: perf_timer.c:30
CPU high-precision timer.
Definition: perf_timer.h:23

Automatically measures and logs a code block to the "perf" category.

◆ PERF_MEASURE_MS

#define PERF_MEASURE_MS (   var_name)
Value:
double var_name = 0.0; \
for (PerfTimer \
_timer##var_name = {0}, \
*_run = (perf_timer_start(&_timer##var_name), (PerfTimer*)1); \
_run; \
var_name = perf_timer_elapsed_ms(&_timer##var_name), _run = NULL)

Automatically measures a code block and stores result in var_name.

Usage:

PERF_MEASURE_MS(load_time) {
// Code to measure
}
printf("Took %.2f ms\n", load_time);
#define PERF_MEASURE_MS(var_name)
Automatically measures a code block and stores result in var_name.
Definition: perf_timer.h:164

Function Documentation

◆ gpu_timer_cleanup()

void gpu_timer_cleanup ( GPUTimer timer)

Releases OpenGL resources associated with the GPU timer.

Parameters
timerPointer to the timer.

◆ gpu_timer_elapsed_ms()

double gpu_timer_elapsed_ms ( GPUTimer timer,
int  wait_for_result 
)

Stops the GPU timer and retrieves the result.

Parameters
timerPointer to the timer.
wait_for_resultIf true, blocks until the GPU is finished and result is ready.
Returns
Elapsed time in milliseconds, or -1.0 if not ready and wait_for_result is false.

◆ gpu_timer_start()

void gpu_timer_start ( GPUTimer timer)

Initializes and starts a GPU measurement.

Parameters
timerPointer to the timer.

◆ gpu_timer_stop()

void gpu_timer_stop ( GPUTimer timer)

Explicitly stops the GPU timer (records end timestamp).

Parameters
timerPointer to the timer.

◆ hybrid_timer_cleanup_raii()

static void hybrid_timer_cleanup_raii ( HybridTimerRAII timer_raii)
inlinestatic

Internal cleanup function for HybridTimerRAII.

◆ perf_hybrid_start()

HybridTimer perf_hybrid_start ( void  )

Starts both CPU and GPU measurement simultaneously.

Returns
Initialized and started HybridTimer structure.
Here is the call graph for this function:

◆ perf_hybrid_stop()

void perf_hybrid_stop ( HybridTimer timer,
const char *  label 
)

Stops both measurements and logs the results to the console.

Parameters
timerPointer to the timer.
labelDescriptive string for the log entry.

◆ perf_hybrid_stop_debug()

double perf_hybrid_stop_debug ( HybridTimer timer,
const char *  label 
)

Like perf_hybrid_stop, but logs at DEBUG level instead of INFO.

Tracy zones are still emitted at full detail. Only the console output is demoted to reduce noise in production. Returns GPU elapsed ms.

Parameters
timerPointer to the timer.
labelDescriptive string for the log entry.
Returns
GPU elapsed time in milliseconds.

◆ perf_timer_elapsed_ms()

double perf_timer_elapsed_ms ( PerfTimer timer)

Stops the timer and returns elapsed time in milliseconds.

Parameters
timerPointer to the timer.
Returns
Elapsed time (double precision).

◆ perf_timer_elapsed_s()

double perf_timer_elapsed_s ( PerfTimer timer)

Stops the timer and returns elapsed time in seconds.

Parameters
timerPointer to the timer.
Returns
Elapsed time (double precision).

◆ perf_timer_elapsed_us()

double perf_timer_elapsed_us ( PerfTimer timer)

Stops the timer and returns elapsed time in microseconds.

Parameters
timerPointer to the timer.
Returns
Elapsed time (double precision).

◆ perf_timer_start()

void perf_timer_start ( PerfTimer timer)

Starts the CPU timer.

Parameters
timerPointer to the timer.