fix(udps): stop packets being dated from an earlier time base
Reported as samples sporadically carrying a previous packet's timestamp: holes on one side of the stream and collisions on the other, in both the Go and the MARTe2 receiver. That it appeared in both is what located it -- the shared cause is upstream of either client. Four independent defects, all of which end in a packet's values being placed at a time that is not theirs. Reassembly slot exhaustion (the "Reassembly slots full; evicting oldest" flood). Chunk size was learnt only from fragment 0, so an out-of-order burst destroyed a packet whose bytes had all arrived and left the slot occupied until the 2 s GC. Slots were keyed on the counter alone, but DATA and CONFIG number independently, so equal counters merged the two streams. The 32-byte received-mask covered 256 of the 512 fragments the client accepts, so a duplicate above 255 was counted as new and the packet was delivered with a hole of stale bytes in it. And one datagram was read per Execute(), which cannot drain a fast producer. Fixed with a pendingTail deferral, (counter, type) keying, a 64-byte mask, a 256-datagram drain, counter-age slot reclamation, and a 1 Hz aggregated warning in place of the per-eviction flood. UDPStreamer dropping whole Accumulate batches. EventSem::ResetWait is Reset-then-Wait, so a Post() landing while the sender thread was inside ServiceClients()/SendData() was destroyed by the next Reset. The batch was then skipped with dataReady false, readyFill was never cleared, and the following flush overwrote it: an entire run of RT cycles never reached the wire. The record of pending work now lives in the buffers rather than in the semaphore edge, which also removes up to UDPS_DATA_WAIT_MS of latency; genuine backpressure overwrites are counted and reported. Against the unfixed code the new test sees 2999/3000 batches never consumed. Period inflation after loss. Accumulated scalars carry no SamplingRate, so the receiver derives dt from the sender-clock gap -- but dividing it by the previous packet's sample count is only right while nothing is lost. One loss doubles the reported period, which spreads a batch a full batch past its own end and into the range the next packet claims. That is the hole and the collision, exactly. Inferring the cycle count from the estimate's own period is not a way out: it has a stable fixed point wherever gap/dt is an integer, so a real rate change locks it at the old one for good (AccumDtGTest.FollowsSustainedRateChange). The packet counter removes the ambiguity, so all three receivers now order on it: a DATA packet that does not advance the counter is dropped rather than delivered, because its values are older than data already handed over. Ordering is on the signed difference so it survives the uint32 wrap, and the sequence resets on reconnect, where the producer's counter restarts independently of ours. The loss count that falls out of the same delta feeds the period estimate as cycles = prevN * (1 + lost), which reduces exactly to gap/prevN when nothing is lost and therefore still tracks a genuine rate change. UDPSClient::AcceptDataCounter (C++), udpsprotocol.SequenceGate (Go), decode_data (C). The C client's existing gap counter was wrap-unsafe and let a stale packet rewind last_counter, which made every subsequent gap wrong; it uses the same code now. Docs/Protocol.md gains an Ordering DATA section stating the requirement for any receiver, including ones outside this repository. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
13fac79400
commit
deabd257e5
@@ -99,6 +99,8 @@ void UDPSourceSession::ResetCalibration() {
|
||||
lastPktWallValid_[i] = false;
|
||||
lastPktWallS_[i] = 0.0;
|
||||
accScalarPrevN_[i] = 0u;
|
||||
accScalarDtValid_[i] = false;
|
||||
accScalarDtEMA_[i] = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,7 +199,8 @@ void UDPSourceSession::OnUDPSConfig(const uint8 *payload, uint32 payloadSize) {
|
||||
}
|
||||
|
||||
void UDPSourceSession::OnUDPSData(const uint8 *payload, uint32 payloadSize) {
|
||||
ParseDataPayload(payload, payloadSize);
|
||||
/* Valid only for the duration of this callback. */
|
||||
ParseDataPayload(payload, payloadSize, client_.GetLastDataGap());
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
@@ -376,7 +379,8 @@ float64 UDPSourceSession::ProducerNewestTime() const {
|
||||
/* DATA parsing */
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
||||
void UDPSourceSession::ParseDataPayload(const uint8 *payload, uint32 size) {
|
||||
void UDPSourceSession::ParseDataPayload(const uint8 *payload, uint32 size,
|
||||
uint32 lostPackets) {
|
||||
if (size < 8u) { return; }
|
||||
|
||||
/* Copy metadata under lock */
|
||||
@@ -581,16 +585,25 @@ void UDPSourceSession::ParseDataPayload(const uint8 *payload, uint32 size) {
|
||||
}
|
||||
|
||||
/* Per-sample dt: samplingRate if present, else derive it from
|
||||
* the sender-HRT gap to the previous packet divided by that
|
||||
* packet's sample count (the flushes carry contiguous RT
|
||||
* cycles, so this is exactly one cycle period). */
|
||||
* the sender-HRT gap to the previous packet.
|
||||
*
|
||||
* The gap is divided by the number of RT cycles it actually
|
||||
* spans, not by the previous packet's sample count. Those two
|
||||
* agree only while nothing is lost; once a packet goes missing
|
||||
* the gap covers cycles the previous count never saw, and
|
||||
* dividing by that count inflates dt until this packet's
|
||||
* samples overrun into the next packet's range. lostPackets
|
||||
* comes from the producer's packet counter, so the divisor
|
||||
* widens with the gap and dt is unchanged. */
|
||||
float64 dt;
|
||||
if (desc.samplingRate > 0.0) {
|
||||
dt = 1.0 / desc.samplingRate;
|
||||
} else if (lastPktWallValid_[s] && (accScalarPrevN_[s] > 0u) &&
|
||||
(hrt0Sec > lastPktWallS_[s])) {
|
||||
dt = (hrt0Sec - lastPktWallS_[s]) /
|
||||
static_cast<float64>(accScalarPrevN_[s]);
|
||||
dt = UDPSEstimateAccumDt(hrt0Sec - lastPktWallS_[s],
|
||||
accScalarPrevN_[s], lostPackets,
|
||||
accScalarDtEMA_[s],
|
||||
accScalarDtValid_[s]);
|
||||
} else {
|
||||
dt = 1.0e-3; /* 1 kHz default until the gap is known */
|
||||
}
|
||||
|
||||
@@ -39,6 +39,69 @@ using MARTe::ConfigurationDatabase;
|
||||
/** Maximum number of signals per source session. */
|
||||
static const uint32 UDPSS_MAX_SIGNALS = 256u;
|
||||
|
||||
/* Accumulated-scalar dt estimator tuning. */
|
||||
/** Weight of a new observation; slow enough that one bad gap barely moves it. */
|
||||
static const float64 UDPSS_DT_EMA_ALPHA = 0.05;
|
||||
/** Observations outside [lo, hi] x the current estimate are treated as a
|
||||
* mis-counted gap and discarded rather than smoothed in. */
|
||||
static const float64 UDPSS_DT_ACCEPT_LO = 0.5;
|
||||
static const float64 UDPSS_DT_ACCEPT_HI = 2.0;
|
||||
|
||||
/**
|
||||
* @brief Per-sample period of an accumulated scalar packet, robust to loss.
|
||||
*
|
||||
* An Accumulate producer batches consecutive RT cycles, so the sender-clock
|
||||
* gap between two packets' first samples covers exactly as many cycles as the
|
||||
* earlier packet carried — but only while nothing is lost in between. Over UDP
|
||||
* (and with a producer that can overwrite a batch the sender never took) that
|
||||
* assumption fails, and dividing the gap by the previous packet's sample count
|
||||
* then inflates the period. The packet's own samples are laid out as
|
||||
* base + e*dt, so an inflated dt walks them past their real end and into the
|
||||
* span the next packet will claim: samples collide there and leave a hole
|
||||
* behind them.
|
||||
*
|
||||
* The number of packets that went missing is not guessed from the gap — that
|
||||
* is circular, and an estimator that infers the cycle count from its own
|
||||
* period has a stable fixed point wherever gap/dt is an integer, so a genuine
|
||||
* rate change locks it at the old period forever. It comes instead from the
|
||||
* UDPS packet counter, which the producer increments once per sent packet. The
|
||||
* gap then spans (1 + lost) batches, each assumed to be prevN cycles, and with
|
||||
* nothing lost the formula reduces exactly to gap/prevN.
|
||||
*
|
||||
* @param gap Sender-clock seconds since the previous packet's first sample.
|
||||
* Must be > 0.
|
||||
* @param prevN Samples in the previous packet. Must be > 0.
|
||||
* @param lost Packets missing between the previous packet and this one,
|
||||
* from the producer's counter.
|
||||
* @param[in,out] dtEMA Smoothed period. Seeded on the first call.
|
||||
* @param[in,out] dtValid False until dtEMA holds an estimate.
|
||||
* @return The period to space this packet's samples by.
|
||||
*/
|
||||
inline float64 UDPSEstimateAccumDt(const float64 gap, const uint32 prevN,
|
||||
const uint32 lost, float64 &dtEMA,
|
||||
bool &dtValid) {
|
||||
float64 cycles = static_cast<float64>(prevN) *
|
||||
(1.0 + static_cast<float64>(lost));
|
||||
if (cycles < 1.0) {
|
||||
cycles = 1.0;
|
||||
}
|
||||
const float64 dtObs = gap / cycles;
|
||||
|
||||
if (!dtValid) {
|
||||
dtEMA = dtObs;
|
||||
dtValid = true;
|
||||
} else if ((dtObs > (dtEMA * UDPSS_DT_ACCEPT_LO)) &&
|
||||
(dtObs < (dtEMA * UDPSS_DT_ACCEPT_HI))) {
|
||||
/* Track slow drift, but ignore observations far outside the current
|
||||
* estimate: those are the signature of a mis-counted gap, and folding
|
||||
* one in would drag the estimate towards the very error it exists to
|
||||
* absorb. */
|
||||
dtEMA = ((1.0 - UDPSS_DT_EMA_ALPHA) * dtEMA) +
|
||||
(UDPSS_DT_EMA_ALPHA * dtObs);
|
||||
}
|
||||
return dtEMA;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief One connected UDPStreamer source.
|
||||
*
|
||||
@@ -229,7 +292,13 @@ private:
|
||||
|
||||
/* DATA payload parsing */
|
||||
void ParseConfigPayload(const uint8 *payload, uint32 size);
|
||||
void ParseDataPayload(const uint8 *payload, uint32 size);
|
||||
/**
|
||||
* @param lostPackets DATA packets missing immediately before this one, from
|
||||
* the producer's counter; the accumulated-scalar period estimate
|
||||
* needs it to know how many cycles the sender-clock gap spans.
|
||||
*/
|
||||
void ParseDataPayload(const uint8 *payload, uint32 size,
|
||||
uint32 lostPackets);
|
||||
void AllocateRingBuffers();
|
||||
|
||||
/** @brief Invalidate all wall-clock calibration state (receive thread only). */
|
||||
@@ -401,6 +470,12 @@ private:
|
||||
float64 hrtFreq_;
|
||||
uint32 accScalarPrevN_[UDPSS_MAX_SIGNALS];
|
||||
|
||||
/* Per-signal state of UDPSEstimateAccumDt (see above): the smoothed
|
||||
* per-sample period for accumulated scalars whose descriptor carries no
|
||||
* SamplingRate. */
|
||||
float64 accScalarDtEMA_[UDPSS_MAX_SIGNALS];
|
||||
bool accScalarDtValid_[UDPSS_MAX_SIGNALS];
|
||||
|
||||
/* Scratch buffers for decoding arrays (receive thread only). */
|
||||
float64 *timeScratch_; ///< Time values scratch
|
||||
float64 *valScratch_; ///< Data values scratch
|
||||
|
||||
Reference in New Issue
Block a user