Two defects behind one report ("a trigger during a capture overrides the first,
and the hold-off does not apply"); the second is also what made a spike in one
channel look like the acquisition resetting.
The trigger engine keyed its invalidation on the trigger channel's *index*, which
every rebuild of the channel list renumbers -- enabling a signal that sorts before
it is enough. Any rebuild therefore abandoned an open capture and set no hold-off,
so the next edge inside the post window became t0. It keys on the channel's name
now, which changes only when the source genuinely does.
A packet whose stamps jump more than 256 packet spans ahead is now rejected as a
fault and *bridged* at the declared cadence, instead of poisoning the monotonic
mark (which pinned every later packet flat at the glitched value) and condemning
the next good packet. Only four consecutive failures abandon the declared time
signal, and that fallback no longer wipes the rings: the arrival clock is anchored
to continue from the monotonic mark, so the history is still on one timeline --
the wipe is what made one glitching packet look like the stream restarting from 0.
The declared unit is resolved before those checks, since they weigh the stamps
against a cadence in seconds.
Guards: UDPSCOPE_PROBE_TRIGRENUM_MS prints t0_held=1 through the real widgets,
and the mock's --time-glitch-at/--time-glitch-by injects the clock fault.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
12 KiB
12 KiB
Invariants: the data path — buffers, decimation and the rings
What rebuilds a buffer, why decimation is an average, and the two rules the time ring's precision and its first stamp impose.
Part of CLAUDE.md. These are rules that cost a defect to learn; read the ones touching the area you are editing before you edit it.
- Any structural change rebuilds the buffer and drops history: a CONFIG, an
enable/disable, a calibration change, or a change of display window that moves the
decimation factor. Calibration is applied at ingest, which is why it re-partitions.
ScopeModel::set_target_span()is the window's path in, and it re-plans only whentime_strideactually changes, so walking the window inside one decade is free; it asksplan_partition()rather than recomputing the factor, because two expressions for that decision is how the buffer and the view end up disagreeing about the stored rate. It takesmutex()itself — do not call it holding the lock. - The shared clock is decimated to fit the requested window; channels never are. A
window too long to store at the wire rate used to be refused (the control was capped to
Partition::span_s), which made a 600 s view of a 1 MS/s stream unreachable.plan_partition()now pickstime_stride, a power of two, after the growth loop has bought all the ring the budget allows — deciding it earlier over-decimates by whatever factor growth was about to add. Per-channelstride[i]stays 1: a stride ofround(dt_i / dt_fastest)keeps one sample per stride, and when that equals the signal's own period — a 2 kHz sine on a 1 MHz clock, stride 500 — every kept sample sits at the same phase and the channel plots flat. - Decimation at ingest is an average, never a stride.
Acquisition::on_data()reads and accumulates every sample and pushes the mean of each group oftime_stride(value_acc_/value_acc_n_, andtime_acc_for the clock). That boxcar's first null is at the stored rate, so content that would fold back is attenuated instead of reappearing as a false low frequency — which is the whole reason decimation was once dropped entirely. Skipping the read for unstored samples re-introduces the aliasing; theUDPSCOPE_PROBE_WINDOW_MSrun under--freq 2000is the guard (the anti-alias check in probe-readings.md). The stamp pushed is the group's mean, not its last member: that is where the boxcar's group delay puts the value, and stamping at the end of the group shifts every trace half a stored sample late. The accumulators are reset whenevertime_strideor the channel count changes, so a re-plan starts a fresh group rather than finishing one at the wrong length. The LOD min/max pyramid still preserves the extremes of what is stored; a spike narrower than a group is now filtered away rather than aliased, which is the correct trade and is why the status line'sratefield is worth reading. - One bad packet is bridged; only a run of them changes the time source. The stream-time
checks in
build_stamps()reject three things: stamps that are not finite, a packet with no span at all, and a packet whose stamps do not advance past the previous packet's last. A fourth was added because a real device's clock glitches: a packet whose stamps jump more than 256 packet spans ahead is a fault, not a gap, and must be rejected — letting it through poisonslast_stamp_, and the monotonic clamp (max(stamps[k] - epoch, last_stamp_)) then pins every later packet flat at the glitched value, so the whole stream freezes at one x and never recovers. A rejected packet is bridged: its stamps are replaced by a continuation of the accepted timeline at the declared cadence, so neither the monotonic mark norlast_stream_anchor_is poisoned and the next good packet is accepted normally. Believing the glitch instead is what condemned the next, perfectly good packet (it now sits below the poisoned anchor, so it fails the advance test) and threw the whole stream onto the arrival clock. Only four consecutive rejected packets abandon the declared time signal, because that is what a signal which is not a time base at all looks like; a first packet with nothing to bridge from still falls back immediately. The unit scale ('ns', 'us', 'ms') is therefore resolved before these checks rather than after: they weigh the stamps against a cadence in seconds, and on a 'ns' stream an unscaled comparison is off by 1e9, so every packet would read as an implausible jump. The guard istools/mock_udpstreamer.py --time-glitch-at/--time-glitch-by(see verification.md). - The arrival-clock fallback does not drop history.
arrival_epoch_is derived fromlast_stamp_, so the arrival clock continues from the monotonic mark and the two timelines share a zero by construction — the recorded stamps are therefore still on one timeline and are kept. The fallback used to callreset_live_history_locked()on the reasoning that the old stamps belonged to a dead clock; they do not, and that wipe is what made a single glitching packet look like "the live stream restarted from 0 with no action from the user". The continuity is also what makes a rebase ofTriggerEngineunnecessary: the engine holds four absolute stamps (holdoff_until_s_,pending_start_s_,last_trigger_s_,last_t_s_) and they stay valid across the change. Anything that makes the clock discontinuous breaks that and has to reset them. - The trigger sees the stored (filtered) signal, not the wire signal.
engine.process()is fed the averaged value, inside theifthat pushes it. An edge found on a sample that is not in the buffer would be cut from data that does not contain it. - The time ring is
f64and the value rings aref32, and the asymmetry is the point. A value's precision is a property of the signal (24 bits is plenty for a sample); a timestamp's is a property of how long the scope has been running, because the stamps are stored relative to an epoch fixed at the first sample ever seen and so grow without bound while the interval they must resolve stays one sample period.f32's ULP is 1.9 µs at 16 s of uptime, 3.8 µs at 32 s, 7.6 µs at 64 s and 61 µs over a 600 s window — so at 1 MS/s two to four consecutive samples shared a stamp after half a minute, eight after a minute, and the LOD envelope over such a plateau drew a zero-width segment, i.e. a vertical spike where the waveform should be. That is "zoom in to ~10 µs and three to five points share a timestamp, and min/max produces artefacts". Do not widen the values to match: they are the memory budget, and nothing about them is magnitude-bound this way. Three consequences:plan_partition()can no longer count slots in its growth loop (a time slot and a value sample are different sizes, so it weighs the budget in bytes —sizeof(f64)for the clock plussizeof(f32) / stride[i]per channel — andbytes_for()is also what bounds the doubling against overflow);live_window_locked()compares its bounds in the ring's own type with no rounding step, the oldf32round-trip having existed only so that a sample sitting exactly on a bound was kept whatever the stream's absolute time was; andEventSnapshot::stampsisf64because it is cut from this ring and is therefore in the same units, whatever a given snapshot's own origin is (both kinds are rebased now — an event's stamps are edge-relative, a pause's freeze-relative — so neither is magnitude-bound the way the ring is, and the width is for the cut rather than for the coordinate).RingBuffer<T>stores slots asstd::atomic<T>behind astatic_assert(is_always_lock_free), whichdoublesatisfies on every supported target. The guard is the export: over a multi-million-row.npy,np.diff(t)must hold one step to within a few ULP of the column's magnitude (see the export guard in probe-readings.md for why it is no longer exactly one value). Three steps at 2, 2.5 and 3 ULP of the stream's uptime — microseconds, not femtoseconds — is this defect. - A dropped time slot is clamped, never answered with a sentinel.
acquire()pins the published window to the value ring's base and both rings keep sliding afterwards — their capacities are equal, so there is no slack — which means the oldest few samples of a saturated window routinely point at time slots the producer has already overwritten.ChannelBuffer::time_of()therefore clamps to the oldest readable slot: a stamp of0would reachx_bounds(), and from theredx()andx0(), which is the renderer's whole index mapping — one unreadable slot inflateddx()by the ratio of elapsed stream time to the retained window and clipped the older part of every live view. The library'sStreamSeriesis immune for a reason worth copying: its x isx0 + i*dx, a function of the absolute index, so an overwritten slot costs a y value, never the geometry. Clamping buys the same bargain here. - The window's first stamp is cached by
acquire()and never read at paint time. The clamp above makes an unreadable slot survivable; it does not make reading it safe, andabsolute(0)is not an ordinary slot —acquire()pins the window tovalues_->base(), so sample 0 is the ring's oldest, the very slot the producer is about to overwrite.time_of()checks the bounds and then copies, and when the producer laps the slot in between the stamp it returns is one whole ring-span too new. Everything else on aChannelBufferis read at paint time with no lock, andx0()was the origin the renderer reconstructed every trace point from (gather[i] + series.x0()), read both inside its increasing-suffix filter and again atstage_xy()— so one torn read of that one slot produced three pictures, all of them "the waveform is gone while the grid and the axes stay":neighbours()tests againstx_at(0)and refused the whole visible range (the renderer then gathered the entire ring at a coarse level and drew it off the axis —idx=[0..8388608) level=7 cover=320%);x0()changing during the filter loop dropped three quarters of the points; andstage_xy()shifted the finished polyline a ring-span off its axis. The gather origin is the view's minimum now (see thegather()invariant), a local of the renderer's, so the second and third of those are structurally impossible — but the first is not,x_at(0)is still whatneighbours()tests the visible range against, and that is the one that gathered the whole ring. The cache is load-bearing for that reason; do not read the slot at paint time because two of its three symptoms are gone. That is the "with measurements and cursors active the plot flickers as if there were no data, at no constant frequency" defect. It is live only (a snapshot is immutable, sox0()is constant forHeld/Paused), it is a race against packet arrival so it is irregular, and the measurements pane makes it both likelier and larger: the pane holdsmutex()for eachMeasureWindowReader's life, so the released producer bursts several packets at once.acquire()runs on the consumer underScopeModel::mutex()— the same lock the producer's pushes hold — and the window is pinned on the line above, so that is the one point in the object's life where the slot is safe to read;window_x0_is also the right answer rather than merely a stable one (it is the stamp samplewindow_baseactually carries, where a paint-time clamp answered with whatever slot happened to be oldest now).x_at(0)is served from it too, for the same reason, andinvalidate()clears it with the window. The guard istrace ink:/[ink](see probe-readings.md). The same torn read still exists for a smalli > 0— the oldest frame or so of samples — but there its observable is a prefix plateau, which is exactly what the renderer's increasing-suffix filter was written to absorb, so it is a different symptom and deliberately not fixed here.