133#include <stdatomic.h>
178static bool g_stats_mutex_initialized =
false;
187 g_stats_mutex_initialized =
true;
196 if (g_stats_mutex_initialized) {
198 g_stats_mutex_initialized =
false;
349 char lock_debug_info[512] = {0};
353 uint64_t total_acquired = 0, total_released = 0;
355 lock_debug_get_stats(&total_acquired, &total_released, ¤tly_held);
358 "Historical Mutex/RWLock Statistics:\n"
359 " Total locks acquired: %llu\n"
360 " Total locks released: %llu\n"
361 " Currently held: %u\n"
362 " Net locks (acquired - released): %lld",
363 (
unsigned long long)total_acquired, (
unsigned long long)total_released, currently_held,
364 (
long long)total_acquired - (
long long)total_released);
376 int active_clients = 0;
377 int clients_with_audio = 0;
378 int clients_with_video = 0;
379 char client_details[2048] = {0};
380 int client_details_len = 0;
386 clients_with_audio++;
389 clients_with_video++;
397 bool is_active = atomic_load(&client->
active);
400 if (is_active && client_id_snapshot != 0) {
403 uint64_t enqueued, dequeued, dropped;
405 if (enqueued > 0 || dequeued > 0 || dropped > 0) {
407 snprintf(client_details + client_details_len,
sizeof(client_details) - client_details_len,
408 " Client %u audio queue: %llu enqueued, %llu dequeued, %llu dropped", client_id_snapshot,
409 (
unsigned long long)enqueued, (
unsigned long long)dequeued, (
unsigned long long)dropped);
410 if (len > 0 && client_details_len + len < (
int)
sizeof(client_details)) {
411 client_details_len += len;
420 int len = snprintf(client_details + client_details_len,
sizeof(client_details) - client_details_len,
421 " Client %u video buffer: %llu frames, %llu dropped (%.1f%% drop rate)",
422 client_id_snapshot, (
unsigned long long)stats.
total_frames,
424 if (len > 0 && client_details_len + len < (
int)
sizeof(client_details)) {
425 client_details_len += len;
434 if (client_details_len > 0) {
435 log_info(
"Stats: Clients: %d, Audio: %d, Video: %d\n%s", active_clients, clients_with_audio, clients_with_video,
488 if (atomic_load(&client->
client_id) != 0 && atomic_load(&client->
active)) {
510 if (total_frames_sent > 0 || total_frames_dropped > 0) {
563 if (!g_stats_mutex_initialized) {
568 " frames_captured=%llu\n"
569 " frames_sent=%llu\n"
570 " frames_dropped=%llu\n"
571 " Average FPS: capture=%.2f, send=%.2f",
🗃️ Lock-Free Unified Memory Buffer Pool with Lazy Allocation
unsigned long long uint64_t
void asciichat_error_stats_print(void)
Print error statistics to stderr.
void asciichat_errno_cleanup(void)
Cleanup error system resources.
#define MAX_CLIENTS
Maximum possible clients (static array size) - actual runtime limit set by –max-clients (1-32)
#define log_info(...)
Log an INFO message.
void packet_queue_get_stats(packet_queue_t *queue, uint64_t *enqueued, uint64_t *dequeued, uint64_t *dropped)
Get queue statistics.
void video_frame_get_stats(video_frame_buffer_t *vfb, video_frame_stats_t *stats)
Get frame statistics for quality monitoring.
🔒 Lock debugging and deadlock detection system for ascii-chat
📬 Thread-safe packet queue system for per-client send threads
Per-client rendering threads with rate limiting.
rwlock_t g_client_manager_rwlock
Reader-writer lock protecting the global client manager.
client_manager_t g_client_manager
Global client manager singleton - central coordination point.
Per-client state management and lifecycle orchestration.
server_stats_t g_stats
Global server statistics structure.
void log_server_stats(void)
Log comprehensive server statistics summary.
void * stats_logger_thread(void *arg)
Main statistics collection and reporting thread function.
int stats_init(void)
Initialize the stats mutex.
atomic_bool g_server_should_exit
Global shutdown flag from main.c - coordinate statistics thread termination.
mutex_t g_stats_mutex
Mutex protecting global server statistics.
void stats_cleanup(void)
Cleanup the stats mutex.
void update_server_stats(void)
Update global server statistics (placeholder)
Server performance statistics tracking.
Per-client state structure for server-side client management.
video_frame_buffer_t * outgoing_video_buffer
packet_queue_t * audio_queue
client_info_t clients[MAX_CLIENTS]
Array of client_info_t structures (backing storage)
Server performance statistics structure.
Frame statistics structure.
uint64_t dropped_frames
Total frames dropped (due to buffer full or errors)
uint64_t total_frames
Total frames received since creation.
float drop_rate
Frame drop rate (dropped_frames / total_frames, 0.0-1.0)
⏱️ High-precision timing utilities using sokol_time.h and uthash