ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
stats.c File Reference

Go to the source code of this file.

Functions

void debug_stats_set_main_thread_id (void)
uint64_t debug_stats_get_main_thread_id (void)
asciichat_error_t debug_stats_init (void)
void debug_stats_print (void)
void debug_stats_set_memory_report_interval (uint64_t interval)
void debug_stats_trigger_print (void)
void debug_stats_trigger_memory_report (void)
bool debug_stats_is_cleanup_in_progress (void)
void debug_stats_poll (void)
asciichat_error_t debug_stats_start_thread (void)
void debug_stats_cleanup_thread (void)
void debug_stats_destroy (void)

Function Documentation

◆ debug_stats_cleanup_thread()

void debug_stats_cleanup_thread ( void )

Definition at line 131 of file lib/debug/stats.c.

131 {
132 if (!g_debug_stats_started)
133 return;
134 atomic_store_bool_impl(&g_debug_stats_cleaning, true);
135 mutex_lock(&g_debug_stats_mutex);
136 atomic_store_bool_impl(&g_debug_stats_exiting, true);
137 cond_signal(&g_debug_stats_condition);
138 mutex_unlock(&g_debug_stats_mutex);
139#ifndef EMSCRIPTEN_BUILD
140 // Join fully: diagnostic data must not be destroyed while a reader is alive.
141 if (thread_join(&g_debug_stats_thread, NULL) != 0)
142 FATAL(ERROR_THREAD, "Cannot join diagnostics worker");
143#endif
144 g_debug_stats_started = false;
145}
#define FATAL(code,...)
Exit with error code and custom message, with stack trace in debug builds.
Definition common.h:160
@ ERROR_THREAD

References ERROR_THREAD, and FATAL.

Referenced by asciichat_shared_destroy(), and debug_stats_destroy().

◆ debug_stats_destroy()

void debug_stats_destroy ( void )

Definition at line 146 of file lib/debug/stats.c.

146 {
148 if (g_debug_stats_initialized) {
149 cond_destroy(&g_debug_stats_condition);
150 mutex_destroy(&g_debug_stats_mutex);
151 g_debug_stats_initialized = false;
152 atomic_store_bool_impl(&g_debug_stats_signal, false);
153 atomic_store_bool_impl(&g_debug_stats_memory_signal, false);
154 g_debug_stats_memory_deadline = g_debug_stats_memory_interval = 0;
155 }
156}
void debug_stats_cleanup_thread(void)

References debug_stats_cleanup_thread().

Referenced by asciichat_shared_destroy().

◆ debug_stats_get_main_thread_id()

uint64_t debug_stats_get_main_thread_id ( void )

Definition at line 28 of file lib/debug/stats.c.

28 {
29 return g_debug_stats_main_thread;
30}

◆ debug_stats_init()

asciichat_error_t debug_stats_init ( void )

Definition at line 31 of file lib/debug/stats.c.

31 {
32 if (g_debug_stats_initialized)
33 return ASCIICHAT_OK;
34 if (mutex_init(&g_debug_stats_mutex, "debug_stats") != 0)
35 return SET_ERRNO(ERROR_THREAD, "Cannot initialize diagnostics mutex");
36 if (cond_init(&g_debug_stats_condition, "debug_stats") != 0) {
37 mutex_destroy(&g_debug_stats_mutex);
38 return SET_ERRNO(ERROR_THREAD, "Cannot initialize diagnostics condition");
39 }
40 g_debug_stats_initialized = true;
41 atomic_store_bool_impl(&g_debug_stats_cleaning, false);
42 return ASCIICHAT_OK;
43}
#define SET_ERRNO(code,...)
@ ASCIICHAT_OK
Definition error_codes.h:46

References ASCIICHAT_OK, ERROR_THREAD, and SET_ERRNO.

Referenced by debug_stats_start_thread(), and main().

◆ debug_stats_is_cleanup_in_progress()

bool debug_stats_is_cleanup_in_progress ( void )

Definition at line 66 of file lib/debug/stats.c.

66 {
67 return atomic_load_bool_impl(&g_debug_stats_cleaning);
68}

◆ debug_stats_poll()

