ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
fps_counter.c
Go to the documentation of this file.
1
8#include <ascii-chat/common.h>
9#include <string.h>
10
11/* ============================================================================
12 * FPS Counter Implementation
13 * ============================================================================ */
14
15#define FPS_WINDOW_SIZE 30
16
28
29/* ============================================================================
30 * Lifecycle Functions
31 * ============================================================================ */
32
34 fps_counter_t *counter = SAFE_CALLOC(1, sizeof(fps_counter_t), fps_counter_t *);
35 if (!counter) {
36 return NULL;
37 }
38
39 // Initialize circular buffer pointers
40 counter->head = 0;
41 counter->count = 0;
42 memset(counter->frame_times, 0, sizeof(counter->frame_times));
43
44 return counter;
45}
46
48 if (counter) {
49 SAFE_FREE(counter);
50 }
51}
52
53/* ============================================================================
54 * FPS Measurement Functions
55 * ============================================================================ */
56
58 if (!counter) {
59 return;
60 }
61
62 // Record current time at head position
63 counter->frame_times[counter->head] = time_get_ns();
64
65 // Advance head pointer with wraparound
66 counter->head = (counter->head + 1) % FPS_WINDOW_SIZE;
67
68 // Track how many valid entries we have (up to FPS_WINDOW_SIZE)
69 if (counter->count < FPS_WINDOW_SIZE) {
70 counter->count++;
71 }
72}
73
75 if (!counter || counter->count < 2) {
76 return 0.0f;
77 }
78
79 // Find indices of oldest and newest timestamps in the buffer
80 int oldest_idx = (counter->head - counter->count + FPS_WINDOW_SIZE) % FPS_WINDOW_SIZE;
81 int newest_idx = (counter->head - 1 + FPS_WINDOW_SIZE) % FPS_WINDOW_SIZE;
82
83 // Get the elapsed time between oldest and newest frames
84 uint64_t oldest_time = counter->frame_times[oldest_idx];
85 uint64_t newest_time = counter->frame_times[newest_idx];
86 uint64_t elapsed_ns = newest_time - oldest_time;
87
88 // Avoid division by zero
89 if (elapsed_ns == 0) {
90 return 0.0f;
91 }
92
93 // FPS = (number of frames) / elapsed_time
94 // We have (count - 1) frames across the elapsed time
95 // (count - 1) frames means count timestamps, so count-1 intervals
96 return (float)(counter->count - 1) * 1e9f / (float)elapsed_ns;
97}
⚙️ Common definitions, error codes, macros, and types shared throughout the application
#define FPS_WINDOW_SIZE
Number of frames to average over.
Definition fps_counter.c:15
⏱️ FPS counter for measuring output write throughput
#define SAFE_FREE(ptr)
Definition common.h:376
#define SAFE_CALLOC(count, size, cast)
Definition common.h:274
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
float fps_counter_get(fps_counter_t *counter)
Get the current FPS value.
Definition fps_counter.c:74
void fps_counter_tick(fps_counter_t *counter)
Record a frame timestamp.
Definition fps_counter.c:57
void fps_counter_destroy(fps_counter_t *counter)
Destroy FPS counter and free resources.
Definition fps_counter.c:47
fps_counter_t * fps_counter_create(void)
Create a new FPS counter.
Definition fps_counter.c:33
FPS counter state.
Definition fps_counter.c:23
int count
Number of valid entries in buffer (0 to FPS_WINDOW_SIZE)
Definition fps_counter.c:26
int head
Current write position (0 to FPS_WINDOW_SIZE-1)
Definition fps_counter.c:25
uint64_t frame_times[30]
Circular buffer of timestamps (ns)
Definition fps_counter.c:24