fix(udpscope): bound accumulated-burst chaining so packet loss cannot displace the trace
Forward-chaining each accumulated burst onto the previous one suppresses arrival jitter, but an unchecked chain never recovers: one lost datagram, or a declared sampling rate that differs from the producer's real one, dates every later sample early for the rest of the run. The chain is now a prediction, compared each packet against the arrival anchor and abandoned beyond kBurstResyncThresholdS, which bounds the error instead of accumulating it. Plan amended so the hrt-fit fallback (unusable here: the fit needs 32 packets and is itself corrupted by bursty arrivals) cannot come back. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
5a8479cda9
commit
892e3eae28
@@ -1,10 +1,25 @@
|
||||
#include "FrameDecoder.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace udpscope {
|
||||
|
||||
/** Fallback cycle period before the first inter-packet gap is known. */
|
||||
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.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
static constexpr double kBurstResyncThresholdS = 0.5;
|
||||
|
||||
void FrameDecoder::setSignals(const std::vector<SignalMeta>& signals) {
|
||||
signals_ = signals;
|
||||
state_.assign(signals_.size(), SigState{});
|
||||
@@ -107,21 +122,25 @@ bool FrameDecoder::timestamps(const FrameView& f, uint32_t idx,
|
||||
: 0.0;
|
||||
|
||||
if (d.samplingRate > 0.0) {
|
||||
/* Forward-chain anchor: t[0] = lastEnd + dt, or wallNow on first
|
||||
* packet (arrival-time for the very first burst only). */
|
||||
double base;
|
||||
/* Where arrival time says this burst begins: its last element was
|
||||
* acquired just before the packet landed. */
|
||||
const double arrivalAnchor =
|
||||
wallNow - static_cast<double>(nElems - 1u) * dt;
|
||||
|
||||
/* Chaining from 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. */
|
||||
double base = arrivalAnchor;
|
||||
if (st.lastEmittedValid) {
|
||||
base = st.lastEmittedEnd + dt;
|
||||
} else {
|
||||
/* First packet: anchor element 0 at arrival time.
|
||||
* This one packet may be slightly off, but subsequent packets
|
||||
* chain from this end and jitter is suppressed thereafter. */
|
||||
base = wallNow - static_cast<double>(nElems - 1u) * dt;
|
||||
/* Calibrate the clock-offset so later hrt-based paths (if any)
|
||||
* are consistent, but we don't use it in this branch. */
|
||||
if (f.hrt != 0u && hrtFit_.ready()) {
|
||||
const double hrtSec = hrtFit_.toSeconds(f.hrt);
|
||||
(void) st.offset.map(hrtSec, wallNow);
|
||||
const double predicted = st.lastEmittedEnd + dt;
|
||||
if (std::fabs(predicted - arrivalAnchor) <= kBurstResyncThresholdS) {
|
||||
base = predicted;
|
||||
}
|
||||
}
|
||||
tsOut.resize(nElems);
|
||||
|
||||
Reference in New Issue
Block a user