ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
runtime.h File Reference

Go to the source code of this file.

Data Structures

struct  stats_view_t

Macros

#define STATS_RUNTIME_MODE_BUFFER_SIZE   32
#define STATS_VIEW_LINE_BUFFER_SIZE   160
#define STATS_VIEW_LINES   128

Typedefs

typedef struct stats_peer stats_peer_t
typedef void(* stats_provider_fn) (stats_view_t *view, void *data)

Functions

asciichat_error_t stats_runtime_start (const char *mode, unsigned interval_seconds)
void stats_runtime_stop (void)
stats_scope_t * stats_runtime_scope (void)
void stats_runtime_set_provider (stats_provider_fn provider, void *data)
void stats_view_add (stats_view_t *view, const char *format,...)
void stats_view_add_header (stats_view_t *view, const char *format,...)
void stats_runtime_toggle (void)
bool stats_runtime_active (void)
stats_peer_t * stats_runtime_peer_open (const char *transport)
void stats_runtime_peer_close (stats_peer_t *peer)
stats_scope_t * stats_runtime_peer_scope (stats_peer_t *peer)
void stats_runtime_packet (stats_peer_t *peer, unsigned type, size_t payload_bytes, bool sent, bool success)
void stats_runtime_connection_state (const char *state)
void stats_runtime_media (unsigned width, unsigned height, double position, double duration)

Macro Definition Documentation

◆ STATS_RUNTIME_MODE_BUFFER_SIZE

#define STATS_RUNTIME_MODE_BUFFER_SIZE   32

Definition at line 4 of file stats/runtime.h.

◆ STATS_VIEW_LINE_BUFFER_SIZE

#define STATS_VIEW_LINE_BUFFER_SIZE   160

Definition at line 7 of file stats/runtime.h.

◆ STATS_VIEW_LINES

#define STATS_VIEW_LINES   128

Definition at line 17 of file stats/runtime.h.

Typedef Documentation

◆ stats_peer_t

typedef struct stats_peer stats_peer_t

Definition at line 16 of file stats/runtime.h.

◆ stats_provider_fn

typedef void(* stats_provider_fn) (stats_view_t *view, void *data)

Provider runs on the stats worker; unregister before destroying its data. Never call stats_runtime_set_provider() from a provider.

Definition at line 37 of file stats/runtime.h.

Function Documentation

◆ stats_runtime_active()

bool stats_runtime_active ( void )

Definition at line 92 of file runtime.c.

92 {
93 return atomic_load_bool_impl(&g_active) &&
97}
bool atomic_load_bool_impl(const atomic_t *a)
int keyboard_help_previous_interactive(void)
int ui_controller_interactive_screen(void)
@ UI_SCREEN_STATS
@ UI_SCREEN_HELP

References atomic_load_bool_impl(), keyboard_help_previous_interactive(), ui_controller_interactive_screen(), UI_SCREEN_HELP, and UI_SCREEN_STATS.

Referenced by stats_runtime_toggle().

◆ stats_runtime_connection_state()

void stats_runtime_connection_state ( const char * state)

Definition at line 143 of file runtime.c.

143 {
144 if (!g_scope)
145 return;
146 mutex_lock(&g_provider_mutex);
147 snprintf(g_state, sizeof(g_state), "%s", state);
148 mutex_unlock(&g_provider_mutex);
149}

◆ stats_runtime_media()

void stats_runtime_media ( unsigned width,
unsigned height,
double position,
double duration )

Definition at line 72 of file runtime.c.

73 {
74 if (!g_scope)
75 return;
76 atomic_store_u64_impl(&g_media_width, width);
77 atomic_store_u64_impl(&g_media_height, height);
78 atomic_store_u64_impl(&g_media_position,
79 isfinite(position) && position >= 0 && position < 1e12
80 ? (uint64_t)(position * 1000) + 1
81 : 0);
82 atomic_store_u64_impl(&g_media_duration,
83 isfinite(duration) && duration > 0 && duration < 1e12
84 ? (uint64_t)(duration * 1000) + 1
85 : 0);
86}
void atomic_store_u64_impl(atomic_t *a, uint64_t value)
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
unsigned long long uint64_t

