Debian Codegraph @

test/rist/unit/rist_pacer_test.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 /* Unit tests for the CBR pacing scheduler (rist_pacer).
8 *
9 * Every failure mode of this module still emits packets, at the wrong cadence or
10 * the wrong rate, so the invariants are asserted rather than inspected. */
11
12 #include "rist_pacer.h"
13
14 #include <assert.h>
15 #include <stdio.h>
16
17 /* A representative CBR datagram: seven MPEG-TS packets at 71.25 Mbps. */
18 #define DGRAM_BYTES (7 * 188)
19 #define RATE_BPS 71250000ULL
20
21 static void test_rate_converges(void)
22 {
23 struct rist_pacer_rate r;
24 uint64_t now_us = 1000000;
25
26 rist_pacer_rate_init(&r, 0);
27 assert(rist_pacer_rate_bps(&r) == 0); /* nothing known yet: do not pace */
28
29 /* Six seconds of exact-rate traffic, fed one datagram at a time. */
30 uint64_t step_us = (uint64_t)DGRAM_BYTES * 8ULL * 1000000ULL / RATE_BPS;
31 for (int i = 0; i < 6 * 1000000 / (int)step_us; i++) {
32 now_us += step_us;
33 rist_pacer_rate_add(&r, DGRAM_BYTES, now_us);
34 }
35
36 uint64_t bps = rist_pacer_rate_bps(&r);
37 double err = ((double)bps - (double)RATE_BPS) / (double)RATE_BPS;
38 if (err < 0)
39 err = -err;
40 assert(err < 0.01);
41 printf(" rate converged to %llu bps (want %llu, err %.3f%%)\n",
42 (unsigned long long)bps, (unsigned long long)RATE_BPS, err * 100.0);
43 }
44
45 static void test_rate_freezes_across_outage(void)
46 {
47 struct rist_pacer_rate r;
48 uint64_t now_us = 1000000;
49
50 rist_pacer_rate_init(&r, RATE_BPS);
51 uint64_t before = rist_pacer_rate_bps(&r);
52 assert(before == RATE_BPS);
53
54 /* Data resuming 30 s later must not average in as a near-zero rate. */
55 rist_pacer_rate_add(&r, DGRAM_BYTES, now_us);
56 now_us += 30000000;
57 rist_pacer_rate_add(&r, DGRAM_BYTES, now_us);
58
59 assert(rist_pacer_rate_bps(&r) == before);
60 printf(" rate held at %llu bps across a 30 s outage\n",
61 (unsigned long long)before);
62 }
63
64 static void test_interval_ignores_sleep_floor(void)
65 {
66 struct rist_pacer p;
67
68 /* Clamping a 148 us interval up to a 250 us floor would pace at 42 Mbps
69 * instead of 71: smooth-looking output, 40% too slow. */
70 rist_pacer_init(&p, 0.0, 250000, 5000000);
71
72 uint64_t interval = rist_pacer_interval_ns(&p, RATE_BPS, DGRAM_BYTES, 0);
73 uint64_t want = (uint64_t)DGRAM_BYTES * 8ULL * 1000000000ULL / RATE_BPS;
74 assert(interval == want);
75 assert(interval < p.min_sleep_ns);
76
77 assert(rist_pacer_interval_ns(&p, 0, DGRAM_BYTES, 0) == 0);
78 printf(" interval stayed at %llu ns, below the %llu ns sleep floor\n",
79 (unsigned long long)interval, (unsigned long long)p.min_sleep_ns);
80 }
81
82 static void test_feedback_direction_and_clamp(void)
83 {
84 struct rist_pacer p, raw;
85 rist_pacer_init(&p, 40.0, 0, 5000000);
86 rist_pacer_init(&raw, 0.0, 0, 5000000);
87
88 /* Reference needs feedback off: occupancy 0 is itself a large fill trim. */
89 uint64_t base = rist_pacer_interval_ns(&raw, RATE_BPS, DGRAM_BYTES, 0);
90
91 /* Target fill is 40 ms at this rate, i.e. ~271 datagrams. */
92 double target_dg = ((double)RATE_BPS / 8.0 * 0.040) / (double)DGRAM_BYTES;
93 uint64_t at_target = rist_pacer_interval_ns(&p, RATE_BPS, DGRAM_BYTES,
94 (size_t)target_dg);
95 uint64_t over = rist_pacer_interval_ns(&p, RATE_BPS, DGRAM_BYTES,
96 (size_t)(target_dg * 4));
97 uint64_t under = rist_pacer_interval_ns(&p, RATE_BPS, DGRAM_BYTES, 0);
98
99 /* At target the trim is a no-op; above it drains, below it fills. */
100 double at_err = ((double)at_target - (double)base) / (double)base;
101 if (at_err < 0)
102 at_err = -at_err;
103 assert(at_err < 0.01);
104 assert(over < at_target);
105 assert(under > at_target);
106
107 /* Both directions clamped, so no queue excursion can slew the output rate
108 * more than 20% away from the measured one. */
109 assert(over >= (uint64_t)((double)base * RIST_PACER_FEEDBACK_MIN) - 1);
110 assert(under <= (uint64_t)((double)base * RIST_PACER_FEEDBACK_MAX) + 1);
111 printf(" feedback: %llu ns at target (raw %llu), %llu when full, %llu when empty\n",
112 (unsigned long long)at_target, (unsigned long long)base,
113 (unsigned long long)over, (unsigned long long)under);
114 }
115
116 static void test_quota_averages_the_true_rate(void)
117 {
118 struct rist_pacer p;
119 rist_pacer_init(&p, 0.0, 250000, 5000000);
120
121 uint64_t interval = rist_pacer_interval_ns(&p, RATE_BPS, DGRAM_BYTES, 0);
122 uint64_t now_ns = 1000000000ULL;
123 uint64_t released = 0;
124 const int wakes = 4000; /* 1 s of wakes at the 250 us floor */
125
126 for (int i = 0; i < wakes; i++) {
127 released += rist_pacer_quota(&p, now_ns, interval, 64);
128 now_ns += 250000;
129 }
130
131 /* A floor coarser than the interval must not throttle: the batch absorbs it. */
132 double got_bps = (double)released * DGRAM_BYTES * 8.0;
133 double err = (got_bps - (double)RATE_BPS) / (double)RATE_BPS;
134 if (err < 0)
135 err = -err;
136 assert(err < 0.01);
137 printf(" quota over %d wakes released %llu datagrams = %.2f Mbps (err %.3f%%)\n",
138 wakes, (unsigned long long)released, got_bps / 1e6, err * 100.0);
139 }
140
141 static void test_quota_ungated_without_an_estimate(void)
142 {
143 struct rist_pacer p;
144 rist_pacer_init(&p, 0.0, 250000, 5000000);
145
146 /* No rate means no authority to hold anything back. */
147 assert(rist_pacer_quota(&p, 1000000000ULL, 0, 32) == 32);
148 printf(" no estimate: quota passes everything through\n");
149 }
150
151 static void test_slip_resyncs_without_catching_up(void)
152 {
153 struct rist_pacer p;
154 rist_pacer_init(&p, 0.0, 250000, 5000000);
155
156 uint64_t interval = rist_pacer_interval_ns(&p, RATE_BPS, DGRAM_BYTES, 0);
157 uint64_t now_ns = 1000000000ULL;
158
159 rist_pacer_quota(&p, now_ns, interval, 64);
160 assert(p.underruns == 0);
161
162 /* Draining 2 s of arrears would be ~13500 datagrams in one wake. */
163 now_ns += 2000000000ULL;
164 unsigned n = rist_pacer_quota(&p, now_ns, interval, 64);
165 assert(p.underruns == 1);
166 assert(n <= 1);
167 printf(" 2 s stall: resynced with %u datagram(s), %llu underrun(s)\n",
168 n, (unsigned long long)p.underruns);
169
170 /* Lateness inside the window is drift, to be corrected, not a resync. */
171 uint64_t before = p.underruns;
172 now_ns += RIST_PACER_MAX_SLIP_NS / 2;
173 rist_pacer_quota(&p, now_ns, interval, 64);
174 assert(p.underruns == before);
175 printf(" lateness inside the slip window did not resync\n");
176 }
177
178 static void test_sleep_clamped_both_ends(void)
179 {
180 struct rist_pacer p;
181 rist_pacer_init(&p, 0.0, 250000, 5000000);
182 uint64_t now_ns = 1000000000ULL;
183
184 /* Idle, nothing scheduled: wait the normal idle interval. */
185 assert(rist_pacer_sleep_ns(&p, now_ns) == 5000000);
186
187 /* Sooner than the floor: floored, bounding the wakeup rate. */
188 rist_pacer_due_ns(&p, now_ns);
189 rist_pacer_advance(&p, 148000);
190 assert(rist_pacer_sleep_ns(&p, now_ns) == 250000);
191
192 /* A far-future due time still respects the ceiling. */
193 rist_pacer_reset(&p);
194 rist_pacer_due_ns(&p, now_ns);
195 rist_pacer_advance(&p, 900000000ULL);
196 assert(rist_pacer_sleep_ns(&p, now_ns) == 5000000);
197
198 /* Already due: wake as soon as the floor allows. */
199 rist_pacer_reset(&p);
200 rist_pacer_due_ns(&p, now_ns);
201 assert(rist_pacer_sleep_ns(&p, now_ns + 1000) == 250000);
202
203 printf(" sleep clamped to [250 us, 5 ms] in all four cases\n");
204 }
205
206 static void test_reset_keeps_configuration(void)
207 {
208 struct rist_pacer p;
209 rist_pacer_init(&p, 40.0, 250000, 5000000);
210
211 uint64_t now_ns = 1000000000ULL;
212 rist_pacer_due_ns(&p, now_ns);
213 rist_pacer_advance(&p, 148000);
214 assert(p.next_due_ns != 0);
215
216 rist_pacer_reset(&p);
217 assert(p.next_due_ns == 0);
218 assert(p.min_sleep_ns == 250000 && p.max_sleep_ns == 5000000);
219 assert(p.target_latency_ms == 40.0);
220
221 /* A known discontinuity must not be charged as an underrun. */
222 uint64_t before = p.underruns;
223 rist_pacer_due_ns(&p, now_ns + 10000000000ULL);
224 assert(p.underruns == before);
225 printf(" reset re-anchors the schedule without counting an underrun\n");
226 }
227
228 /* A replay of the receiver's output gate over a bursty source, where the whole of
229 * a burst shares one release deadline. Modelled in the units the gate uses, so a
230 * regression shows up as a batch - the symptom that matters - rather than as a
231 * changed constant. */
232 #define GATE_WAKE_NS 107000ULL /* measured output-loop wake gap */
233 #define GATE_HOLD_NS 10000000ULL /* 2 x the 5 ms wake ceiling */
234 #define GATE_BURST 12
235 #define GATE_QUEUE 64 /* undrained bursts; one is the observed depth */
236
237 static void gate_replay(uint64_t hold_ns, int reanchor_when_idle,
238 unsigned duty, unsigned *max_per_wake, double *out_bps)
239 {
240 struct rist_pacer p;
241 rist_pacer_init(&p, 0.0, 250000, 5000000);
242
243 uint64_t interval = (uint64_t)DGRAM_BYTES * 8ULL * 1000000000ULL / RATE_BPS;
244 uint64_t now = 1000000000ULL;
245 uint64_t next_arrival = now;
246 unsigned released = 0, worst = 0;
247 const unsigned wakes = 200000;
248
249 /* Deadlines rather than a count, because a burst carries one between all of
250 * it and a packet stays overdue against it across however many wakes it
251 * waits - which is what a tight ceiling eventually trips on. */
252 uint64_t due[GATE_QUEUE];
253 unsigned left[GATE_QUEUE];
254 unsigned qh = 0, qt = 0;
255
256 for (unsigned w = 0; w < wakes; w++, now += GATE_WAKE_NS) {
257 while (next_arrival <= now) {
258 assert(qt - qh < GATE_QUEUE);
259 due[qt & (GATE_QUEUE - 1)] = next_arrival;
260 left[qt & (GATE_QUEUE - 1)] = GATE_BURST;
261 qt++;
262 next_arrival += duty * GATE_BURST * interval;
263 }
264
265 unsigned this_wake = 0;
266 int stopped_on_pacer = 0;
267 while (qh != qt) {
268 uint64_t head_due = due[qh & (GATE_QUEUE - 1)];
269 uint64_t overdue = now > head_due ? now - head_due : 0;
270 if (rist_pacer_due_ns(&p, now) > now) {
271 if (overdue < hold_ns) {
272 stopped_on_pacer = 1;
273 break;
274 }
275 rist_pacer_reset(&p);
276 rist_pacer_due_ns(&p, now);
277 }
278 rist_pacer_advance(&p, interval);
279 if (--left[qh & (GATE_QUEUE - 1)] == 0)
280 qh++;
281 this_wake++;
282 released++;
283 }
284 if (reanchor_when_idle && !stopped_on_pacer)
285 rist_pacer_reset(&p);
286
287 if (w > wakes / 10 && this_wake > worst)
288 worst = this_wake;
289 }
290
291 *max_per_wake = worst;
292 *out_bps = (double)released * DGRAM_BYTES * 8.0
293 / ((double)wakes * GATE_WAKE_NS / 1e9);
294 }
295
296 static void test_burst_spreads_at_the_paced_cadence(void)
297 {
298 unsigned worst;
299 double bps;
300
301 /* Both rules in force: the burst leaves one datagram at a time, and the
302 * spreading costs nothing in throughput. */
303 gate_replay(GATE_HOLD_NS, 1, 1, &worst, &bps);
304 assert(worst == 1);
305 double err = (bps - (double)RATE_BPS) / (double)RATE_BPS;
306 if (err < 0)
307 err = -err;
308 assert(err < 0.01);
309 printf(" burst of %d spread to %u datagram per wake at %.2f Mbps\n",
310 GATE_BURST, worst, bps / 1e6);
311
312 /* A ceiling under the drain time re-anchors mid-burst, and the rest of the
313 * burst follows it out in the same wake. */
314 gate_replay(1000000ULL, 1, 1, &worst, &bps);
315 assert(worst > 1);
316 printf(" ceiling below the drain released %u at once\n", worst);
317
318 /* A source that pauses between bursts leaves the schedule behind the clock.
319 * Re-anchored it still spreads; left running it spends the gap at once. */
320 gate_replay(GATE_HOLD_NS, 1, 2, &worst, &bps);
321 assert(worst == 1);
322 printf(" gapped source, re-anchored: still %u per wake\n", worst);
323
324 gate_replay(GATE_HOLD_NS, 0, 2, &worst, &bps);
325 assert(worst > 1);
326 printf(" gapped source, schedule left running: %u at once\n", worst);
327 }
328
329 /* A second, independent implementation of the schedule, so the whole sequence of
330 * send times can be compared rather than individual calls. Deliberately not
331 * factored against the module: a shared helper would defeat the purpose. */
332 #define REF_TARGET_LATENCY_MS 40.0
333 #define REF_FEEDBACK_K 0.5
334 #define REF_MAX_SLIP_NS 50000000ULL
335
336 static uint64_t ref_feedback(uint64_t interval_ns, size_t occ, uint64_t rate, size_t len)
337 {
338 double target_bytes = (double)rate / 8.0 * (REF_TARGET_LATENCY_MS / 1000.0);
339 double target_dg = target_bytes / (double)(len ? len : 1);
340 if (target_dg < 1.0)
341 target_dg = 1.0;
342 double err = ((double)occ - target_dg) / target_dg;
343 double factor = 1.0 - REF_FEEDBACK_K * err;
344 if (factor < 0.80)
345 factor = 0.80;
346 if (factor > 1.20)
347 factor = 1.20;
348 return (uint64_t)((double)interval_ns * factor);
349 }
350
351 static void test_matches_reference_implementation(void)
352 {
353 struct rist_pacer p;
354 rist_pacer_init(&p, REF_TARGET_LATENCY_MS, 0, 0);
355
356 uint64_t ref_next = 0, ref_underruns = 0;
357 uint64_t now_ns = 5000000000ULL;
358 unsigned compared = 0;
359
360 for (int i = 0; i < 20000; i++) {
361 /* Sweeps both feedback clamps, a rate step, a no-estimate stretch and a
362 * gap past the slip threshold. */
363 size_t occ = (size_t)((i * 37) % 600);
364 size_t len = DGRAM_BYTES;
365 uint64_t rate = (i > 500 && i < 700) ? 0
366 : (i < 9000 ? RATE_BPS : RATE_BPS + 250000);
367 if (i == 12000)
368 now_ns += 3 * RIST_PACER_MAX_SLIP_NS;
369
370 /* --- reference --- */
371 uint64_t ref_due = 0;
372 int ref_paced = 0;
373 if (rate == 0) {
374 ref_next = 0;
375 } else {
376 uint64_t iv = ((uint64_t)len * 8ULL * 1000000000ULL) / rate;
377 iv = ref_feedback(iv, occ, rate, len);
378 if (ref_next == 0) {
379 ref_next = now_ns;
380 } else if (now_ns > ref_next + REF_MAX_SLIP_NS) {
381 ref_next = now_ns;
382 ref_underruns++;
383 }
384 ref_due = ref_next;
385 ref_next += iv;
386 ref_paced = 1;
387 }
388
389 /* --- module --- */
390 uint64_t mod_due = 0;
391 int mod_paced = 0;
392 uint64_t iv = rist_pacer_interval_ns(&p, rate, len, occ);
393 if (iv == 0) {
394 rist_pacer_reset(&p);
395 } else {
396 mod_due = rist_pacer_due_ns(&p, now_ns);
397 rist_pacer_advance(&p, iv);
398 mod_paced = 1;
399 }
400
401 assert(mod_paced == ref_paced);
402 if (ref_paced) {
403 assert(mod_due == ref_due);
404 compared++;
405 }
406
407 /* Advance as a real sender would: to the due time it just honoured. */
408 now_ns = (ref_paced && ref_due > now_ns) ? ref_due + 1000 : now_ns + 148000;
409 }
410
411 assert(p.underruns == ref_underruns);
412 assert(ref_underruns >= 1);
413 printf(" reference cross-check: %u send times identical, "
414 "%llu underrun(s) both ways\n",
415 compared, (unsigned long long)ref_underruns);
416 }
417
418 int main(void)
419 {
420 printf("shared CBR pacing scheduler\n");
421 test_rate_converges();
422 test_rate_freezes_across_outage();
423 test_interval_ignores_sleep_floor();
424 test_feedback_direction_and_clamp();
425 test_quota_averages_the_true_rate();
426 test_quota_ungated_without_an_estimate();
427 test_slip_resyncs_without_catching_up();
428 test_sleep_clamped_both_ends();
429 test_reset_keeps_configuration();
430 test_burst_spreads_at_the_paced_cadence();
431 test_matches_reference_implementation();
432 printf("all pacer invariants held\n");
433 return 0;
434 }