Skip to content
Advanced

When the ring is full

The drive's card holds the last 20.9 days of the bench. On the next day, the data ring has no free segment left, and the next flush needs one. This chapter answers what the writer does then. By default it overwrites the oldest segment and goes on; it can instead refuse new records and keep the oldest. The chapter also shows how the writes spread over the card as the ring goes round.

Going round

The writer numbers every data segment it opens with a segment seq, from 0 for the life of the log. In a data ring of n slots, the segment with seq s goes to the slot s mod n of the ring. Seq n is the first that finds no free slot, so it lands over seq 0 in the first slot. Each segment the writer opens after that replaces the oldest one on the card.

slot 0 slot 1 slot 2 slot 3 slot 4 slot 5 lap 0 seq 0 seq 1 seq 2 seq 3 seq 4 seq 5 lap 1 seq 6 seq 7 seq 8 seq 9 seq 10 seq 11 lap 2 seq 12 seq 13 seq 14 seq 15 seq 16 overwritten on the card being written
Segment seqs of a ring that went round. Every seq lands in slot seq mod n, over the seq one lap before it, so the card holds the last n seqs. Drawn by logring_figures from the drive's first minute, with the ring shrunk to 6 data segments of 64 KiB.

The drive example runs ten minutes of the bench on its simulated card. Its ring has 59 data segments. The card holds seqs 102 to 160 at the end of the run. On the way, the writer overwrote 102 segments, as the superblock's segments_reused counts.

Nothing else changes when the ring goes round. The writer opens and fills segments as before. Every segment still starts with its track table, so every segment still decodes on its own. A reader on a personal computer (PC) finds the oldest seq on the card and reads from there. The first positions it finds on a track is no longer 0.

Refusing new data

Some logs must keep their beginning. An acceptance test wants the first fault and every frame before it, not the last 20.9 days. For such a log the firmware sets full in the Config to FullPolicy::DropNewest. The writer then fills the data ring once and refuses every record after that: a push returns logring::Error::Full.

The drive example runs the same ten minutes with that policy. The ring takes records until 219.9 seconds into the run, and the card keeps the first 219 943 frames. The drive's bench stops on the error; a firmware that keeps running instead counts what it refused.

The firmware passes the policy at every mount, and the card does not store it. A firmware that switches to drop newest on a card whose ring went round cannot mount it. The mount itself gets logring::Error::Full, since its session start needs a new segment.

How wear spreads

Flash wears out with each erase and write. A ring spreads its writes as evenly as a store can. The writer writes each block of the data ring once per lap and never rewrites one in place. On the drive's card one lap takes the 20.9 days that the card's chapter found. Each data block thus sees one write per lap.

Two small structures are the exception and take a write at the same place again and again. The writer puts the superblock into its two slots in turn. It does so at each mount, at each recording start and stop, when a meta segment opens and every few data segments. In the ten minutes above it wrote the superblock 17 times, while the data ring went up to seq 160 in the same time. The meta area takes a record at each mount and at each recording start and stop.

A Secure Digital (SD) card hides its flash behind a controller, and the host never sees which cells a write goes to. What the host controls is the pattern of its writes. The ring writes each allocation unit whole and in order, the pattern for which the SD specification rates a card's speed. On a raw flash chip, which has no such controller, the writer erases each segment when it opens it. The format reference says how a mount handles a power cut during that erase.