ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
debug/atomic.c
Go to the documentation of this file.
1
16#include <ascii-chat/atomic.h>
18#include <stdlib.h>
19#include <stdio.h>
20#include <string.h>
21
22static bool g_atomic_debug_initialized = false;
23
24// ============================================================================
25// Debug Initialization/Shutdown
26// ============================================================================
27
28#ifndef NDEBUG
29
31 g_atomic_debug_initialized = true;
32}
33
35 g_atomic_debug_initialized = false;
36}
37
39 return g_atomic_debug_initialized;
40}
41
42// ============================================================================
43// Print State (called from debug_sync_print_state in lib/debug/sync.c)
44// ============================================================================
45
53int debug_atomic_format_timing(const atomic_t *atomic, char *buffer, size_t size) {
54 if (!atomic || !buffer || size == 0)
55 return 0;
56
57 // If never accessed, return empty (so caller can skip output)
58 if (atomic->last_load_time_ns == 0 && atomic->last_store_time_ns == 0) {
59 return 0;
60 }
61
62 int offset = 0;
63 uint64_t now_ns = time_get_ns();
64
65 char load_str[64] = "";
66 char store_str[64] = "";
67 char count_str[128] = "";
68
69 if (atomic->last_load_time_ns > 0 && atomic->last_load_time_ns <= now_ns) {
70 char elapsed_str[64];
71 time_pretty(now_ns - atomic->last_load_time_ns, -1, elapsed_str, sizeof(elapsed_str));
72 snprintf(load_str, sizeof(load_str), "load=%s", elapsed_str);
73 }
74
75 if (atomic->last_store_time_ns > 0 && atomic->last_store_time_ns <= now_ns) {
76 char elapsed_str[64];
77 time_pretty(now_ns - atomic->last_store_time_ns, -1, elapsed_str, sizeof(elapsed_str));
78 snprintf(store_str, sizeof(store_str), "store=%s", elapsed_str);
79 }
80
81 if (atomic->load_count > 0 || atomic->store_count > 0 || atomic->cas_count > 0 || atomic->fetch_count > 0) {
82 snprintf(count_str, sizeof(count_str), "[ops: load=%llu store=%llu cas=%llu/%llu fetch=%llu]",
83 (unsigned long long)atomic->load_count, (unsigned long long)atomic->store_count,
84 (unsigned long long)atomic->cas_success_count, (unsigned long long)atomic->cas_count,
85 (unsigned long long)atomic->fetch_count);
86 }
87
88 offset += snprintf(buffer + offset, size - offset, "%s %s %s", load_str, store_str, count_str);
89 return offset;
90}
91
99int debug_atomic_ptr_format_timing(const atomic_ptr_t *atomic, char *buffer, size_t size) {
100 if (!atomic || !buffer || size == 0)
101 return 0;
102
103 // If never accessed, return empty (so caller can skip output)
104 if (atomic->last_load_time_ns == 0 && atomic->last_store_time_ns == 0) {
105 return 0;
106 }
107
108 int offset = 0;
109 uint64_t now_ns = time_get_ns();
110
111 char load_str[64] = "";
112 char store_str[64] = "";
113 char count_str[128] = "";
114
115 if (atomic->last_load_time_ns > 0 && atomic->last_load_time_ns <= now_ns) {
116 char elapsed_str[64];
117 time_pretty(now_ns - atomic->last_load_time_ns, -1, elapsed_str, sizeof(elapsed_str));
118 snprintf(load_str, sizeof(load_str), "load=%s", elapsed_str);
119 }
120
121 if (atomic->last_store_time_ns > 0 && atomic->last_store_time_ns <= now_ns) {
122 char elapsed_str[64];
123 time_pretty(now_ns - atomic->last_store_time_ns, -1, elapsed_str, sizeof(elapsed_str));
124 snprintf(store_str, sizeof(store_str), "store=%s", elapsed_str);
125 }
126
127 if (atomic->load_count > 0 || atomic->store_count > 0 || atomic->cas_count > 0 || atomic->exchange_count > 0) {
128 snprintf(count_str, sizeof(count_str), "[ops: load=%llu store=%llu cas=%llu/%llu exchange=%llu]",
129 (unsigned long long)atomic->load_count, (unsigned long long)atomic->store_count,
130 (unsigned long long)atomic->cas_success_count, (unsigned long long)atomic->cas_count,
131 (unsigned long long)atomic->exchange_count);
132 }
133
134 offset += snprintf(buffer + offset, size - offset, "%s %s %s", load_str, store_str, count_str);
135 return offset;
136}
137
139 if (!g_atomic_debug_initialized)
140 return;
141
142 // Iterate named registry for atomic entries
143 // Note: this is now kept for backward compatibility
144 // The preferred method is to use sync.c's atomic_iter_callback
145 // which integrates atomics into the synchronized output buffer
146}
147
148#endif // !NDEBUG
⚛️ Atomic operations abstraction layer with debug tracking
int debug_atomic_ptr_format_timing(const atomic_ptr_t *atomic, char *buffer, size_t size)
Format timing info for an atomic_ptr_t and write to buffer.
void debug_atomic_init(void)
Lifecycle initialization for debug atomic tracking.
void debug_atomic_print_state(void)
Print all named atomic operations state (integration with –sync-state)
void debug_atomic_shutdown(void)
Lifecycle shutdown for debug atomic tracking.
bool debug_atomic_is_initialized(void)
Check if debug atomic tracking is initialized.
int debug_atomic_format_timing(const atomic_t *atomic, char *buffer, size_t size)
Format timing info for an atomic and write to buffer.
Debug tracking for atomic operations.
Named object registry for debugging — log identifiable resource names.
unsigned long long uint64_t
Definition common.h:59
uint64_t time_get_ns(void)
Get current monotonic time in nanoseconds.
Definition util/time.c:108
int time_pretty(uint64_t nanoseconds, int decimals, char *buffer, size_t buffer_size)
Format nanoseconds as pretty duration with spaces and configurable precision.
Definition util/time.c:424
Atomic pointer wrapper.
Definition atomic.h:106
uint64_t last_load_time_ns
Definition atomic.h:110
uint64_t store_count
Definition atomic.h:111
uint64_t cas_success_count
Definition atomic.h:114
uint64_t cas_count
Definition atomic.h:113
uint64_t last_store_time_ns
Definition atomic.h:109
uint64_t exchange_count
Definition atomic.h:115
uint64_t load_count
Definition atomic.h:112
Atomic value wrapper for integral/boolean types.
Definition atomic.h:76
uint64_t last_load_time_ns
Definition atomic.h:80
uint64_t fetch_count
Definition atomic.h:85
uint64_t cas_count
Definition atomic.h:83
uint64_t store_count
Definition atomic.h:81
uint64_t load_count
Definition atomic.h:82
uint64_t last_store_time_ns
Definition atomic.h:79
uint64_t cas_success_count
Definition atomic.h:84
⏱️ High-precision timing utilities using sokol_time.h and uthash