Debian Codegraph @

src/rist_pacer.c

raw symbols SCIP
1 /* librist. Copyright © 2026 SipRadius LLC. All right reserved.
2 * Author: Sergio Ammirata <[email protected]>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7 /* Constant-bitrate output pacing. See rist_pacer.h for the rationale. */
8
9 #include "rist_pacer.h"
10
11 #include <string.h>
12
13 void rist_pacer_rate_init(struct rist_pacer_rate *r, uint64_t seed_bps)
14 {
15 memset(r, 0, sizeof(*r));
16 if (seed_bps) {
17 r->eight_times = seed_bps * 8ULL;
18 r->bps = seed_bps;
19 }
20 }
21
22 void rist_pacer_rate_add(struct rist_pacer_rate *r, size_t bytes, uint64_t now_us)
23 {
24 if (!r->last_calc_us) {
25 /* Anchor the first window; these bytes cannot be timed. */
26 r->last_calc_us = now_us;
27 r->bytes = 0;
28 return;
29 }
30
31 r->bytes += bytes;
32
33 uint64_t dt = now_us - r->last_calc_us;
34 if (dt < RIST_PACER_RATE_WINDOW_US)
35 return;
36
37 if (dt <= RIST_PACER_RATE_WINDOW_US * RIST_PACER_RATE_STALL_FACTOR) {
38 uint64_t bitrate = (8ULL * r->bytes * 1000000ULL) / dt;
39 if (r->eight_times == 0)
40 r->eight_times = bitrate * 8ULL; /* prime, skip the 8-window ramp */
41 else
42 r->eight_times += bitrate - r->eight_times / 8;
43 r->bps = r->eight_times / 8;
44 }
45 /* else: the window spans an outage; keep the last good rate. */
46
47 r->last_calc_us = now_us;
48 r->bytes = 0;
49 }
50
51 void rist_pacer_init(struct rist_pacer *p, double target_latency_ms,
52 uint64_t min_sleep_ns, uint64_t max_sleep_ns)
53 {
54 memset(p, 0, sizeof(*p));
55 p->target_latency_ms = target_latency_ms;
56 p->min_sleep_ns = min_sleep_ns;
57 p->max_sleep_ns = max_sleep_ns;
58 p->max_slip_ns = RIST_PACER_MAX_SLIP_NS;
59 }
60
61 void rist_pacer_reset(struct rist_pacer *p)
62 {
63 p->next_due_ns = 0;
64 }
65
66 uint64_t rist_pacer_interval_ns(const struct rist_pacer *p, uint64_t rate_bps,
67 size_t bytes, size_t occupancy)
68 {
69 if (!rate_bps || !bytes)
70 return 0;
71
72 uint64_t interval_ns = ((uint64_t)bytes * 8ULL * 1000000000ULL) / rate_bps;
73
74 if (p->target_latency_ms <= 0.0)
75 return interval_ns;
76
77 /* Above target shortens to drain, below lengthens to fill; clamped so a
78 * transient excursion cannot slew the rate far. */
79 double target_bytes = (double)rate_bps / 8.0 * (p->target_latency_ms / 1000.0);
80 double target_dg = target_bytes / (double)bytes;
81 if (target_dg < 1.0)
82 target_dg = 1.0;
83
84 double err = ((double)occupancy - target_dg) / target_dg;
85 double factor = 1.0 - RIST_PACER_FEEDBACK_K * err;
86 if (factor < RIST_PACER_FEEDBACK_MIN)
87 factor = RIST_PACER_FEEDBACK_MIN;
88 if (factor > RIST_PACER_FEEDBACK_MAX)
89 factor = RIST_PACER_FEEDBACK_MAX;
90
91 return (uint64_t)((double)interval_ns * factor);
92 }
93
94 uint64_t rist_pacer_due_ns(struct rist_pacer *p, uint64_t now_ns)
95 {
96 if (p->next_due_ns == 0) {
97 p->next_due_ns = now_ns;
98 } else if (now_ns > p->next_due_ns + p->max_slip_ns) {
99 p->next_due_ns = now_ns;
100 p->underruns++;
101 }
102 return p->next_due_ns;
103 }
104
105 void rist_pacer_advance(struct rist_pacer *p, uint64_t interval_ns)
106 {
107 p->next_due_ns += interval_ns;
108 }
109
110 unsigned rist_pacer_quota(struct rist_pacer *p, uint64_t now_ns,
111 uint64_t interval_ns, unsigned max)
112 {
113 /* No estimate: nothing to pace against, so do not gate. */
114 if (interval_ns == 0)
115 return max;
116
117 /* Shared first-send and slip handling. */
118 (void)rist_pacer_due_ns(p, now_ns);
119
120 unsigned n = 0;
121 while (n < max && p->next_due_ns <= now_ns) {
122 p->next_due_ns += interval_ns;
123 n++;
124 }
125 return n;
126 }
127
128 uint64_t rist_pacer_sleep_ns(const struct rist_pacer *p, uint64_t now_ns)
129 {
130 uint64_t sleep_ns;
131
132 if (p->next_due_ns == 0)
133 sleep_ns = p->max_sleep_ns; /* nothing scheduled: idle normally */
134 else if (p->next_due_ns <= now_ns)
135 sleep_ns = p->min_sleep_ns; /* already due: wake as soon as allowed */
136 else
137 sleep_ns = p->next_due_ns - now_ns;
138
139 if (sleep_ns < p->min_sleep_ns)
140 sleep_ns = p->min_sleep_ns;
141 if (p->max_sleep_ns && sleep_ns > p->max_sleep_ns)
142 sleep_ns = p->max_sleep_ns;
143 return sleep_ns;
144 }