fix(udpscope): reconstruct lost accumulated bursts from the packet counter
Review found the decoder was estimating something the wire states exactly. FrameView::counter increments once per update, so a gap of g means g-1 lost datagrams; reinstating their duration restores the hole precisely, with no threshold and no dependence on arrival time. The arrival-anchor comparison survives only as a backstop for what the counter cannot express — a producer restart, a counter stuck at zero, a wrong declared rate — and can no longer step a signal's timestamps backwards, which the ring and trigger forbid. Also from review: guard the time-signal lookup against a frame carrying more signals than the installed table, and give FrameBuilder a counter parameter. Leaving it at zero had hidden the counter rules from every test, and made the hrt-gap test vacuous — under uniform arrivals the hrt path and packetBurst agree by construction, so it could not tell which branch answered. Its arrivals now carry zero-mean jitter. Each new assertion was proven non-vacuous by sabotage: dropping the gap term, the backward guard, or the hrt branch fails exactly its own test. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
892e3eae28
commit
7102412a9f
@@ -8,15 +8,17 @@ namespace udpscope {
|
||||
static constexpr double kDefaultDt = 1.0e-3;
|
||||
|
||||
/**
|
||||
* How far the forward-chained prediction for an accumulated burst may sit from
|
||||
* where arrival time says it should be before the chain is abandoned.
|
||||
* How far a chained burst prediction may sit from where arrival time says it
|
||||
* should be before the chain is abandoned and time is re-anchored on arrival.
|
||||
*
|
||||
* A kernel draining a backlog of queued datagrams can legitimately put the
|
||||
* prediction a few hundred milliseconds ahead of arrival, so the threshold has
|
||||
* to be well clear of that. Anything larger is not delivery jitter: it is lost
|
||||
* packets or a declared sampling rate that does not match the producer's real
|
||||
* one, and both must resynchronise rather than accumulate forever. Same value
|
||||
* and same reasoning as ClockOffset::kRecalibThresholdS.
|
||||
* This is a backstop, not the primary mechanism: the packet counter normally
|
||||
* accounts for lost datagrams exactly, so the prediction and arrival agree.
|
||||
* It catches what the counter cannot describe — a producer restart (the
|
||||
* counter returns to zero), a counter that never advances, and a declared
|
||||
* sampling rate that does not match the producer's real one. A kernel draining
|
||||
* a backlog of queued datagrams can legitimately put the prediction a couple of
|
||||
* hundred milliseconds from arrival, so the threshold sits well clear of that.
|
||||
* Same value and same reasoning as ClockOffset::kRecalibThresholdS.
|
||||
*/
|
||||
static constexpr double kBurstResyncThresholdS = 0.5;
|
||||
|
||||
@@ -59,7 +61,8 @@ bool FrameDecoder::packetBurst(uint32_t idx, uint32_t nElems, double wallNow,
|
||||
bool FrameDecoder::timestamps(const FrameView& f, uint32_t idx,
|
||||
std::vector<double>& tsOut) {
|
||||
tsOut.clear();
|
||||
if (idx >= signals_.size() || idx >= f.numSignals || f.counts == nullptr) {
|
||||
if (idx >= signals_.size() || idx >= f.numSignals ||
|
||||
f.counts == nullptr || f.values == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -70,7 +73,12 @@ bool FrameDecoder::timestamps(const FrameView& f, uint32_t idx,
|
||||
const double wallNow = f.recvTime;
|
||||
SigState& st = state_[idx];
|
||||
|
||||
const bool hasTimeSig = d.hasTimeSignal(f.numSignals);
|
||||
/* hasTimeSignal() bounds the index against the FRAME's signal count, but
|
||||
* the time signal's type code is read from our own table, whose size is
|
||||
* independent — a frame carrying more signals than the installed table
|
||||
* (briefly possible after a CONFIG change) would otherwise read past it. */
|
||||
const bool hasTimeSig = d.hasTimeSignal(f.numSignals) &&
|
||||
d.timeSignalIdx < signals_.size();
|
||||
const uint32_t tIdx = hasTimeSig ? d.timeSignalIdx : 0u;
|
||||
const double tScale = hasTimeSig
|
||||
? TimeSignalScale(signals_[tIdx].typeCode)
|
||||
@@ -127,27 +135,50 @@ bool FrameDecoder::timestamps(const FrameView& f, uint32_t idx,
|
||||
const double arrivalAnchor =
|
||||
wallNow - static_cast<double>(nElems - 1u) * dt;
|
||||
|
||||
/* Chaining from the end of the previous burst is immune to arrival
|
||||
/* Chaining onto the end of the previous burst is immune to arrival
|
||||
* jitter — a kernel draining several queued datagrams microseconds
|
||||
* apart still yields contiguous timestamps. But a pure chain is
|
||||
* blind: one lost datagram, or a declared rate that does not match
|
||||
* the producer's real one, displaces every later sample and never
|
||||
* recovers. So the chain is a PREDICTION, checked each packet
|
||||
* against arrival and abandoned when the two disagree by more than
|
||||
* a delivery backlog can explain. That bounds the error instead of
|
||||
* letting it accumulate. */
|
||||
* apart still yields contiguous timestamps. What a bare chain gets
|
||||
* wrong is loss: it closes the hole a dropped datagram left, and
|
||||
* every later sample is then dated early for the rest of the run.
|
||||
*
|
||||
* The wire says exactly how much is missing. counter increments
|
||||
* once per update, so a gap of g means g-1 lost packets, each
|
||||
* carrying (as far as we can tell) as many samples as the last one
|
||||
* we saw. Reinstating that duration keeps the chain honest without
|
||||
* consulting arrival time at all. */
|
||||
double base = arrivalAnchor;
|
||||
if (st.lastEmittedValid) {
|
||||
const double predicted = st.lastEmittedEnd + dt;
|
||||
/* Unsigned subtraction wraps, so this stays right across the
|
||||
* counter's own 2^32 rollover. */
|
||||
const uint32_t gap = f.counter - st.lastCounter;
|
||||
const double lost = (gap > 1u)
|
||||
? static_cast<double>(gap - 1u) *
|
||||
static_cast<double>(st.prevAccCount)
|
||||
: 0.0;
|
||||
const double predicted = st.lastEmittedEnd + dt * (1.0 + lost);
|
||||
|
||||
/* Backstop for what the counter cannot express: a producer
|
||||
* restart, a counter stuck at zero, or a declared rate that is
|
||||
* simply wrong. Beyond this the chain is not recoverable and
|
||||
* arrival time is the better of two bad answers. */
|
||||
if (std::fabs(predicted - arrivalAnchor) <= kBurstResyncThresholdS) {
|
||||
base = predicted;
|
||||
}
|
||||
/* Re-anchoring must never move time backwards: the ring, the
|
||||
* trigger and the exporter all assume a signal's timestamps
|
||||
* increase. A backward resync would be indistinguishable from
|
||||
* corruption downstream, so give up the correction instead. */
|
||||
if (base <= st.lastEmittedEnd) {
|
||||
base = st.lastEmittedEnd + dt;
|
||||
}
|
||||
}
|
||||
tsOut.resize(nElems);
|
||||
for (uint32_t e = 0; e < nElems; e++) {
|
||||
tsOut[e] = base + static_cast<double>(e) * dt;
|
||||
}
|
||||
st.lastEmittedEnd = tsOut[nElems - 1u];
|
||||
st.lastCounter = f.counter;
|
||||
st.prevAccCount = nElems;
|
||||
st.lastEmittedValid = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user