References atomic_store_u64_impl(), height, and width.

Referenced by stats_runtime_start().

◆ stats_runtime_packet()

void stats_runtime_packet ( stats_peer_t * peer,
unsigned type,
size_t payload_bytes,
bool sent,
bool success )

Application packet boundary, after decryption on receive. Header bytes are included internally.

Definition at line 205 of file runtime.c.

206 {
207 if (success) {
208 ascii_otel_metric(sent ? ASCII_OTEL_METRIC_NET_TX_SIZE
209 : ASCII_OTEL_METRIC_NET_RX_SIZE,
210 (double)payload_bytes);
211 }
212 if (sent && type == PACKET_TYPE_ERROR_MESSAGE)
214 stats_scope_t *scopes[] = {g_scope, peer ? peer->scope : NULL};
215 for (unsigned i = 0; i < 2; ++i) {
216 if (!success) {
218 scopes[i],
220 continue;
221 }
223 scopes[i],
225 stats_counter_add(scopes[i],
228 sizeof(packet_header_t) + payload_bytes);
229 if (type == PACKET_TYPE_ASCII_FRAME || type == PACKET_TYPE_IMAGE_FRAME ||
233 scopes[i],
235 }
236}
@ PACKET_TYPE_IMAGE_FRAME
Complete RGB image with dimensions.
Definition packet.h:399
@ PACKET_TYPE_IMAGE_FRAME_H265
H.265-encoded frame from client (HEVC/H.265 codec).
Definition packet.h:401
@ PACKET_TYPE_ASCII_FRAME
Complete ASCII frame with all metadata.
Definition packet.h:397
@ PACKET_TYPE_IMAGE_FRAME_H264
Definition packet.h:403
@ PACKET_TYPE_ERROR_MESSAGE
Error packet with asciichat_error_t code and human-readable message.
Definition packet.h:388
struct stats_scope stats_scope_t
void stats_counter_add(stats_scope_t *scope, stats_counter_id_t id, uint64_t amount)
void ascii_otel_metric(ascii_otel_metric_t metric, double value)
Definition otel.c:175
Fixed prefix shared by every ACIP packet on the wire.
Definition packet.h:641
stats_scope_t * scope
Definition runtime.c:50

References ascii_otel_metric(), PACKET_TYPE_ASCII_FRAME, PACKET_TYPE_ERROR_MESSAGE, PACKET_TYPE_IMAGE_FRAME, PACKET_TYPE_IMAGE_FRAME_H264, PACKET_TYPE_IMAGE_FRAME_H265, stats_peer::scope, stats_counter_add(), STATS_COUNTER_BYTES_RECEIVED, STATS_COUNTER_BYTES_SENT, STATS_COUNTER_FRAMES_RECEIVED, STATS_COUNTER_FRAMES_SENT, STATS_COUNTER_PACKETS_RECEIVED, STATS_COUNTER_PACKETS_SENT, STATS_COUNTER_RECEIVE_ERRORS, STATS_COUNTER_REQUEST_FAILURES, and STATS_COUNTER_SEND_ERRORS.

Referenced by acip_handle_acds_packet(), acip_handle_client_packet(), acip_handle_server_packet(), packet_receive_via_transport(), and packet_send_via_transportv().

◆ stats_runtime_peer_close()

void stats_runtime_peer_close ( stats_peer_t * peer)

Definition at line 190 of file runtime.c.

190 {
191 if (!peer)
192 return;
193 mutex_lock(&g_peers_mutex);
194 for (unsigned i = 0; i < STATS_PEER_CAPACITY; ++i)
195 if (g_peers[i] == peer)
196 g_peers[i] = NULL;
197 --g_peer_count;
198 stats_gauge_set(g_scope, STATS_GAUGE_CONNECTIONS_ACTIVE, g_peer_count);
199 mutex_unlock(&g_peers_mutex);
202 SAFE_FREE(peer);
203}
#define SAFE_FREE(ptr)
void stats_scope_destroy(stats_scope_t *scope)
void stats_sampler_destroy(stats_sampler_t *sampler)
void stats_gauge_set(stats_scope_t *scope, stats_gauge_id_t id, uint64_t value)
#define STATS_PEER_CAPACITY
Definition runtime.c:10
stats_sampler_t * sampler
Definition runtime.c:51

References SAFE_FREE, stats_peer::sampler, stats_peer::scope, STATS_GAUGE_CONNECTIONS_ACTIVE, stats_gauge_set(), STATS_PEER_CAPACITY, stats_sampler_destroy(), and stats_scope_destroy().

Referenced by acip_transport_destroy(), and acip_websocket_client_transport_create().

◆ stats_runtime_peer_open()

stats_peer_t * stats_runtime_peer_open ( const char * transport)

Definition at line 161 of file runtime.c.

161 {
162 if (!g_scope)
163 return NULL;
164 stats_peer_t *peer = SAFE_CALLOC(1, sizeof(*peer), stats_peer_t *);
165 if (!peer)
166 return NULL;
167 stats_capabilities_t caps = network_capabilities();
169 if (stats_scope_create(&caps, &peer->scope) != ASCIICHAT_OK ||
170 stats_sampler_create(&config, &peer->sampler) != ASCIICHAT_OK) {
172 SAFE_FREE(peer);
173 return NULL;
174 }
175 snprintf(peer->transport, sizeof(peer->transport), "%s", transport);
176 mutex_lock(&g_peers_mutex);
177 peer->id = ++g_peer_id;
178 ++g_peer_count;
179 stats_gauge_set(g_scope, STATS_GAUGE_CONNECTIONS_ACTIVE, g_peer_count);
180 for (unsigned i = 0; i < STATS_PEER_CAPACITY; ++i) {
181 if (!g_peers[i]) {
182 g_peers[i] = peer;
183 break;
184 }
185 }
186 mutex_unlock(&g_peers_mutex);
187 return peer;
188}
#define SAFE_CALLOC(count, size, cast)
@ ASCIICHAT_OK
Definition error_codes.h:50
#define NS_PER_SEC_INT
Definition time.h:173
#define NS_PER_MS_INT
Definition time.h:172
asciichat_error_t stats_scope_create(const stats_capabilities_t *capabilities, stats_scope_t **out_scope)
asciichat_error_t stats_sampler_create(const stats_sampler_config_t *config, stats_sampler_t **out_sampler)
struct stats_peer stats_peer_t
stats descriptor is intentionally a small, concrete record. Fields such as name, unit,...
uint64_t id
Definition runtime.c:53
char transport[16]
Definition runtime.c:52

References ASCIICHAT_OK, stats_peer::id, NS_PER_MS_INT, NS_PER_SEC_INT, SAFE_CALLOC, SAFE_FREE, stats_peer::sampler, stats_peer::scope, STATS_GAUGE_CONNECTIONS_ACTIVE, stats_gauge_set(), STATS_PEER_CAPACITY, stats_sampler_create(), stats_scope_create(), stats_scope_destroy(), and stats_peer::transport.

Referenced by acip_async_tcp_transport_create(), acip_tcp_transport_create(), acip_websocket_client_transport_create(), and acip_websocket_server_transport_create().

◆ stats_runtime_peer_scope()

stats_scope_t * stats_runtime_peer_scope ( stats_peer_t * peer)

Definition at line 89 of file runtime.c.

89 {
90 return peer ? peer->scope : NULL;
91}

References stats_peer::scope.

◆ stats_runtime_scope()

◆ stats_runtime_set_provider()

void stats_runtime_set_provider ( stats_provider_fn provider,
void * data )

Definition at line 134 of file runtime.c.

134 {
135 if (!g_scope)
136 return;
137 mutex_lock(&g_provider_mutex);
138 g_provider = provider;
139 g_provider_data = data;
140 mutex_unlock(&g_provider_mutex);
141}

Referenced by mcp_server_main().

◆ stats_runtime_start()

asciichat_error_t stats_runtime_start ( const char * mode,
unsigned interval_seconds )

Definition at line 605 of file runtime.c.

606 {
607 if (!mode)
608 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing statistics mode");
609 if (g_started)
610 return ASCIICHAT_OK;
611 bool mcp = strcmp(mode, "mcp-server") == 0;
612 stats_capabilities_t caps = network_capabilities();
613 bool mirror = strcmp(mode, "mirror") == 0,
614 acds = strcmp(mode, "discovery-service") == 0;
615 if (mirror)
616 memset(&caps, 0, sizeof(caps));
617 if (!acds) {
618 for (int i = 0; i <= STATS_COUNTER_FRAMES_DROPPED; ++i)
619 caps.counters[i] = true;
623 caps.counters[STATS_COUNTER_AUDIO_OVERRUNS] = GET_OPTION(audio_enabled);
624 for (int i = 0; i <= STATS_DURATION_TERMINAL_WRITE; ++i)
625 caps.durations[i] = true;
628 GET_OPTION(audio_enabled) && !mirror;
629 if (mirror) {
633 caps.durations[STATS_DURATION_DECODE] = false;
634 }
635 } else {
640 caps.counters[i] = true;
645 }
646 if (strcmp(mode, "discovery") == 0)
648 caps.gauges[STATS_GAUGE_VIDEO_QUEUE_DEPTH] = strcmp(mode, "server") == 0;
649 if (GET_OPTION(render_file)[0]) {
652 }
653 if (!mirror) {
655 caps.durations[STATS_DURATION_RTT] = !acds;
660 }
661 if (mcp) {
662 memset(&caps, 0, sizeof(caps));
663 for (unsigned i = STATS_COUNTER_MCP_REQUESTS;
665 caps.counters[i] = true;
666 for (unsigned i = STATS_COUNTER_HTTP_REQUESTS;
668 caps.counters[i] = true;
673 }
674 asciichat_error_t result = stats_scope_create(&caps, &g_scope);
675 if (result != ASCIICHAT_OK)
676 return result;
679 result = stats_sampler_create(&config, &g_sampler);
680 if (result != ASCIICHAT_OK)
681 goto fail_scope;
682 if (mutex_init(&g_peers_mutex, "stats_peers") != 0) {
683 result = SET_ERRNO(ERROR_THREAD, "Cannot initialize stats peer mutex");
684 goto fail_sampler;
685 }
686 if (mutex_init(&g_provider_mutex, "stats_provider") != 0) {
687 result = SET_ERRNO(ERROR_THREAD, "Cannot initialize stats provider mutex");
688 goto fail_peers;
689 }
690 g_peer_count = g_peer_id = 0;
691 memset(g_peers, 0, sizeof(g_peers));
692 g_state[0] = 0;
693 stats_runtime_media(0, 0, -1, -1);
694 snprintf(g_mode, sizeof(g_mode), "%s", mode);
695 g_interval = interval_seconds;
696 g_fd = platform_isatty(STDOUT_FILENO) ? STDOUT_FILENO : STDERR_FILENO;
697 // Mirror mode has no statistics-screen interaction. Avoid installing the
698 // console reader there so shutdown cannot leave the worker inside a UI
699 // input path while the mirror tears down its display.
700 g_keyboard = !mirror &&
701 (!mcp || !strcmp(GET_OPTION(mcp_transport), "http")) &&
702 platform_isatty(STDIN_FILENO) && platform_isatty(g_fd) &&
703 keyboard_init() == ASCIICHAT_OK;
704 atomic_store_bool_impl(&g_stop, false);
705 atomic_store_bool_impl(&g_active, false);
706 atomic_store_bool_impl(&g_onboarding_wait, false);
707 g_started = true;
708 bool server_like = strcmp(mode, "server") == 0 ||
709 strcmp(mode, "discovery-service") == 0 ||
710 (mcp && !strcmp(GET_OPTION(mcp_transport), "http"));
711 if (g_keyboard && server_like &&
713 LOG_ERRNO_IF_SET("Cannot start terminal presentation");
714 if (platform_thread_create(&g_worker, "stats", stats_worker, NULL) == 0) {
715 atomic_store_bool_impl(&g_onboarding_wait, true);
716 bool shown = show_first_use_instructions();
720 if (state.screen == UI_SCREEN_NOTICE && state.covered)
721 break;
722 platform_sleep_ms(10);
723 }
724 atomic_store_bool_impl(&g_onboarding_wait, false);
725 return ASCIICHAT_OK;
726 }
727 result = SET_ERRNO(ERROR_THREAD, "Cannot start stats worker");
728 g_started = false;
729 if (g_keyboard)
731 mutex_destroy(&g_provider_mutex);
732fail_peers:
733 mutex_destroy(&g_peers_mutex);
734fail_sampler:
735 stats_sampler_destroy(g_sampler);
736 g_sampler = NULL;
737fail_scope:
738 stats_scope_destroy(g_scope);
739 g_scope = NULL;
740 return result;
741}
#define SET_ERRNO(code,...)
#define LOG_ERRNO_IF_SET(message)
void atomic_store_bool_impl(atomic_t *a, uint64_t value)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_PARAM
@ ERROR_THREAD
#define GET_OPTION(field)
Definition options.h:1339
bool shutdown_is_requested(void)
Check if shutdown has been requested.
Definition common.c:87
@ STATS_COUNTER_RATE_LIMIT_REJECTIONS
@ STATS_COUNTER_MIGRATION_FAILURES
@ STATS_COUNTER_MCP_HTTP_RESPONSES
@ STATS_GAUGE_PARTICIPANTS_ACTIVE
@ STATS_GAUGE_HTTP_CONNECTIONS_ACTIVE
@ STATS_GAUGE_HTTP_STREAMS_ACTIVE
@ STATS_GAUGE_AUDIO_BUFFERED_SAMPLES
@ STATS_DURATION_CONNECTION_SETUP
void keyboard_destroy(void)
Definition keyboard.c:15
void ascii_otel_stats_attach(stats_scope_t *scope)
Definition otel.c:237
void stats_runtime_media(unsigned width, unsigned height, double position, double duration)
Definition runtime.c:72
bool counters[STATS_COUNTER_COUNT]
bool durations[STATS_DURATION_COUNT]
asciichat_error_t ui_controller_start_terminal(int fd)
ui_presentation_state_t ui_controller_state(void)
bool ui_controller_screen_live(ui_screen_t screen)
@ UI_SCREEN_NOTICE
int platform_isatty(int fd)
Definition util.c:70

References ascii_otel_stats_attach(), ASCIICHAT_OK, atomic_store_bool_impl(), stats_capabilities_t::counters, ui_presentation_state_t::covered, stats_capabilities_t::durations, ERROR_INVALID_PARAM, ERROR_INVALID_STATE, ERROR_THREAD, stats_capabilities_t::gauges, GET_OPTION, keyboard_destroy(), LOG_ERRNO_IF_SET, NS_PER_MS_INT, NS_PER_SEC_INT, platform_isatty(), ui_presentation_state_t::screen, SET_ERRNO, shutdown_is_requested(), STATS_COUNTER_AUDIO_OVERRUNS, STATS_COUNTER_AUDIO_UNDERRUNS, STATS_COUNTER_FRAMES_DECODED, STATS_COUNTER_FRAMES_DROPPED, STATS_COUNTER_FRAMES_ENCODED, STATS_COUNTER_FRAMES_ENQUEUED, STATS_COUNTER_FRAMES_RECEIVED, STATS_COUNTER_FRAMES_SENT, STATS_COUNTER_HOST_MIGRATIONS, STATS_COUNTER_HTTP_BYTES_SENT, STATS_COUNTER_HTTP_REQUESTS, STATS_COUNTER_MCP_HTTP_RESPONSES, STATS_COUNTER_MCP_REQUESTS, STATS_COUNTER_MIGRATION_FAILURES, STATS_COUNTER_QUEUE_DROPS, STATS_COUNTER_RATE_LIMIT_REJECTIONS, STATS_COUNTER_RECONNECTS, STATS_COUNTER_RECORDING_FRAMES, STATS_COUNTER_SESSION_CREATES, STATS_DURATION_AUDIO_MIX, STATS_DURATION_CONNECTION_SETUP, STATS_DURATION_DECODE, STATS_DURATION_ENCODE, STATS_DURATION_HTTP_REQUEST, STATS_DURATION_MCP_RENDER, STATS_DURATION_RECORDING, STATS_DURATION_REQUEST, STATS_DURATION_RTT, STATS_DURATION_TERMINAL_WRITE, STATS_GAUGE_AUDIO_BUFFERED_SAMPLES, STATS_GAUGE_CONNECTIONS_ACTIVE, STATS_GAUGE_HTTP_CONNECTIONS_ACTIVE, STATS_GAUGE_HTTP_STREAMS_ACTIVE, STATS_GAUGE_PARTICIPANTS_ACTIVE, STATS_GAUGE_SESSIONS_ACTIVE, STATS_GAUGE_VIDEO_QUEUE_DEPTH, stats_runtime_media(), stats_sampler_create(), stats_sampler_destroy(), stats_scope_create(), stats_scope_destroy(), ui_controller_screen_live(), ui_controller_start_terminal(), ui_controller_state(), and UI_SCREEN_NOTICE.

◆ stats_runtime_stop()

void stats_runtime_stop ( void )

Definition at line 743 of file runtime.c.

743 {
744 if (!g_started)
745 return;
746 atomic_store_bool_impl(&g_stop, true);
747 thread_join(&g_worker, NULL);
749 if (g_keyboard)
751 g_provider = NULL;
752 g_provider_data = NULL;
753 stats_sampler_destroy(g_sampler);
754 stats_scope_destroy(g_scope);
755 g_sampler = NULL;
756 g_scope = NULL;
757 mutex_destroy(&g_provider_mutex);
758 mutex_destroy(&g_peers_mutex);
759 g_started = false;
760}
void ui_controller_shutdown(void)

References atomic_store_bool_impl(), keyboard_destroy(), stats_sampler_destroy(), stats_scope_destroy(), and ui_controller_shutdown().

Referenced by asciichat_shared_destroy().

◆ stats_runtime_toggle()

void stats_runtime_toggle ( void )

Definition at line 99 of file runtime.c.

99 {
100 if (g_started && g_keyboard) {
101 bool active = !stats_runtime_active();
102 atomic_store_bool_impl(&g_active, active);
104 log_debug("Statistics TUI requested: %s", active ? "open" : "closed");
105 }
106}
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
bool stats_runtime_active(void)
Definition runtime.c:92
void ui_controller_select_interactive(int screen)

References atomic_store_bool_impl(), log_debug, stats_runtime_active(), ui_controller_select_interactive(), and UI_SCREEN_STATS.

◆ stats_view_add()

void stats_view_add ( stats_view_t * view,
const char * format,
... )

Definition at line 120 of file runtime.c.

120 {
121 va_list args;
122 va_start(args, format);
123 stats_view_vadd(view, false, format, args);
124 va_end(args);
125}
action_args_t args

References args.

◆ stats_view_add_header()

void stats_view_add_header ( stats_view_t * view,
const char * format,
... )

Definition at line 127 of file runtime.c.

127 {
128 va_list args;
129 va_start(args, format);
130 stats_view_vadd(view, true, format, args);
131 va_end(args);
132}

References args.