ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
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 137 of file lib/debug/stats.c.

137 {
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}
void atomic_store_bool_impl(atomic_t *a, uint64_t value)
#define FATAL(code,...)
Exit with error code and custom message, with stack trace in debug builds.
@ ERROR_THREAD

References atomic_store_bool_impl(), ERROR_THREAD, and FATAL.

Referenced by asciichat_shared_destroy(), and debug_stats_destroy().

◆ debug_stats_destroy()

void debug_stats_destroy ( void )

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

152 {
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 debug_stats_cleanup_thread(void)

References atomic_store_bool_impl(), and 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 27 of file lib/debug/stats.c.

27 {
28 return g_debug_stats_main_thread;
29}

◆ debug_stats_init()

asciichat_error_t debug_stats_init ( void )

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

30 {
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}
#define SET_ERRNO(code,...)
@ ASCIICHAT_OK
Definition error_codes.h:50

References ASCIICHAT_OK, atomic_store_bool_impl(), ERROR_THREAD, and SET_ERRNO.

Referenced by debug_stats_start_thread().

◆ 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}
bool atomic_load_bool_impl(const atomic_t *a)

References atomic_load_bool_impl().

◆ 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 =
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}
bool atomic_exchange_bool_impl(atomic_t *a, bool new_value)
uint64_t debug_report_deadline_after(uint64_t now, uint64_t delay_ns)
unsigned long long uint64_t
#define GET_OPTION(field)
Definition options.h:1339
void debug_stats_print(void)

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

◆ debug_stats_print()

void debug_stats_print ( void )

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

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

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 24 of file lib/debug/stats.c.

24 {
25 g_debug_stats_main_thread = thread_current_id();
26}

◆ debug_stats_set_memory_report_interval()

void debug_stats_set_memory_report_interval ( uint64_t interval)

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

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

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 115 of file lib/debug/stats.c.

115 {
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}
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
#define NS_PER_SEC_INT
Definition time.h:173
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
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:1243

References ASCIICHAT_OK, atomic_store_bool_impl(), 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.

◆ 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}

References atomic_store_bool_impl().

◆ 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}

References atomic_store_bool_impl().