Keeping the bursts of a fast band¶
A wideband radio receiver digitizes a band at hundreds of millions of complex samples a second. Most of the time the band holds the same carriers and noise, and what matters are the short bursts between them. How does one PC keep the bursts of such a band without writing all of it?
It captures into memory, finds the bursts there from the energy in the summaries, and writes only the stretches around
them. The case here builds on the RF capture of Series, at 12.5 times its rate.
The crate example_wideband makes it.
Channels, each a track¶
A receiver at this rate splits its band into channels of equal width, each a stream at its share of the rate. The example takes 250 million samples a second in 2 channels of 125 million. Each channel carries the RF capture's signal, its carriers and its bursts, with noise of its own.
Each channel is a series track iq with a producer thread of its own. The thread pushes the channel's samples into
a grid handle. The handle packs them at 12 bits and cuts them into chunks of 1 MiB of samples:
let mut iq = mux.grid::<i16>(
track,
GridOptions {
chunk_steps: rf.chunk_samples,
latency: None, // the receiver fills every chunk
codec: self.codec,
..GridOptions::default() // chunks of 1 MiB of samples at most, summaries on
},
)?;
The split into channels is what lets the work spread. A track has one producer, and its handle does the costly work on that producer's thread. It packs the samples, hashes them and computes the leaves of the summaries. One thread then serves one channel's 125 million samples a second, not the whole band.
What the example uses here: Wideband
/// A capture of the wideband receiver: its rate, its channels, its length and how it is stored.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Wideband {
/// Complex samples a second, of all channels together.
pub rate_hz: u32,
/// The channels the band is split into, each at `rate_hz / channels`: 1 to [CHANNELS_MAX].
pub channels: u32,
/// Length of the capture, ms.
pub ms: u64,
/// The codec of the samples.
pub codec: Codec,
/// Bytes of a piece of a chain at most: 0 for no cut by size.
pub piece_bytes: u64,
}
Energy in the summary nodes¶
A burst raises the power I² + Q² of the samples it covers. The leaves of a series' summaries already hold that
power. Each summary node of level 0 keeps the mean and the largest power of 1 024 samples. An analyser that detects
energy therefore reads the nodes, never the samples: a few values for each 1 024 samples.
The analyser opens a piece, reads the summary blocks of level 0 of each channel, and compares each node's largest power
with a threshold. The carriers of the band add up to 1 050 steps of amplitude, and a burst adds 900 more. A threshold of
1.5 million, POWER_MIN, lies between a quiet node and one that holds a burst:
// the level-0 nodes only, each 1 024 samples: never the samples themselves
let nodes = summary_nodes(piece, &self.def, c.iq, 0, &mut self.buf)
.map_err(LookError::Piece)?;
for n in nodes {
if n.power_max < self.power_min {
if let Some(b) = c.open.take() {
push(&mut c.bursts, &b)?;
self.found.push(b);
}
continue;
}
hit = true;
let b = c.open.get_or_insert(Burst {
channel: c.k,
start: n.first_tick,
end: n.end_tick,
power_max: n.power_max,
});
b.end = n.end_tick;
b.power_max = b.power_max.max(n.power_max);
}
Consecutive loud nodes make one burst, from the first node's start to the last node's end. A burst may cross from one piece into the next: the analyser carries it over and ends it at the next quiet node. The nodes come from one function, which the overview below uses too:
/// The summary nodes of level `level` of track `id`, a channel's `iq` of definition `def`, in the first piece of
/// `reader`: only the summary blocks of that level read, in order.
///
/// # Errors
///
/// A summary block that doesn't read.
pub fn summary_nodes<S: ByteSource>(
reader: &TapeReader<S>,
def: &GridDef<'_>,
id: TrackId,
level: u8,
buf: &mut Vec<u8>,
) -> Result<Vec<OverviewNode>, WidebandError<S::Error>> {
let grid = def.first_grid(0);
let tick = |n: u64| {
grid.tick(def.period, ItemNum::new(n))
.ok_or(WidebandError::Read("a node's tick"))
};
let mut out = Vec::new();
for entry in reader.track_entries(0, id)? {
if entry.kind != BlockKind::Summary || entry.level != level {
continue;
}
let block = reader.read_block(0, &entry, buf)?;
let summary = Summary::of_block(&block).ok_or(WidebandError::Read("a summary"))?;
let head = summary.head();
for i in 0..summary.node_count() {
let stats = summary
.channel_stats(0, i)
.ok_or(WidebandError::Read("a node"))?;
// nodes lie on multiples of their size in item numbers: the first and last may be cut
let (from, to) = (head.first_item.get(), head.first_item.get() + head.items);
let start =
(from >> head.node_shift << head.node_shift) + ((i as u64) << head.node_shift);
let (first, end) = (start.max(from), (start + (1 << head.node_shift)).min(to));
out.push(OverviewNode {
first_tick: tick(first)?,
end_tick: tick(end)?,
samples: end.saturating_sub(first),
power_mean: stats.power_mean,
power_max: stats.power_max,
});
}
}
Ok(out)
}
A window in memory¶
The receiver records into the memory window, as the protocol analyser does in Keeping what matters of a 10 Gbps
link. The window is a chain of pieces kept in memory, the tape of the
capture held in RAM. A mux writes the chain into a MemPieces, a piece every 64 MiB. Each piece holds the same
tracks, blocks and summaries a piece in a file holds. A triage thread opens each piece as it closes and asks the
analyser whether it holds a burst.
A piece with a burst is copied to a piece file, with one piece before it and one after it. Every other piece leaves memory unwritten. The band takes fewer bytes a second than the link. In the bench below the replayed run keeps up with it in one chain, and the live run is held back by the signal's maths.
/// A capture of the receiver into a window in memory, the pieces around each burst kept.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct RfCapture {
/// The receiver: its rate, channels, length, codec and pieces.
pub wb: Wideband,
pub window: Window,
/// The largest power of a summary node that holds a burst, at least.
pub power_min: f32,
}
The analyser's findings go to a chain of results on disk. Each channel has a track bursts, one span per burst with its
largest power. A track kept names each kept piece with the span of time it covers. A minute of the band holds five
bursts of 2 ms each, so the disk sees a few pieces a minute.
The test tests/wideband_ram.rs records a minute at ⅕ 000 of the rate. It checks that each channel's five bursts are
found and that their samples lie in the kept pieces. It also checks that those pieces hold the signal's own samples.
The crate's binary runs the receiver headless: cargo run --release -p example_wideband -- DIR captures 10 s of the
band, past its first burst, and keeps the pieces around each burst under DIR. Its main names each thread and what
it feeds, and RfCapture::start hands out the memory window while the capture runs, as the analyser's
pipeline does:
fn main() -> Result<(), Box<dyn std::error::Error>> {
let (dir, ms) = args()?;
let capture = capture(ms);
// where the triage keeps: DIR/kept-0/ for the pieces, DIR/results/ for what the analyser finds
let [kept] =
<[FilePieces; 1]>::try_from(files::kept(&dir, 1)?).map_err(|_k| "one kept store")?;
let results = files::results(&dir, RESULTS_OWNER)?;
// a producer thread per channel -> one mux -> the memory window -> a triage thread
let running = capture.start(Source::Live, kept, &results)?;
// a viewer beside the triage: the pieces the chain begins
follow(&running)?;
// the capture ended, the triage read its last piece
let ran = running.join()?;
let closed = results.close()?;
report(&capture, &ran, &closed);
Ok(())
}
What one PC holds and keeps¶
just bench-wideband captures the band into the window at full speed. One run replays chunks made before it, so it
times the recorder and the triage, not the signal's maths; it holds no bursts. A second run makes the signal as it
goes for a minute, its bursts included. It keeps the pieces around the bursts in piece files on the machine's disk.
| Kept to | Source | Signal ms | Capture ms | Of the pace | Wall ms | Pieces | Overrun | Held max MB | Tape MB per s | Window s |
|---|---|---|---|---|---|---|---|---|---|---|
| memory | replayed | 1000 | 551 | 1.82 | 555 | 12 | 0 | 136 | 764 | 21.4 |
| piece files | live | 60 000 | 75 484 | 0.79 | 75 487 | 682 | 0 | 209 | 764 | 21.4 |
just bench-wideband on gpd-omarchy, load average 6.61 before, 5.15 after.
The benches ran on gpd-omarchy, a machine with 24 logical CPUs (AMD Ryzen AI 9 HX 370 w/ Radeon 890M, x86_64) and 30 GiB of memory. Each was a release build at opt-level 3 of commit 28d4e3e28, built with 24 jobs. The load average ran from 2.47 to 6.61 across the tables.
"Capture ms" is the time until every sample was in memory. The column "Of the pace" is the seconds of the band captured per second of that time: a value of 1 or more keeps up. The live run makes its samples as it goes, and that maths sets its pace. "Window s" is how many seconds of the band half the machine's memory holds.
| Kept to | Source | Signal ms | Triage MB/s | Of the pace | Kept pieces | Kept % | Results KB | To disk MB per s | Copy MB/s |
|---|---|---|---|---|---|---|---|---|---|
| memory | replayed | 1000 | 33 495 | 43.9 | 0 | 0.0 | 4 | 0.0 | none kept |
| piece files | live | 60 000 | 35 113 | 46.0 | 23 | 3.4 | 8 | 25.8 | 649 |
just bench-wideband on gpd-omarchy, load average 6.61 before, 5.15 after.
The triage reads only summary nodes, so it gets through the band many times faster than the band arrives.
"Kept %" is the share of the capture's bytes that reaches the disk, and "To disk MB per s" what the disk writes per second of the band.
The overview from the summaries¶
A viewer that shows a minute of a channel draws from its summary nodes, never from its samples. Each node holds
the smallest and largest I and Q and the mean and largest power I² + Q² of its samples. A node of level 3 covers
4 194 304 samples, 34 ms of a channel. The overview opens each piece of the chain and reads only the summary
blocks of one level:
/// The overview of channel `k` of capture `wb` at summary level `level` from `pieces`: each piece opened, and only
/// the summary blocks of that level read.
///
/// # Errors
///
/// A piece that doesn't open, or a summary block that doesn't read.
pub fn overview<S: ByteSource>(
wb: &Wideband,
pieces: impl IntoIterator<Item = S>,
k: u32,
level: u8,
) -> Result<Overview, WidebandError<S::Error>> {
let (bytes, reads) = (Cell::new(0), Cell::new(0));
let def = wb
.channel()
.def()
.ok_or(WidebandError::Settings("a rate"))?;
let mut out = Overview::default();
let mut buf = Vec::new();
for source in pieces {
let reader = TapeReader::open(Counted {
source,
bytes: &bytes,
reads: &reads,
})?;
out.nodes.extend(summary_nodes(
&reader,
&def,
channel_id(k),
level,
&mut buf,
)?);
}
Each piece has a tree of its own. The nodes at a piece's ends may therefore hold fewer samples. The test finds the five bursts of a minute in the overview alone. Exactly the nodes that hold a burst stand out by their largest power. The bench reads channel 0's overview from its chain of files:
| Level | Samples a node | Nodes | ms | Bytes read | Reads | Chain bytes |
|---|---|---|---|---|---|---|
| 2 | 262 144 | 239 | 0.2 | 288 401 | 18 | 763 670 528 |
| 3 | 4 194 304 | 15 | 0.1 | 278 727 | 11 | 763 670 528 |
just bench-wideband on gpd-omarchy, load average 6.61 before, 6.61 after.
Writing the whole band to disk¶
Some captures must keep every sample. A receiver of a billion samples a second splits its band into 8 channels of 125 million. Packed at 12 bits, a sample takes 3 bytes and a little more for its chunk, its summaries and its share of the index. At 1 GS/s, a second of the receiver takes about 3.1 GB, and a minute about 183 GB.
A TapeMux places every chunk of its handles on its one writing thread, into one chain. So channel k goes to mux
k % m, and each mux writes a chain of its own, on a disk of its own if the machine has several. The test
tests/wideband.rs records the channels through two muxes and reads every sample back from its chain.
just bench-wideband records the receiver at full speed into a store that keeps no bytes, once per number of channels,
muxes and codec. With the most channels and muxes it then writes chains of piece files on the machine's disk:
| Store | Channels | Muxes | Codec | Wall ms | MS/s | Of 1 GS/s | MB/s written | Bytes per sample | CPU ns per sample | Waits |
|---|---|---|---|---|---|---|---|---|---|---|
| no bytes kept | 4 | 1 | PACKED12_LSB |
1143 | 875 | 0.87 | 2673 | 3.05 | 5.2 | 0 |
| no bytes kept | 4 | 2 | PACKED12_LSB |
1220 | 820 | 0.82 | 2504 | 3.05 | 5.3 | 0 |
| no bytes kept | 4 | 4 | PACKED12_LSB |
1219 | 820 | 0.82 | 2505 | 3.05 | 5.4 | 0 |
| no bytes kept | 4 | 1 | RAW |
788 | 1269 | 1.27 | 5147 | 4.05 | 3.0 | 2784 |
| no bytes kept | 4 | 2 | RAW |
659 | 1517 | 1.52 | 6150 | 4.05 | 3.4 | 0 |
| no bytes kept | 4 | 4 | RAW |
666 | 1502 | 1.50 | 6090 | 4.05 | 3.4 | 0 |
| no bytes kept | 8 | 1 | PACKED12_LSB |
779 | 1283 | 1.28 | 3920 | 3.05 | 6.3 | 687 |
| no bytes kept | 8 | 2 | PACKED12_LSB |
818 | 1223 | 1.22 | 3734 | 3.05 | 7.0 | 0 |
| no bytes kept | 8 | 4 | PACKED12_LSB |
810 | 1234 | 1.23 | 3769 | 3.05 | 6.9 | 0 |
| no bytes kept | 8 | 1 | RAW |
819 | 1221 | 1.22 | 4950 | 4.05 | 3.3 | 3693 |
| no bytes kept | 8 | 2 | RAW |
530 | 1885 | 1.89 | 7645 | 4.05 | 4.6 | 655 |
| no bytes kept | 8 | 4 | RAW |
522 | 1916 | 1.92 | 7768 | 4.05 | 5.0 | 0 |
| no bytes kept | 16 | 1 | PACKED12_LSB |
868 | 1152 | 1.15 | 3519 | 3.06 | 7.6 | 3642 |
| no bytes kept | 16 | 2 | PACKED12_LSB |
682 | 1466 | 1.47 | 4477 | 3.05 | 9.9 | 1683 |
| no bytes kept | 16 | 4 | PACKED12_LSB |
674 | 1484 | 1.48 | 4532 | 3.05 | 10.8 | 0 |
| no bytes kept | 16 | 1 | RAW |
812 | 1232 | 1.23 | 4994 | 4.06 | 3.8 | 3753 |
| no bytes kept | 16 | 2 | RAW |
589 | 1699 | 1.70 | 6889 | 4.05 | 5.3 | 3550 |
| no bytes kept | 16 | 4 | RAW |
558 | 1792 | 1.79 | 7265 | 4.05 | 7.7 | 1402 |
| piece files | 16 | 4 | PACKED12_LSB |
3435 | 291 | 0.29 | 889 | 3.05 | 7.8 | 2203 |
just bench-wideband on gpd-omarchy, load average 5.22 before, 6.61 after.
The column "Of 1 GS/s" is the samples recorded a second against the receiver's rate. The waits count the times a handle found its mux's queue full. Without a disk, enough channels and muxes reach the full rate. With a disk, a second of the receiver is about 3.1 GB, more than one disk of a common PC writes in a second. That is why the receiver keeps the band in memory and writes only the bursts.
The band has no payload column to compress. A channel is a series track, and its samples go through a sample codec:
RAW and PACKED12_LSB are the two the table compares. "Bytes per sample" is what the chain keeps for each sample,
and "CPU ns per sample" is the CPU time of every thread of the process for it. Packing saves a byte of each four and
costs more CPU time. The bursts and the kept pieces the analyser writes are events of 8 bytes, which no payload codec
shrinks.
pub const POWER_MIN: f32 = 1.5e6;
The largest power of a node of a quiet band stays below this: the carriers' amplitudes add up to 1 050 steps, and noise adds a few. A burst adds 900 steps.
pub struct MemPieces<K = u64>(Arc<Mutex<BTreeMap<K, MemPiece<K>>>>);
Pieces in memory by key, shared by every clone: a PieceStore (keyed by a piece's seq with K = u64), and
where its readers find each piece (Self::get).
pub fn start<K, R>(
&self,
source: Source,
kept: K,
results: &TapeMux<R>,
) -> Result<RfRunning<K, R>, RfError<K::Error, R::Error>>
where
K: PieceStore + Send + 'static,
K::Error: Send,
R: PieceStore + Send + 'static,
R::Sink: Send,
R::Key: Send,
R::Error: Send + Sync,
Start recording the receiver into memory from source, the pieces around each burst kept in kept and the
analyser's results pushed through results, as the module docs say.
pub struct TapeMux<T: PieceStore> {
shared: Arc<Shared<T>>,
thread: Option<JoinHandle<()>>,
}
Writes one chain from a thread of its own, for producers on any thread; see the crate docs.