ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
lib/debug/stats.c
Go to the documentation of this file.
6#include <libplatform/atomic.h>
7#include <libplatform/cond.h>
8#include <libplatform/thread.h>
9#include <options/options.h>
10
11static atomic_t g_debug_stats_signal = {0};
12static atomic_t g_debug_stats_memory_signal = {0};
13static atomic_t g_debug_stats_exiting = {0};
14static atomic_t g_debug_stats_cleaning = {0};
15static platform_thread_t g_debug_stats_thread;
16static uint64_t g_debug_stats_main_thread;
17static mutex_t g_debug_stats_mutex;
18static cond_t g_debug_stats_condition;
19static bool g_debug_stats_initialized;
20static bool g_debug_stats_started;
21static uint64_t g_debug_stats_memory_deadline;
22static uint64_t g_debug_stats_memory_interval;
23
25 g_debug_stats_main_thread = thread_current_id();
26}
28 return g_debug_stats_main_thread;
29}
31 if (g_debug_stats_initialized)
32 return ASCIICHAT_OK;
33 if (mutex_init(&g_debug_stats_mutex, "debug_stats") != 0)
34 return SET_ERRNO(ERROR_THREAD, "Cannot initialize diagnostics mutex");
35 if (cond_init(&g_debug_stats_condition, "debug_stats") != 0) {
36 mutex_destroy(&g_debug_stats_mutex);
37 return SET_ERRNO(ERROR_THREAD, "Cannot initialize diagnostics condition");
38 }
39 g_debug_stats_initialized = true;
40 atomic_store_bool_impl(&g_debug_stats_cleaning, false);
41 return ASCIICHAT_OK;
42}
44 log_info("Diagnostic statistics:");
46}
48 if (!g_debug_stats_initialized)
49 return;
50 mutex_lock(&g_debug_stats_mutex);
51 g_debug_stats_memory_interval = interval;
52 g_debug_stats_memory_deadline =
53 interval ? debug_report_deadline_after(time_get_ns(), interval) : 0;
54 cond_signal(&g_debug_stats_condition);
55 mutex_unlock(&g_debug_stats_mutex);
56}
58 // Do not signal a condition variable or invoke diagnostic hooks in a signal
59 // handler. The worker checks this flag at most 100ms after its next wakeup.
60 atomic_store_bool_impl(&g_debug_stats_signal, true);
61}
63 // Signal handlers may only set the request; the worker performs the report.
64 atomic_store_bool_impl(&g_debug_stats_memory_signal, true);
65}
67 return atomic_load_bool_impl(&g_debug_stats_cleaning);
68}
69void debug_stats_poll(void) {
70 if (!g_debug_stats_initialized)
71 return;
72 uint64_t now = time_get_ns();
73 bool all = atomic_exchange_bool_impl(&g_debug_stats_signal, false);
74 bool requested_memory =
75 atomic_exchange_bool_impl(&g_debug_stats_memory_signal, false);
76 mutex_lock(&g_debug_stats_mutex);
77 bool memory =
78 g_debug_stats_memory_deadline && now >= g_debug_stats_memory_deadline;
79 if (memory)
80 g_debug_stats_memory_deadline =
81 debug_report_deadline_after(now, g_debug_stats_memory_interval);
82 mutex_unlock(&g_debug_stats_mutex);
83 if (all)
85#if defined(DEBUG_MEMORY) && !defined(NDEBUG)
86 if (memory || requested_memory)
87 debug_memory_report(GET_OPTION(color));
88#else
89 (void)memory;
90 (void)requested_memory;
91#endif
92}
93#ifndef EMSCRIPTEN_BUILD
94static void *debug_stats_worker(void *unused) {
95 (void)unused;
96 while (!atomic_load_bool_impl(&g_debug_stats_exiting)) {
98 mutex_lock(&g_debug_stats_mutex);
99 uint64_t now = time_get_ns();
100 uint64_t wait = 100 * NS_PER_MS_INT;
101 uint64_t deadlines[] = {g_debug_stats_memory_deadline};
102 for (size_t i = 0; i < sizeof(deadlines) / sizeof(deadlines[0]); ++i)
103 if (deadlines[i]) {
104 uint64_t remaining = deadlines[i] > now ? deadlines[i] - now : 1;
105 if (remaining < wait)
106 wait = remaining;
107 }
108 if (!atomic_load_bool_impl(&g_debug_stats_exiting))
109 cond_timedwait(&g_debug_stats_condition, &g_debug_stats_mutex, wait);
110 mutex_unlock(&g_debug_stats_mutex);
111 }
112 return NULL;
113}
114#endif
116 if (g_debug_stats_started)
117 return ASCIICHAT_OK;
119 if (result != ASCIICHAT_OK)
120 return result;
121 atomic_store_bool_impl(&g_debug_stats_exiting, false);
122 atomic_store_bool_impl(&g_debug_stats_cleaning, false);
123 const options_t *opts = options_get();
124 if (opts) {
125 if (opts->debug_memory_report_interval > 0)
128 }
129#ifndef EMSCRIPTEN_BUILD
130 if (platform_thread_create(&g_debug_stats_thread, "debug_stats",
131 debug_stats_worker, NULL) != 0)
132 return SET_ERRNO(ERROR_THREAD, "Cannot start diagnostics worker");
133#endif
134 g_debug_stats_started = true;
135 return ASCIICHAT_OK;
136}
138 if (!g_debug_stats_started)
139 return;
140 atomic_store_bool_impl(&g_debug_stats_cleaning, true);
141 mutex_lock(&g_debug_stats_mutex);
142 atomic_store_bool_impl(&g_debug_stats_exiting, true);
143 cond_signal(&g_debug_stats_condition);
144 mutex_unlock(&g_debug_stats_mutex);
145#ifndef EMSCRIPTEN_BUILD
146 // Join fully: diagnostic data must not be destroyed while a reader is alive.
147 if (thread_join(&g_debug_stats_thread, NULL) != 0)
148 FATAL(ERROR_THREAD, "Cannot join diagnostics worker");
149#endif
150 g_debug_stats_started = false;
151}
154 if (g_debug_stats_initialized) {
155 cond_destroy(&g_debug_stats_condition);
156 mutex_destroy(&g_debug_stats_mutex);
157 g_debug_stats_initialized = false;
158 atomic_store_bool_impl(&g_debug_stats_signal, false);
159 atomic_store_bool_impl(&g_debug_stats_memory_signal, false);
160 g_debug_stats_memory_deadline = g_debug_stats_memory_interval = 0;
161 }
162}
void asciichat_error_stats_print(void)
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
#define SET_ERRNO(code,...)
bool atomic_load_bool_impl(const atomic_t *a)
void atomic_store_bool_impl(atomic_t *a, uint64_t value)
bool atomic_exchange_bool_impl(atomic_t *a, bool new_value)
uint64_t debug_report_deadline_after(uint64_t now, uint64_t delay_ns)
Shared deadline and scheduling helpers for diagnostic reports.
#define FATAL(code,...)
Exit with error code and custom message, with stack trace in debug builds.
unsigned long long uint64_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_THREAD
#define log_info(...)
Log an INFO message.
Definition log/log.h:578
#define NS_PER_SEC_INT
Definition time.h:173
#define NS_PER_MS_INT
Definition time.h:172
#define GET_OPTION(field)
Definition options.h:1339
struct options_state options_t
Immutable, RCU-published snapshot of the application's runtime configuration.
const options_t * options_get(void)
Get pointer to current options struct (lock-free, thread-safe).
Definition rcu.c:513
Statistics worker for error totals and memory reports. Signal handlers may only request a report.
void debug_stats_cleanup_thread(void)
void debug_stats_set_main_thread_id(void)
void debug_stats_destroy(void)
void debug_stats_print(void)
asciichat_error_t debug_stats_init(void)
bool debug_stats_is_cleanup_in_progress(void)
void debug_stats_trigger_print(void)
void debug_stats_trigger_memory_report(void)
uint64_t debug_stats_get_main_thread_id(void)
void debug_stats_poll(void)
void debug_stats_set_memory_report_interval(uint64_t interval)
asciichat_error_t debug_stats_start_thread(void)
⚙️ Unified options parsing system for ascii-chat with builder pattern and lock-free access
Atomic value wrapper for integral/boolean types.
Definition atomic.h:80
double debug_memory_report_interval
Interval in seconds for periodic memory reports.
Definition options.h:1243
⏱️ High-precision timing utilities using sokol_time.h and uthash