nandlog v1.1

Performance

What each call costs in flash operations, in time on a real board, and in RAM. Read this page to size a system around nandlog.

A page read, a program, or an erase takes hundreds of microseconds to milliseconds, while the library's own work between them takes tens of microseconds. This page therefore counts each call's cost in flash operations first, then gives what those operations take on real hardware.

The measured figures come from the on-device benchmark, tests/test_nandlog_bench.c, run on an Ambiq Apollo4 Plus (Cortex-M4F at 96 MHz, 48 MHz SPI) with the Alliance AS5F18G04SND, on-die ECC on, and the 1.0 library. On that board:

With the Winbond W25N01GWZEIG (2 KB pages, 1,024 blocks) and the 1.1 library on the same MCU:

Every loop has a stated bound, so a part that stops working ends in nandlog_port_unwritable() or nandlog_port_fatal(), never in a hang. If you are sizing a system, read Complexity, Boot, Blocking time, and Memory. The other sections give the simulator's exact counts and the measurements behind each figure.

Complexity

The table gives each call's flash work. A header read latches a page and clocks out 32 bytes. A raw read latches a page with the part's ECC off and clocks out the whole page and spare area, which is how the log shows that a page is blank. An epoch is one generation of the log, and its metadata slot, written to a pair of pages in the metadata ring, records it. The window is the NANDLOG_ERASE_AHEAD_BLOCKS blocks the log keeps erased ahead of the write head, the position the next page goes to. On-Flash Format defines these in full.

SymbolMeaning
NGood pages in the log region: the pages of blocks that are not retired
EPages in the selected epoch
SPages an open read will deliver
RRetired blocks in the log region, held sorted in RAM
BEntries in the driver's bad-block table
CBlocks on the part
rPending retransmission requests
CallFlash work
nandlog_probe()One identity read
nandlog_init(), first bootO(C): one ECC-off read of each block's mark, then the table written to a pair of pages and verified
nandlog_init(), every later bootO(log N + B): the driver's table, 18–20 reads for the ring, about log2 N probes for the write head, and 2 or 3 erases (details below)
nandlog_store_record()None directly. One page commit per page's worth of records, and at most one erase in a call that commits nothing
  One page commit1 program and 1 read-back of header and payload, compared byte for byte
  A commit whose block failsA slot pair recording the failed page, then up to NANDLOG_PAGE_PLACEMENT_ATTEMPTS blocks in all (details below)
nandlog_flush(true)One page commit
nandlog_flush(false)None, or the one erase the window is due
nandlog_begin_epoch()A commit if records are buffered, a slot written to a pair of pages, then 1 + NANDLOG_ERASE_AHEAD_BLOCKS erases. The first on a part whose ring holds no slot also reads a header from each good block of the log region, O(B), to number the epoch
nandlog_retrieve_epoch_details()1 page read, 2 if the first copy's details do not check
nandlog_epoch_count(), _info()One 64-byte read per slot walked back over, and about seven per ring block stepped into
nandlog_select_epoch()The same walk, plus header reads of up to two blocks, from the end back, to find where the epoch ended
nandlog_begin_reading(0, 0)None, or one page commit if records are buffered
nandlog_begin_reading(t, 0)O(E) header reads, stopping at the first page that reaches the bound
nandlog_begin_reading(0, t)O(E) header reads: a full pass over the epoch, always
nandlog_begin_reading(t0, t1)O(E) header reads, up to 2E
nandlog_read_span(&pages, NULL)None: the count is arithmetic
nandlog_read_span(&pages, &bytes)O(S) whole-page reads
nandlog_retrieve_next_page()1 page read
nandlog_retrieve_page_by_seq()1 page read in the current epoch. In an earlier one, a header read to check the guess, then O(log E), or O(E) at worst, if a block retired since has shifted its positions
nandlog_retransmit_total_bytes()r lookups as for retrieve_page_by_seq()
nandlog_retrieve_retransmit_page()As for retrieve_page_by_seq()
nandlog_read_recent_page(k)1 page read, and O(R) arithmetic to find it
nandlog_reset_bad_block_table()A recovery utility: every entry's mark programmed and read back, and the table's blocks erased
All other callsNone

A boot reads the Alliance driver's table in about 21 reads, plus one ECC-off mark read per table entry. The write head takes ⌈log2(N + 1)⌉ probes and at most three more, each a header read plus a raw read if the header reads erased. After a power cut during a metadata write, or one that tore a page, the boot also writes one more pair of pages. On-Flash Format gives each step's reads.

