ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
fps_counter.c
Go to the documentation of this file.
1
6
7#include <ascii-chat/common.h>
10#include <stdio.h>
11#include <string.h>
12
13/* ============================================================================
14 * FPS Counter Implementation
15 * ============================================================================
16 */
17
18#define FPS_WINDOW_SIZE 30
19
47
48/* ============================================================================
49 * Lifecycle Functions
50 * ============================================================================
51 */
52
54 fps_counter_t *counter =
56 if (!counter) {
57 return NULL;
58 }
59
60 counter->last_fps = -1;
61 counter->effective_fps = 60;
62 counter->dark_background = true;
63 counter->report_start = time_get_ns();
64
65 // Initialize circular buffer pointers
66 counter->head = 0;
67 counter->count = 0;
68 memset(counter->frame_times, 0, sizeof(counter->frame_times));
69
70 return counter;
71}
72
74 if (counter) {
75 SAFE_FREE(counter);
76 }
77}
78
79/* ============================================================================
80 * FPS Measurement Functions
81 * ============================================================================
82 */
83
85 fps_counter_tick_at(counter, time_get_ns());
86}
87
89 if (counter) {
90 counter->head = counter->count = 0;
91 memset(counter->frame_times, 0, sizeof(counter->frame_times));
92 }
93}
94
96 if (!counter) {
97 return;
98 }
99
100 if (counter->count) {
101 uint64_t previous =
102 counter->frame_times[(counter->head + FPS_WINDOW_SIZE - 1) %
104 if (now <= previous || now - previous >= 2000000000ULL)
105 fps_counter_reset(counter);
106 }
107
108 // Record current time at head position
109 counter->frame_times[counter->head] = now;
110
111 // Advance head pointer with wraparound
112 counter->head = (counter->head + 1) % FPS_WINDOW_SIZE;
113
114 // Track how many valid entries we have (up to FPS_WINDOW_SIZE)
115 if (counter->count < FPS_WINDOW_SIZE) {
116 counter->count++;
117 }
118}
119
121 return fps_counter_get_at(counter, time_get_ns());
122}
123
125 if (!counter || counter->count < 2) {
126 return 0.0f;
127 }
128
129 // Find indices of oldest and newest timestamps in the buffer
130 int oldest_idx =
131 (counter->head - counter->count + FPS_WINDOW_SIZE) % FPS_WINDOW_SIZE;
132 int newest_idx = (counter->head - 1 + FPS_WINDOW_SIZE) % FPS_WINDOW_SIZE;
133
134 // Get the elapsed time between oldest and newest frames
135 uint64_t oldest_time = counter->frame_times[oldest_idx];
136 uint64_t newest_time = counter->frame_times[newest_idx];
137 if (now < newest_time || now - newest_time >= 2000000000ULL)
138 return 0.0f;
139 // Include time since the last completed frame so stalled output decays
140 // immediately.
141 uint64_t elapsed_ns = now - oldest_time;
142
143 // Avoid division by zero
144 if (elapsed_ns == 0) {
145 return 0.0f;
146 }
147
148 // FPS = (number of frames) / elapsed_time
149 // We have (count - 1) frames across the elapsed time
150 // (count - 1) frames means count timestamps, so count-1 intervals
151 return (float)(counter->count - 1) * 1e9f / (float)elapsed_ns;
152}
153
154bool fps_counter_set_visible(fps_counter_t *counter, bool visible) {
155 if (!counter)
156 return false;
157 bool changed = counter->visible != visible;
158 counter->visible = visible;
159 if (changed)
160 counter->last_fps = -1;
161 return changed;
162}
163
164void fps_counter_set_effective_fps(fps_counter_t *counter, unsigned int fps) {
165 if (counter) {
166 counter->effective_fps = fps > 0 ? fps : 60;
167 counter->last_fps = -1;
168 }
169}
170
171void fps_counter_frame_begin(fps_counter_t *counter, bool measure) {
172 if (!counter)
173 return;
174 counter->current_frame_id = 0;
175 counter->measuring = measure;
176 counter->written = counter->failed = false;
177 counter->frame_write_ns = 0;
178}
179
181 if (!counter)
182 return false;
184 frame_id && frame_id != counter->completed_frame_id);
185 counter->current_frame_id = frame_id;
186 return counter->measuring;
187}
188
190 if (counter && counter->measuring)
191 counter->write_start = time_get_ns();
192}
193
194void fps_counter_write_end(fps_counter_t *counter, bool complete) {
195 if (!counter || !counter->measuring)
196 return;
197 counter->frame_write_ns += time_get_ns() - counter->write_start;
198 counter->failed |= !complete;
199 counter->written |= complete;
200}
201
203 if (!counter)
204 return;
205 if (counter->measuring && counter->written && !counter->failed) {
206 fps_counter_tick_at(counter, now);
207 if (counter->current_frame_id)
208 counter->completed_frame_id = counter->current_frame_id;
209 counter->report_writes += counter->frame_write_ns;
210 counter->report_frames++;
211 }
212 counter->measuring = false;
213}
214
215void fps_counter_render(fps_counter_t *counter, int fd, int columns,
216 bool redrawn) {
217 char output[128];
218 size_t length =
219 fps_counter_format(counter, output, sizeof(output), columns, redrawn);
220 if (length)
221 (void)ui_output_write(fd, output, length);
222}
223
224size_t fps_counter_format(fps_counter_t *counter, char *buffer, size_t capacity,
225 int columns, bool redrawn) {
226 if (!counter)
227 return 0;
228 size_t output_length = 0;
229 uint64_t now = time_get_ns();
230 float measured = fps_counter_get_at(counter, now);
231 // A UI overlay can repaint for longer than the media counter's inactivity
232 // window. Keep the last visible value during a forced overlay redraw so
233 // opening keyboard help does not make an otherwise-live stream display 0.
234 int value = measured >= 999.0f ? 999 : (int)(measured + 0.5f);
235 if (redrawn && value == 0 && counter->last_fps > 0)
236 value = counter->last_fps;
237 // Keep both FPS fields three columns wide. A fixed overlay width prevents
238 // the label from shifting or leaving stale cells as values change digits.
239 // `FPS:ddd/ddd` is eleven columns wide, so start ten columns before the
240 // terminal's rightmost column.
241 if (counter->visible && columns >= 11 &&
242 (redrawn || value != counter->last_fps) && buffer && capacity > 0) {
243 const char *style =
244 counter->dark_background ? "\033[97;40m" : "\033[30;47m";
245 int length = snprintf(buffer, capacity,
246 "\033"
247 "7\033[1;%dH%sFPS:%3d/%3u\033[0m\033"
248 "8",
249 columns - 10, style, value, counter->effective_fps);
250 if (length > 0 && (size_t)length < capacity) {
251 counter->last_fps = value;
252 output_length = (size_t)length;
253 }
254 }
255 if (now - counter->report_start >= 3 * NS_PER_SEC_INT) {
256 if (counter->report_frames)
257 log_debug("FPS_OUTPUT: frames=%llu elapsed_ms=%.3f write_ms=%.3f",
258 (unsigned long long)counter->report_frames,
259 (double)(now - counter->report_start) / NS_PER_MS_INT,
260 (double)counter->report_writes / NS_PER_MS_INT);
261 counter->report_start = now;
262 counter->report_writes = counter->report_frames = 0;
263 }
264 return output_length;
265}
bool fps_counter_frame_begin_id(fps_counter_t *counter, uint64_t frame_id)
#define FPS_WINDOW_SIZE
Number of frames to average over.
Definition fps_counter.c:18
⏱️ FPS counter for measuring output write throughput
#define SAFE_FREE(ptr)
#define SAFE_CALLOC(count, size, cast)
unsigned long long uint64_t
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
#define NS_PER_SEC_INT
Definition time.h:173
#define NS_PER_MS_INT
Definition time.h:172
void fps_counter_reset(fps_counter_t *counter)
Definition fps_counter.c:88
void fps_counter_frame_begin(fps_counter_t *counter, bool measure)
void fps_counter_frame_end(fps_counter_t *counter, uint64_t now)
void fps_counter_write_begin(fps_counter_t *counter)
float fps_counter_get(fps_counter_t *counter)
Get the current FPS value.
void fps_counter_tick(fps_counter_t *counter)
Record a frame timestamp.
Definition fps_counter.c:84
void fps_counter_set_effective_fps(fps_counter_t *counter, unsigned int fps)
size_t fps_counter_format(fps_counter_t *counter, char *buffer, size_t capacity, int columns, bool redrawn)
float fps_counter_get_at(fps_counter_t *counter, uint64_t now)
bool fps_counter_set_visible(fps_counter_t *counter, bool visible)
void fps_counter_destroy(fps_counter_t *counter)
Destroy FPS counter and free resources.
Definition fps_counter.c:73
void fps_counter_tick_at(fps_counter_t *counter, uint64_t now)
Definition fps_counter.c:95
void fps_counter_render(fps_counter_t *counter, int fd, int columns, bool redrawn)
void fps_counter_write_end(fps_counter_t *counter, bool complete)
struct fps_counter_s fps_counter_t
Opaque FPS counter handle.
Definition fps_counter.h:45
fps_counter_t * fps_counter_create(void)
Create a new FPS counter.
Definition fps_counter.c:53
⚙️ Common definitions, error codes, macros, and types shared throughout the application
asciichat_error_t ui_output_write(int fd, const char *data, size_t length)
Definition output.c:16
🔤 String Manipulation and Shell Escaping Utilities
Per-renderer timing state for the on-screen FPS overlay.
Definition fps_counter.c:29
uint64_t frame_write_ns
Definition fps_counter.c:39
uint64_t report_start
Definition fps_counter.c:40
int count
Number of valid entries in buffer (0 to FPS_WINDOW_SIZE).
Definition fps_counter.c:45
uint64_t completed_frame_id
Definition fps_counter.c:37
uint64_t write_start
Definition fps_counter.c:38
uint64_t report_writes
Definition fps_counter.c:41
int head
Current write position (0 to FPS_WINDOW_SIZE-1).
Definition fps_counter.c:44
uint64_t frame_times[30]
Circular buffer of timestamps (ns).
Definition fps_counter.c:43
uint64_t report_frames
Definition fps_counter.c:42
uint64_t current_frame_id
Definition fps_counter.c:37
unsigned int effective_fps
Definition fps_counter.c:32
bool dark_background
Definition fps_counter.c:31