ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
src/server/stats.c
Go to the documentation of this file.
134
135#include "client.h"
136#include "main.h"
137#include "render.h"
138#include "stats.h"
139#include <ascii-chat/buffer.h>
140#include <ascii-chat/common.h>
145#include <libplatform/atomic.h>
146#include <libplatform/lifecycle.h>
147#include <stdio.h>
148#include <string.h>
149#include <time.h>
150
165server_stats_t g_stats = {0};
166
174mutex_t g_stats_mutex = {0};
175
184static lifecycle_t g_stats_lc = LIFECYCLE_INIT_MUTEX(&g_stats_mutex);
185
190int stats_init(void) {
191 if (!lifecycle_init(&g_stats_lc, "stats")) {
192 return 0; // Already initialized
193 }
194 return 0;
195}
196
203void stats_cleanup(void) { lifecycle_shutdown(&g_stats_lc); }
204
205/* ============================================================================
206 * Statistics Collection and Reporting Thread
207 * ============================================================================
208 */
209
327
328void *stats_logger_thread(void *arg) {
329 (void)arg;
330
332 // Log buffer domain statistics every 10 seconds with fast exit checking
333 // (10ms intervals)
334 for (int i = 0; i < 1000 && !atomic_load_bool(&g_should_exit); i++) {
335 platform_sleep_us(10 * US_PER_MS_INT); // 10ms sleep
336 }
337
338 // Check exit condition before proceeding with statistics logging
339 // Check multiple times to avoid accessing freed resources during shutdown.
341 break;
342 }
343
348
349 // Collect all statistics data first
350#ifndef NDEBUG
351 char lock_debug_info[BUFFER_SIZE_MEDIUM] = {0};
352 // Check exit condition again before accessing lock_debug.
353 // lock_debug might be destroyed during shutdown
354 if (!atomic_load_bool(&g_should_exit) && debug_sync_is_initialized()) {
355 uint64_t total_acquired = 0, total_released = 0;
356 uint32_t currently_held = 0;
357 debug_sync_get_stats(&total_acquired, &total_released, &currently_held);
358
359 safe_snprintf(lock_debug_info, sizeof(lock_debug_info),
360 "Historical Mutex/RWLock Statistics:\n"
361 " Total locks acquired: %llu\n"
362 " Total locks released: %llu\n"
363 " Currently held: %u\n"
364 " Net locks (acquired - released): %lld",
365 (unsigned long long)total_acquired,
366 (unsigned long long)total_released, currently_held,
367 (long long)total_acquired - (long long)total_released);
368 }
369#endif
370
371 // Collect client statistics
372 // Check exit condition again before accessing rwlock.
373 // rwlock might be destroyed during shutdown.
375 break;
376 }
377
378 rwlock_rdlock(&g_client_manager_rwlock);
379 int active_clients = 0;
380 int clients_with_audio = 0;
381 int clients_with_video = 0;
382
383 for (int i = 0; i < MAX_CLIENTS; i++) {
384 if (atomic_load_bool(&g_client_manager.clients[i].active)) {
385 active_clients++;
386 if (atomic_load_bool(&g_client_manager.clients[i].is_sending_audio)) {
387 clients_with_audio++;
388 }
389 if (atomic_load_bool(&g_client_manager.clients[i].is_sending_video)) {
390 clients_with_video++;
391 }
392 }
393 }
394
395 rwlock_rdunlock(&g_client_manager_rwlock);
396
397 log_info("Stats: Clients: %d, Audio: %d, Video: %d", active_clients,
398 clients_with_audio, clients_with_video);
399 }
400
402
404
405 // Clean up thread-local error context before exit
406
407 return NULL;
408}
409
412 stats_snapshot_t snapshot;
413 if (!lifecycle_is_initialized(&g_stats_lc) ||
415 return;
416 mutex_lock(&g_stats_mutex);
417 g_stats.frames_captured = snapshot.counters[STATS_COUNTER_FRAMES_RECEIVED];
418 g_stats.frames_sent = snapshot.counters[STATS_COUNTER_FRAMES_SENT];
419 g_stats.frames_dropped = snapshot.counters[STATS_COUNTER_FRAMES_DROPPED];
420 double seconds = (double)(snapshot.sampled_ns - snapshot.started_ns) / 1e9;
421 g_stats.avg_capture_fps =
422 seconds > 0 ? (double)g_stats.frames_captured / seconds : 0;
423 g_stats.avg_send_fps =
424 seconds > 0 ? (double)g_stats.frames_sent / seconds : 0;
425 mutex_unlock(&g_stats_mutex);
426}
427
471 // Check if stats mutex is initialized (safe in debug builds where
472 // stats_init() may not be called)
473 if (!lifecycle_is_initialized(&g_stats_lc)) {
474 return; // Mutex not initialized, skip logging
475 }
476
477 // Now safe to use the mutex
478 mutex_lock(&g_stats_mutex);
479 log_info("Server Statistics:\n"
480 " frames_captured=%llu\n"
481 " frames_sent=%llu\n"
482 " frames_dropped=%llu\n"
483 " Average FPS: capture=%.2f, send=%.2f",
484 (unsigned long long)g_stats.frames_captured,
485 (unsigned long long)g_stats.frames_sent,
486 (unsigned long long)g_stats.frames_dropped, g_stats.avg_capture_fps,
487 g_stats.avg_send_fps);
488 mutex_unlock(&g_stats_mutex);
489}
void asciichat_error_stats_print(void)
bool atomic_load_bool(atomic_t *a)
Atomic pointer wrapper.
void buffer_domain_log_stats(buffer_domain_t *domain, const char *name)
Definition buffer.c:416
buffer_domain_t * buffer_default_domain(void)
Definition buffer.c:452
void buffer_domain_trim(buffer_domain_t *domain)
Definition buffer.c:181
buffer_domain_t * buffer_control_domain(void)
Definition buffer.c:465
Common buffer size definitions.
Platform initialization and static synchronization helpers.
atomic_t g_should_exit
Global application exit flag (shared across all modes).
Definition globals.c:32
#define BUFFER_SIZE_MEDIUM
Medium buffer size (512 bytes).
unsigned int uint32_t
unsigned long long uint64_t
@ ASCIICHAT_OK
Definition error_codes.h:50
#define MAX_CLIENTS
Maximum possible clients (static array size) - actual runtime limit set by –max-clients (1-128).
#define log_info(...)
Log an INFO message.
Definition log/log.h:578
#define US_PER_MS_INT
Definition time.h:176
⚙️ Common definitions, error codes, macros, and types shared throughout the application
asciichat_error_t stats_scope_snapshot(const stats_scope_t *scope, stats_snapshot_t *out)
stats sampler config keeps the values that move through the subsystem: window_ns, resolution_ns....
stats_scope_t * stats_runtime_scope(void)
Definition runtime.c:88
rwlock_t g_client_manager_rwlock
Reader-writer lock protecting the global client manager.
client_manager_t g_client_manager
Hash table entry for client packet dispatch.
Per-client state management and lifecycle orchestration.
Per-client rendering threads with rate limiting.
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.
mutex_t g_stats_mutex
Mutex protecting global server statistics.
void stats_cleanup(void)
Cleanup the stats mutex.
void update_server_stats(void)
Server performance statistics tracking.
🔤 String Manipulation and Shell Escaping Utilities
Counters and rates describing the server's media pipeline.
uint64_t counters[STATS_COUNTER_COUNT]
⏱️ High-precision timing utilities using sokol_time.h and uthash