|
ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
|
🛠️ Core utility functions for string manipulation, paths, IP parsing, and formatting More...
Files | |
| file | time.c |
| ⏱️ High-precision timing utilities implementation | |
| file | time.h |
| ⏱️ High-precision timing utilities using sokol_time.h and uthash | |
Data Structures | |
| struct | timer_record |
| Individual timer record for a named timing operation. More... | |
| struct | adaptive_sleep_config_t |
| Configuration for adaptive sleep behavior. More... | |
| struct | adaptive_sleep_state_t |
| Runtime state for adaptive sleep. More... | |
Macros | |
| #define | START_TIMER(name_fmt, ...) |
| Start a timer with formatted name. | |
| #define | STOP_TIMER(name_fmt, ...) |
| Stop a timer with formatted name and return elapsed time. | |
| #define | NS_PER_US 1000.0 |
| #define | NS_PER_MS 1000000.0 |
| #define | NS_PER_SEC 1000000000.0 |
| #define | NS_PER_MIN (NS_PER_SEC * 60.0) |
| #define | NS_PER_HOUR (NS_PER_MIN * 60.0) |
| #define | NS_PER_DAY (NS_PER_HOUR * 24.0) |
| #define | NS_PER_YEAR (NS_PER_DAY * 365.25) |
| #define | STOP_TIMER_AND_LOG(timer_name, log_func, msg_fmt, ...) |
| Stop a timer and log the result with a custom message. | |
Typedefs | |
| typedef struct timer_record | timer_record_t |
| Individual timer record for a named timing operation. | |
Functions | |
| bool | timer_system_init (void) |
| Initialize the timing system. | |
| void | timer_system_cleanup (void) |
| Cleanup the timing system. | |
| bool | timer_start (const char *name) |
| Start a named timer. | |
| double | timer_stop (const char *name) |
| Stop a named timer and return elapsed time. | |
| bool | timer_is_initialized (void) |
| Check if timing system is initialized. | |
| int | format_duration_ms (double milliseconds, char *buffer, size_t buffer_size) |
| Format milliseconds as human-readable duration string. | |
| int | format_duration_ns (double nanoseconds, char *buffer, size_t buffer_size) |
| Format nanoseconds as human-readable duration string. | |
| int | format_duration_s (double seconds, char *buffer, size_t buffer_size) |
| Format seconds as human-readable duration string. | |
| void | adaptive_sleep_init (adaptive_sleep_state_t *state, const adaptive_sleep_config_t *config) |
| Initialize adaptive sleep state with configuration. | |
| 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. | |
| 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. | |
🛠️ Core utility functions for string manipulation, paths, IP parsing, and formatting
String utilities, path handling, IP address parsing, UTF-8 support, CRC32, and formatting.
This module provides a simple timing API for performance measurement:
Features:
Example usage:
| #define NS_PER_DAY (NS_PER_HOUR * 24.0) |
| #define NS_PER_HOUR (NS_PER_MIN * 60.0) |
| #define NS_PER_MIN (NS_PER_SEC * 60.0) |
| #define NS_PER_YEAR (NS_PER_DAY * 365.25) |
| #define START_TIMER | ( | name_fmt, | |
| ... | |||
| ) |
#include <time.h>
Start a timer with formatted name.
The timer name is created by formatting the name_fmt string with the provided arguments. This complete formatted string becomes the unique key in the hashtable.
IMPORTANT: Timer names must be unique. If a timer with the same formatted name already exists, timer_start() will return false and log an error.
Usage: START_TIMER("lock_%p", lock_ptr); // Unique per lock address START_TIMER("process_frame_%d", frame_id); // Unique per frame START_TIMER("client_%u_decode", client_id); // Unique per client
| name_fmt | Printf-style format string for timer name |
| ... | Format arguments to create unique key |
Definition at line 141 of file time.h.
| #define STOP_TIMER | ( | name_fmt, | |
| ... | |||
| ) |
#include <time.h>
Stop a timer with formatted name and return elapsed time.
The timer name must match exactly (including all format arguments) with the name used in START_TIMER(). The formatted string is used as the hashtable key lookup.
Usage: double ns = STOP_TIMER("lock_%p", lock_ptr); double ns = STOP_TIMER("process_frame_%d", frame_id);
| name_fmt | Printf-style format string for timer name (must match START_TIMER) |
| ... | Format arguments (must match START_TIMER to create same key) |
Definition at line 165 of file time.h.
| #define STOP_TIMER_AND_LOG | ( | timer_name, | |
| log_func, | |||
| msg_fmt, | |||
| ... | |||
| ) |
#include <time.h>
Stop a timer and log the result with a custom message.
Combines STOP_TIMER() with logging. The timer is stopped, elapsed time is retrieved, and a log message is generated with the elapsed time appended in human-readable format.
Usage: STOP_TIMER_AND_LOG("client_handshake", log_info, "Crypto handshake completed successfully"); STOP_TIMER_AND_LOG("process_frame_%d", log_debug, "Frame %d processed", frame_id, frame_id);
The macro will append " in X.XXms" (or appropriate unit) to your message automatically.
| timer_name | Timer name (must match START_TIMER call) |
| log_func | Logging function to use (log_info, log_debug, etc.) |
| msg_fmt | Printf-style format string for the message |
| ... | Format arguments for both timer name and message |
Definition at line 271 of file time.h.
| typedef struct timer_record timer_record_t |
#include <time.h>
Individual timer record for a named timing operation.
| uint64_t adaptive_sleep_calculate | ( | adaptive_sleep_state_t * | state, |
| size_t | queue_depth, | ||
| size_t | target_depth | ||
| ) |
#include <time.h>
Calculate adaptive sleep time based on queue depth.
Evaluates current queue depth against target and adjusts the speed multiplier:
The actual sleep time is calculated as: baseline_sleep_ns / current_speed_multiplier
Example usage:
| state | Pointer to adaptive sleep state (will be updated) |
| queue_depth | Current number of items in queue/buffer |
| target_depth | Desired queue depth (0 = empty queue target) |
Definition at line 300 of file time.c.
References adaptive_sleep_config_t::baseline_sleep_ns, adaptive_sleep_state_t::config, adaptive_sleep_state_t::current_speed_multiplier, adaptive_sleep_state_t::last_sleep_ns, adaptive_sleep_config_t::max_speed_multiplier, adaptive_sleep_config_t::min_speed_multiplier, adaptive_sleep_config_t::slowdown_rate, and adaptive_sleep_config_t::speedup_rate.
Referenced by adaptive_sleep_do().
| void adaptive_sleep_do | ( | adaptive_sleep_state_t * | state, |
| size_t | queue_depth, | ||
| size_t | target_depth | ||
| ) |
#include <time.h>
Calculate sleep time and immediately sleep for that duration.
Convenience wrapper that combines adaptive_sleep_calculate() with platform_sleep_usec(). Useful for simple loops that don't need to inspect the calculated sleep time.
| state | Pointer to adaptive sleep state (will be updated) |
| queue_depth | Current number of items in queue/buffer |
| target_depth | Desired queue depth (0 = empty queue target) |
Definition at line 357 of file time.c.
References adaptive_sleep_calculate(), and platform_sleep_usec().
| void adaptive_sleep_init | ( | adaptive_sleep_state_t * | state, |
| const adaptive_sleep_config_t * | config | ||
| ) |
#include <time.h>
Initialize adaptive sleep state with configuration.
Sets up an adaptive sleep state structure with the provided configuration. The configuration is copied internally, so the caller doesn't need to keep the config structure alive.
| state | Pointer to adaptive sleep state to initialize |
| config | Pointer to configuration (will be copied) |
Definition at line 285 of file time.c.
References adaptive_sleep_config_t::baseline_sleep_ns, adaptive_sleep_state_t::config, adaptive_sleep_state_t::current_speed_multiplier, adaptive_sleep_state_t::last_sleep_ns, and adaptive_sleep_config_t::min_speed_multiplier.
| int format_duration_ms | ( | double | milliseconds, |
| char * | buffer, | ||
| size_t | buffer_size | ||
| ) |
#include <time.h>
Format milliseconds as human-readable duration string.
| milliseconds | Duration in milliseconds |
| buffer | Output buffer for formatted string |
| buffer_size | Size of output buffer |
Formats a duration in milliseconds into a human-readable string. The function automatically selects the most appropriate units and precision based on the duration magnitude.
Format rules:
Definition at line 269 of file time.c.
References format_duration_ns(), and NS_PER_MS.
Referenced by broadcast_server_state_to_all_clients().
| int format_duration_ns | ( | double | nanoseconds, |
| char * | buffer, | ||
| size_t | buffer_size | ||
| ) |
#include <time.h>
Format nanoseconds as human-readable duration string.
| nanoseconds | Duration in nanoseconds |
| buffer | Output buffer for formatted string |
| buffer_size | Size of output buffer |
Similar to format_duration_ms() but accepts nanosecond precision input. Useful for high-precision timing measurements.
Definition at line 187 of file time.c.
References NS_PER_DAY, NS_PER_HOUR, NS_PER_MIN, NS_PER_MS, NS_PER_SEC, NS_PER_US, and NS_PER_YEAR.
Referenced by asciichat_instr_log_line(), format_duration_ms(), format_duration_s(), mixer_process_excluding_source(), and timer_stop().
| int format_duration_s | ( | double | seconds, |
| char * | buffer, | ||
| size_t | buffer_size | ||
| ) |
#include <time.h>
Format seconds as human-readable duration string.
| seconds | Duration in seconds |
| buffer | Output buffer for formatted string |
| buffer_size | Size of output buffer |
Similar to format_duration_ms() but accepts seconds as input. Useful for timing measurements already in seconds.
Definition at line 275 of file time.c.
References format_duration_ns(), and NS_PER_SEC.
Referenced by crypto_init(), and crypto_rekey_init().
| bool timer_is_initialized | ( | void | ) |
| bool timer_start | ( | const char * | name | ) |
#include <time.h>
Start a named timer.
Creates a new timer record and stores the start time. If a timer with the same name exists, it will be overwritten.
| name | Timer name (must be unique, will be copied) |
Definition at line 84 of file time.c.
References ERROR_INVALID_PARAM, ERROR_INVALID_STATE, ERROR_MEMORY, timer_record::name, rwlock_wrlock, rwlock_wrunlock, SAFE_FREE, SAFE_MALLOC, SAFE_STRNCPY, SET_ERRNO, and timer_record::start_ticks.
| double timer_stop | ( | const char * | name | ) |
#include <time.h>
Stop a named timer and return elapsed time.
Looks up the timer by name, calculates elapsed time, logs it, and removes the timer from the hashtable.
| name | Timer name to stop |
Definition at line 135 of file time.c.
References ERROR_INVALID_PARAM, ERROR_INVALID_STATE, format_duration_ns(), log_debug, log_warn, timer_record::name, rwlock_wrlock, rwlock_wrunlock, SAFE_FREE, SET_ERRNO, and timer_record::start_ticks.
| void timer_system_cleanup | ( | void | ) |
#include <time.h>
Cleanup the timing system.
Frees all timer records and destroys mutex. Should be called at program exit.
Definition at line 60 of file time.c.
References log_debug, timer_record::name, rwlock_destroy(), rwlock_wrlock, rwlock_wrunlock, and SAFE_FREE.
| bool timer_system_init | ( | void | ) |
#include <time.h>
Initialize the timing system.
Must be called before using any timer functions. Automatically calls stm_setup() for sokol_time initialization.
Definition at line 39 of file time.c.
References ERROR_PLATFORM_INIT, log_debug, rwlock_init(), and SET_ERRNO.