void debug_stats_poll ( void )

Definition at line 69 of file lib/debug/stats.c.

69 {
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 = atomic_exchange_bool_impl(&g_debug_stats_memory_signal, false);
75 mutex_lock(&g_debug_stats_mutex);
76 bool memory = g_debug_stats_memory_deadline && now >= g_debug_stats_memory_deadline;
77 if (memory)
78 g_debug_stats_memory_deadline = debug_report_deadline_after(now, g_debug_stats_memory_interval);
79 mutex_unlock(&g_debug_stats_mutex);
80 if (all)
82#if defined(DEBUG_MEMORY) && !defined(NDEBUG)
83 if (memory || requested_memory)
84 debug_memory_report(GET_OPTION(color));
85#else
86 (void)memory;
87#endif
88}
uint64_t debug_report_deadline_after(uint64_t now, uint64_t delay_ns)
unsigned long long uint64_t
Definition common.h:63
#define GET_OPTION(field)
Safely get a specific option field (lock-free read).
Definition options.h:1336
void debug_stats_print(void)

References debug_report_deadline_after(), debug_stats_print(), and GET_OPTION.

◆ debug_stats_print()

void debug_stats_print ( void )

Definition at line 44 of file lib/debug/stats.c.

44 {
45 log_info("Diagnostic statistics:");
47}
void asciichat_error_stats_print(void)
#define log_info(...)
Log an INFO message.
Definition log/log.h:608

References asciichat_error_stats_print(), and log_info.

Referenced by debug_stats_poll().

◆ debug_stats_set_main_thread_id()

void debug_stats_set_main_thread_id ( void )

Definition at line 25 of file lib/debug/stats.c.

25 {
26 g_debug_stats_main_thread = thread_current_id();
27}

Referenced by main().

◆ debug_stats_set_memory_report_interval()

void debug_stats_set_memory_report_interval ( uint64_t interval)

Definition at line 48 of file lib/debug/stats.c.

48 {
49 if (!g_debug_stats_initialized)
50 return;
51 mutex_lock(&g_debug_stats_mutex);
52 g_debug_stats_memory_interval = interval;
53 g_debug_stats_memory_deadline = 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}

References debug_report_deadline_after().

Referenced by debug_stats_start_thread().

◆ debug_stats_start_thread()

asciichat_error_t debug_stats_start_thread ( void )

Definition at line 111 of file lib/debug/stats.c.

111 {
112 if (g_debug_stats_started)
113 return ASCIICHAT_OK;
115 if (result != ASCIICHAT_OK)
116 return result;
117 atomic_store_bool_impl(&g_debug_stats_exiting, false);
118 atomic_store_bool_impl(&g_debug_stats_cleaning, false);
119 options_t *opts = options_get();
120 if (opts) {
121 if (opts->debug_memory_report_interval > 0)
123 }
124#ifndef EMSCRIPTEN_BUILD
125 if (platform_thread_create(&g_debug_stats_thread, "debug_stats", debug_stats_worker, NULL) != 0)
126 return SET_ERRNO(ERROR_THREAD, "Cannot start diagnostics worker");
127#endif
128 g_debug_stats_started = true;
129 return ASCIICHAT_OK;
130}
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
#define NS_PER_SEC_INT
Definition time.h:169
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:511
asciichat_error_t debug_stats_init(void)
void debug_stats_set_memory_report_interval(uint64_t interval)
double debug_memory_report_interval
Interval in seconds for periodic memory reports.
Definition options.h:1252

References ASCIICHAT_OK, options_state::debug_memory_report_interval, debug_stats_init(), debug_stats_set_memory_report_interval(), ERROR_THREAD, NS_PER_SEC_INT, options_get(), and SET_ERRNO.

Referenced by main().

◆ debug_stats_trigger_memory_report()

void debug_stats_trigger_memory_report ( void )

Request a deferred memory report from the diagnostics worker.

Definition at line 62 of file lib/debug/stats.c.

62 {
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}

◆ debug_stats_trigger_print()

void debug_stats_trigger_print ( void )

Definition at line 57 of file lib/debug/stats.c.

57 {
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}