ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
fps.c
Go to the documentation of this file.
1
6
8#include <ascii-chat/common.h>
10#include <ascii-chat/log/log.h>
11
15static void log_fps_report(const char *tracker_name, double actual_fps, uint64_t frame_count, double elapsed_seconds) {
16 log_dev_every(4500 * US_PER_MS_INT, "[%s] %.1f fps (%llu frames in %.1fs)", tracker_name, actual_fps, (unsigned long long)frame_count, elapsed_seconds);
17}
18
24static void log_lag_event(const char *tracker_name, const char *context, double late_ms, double expected_ms, double actual_ms, double actual_fps) {
25 // Rate limit to once per second to avoid log spam
26 log_warn_every(NS_PER_MS_INT, "[%s] LAG: %s late by %.1fms (expected %.1fms, got %.1fms, %.2f fps)", tracker_name, context, late_ms, expected_ms, actual_ms, actual_fps);
27}
28
29void fps_init(fps_t *tracker, int expected_fps, const char *name) {
30 // Default report interval: 1 second
31 fps_init_with_interval(tracker, expected_fps, name, 1 * NS_PER_SEC_INT);
32}
33
34void fps_init_with_interval(fps_t *tracker, int expected_fps, const char *name, uint64_t report_interval_ns) {
35 if (!tracker) {
36 return;
37 }
38
39 tracker->frame_count = 0;
40 tracker->expected_fps = expected_fps > 0 ? expected_fps : 60;
41 tracker->tracker_name = name ? name : "FPS";
42 tracker->report_interval_ns = report_interval_ns;
43
44 // Initialize timestamps to zero (will be set on first frame)
45 tracker->last_fps_report_ns = 0;
46 tracker->last_frame_time_ns = 0;
47}
48
49void fps_frame_ns(fps_t *tracker, uint64_t current_time_ns, const char *context) {
50 if (!tracker) {
51 return;
52 }
53
54 // Initialize on first frame
55 if (tracker->last_fps_report_ns == 0) {
56 tracker->last_fps_report_ns = current_time_ns;
57 tracker->last_frame_time_ns = current_time_ns;
58 }
59
60 tracker->frame_count++;
61
62 // Calculate time since last frame in nanoseconds
63 uint64_t frame_interval_ns = time_elapsed_ns(tracker->last_frame_time_ns, current_time_ns);
64 tracker->last_frame_time_ns = current_time_ns;
65
66 // Expected frame interval in nanoseconds
67 // For 60 FPS: 1 second / 60 = 16,666,666 ns per frame
68 uint64_t expected_interval_ns = NS_PER_SEC_INT / (uint64_t)tracker->expected_fps;
69 uint64_t lag_threshold_ns = expected_interval_ns + (expected_interval_ns / 2); // 50% over expected
70
71 // Log error if frame arrived too late (only after first frame)
72 if (tracker->frame_count > 1 && frame_interval_ns > lag_threshold_ns) {
73 const char *context_str = context ? context : "Frame";
74
75 // Convert to milliseconds for display (but calculations stay in nanoseconds)
76 double late_ms = time_ns_to_us(frame_interval_ns - expected_interval_ns) / 1000.0;
77 double expected_ms = time_ns_to_us(expected_interval_ns) / 1000.0;
78 double actual_ms = time_ns_to_us(frame_interval_ns) / 1000.0;
79 double actual_fps = 1e9 / (double)frame_interval_ns;
80
81 log_lag_event(tracker->tracker_name, context_str, late_ms, expected_ms, actual_ms, actual_fps);
82 }
83
84 // Report FPS every report_interval_ns
85 uint64_t elapsed_ns = time_elapsed_ns(tracker->last_fps_report_ns, current_time_ns);
86
87 if (elapsed_ns >= tracker->report_interval_ns) {
88 double elapsed_seconds = time_ns_to_s(elapsed_ns);
89 double actual_fps = (double)tracker->frame_count / elapsed_seconds;
90 log_fps_report(tracker->tracker_name, actual_fps, tracker->frame_count, elapsed_seconds);
91
92 // Reset counters for next interval
93 tracker->frame_count = 0;
94 tracker->last_fps_report_ns = current_time_ns;
95 }
96}
⏱️ FPS tracking utility for monitoring frame throughput across all threads
void fps_init_with_interval(fps_t *tracker, int expected_fps, const char *name, uint64_t report_interval_ns)
Initialize FPS tracker with custom report interval.
Definition fps.c:34
void fps_frame_ns(fps_t *tracker, uint64_t current_time_ns, const char *context)
Track a frame and detect lag conditions (nanosecond version - PRIMARY).
Definition fps.c:49
unsigned long long uint64_t
void fps_init(fps_t *tracker, int expected_fps, const char *name)
Initialize FPS tracker.
Definition fps.c:29
#define NS_PER_SEC_INT
Definition time.h:173
#define NS_PER_MS_INT
Definition time.h:172
uint64_t time_elapsed_ns(uint64_t start_ns, uint64_t end_ns)
Calculate elapsed time with wraparound safety.
Definition time.c:117
#define US_PER_MS_INT
Definition time.h:176
⚙️ Common definitions, error codes, macros, and types shared throughout the application
📝 Logging API with multiple log levels and terminal output control
#define log_warn_every(interval_us, fmt,...)
Rate-limited WARN logging.
Definition log/log.h:749
#define log_dev_every(interval_us, fmt,...)
Rate-limited DEV logging.
Definition log/log.h:740
Counters and timestamps used to calculate and report frame rate.
Definition fps.h:57
const char * tracker_name
Definition fps.h:63
uint64_t frame_count
Definition fps.h:58
uint64_t report_interval_ns
Definition fps.h:62
uint64_t last_frame_time_ns
Definition fps.h:60
int expected_fps
Definition fps.h:61
uint64_t last_fps_report_ns
Definition fps.h:59
⏱️ High-precision timing utilities using sokol_time.h and uthash