A commit whose block fails pays, for each further block it tries, 1 erase and, per page already in the failing block, a whole-page read, a copy, and a header read of the copy. Retiring the block costs a table version and a slot pair. Turning a position into a page is arithmetic over the sorted list of retired blocks, O(R), with no flash access.

Bus traffic versus flash time

Almost every read the log makes to find something is a 32-byte header, through nandlog_chip_read_page_region(). It clocks out more only to verify a commit, to serve or total a page, to move one, and at boot to judge a slot or the page below the head. This saves bus time, not flash time: on the bench, a header read still costs 29 % of a whole-page read, because the part's page latch dominates (see What a read costs).

Simulator operation counts

These are exact counts from the simulator, for the Alliance part's geometry: 4 KB pages, 64 pages a block, and 4,096 blocks, 7 of them factory-bad, with the internal page copy on. A latch is one page-read command, of any length, and raw reads are counted within it.

OperationLatches (raw)ProgramsErasesBytes read
First boot, factory-fresh part4,217 (4,103)3124,938
Warm boot, no epoch yet36 (10)0020,790
Warm boot, one 100-page epoch83 (24)0295,064
  of which the driver's table and OTP check29 (10)0020,327
Warm boot, 101 epochs in the ring89 (29)02116,868
Warm boot, 521 epochs, the ring wrapped91 (29)02116,968
Warm boot, 20,000-page epoch81 (20)0277,748
Warm boot, 220,000-page epoch76 (16)0260,298
Warm boot after a power cut tore a commit, passing it over82 (18)22103,749
Warm boot, a block retired inside the epoch79 (19)1299,469
Warm boot, the table kept below the reserve203 (58)02105,930
Warm boot in degraded mode, marks only4,301 (4,188)782105,180
A commit of a full page1104,101
A commit of a 20-byte partial page11052
flush(false) carrying the erase-ahead0012
begin_epoch()3 (1)234,506
begin_epoch(), the first on a part with no slot4,06824138,398
epoch_count(), 16 listed30001,596
select_epoch(), an earlier 100-page epoch48002,240
begin_reading(t0, t1), 403-page epoch5040017,136
retrieve_next_page(), retrieve_page_by_seq()1004,098
A commit that relocates 40 pages and retires the block133 (4)491193,152
The same, the block failing at its last page: 63 pages202 (4)721288,257
The worst the placement bound allows: two targets each refuse their last copy396 (7)1392559,643

In 300 consecutive commits, every erase landed in a call that committed nothing. A warm boot reads less as the epoch grows, because more of the search's probes land on written pages, which a header read settles, and fewer on erased pages, which need a raw read. The rotated probes move a boot's count by a few latches either way. In degraded mode, every boot rebuilds the table from all 4,096 marks and programs each entry's mark again. make opcounts in sim/ regenerates every figure here, and splits each call's reads by how much they clock out.

What a read costs

On the bench, a read's cost is mostly fixed, so reading fewer bytes saves less time than the byte counts suggest. Medians:

ReadBytesTime
A page header, through nandlog_chip_read_page_region()32279 µs
A whole page4,096972 µs
A raw read for a blank check: page and spare, ECC off4,3521,030 µs
Clocking bytes out of the part's cache register, no latch32 / 4,09636 / 729 µs
One status read132 µs
What a read costs, and what it is made of whole page 274 fixed 699 µs of data (4096 B) 972 µs 32-byte header 274 fixed 5 µs of data 279 µs 0 500 1000 µs Reading 128× fewer bytes takes 3.5× less time, not 128×: the fixed cost dominates.
Figure 1. Measured on the Apollo4 Plus at 48 MHz SPI. Fitting the two points gives 0.171 µs a byte, 97.7 % of wire speed, and 274 µs of fixed cost per read.

A header read is therefore 98 % fixed cost, and most of that is the part's page latch. Polled from the end of the read command, a latch takes a median 261 µs with ECC on, written or erased page alike, and 163 µs with ECC off. It also varies with the order of reads, from 212 to 436 µs across the bench's access patterns. Each nandlog_port_transfer_*() call costs 28–32 µs on this port, even for one byte.

Budget from measurement. The latch and the port's overhead differ from board to board, so run the benchmark on yours before sizing anything around read latency (see Testing). It erases the metadata ring and the whole log region.

Download cost

A download costs one read for each page it touches, so its time follows from the read costs above. Measured on the bench:

