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);
|
||||
|
||||
@@ -54,8 +54,11 @@ private:
|
||||
bool lastAccValid = false;
|
||||
uint32_t prevAccCount = 0;
|
||||
/** For accumulated scalars with a declared sampling rate: end timestamp
|
||||
* of the most recently emitted burst, used as a forward-chain anchor
|
||||
* that is immune to arrival-time jitter. */
|
||||
* of the most recently emitted burst. The next burst is PREDICTED to
|
||||
* start one sample period after it — immune to arrival-time jitter —
|
||||
* but the prediction is discarded when arrival time disagrees with it
|
||||
* by more than a delivery backlog can explain, so packet loss cannot
|
||||
* displace the trace permanently. */
|
||||
double lastEmittedEnd = 0.0;
|
||||
bool lastEmittedValid = false;
|
||||
};
|
||||
|
||||
@@ -177,6 +177,45 @@ TEST(FrameDecoder, AccumulatedScalarSurvivesBurstyDelivery) {
|
||||
}
|
||||
}
|
||||
|
||||
// The counterweight to the test above. Suppressing arrival jitter by chaining
|
||||
// each burst onto the previous one is only safe while the chain is checked: on
|
||||
// UDP, packets are lost, and a chain that ignores arrival entirely closes the
|
||||
// hole silently and dates every later sample a full second early — for the rest
|
||||
// of the run, because nothing ever pulls it back. The prediction has to be
|
||||
// abandoned once arrival contradicts it by more than a delivery backlog could.
|
||||
TEST(FrameDecoder, AccumulatedScalarResynchronisesAfterLostPackets) {
|
||||
FrameDecoder dec;
|
||||
SignalMeta m;
|
||||
m.name = "Acc";
|
||||
m.typeCode = 9;
|
||||
m.numRows = 1;
|
||||
m.samplingRate = 1000.0; /* 10 samples = 10 ms per packet */
|
||||
dec.setSignals({m});
|
||||
|
||||
std::vector<double> ts;
|
||||
for (int p = 0; p < 10; p++) {
|
||||
FrameBuilder fb;
|
||||
fb.addSignal(std::vector<double>(10, 1.0));
|
||||
const FrameView& f = fb.build(0, 500.0 + p * 0.010, 10);
|
||||
dec.beginFrame(f);
|
||||
ASSERT_TRUE(dec.timestamps(f, 0, ts));
|
||||
}
|
||||
/* Contiguous so far: burst 9 ends at 500.090. */
|
||||
EXPECT_NEAR(ts[9], 500.090, 1e-9);
|
||||
|
||||
/* A full second of packets never arrives. The next one lands at 501.100. */
|
||||
FrameBuilder fb;
|
||||
fb.addSignal(std::vector<double>(10, 1.0));
|
||||
const FrameView& f = fb.build(0, 501.100, 10);
|
||||
dec.beginFrame(f);
|
||||
ASSERT_TRUE(dec.timestamps(f, 0, ts));
|
||||
|
||||
/* Chaining blindly would put this burst at 500.091..500.100, overlapping
|
||||
* the gap as though no data were missing. */
|
||||
EXPECT_NEAR(ts[0], 501.091, 1e-9);
|
||||
EXPECT_NEAR(ts[9], 501.100, 1e-9);
|
||||
}
|
||||
|
||||
TEST(FrameDecoder, AccumulatedScalarDerivesDtFromTheHrtGapWhenNoRateIsDeclared) {
|
||||
FrameDecoder dec;
|
||||
SignalMeta m;
|
||||
|
||||
Reference in New Issue
Block a user