Section 8.14

Circular log

Definition

A circular log is an append-only log §8.13, a store that only adds records at its end and never changes a written one, kept in a fixed storage area that it reuses in a cycle. The area is divided into sectors, regions that can each be erased on their own. Records fill one sector, then the next, and after the last sector writing continues at the first. Before a sector is erased for reuse, each record in it that still holds a current value is copied forward.

The idea

A device keeps its settings as an append-only log in a small area of flash memory §6.6. Each record holds a key, the number that names a setting, and the setting’s new value; the newest record of a key holds the current value. Eventually the area is full, mostly of obsolete records that newer ones have replaced. Flash memory erases only whole erase blocks, so space is reclaimed a sector at a time, and a sector may still hold current records.

A ring buffer §14.15 in working memory simply overwrites its oldest slot, which suits a stream of measurements. It does not suit settings: a setting changed once, long ago, has its only record in the oldest sector, and that record is current. A circular log therefore first copies the oldest sector’s live records, those that still hold the current value of their key, into the sector being filled, and only then erases.

How it works

The SS sectors form a ring, and each sector is one or more whole erase blocks. The current sector receives new records. At least one sector, the spare, is kept erased. The sector after the spare, the oldest, is the next to be reclaimed. Each record carries a key and a sequence number, a counter that grows with every record written, so that the newer of two records of a key can be identified. When a record does not fit into the current sector, the writer:

  1. marks the current sector as full;

  2. makes the spare the current sector;

  3. finds the live records of the oldest sector, meaning those that are not deletion records (records saying that their key has no value) and that have no record of the same key with a higher sequence number anywhere;

  4. copies each live record into the current sector with a new sequence number, and reads each copy back;

  5. writes a marker saying that the copying is complete;

  6. erases the oldest sector, which becomes the spare;

  7. appends the waiting record.

The spare gives the copies a destination before their source is erased. Without it, the only way to make room would be to erase a sector that holds live records. The live records of one sector always fit into one empty sector of the same size. If the waiting record does not fit as well, the writer repeats the steps with the next sector.

A power failure at any step leaves every current value somewhere. Until step 6 the oldest sector is untouched. Recovery that finds no completion marker erases the current sector, which holds nothing but copies, and copies again. An erase cut short in step 6 is simply repeated, because the marker shows that every live record already has a checked copy. After a restart, recovery identifies the current sector from the sequence numbers.

Worked example

Three sectors, S0, S1 and S2, each hold 4 records of 32 bytes. A record is shown as key = value, then # and its sequence number.

The application sets A to 14. S2 becomes the current sector and S0 is examined. Records #1 and #3 are obsolete, because #5 and #7 are newer records of A, and #4 is obsolete because of #6. Record #2, B = 7, is live: no other record of B exists, so it is the only current copy of B.

The writer copies B = 7 into S2 as #9, checks it, writes the marker, erases S0 and appends A = 14 as #10, using 2×32=642 \times 32 = 64 of the 128 bytes of S2. Erasing S0 without the copy would have lost B, although nobody deleted it.

If instead S0 and S1 held 8 live records of 8 different keys, a ninth key could never be stored: each collection would copy a full sector into the new one, erase the old one and gain nothing, round and round. Zephyr §6.33 NVS (Non-Volatile Storage), a circular log in the Zephyr RTOS (real-time operating system), detects this and returns an out-of-space error after it has collected every sector once without gaining room.

Stated precisely

Let SS be the number of sectors, CC the bytes of records that one sector holds, and VV the total bytes of the live records. With one spare, a new record of rr bytes can be accepted only if V+r≤(S−1)C.V + r \le (S - 1)\,C. In the example, at most (3−1)×128/32=8(3 - 1) \times 128 / 32 = 8 records, the new one included, can be live. Otherwise the store must refuse the write.

The safety rule is an invariant, a condition that must hold at every moment, including inside a collection or an erase that a power failure cuts short: every key with a current value has a complete, valid record of that value in a sector that is not being erased, and recovery selects that record or one with the same value. A sector may therefore be erased only after each of its live records has a checked copy elsewhere. Each copy must count as newer than its original and older than any later write by the application. Copying before the waiting record is appended, under the mutex §9.5 (a lock that lets one thread at a time run the code it guards) that guards ordinary writes, gives that order.

Limits and common mistakes

The spare costs capacity: SS sectors keep data in at most S−1S - 1, so a log of 2 sectors gives up half its area.

The write that triggers a collection also copies and erases, which takes far longer than an ordinary write (the garbage collection §8.16 entry gives figures) and belongs in the worst-case response time §9.32 of whatever waits for it.

A record that never changes is copied on every round, adding flash traffic, which the write amplification §8.18 entry measures, and erases. Rotation spreads the erases evenly but adds no capacity.

A deletion record may be dropped only when no older record of its key remains anywhere. Reclaiming the oldest sector first guarantees this; a store that reclaims sectors in another order must keep deletion records longer, or deleted values return after a restart.

Sequence numbers of fixed width wrap around to zero, and this counter wraparound §6.21 must not make the newest sector look like the oldest.

Questions answered

How do you avoid erasing the only current copy of a value?

You avoid erasing the only current copy of a value by never erasing a sector until every live record in it, meaning every record that still holds the current value of its key, has a complete and checked copy in another sector that recovery will select after a restart. Keep at least one sector erased at all times, so that copies have a destination. Before erasing the oldest sector, find its live records by comparing their sequence numbers with all other records of the same keys. Copy them into the sector being filled, read the copies back, record that the copying is complete, and only then erase. A power failure before the erase leaves the original, and one during the erase leaves the copy. When the live data no longer fits into all sectors but the spare, refuse the write instead of erasing a sector to make room.

Related entries: Garbage collection (§8.16), Flash wear and endurance (§8.17), Crash consistency (§8.4). Further reading: [12].