CallMeasured
begin_reading(0, 0)7.9 µs, no flash reads
begin_reading(0, t), end bound300 µs per page of the epoch: 300 ms for 1,000 pages, 2.99 s for 10,000
begin_reading(t, 0), start bound mid-epoch300 µs per page up to the bound: 150 ms for 501 pages
read_span(&pages, &bytes)1.03 ms per page: 1.03 s for 1,000 pages
retrieve_next_page()1.11 ms per page, about 3.6 MB/s of payload
retrieve_page_by_seq()1.26 ms

At those rates, a full epoch on this part (about 260,000 pages) takes about 78 s to resolve an end bound, another 267 s for a byte total, and 289 s to download. To keep a download short:

To keep logging during a long download, close the read and fetch pages by sequence number instead (see API Reference).

Relocation

When a block fails, the log moves the epoch's pages in it to the next good block, inside the part when the driver supports it (NANDLOG_CHIP_PAGE_COPY). Measured on the bench, per page:

MethodPer pagePer 64-page block
Internal copy1,239 µs79 ms
Read into RAM and write back2,676 µs171 ms
Saving1,437 µs (54 %)92 ms
Relocating one page internal copy 1,239 µs read + write back +1,437 µs on the bus 0 1000 2000 3000
Figure 2. The 1,437 µs saved is almost all bus time: 4,096 bytes in each direction cost 1,398 µs at the measured rate.

These are the primitives alone. The log also reads each page whole before it moves it, either way, so that the decision to copy is made from a single read, and then reads back the copy's header. In the simulator, a commit that relocates 40 pages and retires the block reads 193,152 bytes and writes 37,775 with the internal copy, against 355,712 and 201,495 without it. The worst case the placement bound allows reads 559,643 bytes and writes 55,663 with it, against 1,063,579 and 571,381 without. The 40-page relocation's 49 programs are the 40 copies, the failed page and its retry, two slot pairs (one recording the failure, one the retirement), a table version (a pair), and the retired block's mark.

Boot

A warm boot on the bench takes 82–89 ms. Each row is the median of 20 boots, and the 20 agree to within 0.1 ms.

BootReads (raw)ErasesTime
Ring empty, log region erased71 (41)047.6 ms
1, 64, 511, or 600 epochs in the ring, the last empty122–124 (59–61)387.2–89.4 ms
600 epochs, the last 1,000 pages long121 (56)284.5 ms
600 epochs, the last 10,000 pages long119 (54)282.0 ms
The driver's nandlog_chip_init() alone63 (41)037.8 ms

The bad-block table's share. Every boot reads the mark of each block the driver's table names, with ECC off, to check that it still reads 00h, so the driver's share grows with the table. The bench part's table names its 35 marked blocks: the 24 retired in its ring and log region, and 11 in the reserve. The bench's own scan of all 4,096 marks takes 1,400.7 ms, or 0.34 ms a mark, and the driver adds two register writes and a status read to each, so a boot spends about 0.43 ms per table entry. A part with the 80 bad blocks its datasheet allows would spend about 34 ms a boot on its marks.

A first boot reads the mark of every block. It has not been timed on hardware. The bench's scan of the same 4,096 marks takes 1.40 s, so a model that adds the driver's status read per block gives about 1.5 s. A boot that rebuilds a lost table, and every boot in degraded mode, reads the same marks.

Wear from the window. Every boot erases the blocks ahead of the head again, because an interrupted erase can leave a block that reads erased but programs unreliably. A device that reboots far more often than it fills a block wears those blocks by up to NANDLOG_ERASE_AHEAD_BLOCKS erases a boot. For example, one boot a minute while logging a page an hour keeps each block in the default two-block window for 128 hours, so it is erased about 7,700 times on each pass of the head. Setting the window to 0 avoids this, at the price of a commit that erases each block it enters (see Getting Started).

CRC-32

Every page carries a CRC-32 over its 28-byte header and one over its payload, and every page read checks both. Each is computed in software unless the port takes it over with NANDLOG_PORT_CRC32 (see Porting). Measured on the bench:

CRC4,064-byte payload28-byte header
Software, table in RAM (the default)466.6 µs, 11.0 cycles a byte4.1 µs
A byte-at-a-time reference with its table in flash881.0 µs, 20.8 cycles a byte7.0 µs
The port's hook on the Apollo4's CRC engine49.8 µs, 1.2 cycles a byte5.3 µs

The engine is slower than software on a header, so the bench's port uses it only for runs of 64 bytes or more, as nandlog_port.c sketches. The bench build uses the engine, so the call timings on this page include it. NANDLOG_CRC_SLICE_BY_4 has not been measured on this board. The work lands here:

Blocking time

