ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
time.h
Go to the documentation of this file.
1#pragma once
2
36#include <stdint.h>
37#include <stdbool.h>
38
39// ============================================================================
40// sokol_time.h Integration
41// ============================================================================
42
43// Include sokol_time.h declarations (WITHOUT implementation)
44// SOKOL_IMPL is defined only in time.c to avoid duplicate symbols
45#include "../../deps/sokol/sokol_time.h"
46#include "util/uthash.h"
47
48// ============================================================================
49// Timer Record Structure
50// ============================================================================
51
56typedef struct timer_record {
57 char *name;
59 UT_hash_handle hh;
61
62// ============================================================================
63// Timer System API
64// ============================================================================
65
75bool timer_system_init(void);
76
85void timer_system_cleanup(void);
86
97bool timer_start(const char *name);
98
109double timer_stop(const char *name);
110
117bool timer_is_initialized(void);
118
119// ============================================================================
120// Convenience Macros
121// ============================================================================
122
141#define START_TIMER(name_fmt, ...) \
142 do { \
143 if (timer_is_initialized()) { \
144 char _timer_name_buf[256]; \
145 snprintf(_timer_name_buf, sizeof(_timer_name_buf), name_fmt, ##__VA_ARGS__); \
146 (void)timer_start(_timer_name_buf); \
147 } \
148 } while (0)
149
165#define STOP_TIMER(name_fmt, ...) \
166 ({ \
167 double _elapsed = -1.0; \
168 if (timer_is_initialized()) { \
169 char _timer_name_buf[256]; \
170 snprintf(_timer_name_buf, sizeof(_timer_name_buf), name_fmt, ##__VA_ARGS__); \
171 _elapsed = timer_stop(_timer_name_buf); \
172 } \
173 _elapsed; \
174 })
175
176// ============================================================================
177// Time Unit Constants
178// ============================================================================
179
180#define NS_PER_US 1000.0
181#define NS_PER_MS 1000000.0
182#define NS_PER_SEC 1000000000.0
183#define NS_PER_MIN (NS_PER_SEC * 60.0)
184#define NS_PER_HOUR (NS_PER_MIN * 60.0)
185#define NS_PER_DAY (NS_PER_HOUR * 24.0)
186#define NS_PER_YEAR (NS_PER_DAY * 365.25) // Account for leap years
187
188// ============================================================================
189// Time Formatting API
190// ============================================================================
191
217int format_duration_ms(double milliseconds, char *buffer, size_t buffer_size);
218
234int format_duration_ns(double nanoseconds, char *buffer, size_t buffer_size);
235
251int format_duration_s(double seconds, char *buffer, size_t buffer_size);
252
271#define STOP_TIMER_AND_LOG(timer_name, log_func, msg_fmt, ...) \
272 do { \
273 double _elapsed_ns = STOP_TIMER(timer_name, ##__VA_ARGS__); \
274 if (_elapsed_ns >= 0.0) { \
275 char _duration_str[32]; \
276 format_duration_ns(_elapsed_ns, _duration_str, sizeof(_duration_str)); \
277 log_func(msg_fmt " in %s", ##__VA_ARGS__, _duration_str); \
278 } \
279 } while (0)
280
281// ============================================================================
282// Adaptive Sleep System
283// ============================================================================
284
301
315
328
368uint64_t adaptive_sleep_calculate(adaptive_sleep_state_t *state, size_t queue_depth, size_t target_depth);
369
381void adaptive_sleep_do(adaptive_sleep_state_t *state, size_t queue_depth, size_t target_depth);
382
unsigned long long uint64_t
Definition common.h:59
double timer_stop(const char *name)
Stop a named timer and return elapsed time.
Definition time.c:135
void timer_system_cleanup(void)
Cleanup the timing system.
Definition time.c:60
bool timer_start(const char *name)
Start a named timer.
Definition time.c:84
int format_duration_ns(double nanoseconds, char *buffer, size_t buffer_size)
Format nanoseconds as human-readable duration string.
Definition time.c:187
uint64_t adaptive_sleep_calculate(adaptive_sleep_state_t *state, size_t queue_depth, size_t target_depth)
Calculate adaptive sleep time based on queue depth.
Definition time.c:300
int format_duration_s(double seconds, char *buffer, size_t buffer_size)
Format seconds as human-readable duration string.
Definition time.c:275
void adaptive_sleep_init(adaptive_sleep_state_t *state, const adaptive_sleep_config_t *config)
Initialize adaptive sleep state with configuration.
Definition time.c:285
int format_duration_ms(double milliseconds, char *buffer, size_t buffer_size)
Format milliseconds as human-readable duration string.
Definition time.c:269
bool timer_is_initialized(void)
Check if timing system is initialized.
Definition time.c:179
bool timer_system_init(void)
Initialize the timing system.
Definition time.c:39
void adaptive_sleep_do(adaptive_sleep_state_t *state, size_t queue_depth, size_t target_depth)
Calculate sleep time and immediately sleep for that duration.
Definition time.c:357
struct timer_record timer_record_t
Individual timer record for a named timing operation.
Configuration for adaptive sleep behavior.
Definition time.h:294
double min_speed_multiplier
Minimum speed (max sleep) - usually 1.0 (baseline speed)
Definition time.h:296
double max_speed_multiplier
Maximum speed (min sleep) - e.g., 4.0 = process 4x faster.
Definition time.h:297
uint64_t baseline_sleep_ns
Normal sleep time in nanoseconds (when queue is at target)
Definition time.h:295
double speedup_rate
Ramp-up rate when queue builds (0.0-1.0, higher = faster ramp)
Definition time.h:298
double slowdown_rate
Ramp-down rate when queue empties (0.0-1.0, higher = faster ramp)
Definition time.h:299
Runtime state for adaptive sleep.
Definition time.h:310
double current_speed_multiplier
Current speed state (1.0 = baseline, >1.0 = faster)
Definition time.h:312
uint64_t last_sleep_ns
Last calculated sleep time (for debugging)
Definition time.h:313
adaptive_sleep_config_t config
Configuration (copied, not referenced)
Definition time.h:311
Individual timer record for a named timing operation.
Definition time.h:56
uint64_t start_ticks
Start time in sokol ticks.
Definition time.h:58
char * name
Timer name (heap-allocated, unique key)
Definition time.h:57
UT_hash_handle hh
uthash handle (makes this hashable by name)
Definition time.h:59
#️⃣ Wrapper for uthash.h that ensures common.h is included first