Events¶
A busy bus delivers thousands of frames a second, each with a time of its own, and a reader usually wants a few of them. A tape keeps such items in an events chunk, with their times, ends and payloads in columns. A reader finds the first event after a tick and reads it in place, without decoding the events before it.
The drive's emergency frame at 52:00, for example, lies on can0 among a thousand frames of that second. A reader
finds it by its time, reads what it says, and reads the frames that followed it.
Instants and spans¶
An event is an item with a time of its own. An event that happens at one tick is an instant, such as a
received frame. An event that lasts is a span, with a start and an end, such as a motor run from start to stop. A
track's definition says which kind its events are (EventsDef); the drive's can0 holds instants.
A chunk stores the ends in one of three end modes (EndMode), whichever costs the least:
- Absent: every item is an instant, and the chunk stores no ends.
- Next start: each item ends where the next one starts, as states that follow each other do. The chunk stores no ends either, and the last item ends at the chunk's latest end.
- Durations: each item's length, stored per group of 128 items.
The chunk head holds the latest end of any item in the chunk, and its index entry does too. A query for the spans that cover a tick therefore skips every chunk whose latest end lies before the tick, without reading it.
Times read in place¶
An events chunk keeps the start ticks of its items in one column, in time groups of 128 items. Each
group stores a line through its starts, and each start as a small distance from that line. Event times and their
codecs explains this codec, and a reader needs none of its detail. EventTimes::read checks the
columns once. It then gives any item's time from its group's head and one load.
| Call | Gives |
|---|---|
EventTimes::start |
item j's start tick |
EventTimes::end |
item j's end: its start for an instant |
EventTimes::flag |
item j's flags, 0 when the chunk stores none |
Starts::lower_bound |
the first item that starts at or after a tick |
Starts::last_at_or_before |
the last item that starts at or before a tick |
A search for a tick bisects the groups' first ticks, then uses the group's line to narrow the items inside it. A viewer finds the first frame at or after the fault this way.
Payloads read in place¶
Each item's value, its payload, is encoded in shrink_wrap, or kept as given on a track of other items. A chunk keeps
the payloads back to back in one column, and their lengths in another, packed per group. Payloads::read opens both,
and Payloads::get gives item j's bytes from one group of lengths, without the payloads before it. A payload column
that the writer compressed with lz4 is decoded once, whole, when the reader opens it.
A payload may be a whole frame as received. A protocol analyser keeps every frame of a network link that way, with the headers its decoder finds on tracks beside them. Keeping what matters of a 10 Gbps link shows how.
The drive's frames carry their 11-bit identifier in the first 4 bytes of the payload and the frame's data after it. The program reads the chunk that holds the first frame at or after the fault, then every frame of the next 5 ms:
// the first chunk of can0 whose last frame is at or after the fault
let entry = reader
.track_entries(piece, key("can0").id())?
.into_iter()
.find(|e| e.kind == BlockKind::Events && e.last_tick >= fault)
.ok_or(FaultError::Missing("frame after the fault"))?;
let mut buf = Vec::new();
let block = reader.read_block(piece, &entry, &mut buf)?; // read and checked by its CRC
let chunk = block
.chunk()
.ok_or(FaultError::Missing("chunk of frames"))?;
let times = EventTimes::read(&chunk).map_err(FaultError::Times)?;
let payloads = Payloads::read(&chunk)
.map_err(FaultError::Payloads)?
.ok_or(FaultError::Missing("payloads"))?;
let mut frames = Vec::new();
for j in times.starts().lower_bound(fault)..times.len() {
let us = times.start(j);
if us >= fault + AFTER_US {
break;
}
let payload = payloads.get(j).ok_or(FaultError::Missing("payload"))?;
let (id, data) = payload
.split_first_chunk::<4>()
.ok_or(FaultError::Missing("id"))?;
frames.push(Frame {
item: chunk.head().first_item.saturating_add(j as u64), // the frame's item number on can0
us,
id: u32::from_le_bytes(*id),
data: data.to_vec(),
});
}
What the example uses here: fault_us, the fault's constants, key, Frame, Frames
/// The ticks of the fault on the board's clock, µs since boot.
pub const fn fault_us() -> i64 {
FAULT_MINUTE as i64 * US_PER_MINUTE
}
/// Microseconds of the board's clock in a minute.
const US_PER_MINUTE: i64 = 60_000_000;
/// Pixel columns of the viewer's plot of the hour.
pub const COLUMNS: usize = 600;
/// [COLUMNS] as a number of µs to divide.
const COLUMNS_I64: i64 = 600;
/// Microseconds of the current before the fault that [current] reads.
pub const BEFORE_US: i64 = 20_000;
/// Microseconds of frames after the fault that [frames] lists.
pub const AFTER_US: i64 = 5_000;
/// The key of a track at `name` in the drive's API.
pub fn key(name: &str) -> TrackKey {
track_key(API_ROOT, &[Segment::Name(name)])
}
/// A CAN frame as [frames] read it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Frame {
/// Its item number on `can0`.
pub item: ItemNum,
/// Its time, µs since boot.
pub us: i64,
/// Its 11-bit identifier.
pub id: u32,
/// The bytes after the identifier.
pub data: Vec<u8>,
}
/// What [frames] read: the chunk that holds the first frame at or after the fault, and the frames of [AFTER_US].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Frames {
/// Frames in the chunk.
pub in_chunk: usize,
/// The chunk's block, bytes.
pub block_len: u32,
pub frames: Vec<Frame>,
}
The chunk holds 1 000 frames in a block of about 1.7 KB. The first frame at or after 52:00 is item 3 120 000 of
can0, the emergency frame with identifier 0x081. Its data begins 10 23: the error code 0x2310, overcurrent,
stored little-endian. The next four frames, items 3 120 001 to 3 120 004, are the drive's status, temperature,
voltage and command frames, one a millisecond. The step reads about 117 KB in 4 reads, almost all of it
can0's list in the index.
Flags¶
A track can give its events a 32-bit flags word, with a severity in its low bits. The writer stores the flags per group of 128, beside the bitwise OR of the group's flags. A viewer that has zoomed out colours a stretch by those ORs alone, without reading an item. A chunk in which every item's flags are 0 stores no flags column, and the drive's frames have none.
From an item number to the event¶
The item number names an event across the whole recording. A reader that holds item 3 120 000 of can0, such as
a marker saved in a viewer, finds it in two steps:
- Among
can0's index entries, it takes the one whosefirst_itemandcounthold 3 120 000. - In that chunk, it reads item
3 120 000 - first_itemwith the calls above.
The address stays valid when the pieces are joined into one file or kept apart. The next chapter reads the current
that led to the fault: the samples of phase_a in the last 20 ms before 52:00.
pub struct EventsDef {
pub ends: Ends,
/// Items carry a `u32` flags word (severity in its low bits), which summaries OR together.
pub flags: bool,
}
An events track: what its chunks may hold besides starts and payloads.
pub enum EndMode {
/// Every item is an instant: no ends column.
Absent,
/// The end of item i is the start of item i + 1, the last item's end is the head's latest end: no ends column.
NextStart,
/// The ends column holds each item's duration.
Durations,
}
How an events chunk stores ends.
pub fn read(chunk: &Chunk<'b>) -> Result<Self, TimesError> {
Self::open(chunk, |_| false)
}
Read the time columns of an intact events chunk, checking every group record and that the starts never decrease.
pub fn read(chunk: &Chunk<'b>) -> Result<Option<Self>, PayloadsError>
Read the payload columns of an intact events chunk, checking every group record; None when the chunk has no payload columns.
pub fn get(&self, i: usize) -> Option<&[u8]> {
(i < self.len()).then(|| self.item(self.range(i)).bytes)
}
The payload of item i, None past the last item. A salvaged item whose place was lost reads as empty.
pub fn start(&self, i: usize) -> i64 {
self.starts.get(i)
}
The start of item i.
Panics
When i is past the last item.
pub fn end(&self, i: usize) -> i64
The end of item i.
Panics
When i is past the last item.
pub fn flag(&self, i: usize) -> u32 {
assert!(i < self.len(), "item {i} of {}", self.len());
self.flags.as_ref().map_or(0, |f| f.get(i))
}
The flags of item i.
Panics
When i is past the last item.
pub fn lower_bound(&self, t: i64) -> usize
The first item starting at or after t; Self::len when none does.
pub fn last_at_or_before(&self, t: i64) -> Option<usize> {
match t.checked_add(1) {
Some(t1) => self.lower_bound(t1).checked_sub(1),
None => self.len().checked_sub(1),
}
}
The last item starting at or before t.