A call blocks its caller for its flash operations, so the longest calls are a commit and an erase, except the first begin_epoch() on a part with no slot, which reads a header from every block. The same bench run stores 1,280 pages each of 32-, 256-, and 1,024-byte records:

CallMedianLongest
store_record() that only buffers: 32-, 256-, 1,024-byte records16.8, 27.4, 50.2 µs17.2, 27.8, 51.2 µs
store_record() that commits a full page2,790–2,974 µs2,978 µs
store_record() that carries an erase-ahead988–1,022 µs3,017 µs
flush(true) of a 156-byte partial page2,086 µs2,091 µs
flush(false) with nothing due4.3 µs4.9 µs
begin_epoch()8.2 ms, or 9.9 ms entering a ring block10.6 ms
begin_epoch(), the first on a part with no slotAbout 1.1 s, estimated: 4,066 header reads at 279 µs each. On the Winbond part's 1,024 blocks, 190 ms measured—

In 3,840 commits, no call both committed a page and erased a block. A commit is mostly the part and the bus. A traced commit spends 727 µs loading 4,096 bytes into the part, about 650 µs programming them, and 711 µs reading back a full page's header and payload, so a partial page commits faster. The erase-ahead in this run mostly met blocks the bench had already erased, at about 1 ms each. A full block takes 3,538 µs to erase, so an erase-ahead over old data takes about that long. A commit erases the block it enters only when no call that commits nothing has run for the 128 pages the default window covers.

Bounded loops

These loops run in whichever task calls the log, so every one has a stated bound. A part that has stopped accepting programs cannot turn a search into a spin that only a watchdog can end.

LoopBound
Walking to the next good blockOne pass over the log region's blocks, in RAM
Placing a page that will not stay writtenNANDLOG_PAGE_PLACEMENT_ATTEMPTS blocks in all, then nandlog_port_unwritable()
Waiting for the part to clear BUSYNANDLOG_BUSY_TIMEOUT_POLLS status polls, NANDLOG_BUSY_POLL_INTERVAL_US apart, then nandlog_port_fatal(). That is at least 0.5 s at the defaults, and about 2.4 s on the Apollo4 Plus port, where a poll takes about 47 µs
Programming one pageOne PROGRAM EXECUTE, never repeated
Erasing one blockNANDLOG_BLOCK_ERRORS_BEFORE_REMOVAL attempts before the block is retired
Writing a metadata slotTwo passes over the ring's blocks, a pair of pages in each
Writing the Alliance driver's tableOne pass over its table blocks, a pair of pages in each
Carrying on with epochs whose slots the ring lostOne head search each, at most once per page of the ring
The write-head search⌈log2(N + 1)⌉ probes and at most three more, plus one header read per position of each run of unreadable positions below a probe, never below the search's lower bound

Memory

All memory is static and sized at compile time, and nothing is allocated. These figures are for NANDLOG_MAX_PAGE_SIZE_BYTES at 4096, compiled by GCC 15.2 for the Cortex-M4F at -O2. Code is each object's .text and .rodata, as sim/baselines/footprint/build.sh measures it.

ObjectRAM (bytes)Code (bytes)
nandlog.c10,97221,905
nandlog_chip_AS5F18G04SND.c5,4499,202

That is about 16.4 KB of RAM for the Alliance part. The library's share is two page buffers (2 × NANDLOG_MAX_PAGE_SIZE_BYTES), 4 bytes per entry of the retransmission list (256 entries) and the retired-block list (128), the CRC's 1,024-byte table, and about 220 bytes of state. Leaving the CRC table in flash (NANDLOG_CRC_TABLE_IN_RAM 0) saves 1,024 bytes, and NANDLOG_CRC_SLICE_BY_4 makes the table 4,096. The Alliance driver holds a page-and-spare scratch buffer and a 256-entry table. The Winbond driver holds a 2,112-byte scratch buffer and an 80-byte mirror of the part's remap table. Matching NANDLOG_MAX_PAGE_SIZE_BYTES to a 2 KB-page part saves 4 KB.

The deepest stack frame is nandlog_select_epoch()'s, at 344 bytes, because the epoch-listing calls build a short array on the stack rather than keep a list of epochs in RAM. By GCC's call graph, the deepest chain with the Alliance driver is 764 bytes, from nandlog_init() to the port's transfer: a boot that relocates the head's block and retires a block, recording it in the ring and in the table. nandlog_store_record()'s deepest chain is 684 bytes, plus the port's CRC where NANDLOG_PORT_CRC32 is set. There is no recursion.

What the log does when the part fails, case by case, is on the Durability page, in Failure handling.