Repository navigation
Put out-of-order sample datagrams back in order - #217
Conversation
1cddabb to
d0a43ca
Compare
About every 1.111 s the device sends its protocol-monitor packet in a datagram of its own, and when that falls partway through a millisecond of samples, the millisecond is split around it (e.g. 27 samples, the monitor, then the remaining 3 a few microseconds later); the split point cycles with an 11.1 s period. On Windows the small trailing datagram is occasionally delivered to sockets before the larger one, although the wire order is correct; Central sees the same reordering. Sample timestamps then step backwards about once a minute. Reported to Blackrock so a future firmware release can avoid the split. cbdev's receive thread now passes each datagram through a SampleReorderBuffer. A datagram is held when, for some sample group it carries, its first sample comes after a gap AND it spans under 0.5 ms -- the tail of a split whose head has not arrived. The head is delivered first when it arrives, then the tail, then anything queued behind it. In-order traffic is never held; a hold also ends on other sample data (a real gap) or after ConnectionParams::reorder_hold_us (3 ms; 0 disables). Groups are tracked independently, so group 6 alongside a slower group 1-4 works, and spikes and config packets never trigger a hold. Restorations are reported on stderr like packet loss; CERELINK_REORDER_STATS=1 prints a per-session summary. Measured on HUB1 (8 ch x 30 kHz, Release): 6 holds in ~496k datagrams over 8 minutes, each ended by the late datagram 23-27 us later; 0 reorders reached the application. Cost: 0.2-0.5 us per datagram (~0.02-0.05% of a core per device) in a microbenchmark; not separable from noise end to end. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The split this fixes is head, monitor, tail on the wire; with the tail delivered first, its head is at most two datagrams behind it. So a held tail now waits for at most ConnectionParams::reorder_window (2) more datagrams: the late head is delivered first if it comes, otherwise everything is released in arrival order. An idle socket (receive timeout) also releases. This drops the 3 ms deadline (reorder_hold_us), the select() wait in the receive loop, the "other samples end the hold" rule and the queue cap. Holds can no longer outlast a lost head by more than the window. CERELINK_REORDER_STATS now reports releases by head / count / idle and the most datagrams a late head arrived after its tail. Tests cover a head two datagrams behind, real loss released after the window, idle release, and all six sample groups active at once. Co-Authored-By: Claude <[email protected]>
d0a43ca to
9b78c66
Compare
Windows bench test: instructions for the agent on the Windows machineGoal: re-measure the count-based reorder buffer (commit 1. Build
2. Run the test scriptSave the script at the bottom of this comment as
3. What to report, per run
4. Pass criteria
macOS results for comparison (this branch, HUB1 + HUB2 + NSP connected)
macOS reorders too, but rarely. It showed no real sample loss even at full load, so the lost-head path has only been exercised by unit tests. The
|
Windows bench results for
|
| Run | Device | Samples | Backwards | Gaps | Missing | Holds (head / count / idle) | head_wait_max |
|---|---|---|---|---|---|---|---|
| 1: HUB1 8 ch RAW, 120 s | HUB1 | 3,599,974 | 0 | 0 | 0 | 0 (0 / 0 / 0) | 0 |
| 2: HUB1 8 ch RAW, 600 s | HUB1 | 17,999,701 | 0 | 0 | 0 | 0 (0 / 0 / 0) | 0 |
| 3: HUB1 256 + HUB2 256 + NSP 16 AIN, 600 s | HUB1 | 17,999,729 | 0 | 0 | 0 | 0 (0 / 0 / 0) | 0 |
| HUB2 | 18,000,565 | 0 | 0 | 0 | 3 (3 / 0 / 0) | 1 | |
| NSP | 18,001,377 | 0 | 0 | 0 | 0 (0 / 0 / 0) | 0 | |
| 4: same as 3, with Central 7.8 running | HUB1 | 17,999,764 | 0 | 0 | 0 | 0 (0 / 0 / 0) | 0 |
| HUB2 | 18,000,687 | 0 | 0 | 0 | 3 (3 / 0 / 0) | 1 | |
| NSP | 18,001,563 | 0 | 0 | 0 | 0 (0 / 0 / 0) | 0 |
app_queue_dropped=0 on every device in every run.
Pass criteria
backwards=0everywhere: no reorder reached the app.head_wait_max≤ 1; never 3 or more.count=0everywhere, withgaps=0, so the window never cut a hold short.- No false holds. Note, though, that the 8-channel runs had no reorders at all, so they didn't exercise the buffer.
- No real loss at full load. The lost-head path is still exercised only by unit tests, as on macOS.
Stderr
[cbdev] put N out-of-order sample datagrams back in order: 3 restorations per full-load run, all on HUB2 (port 51003).[cbdev] dropped N of M:- throughout every run at 0.0000–0.0047%, plus one 0.098% line early in run 3 (33 of 33,550);
- all while the RAW streams were gap-free, as on macOS.
Follow-up: run 5, group 5 (SR_30kHz)
To check whether the reorder rate depends on the sample group, I repeated run 2 in group 5. Same script, with a --rate option added to a copy so set_sample_group and on_group_batch use SampleRate.SR_30kHz. HUB2 and the NSP were idle, and Central was closed.
| Run | Device | Samples | Backwards | Gaps | Missing | Holds (head / count / idle) | head_wait_max |
|---|---|---|---|---|---|---|---|
| 5: HUB1 8 ch group 5, 600 s | HUB1 | 17,999,730 | 0 | 0 | 0 | 9 (9 / 0 / 0) | 1 |
- 9 restorations in 10 minutes, all ended by the late head, none reaching the app. That's the same rate as before
9b78c66on this bench (6–11 per 10 minutes in group 5). head_wait_max=1andcount=0, so the 2-datagram window held with room to spare.- The reorder rate depends on the sample group. HUB1 at 8 channels had 0 reorders in 12 minutes of RAW (group 6) and 9 in 10 minutes of group 5. So runs 1–2 passed without exercising the buffer, and run 5 is the HUB1 run that does.
- Stderr: one
[cbdev] dropped 104 of 1008748 packets (0.0103%)line, with no sample gaps.
🤖 Generated with Claude Code
Stacked on #215 (base:
feat/native-default-central-deprecated). Follow-up to #213.Problem
About once a minute, sample timestamps step backwards by ~29 samples at 30 kHz. Nothing is lost; two datagrams arrive in the wrong order. Central sees exactly the same reordering, and it happens with or without Central running.
Cause, as seen on the wire: the device normally sends one datagram per millisecond of samples. Its protocol-monitor packet goes out in a datagram of its own about every 1.111 s (33,333 sample periods). When that falls partway through a millisecond, the millisecond's samples are split around it: e.g. 27 samples, then the monitor, then the remaining 3 samples a few microseconds later.
The monitor interval isn't a whole number of milliseconds (33,333 = 30 × 1111 + 3 sample periods), so the split point moves by 3 samples each time and the pattern repeats every 10 monitors, i.e. every 11.11 s. The 27 + 3 split, a 3-sample datagram ~3 µs behind a 27-sample one, is the one Windows occasionally delivers tail-first, although an Npcap capture shows the wire order is correct. Reported to Blackrock so a future firmware release can avoid the split.
Host-side settings didn't help:
DisableDatagramProcessing(itswindefend_datagram_v4WFP callout stays installed regardless);This adapter (Realtek 2.5GbE, Microsoft inbox driver) has no RSS.
Fix
cbdev::SampleReorderBuffer(newsrc/cbdev/src/sample_reorder.h) sits betweenreceivePackets()and callback dispatch in cbdev's receive thread.ConnectionParams::reorder_window(default 2; 0 disables) more datagrams, everything is released in arrival order; the gap was real loss.select(): holds are bounded by datagram count, so a lost head can never hold data longer than the window.CERELINK_REORDER_STATS=1prints a per-session summary: datagrams, holds, what ended each hold (head / count / idle), and the most datagrams a late head arrived after its tail.Measurements (HUB1, 8 ch × 30 kHz)
-O2): 0.2–0.5 µs per datagram, ~0.02–0.05% of a core per device at 1,000 datagrams/s. The 256-channel figure is with cold-cache synthetic data.simple_device, 8 alternating A/B rounds: 1.88% vs 2.13% of a core. The paired 95% CI is −0.07 to +0.56 points, i.e. not separable from noise.Tests
SampleReorder.*unit tests (synthetic datagrams). They cover:The measurements above were taken with the earlier time-based (3 ms) hold. Every hold then ended by the late head 23–27 µs after the tail, well before the next millisecond's datagram, which the count-based rule also covers. The count-based version has not been re-measured on Windows.
Not covered: a spike-only configuration (no sample group enabled). The monitor still splits datagrams there, but the detector needs sample timestamps.
🤖 Generated with Claude Code