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:
- A
nandlog_store_record()that only buffers takes 17–50 µs, and one that commits a 4 KB page about 3.0 ms. - Erasing a block ahead of the write head, where the next page goes, takes 1.0–3.5 ms, and no call both commits a page and erases a block.
- A warm boot takes 82–89 ms.
- Serving a page takes about 1.1 ms, and resolving a time bound about 0.3 ms for each page of the epoch it reads.
- The library and the Alliance driver use 16.4 KB of static RAM, and nothing is allocated.
With the Winbond W25N01GWZEIG (2 KB pages, 1,024 blocks) and the 1.1 library on the same MCU:
- A
nandlog_store_record()that commits a 2 KB page takes 1.4–1.6 ms, and an erase ahead of the write head 0.7 ms. - A warm boot takes 23–29 ms.
- Serving a page takes about 0.6 ms, 3.3 MB/s of payload, and resolving a time bound about 0.2 ms for each page of the epoch it reads.
- Beginning an epoch takes 5 ms, or 190 ms the first time on a part whose ring holds no slot.
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.
| Symbol | Meaning |
|---|---|
| N | Good pages in the log region: the pages of blocks that are not retired |
| E | Pages in the selected epoch |
| S | Pages an open read will deliver |
| R | Retired blocks in the log region, held sorted in RAM |
| B | Entries in the driver's bad-block table |
| C | Blocks on the part |
| r | Pending retransmission requests |
| Call | Flash work |
|---|---|
nandlog_probe() | One identity read |
nandlog_init(), first boot | O(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 boot | O(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 commit | 1 program and 1 read-back of header and payload, compared byte for byte |
| A commit whose block fails | A 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 calls | None |
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.
| Operation | Latches (raw) | Programs | Erases | Bytes read |
|---|---|---|---|---|
| First boot, factory-fresh part | 4,217 (4,103) | 3 | 1 | 24,938 |
| Warm boot, no epoch yet | 36 (10) | 0 | 0 | 20,790 |
| Warm boot, one 100-page epoch | 83 (24) | 0 | 2 | 95,064 |
| of which the driver's table and OTP check | 29 (10) | 0 | 0 | 20,327 |
| Warm boot, 101 epochs in the ring | 89 (29) | 0 | 2 | 116,868 |
| Warm boot, 521 epochs, the ring wrapped | 91 (29) | 0 | 2 | 116,968 |
| Warm boot, 20,000-page epoch | 81 (20) | 0 | 2 | 77,748 |
| Warm boot, 220,000-page epoch | 76 (16) | 0 | 2 | 60,298 |
| Warm boot after a power cut tore a commit, passing it over | 82 (18) | 2 | 2 | 103,749 |
| Warm boot, a block retired inside the epoch | 79 (19) | 1 | 2 | 99,469 |
| Warm boot, the table kept below the reserve | 203 (58) | 0 | 2 | 105,930 |
| Warm boot in degraded mode, marks only | 4,301 (4,188) | 78 | 2 | 105,180 |
| A commit of a full page | 1 | 1 | 0 | 4,101 |
| A commit of a 20-byte partial page | 1 | 1 | 0 | 52 |
flush(false) carrying the erase-ahead | 0 | 0 | 1 | 2 |
begin_epoch() | 3 (1) | 2 | 3 | 4,506 |
begin_epoch(), the first on a part with no slot | 4,068 | 2 | 4 | 138,398 |
epoch_count(), 16 listed | 30 | 0 | 0 | 1,596 |
select_epoch(), an earlier 100-page epoch | 48 | 0 | 0 | 2,240 |
begin_reading(t0, t1), 403-page epoch | 504 | 0 | 0 | 17,136 |
retrieve_next_page(), retrieve_page_by_seq() | 1 | 0 | 0 | 4,098 |
| A commit that relocates 40 pages and retires the block | 133 (4) | 49 | 1 | 193,152 |
| The same, the block failing at its last page: 63 pages | 202 (4) | 72 | 1 | 288,257 |
| The worst the placement bound allows: two targets each refuse their last copy | 396 (7) | 139 | 2 | 559,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:
| Read | Bytes | Time |
|---|---|---|
A page header, through nandlog_chip_read_page_region() | 32 | 279 µs |
| A whole page | 4,096 | 972 µs |
| A raw read for a blank check: page and spare, ECC off | 4,352 | 1,030 µs |
| Clocking bytes out of the part's cache register, no latch | 32 / 4,096 | 36 / 729 µs |
| One status read | 1 | 32 µs |
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:
| Call | Measured |
|---|---|
begin_reading(0, 0) | 7.9 µs, no flash reads |
begin_reading(0, t), end bound | 300 µ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-epoch | 300 µ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:
- Pass
NULLfor the byte total unless a host needs it to size a buffer. That saves a whole-page read of every page in the span. - Leave the end bound at zero and filter on the host, because the end-bound seek is a full pass over the epoch, every time.
- Use a start bound alone where you can, since it reads only as far as the page it finds.
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:
| Method | Per page | Per 64-page block |
|---|---|---|
| Internal copy | 1,239 µs | 79 ms |
| Read into RAM and write back | 2,676 µs | 171 ms |
| Saving | 1,437 µs (54 %) | 92 ms |
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.
| Boot | Reads (raw) | Erases | Time |
|---|---|---|---|
| Ring empty, log region erased | 71 (41) | 0 | 47.6 ms |
| 1, 64, 511, or 600 epochs in the ring, the last empty | 122–124 (59–61) | 3 | 87.2–89.4 ms |
| 600 epochs, the last 1,000 pages long | 121 (56) | 2 | 84.5 ms |
| 600 epochs, the last 10,000 pages long | 119 (54) | 2 | 82.0 ms |
The driver's nandlog_chip_init() alone | 63 (41) | 0 | 37.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:
| CRC | 4,064-byte payload | 28-byte header |
|---|---|---|
| Software, table in RAM (the default) | 466.6 µs, 11.0 cycles a byte | 4.1 µs |
| A byte-at-a-time reference with its table in flash | 881.0 µs, 20.8 cycles a byte | 7.0 µs |
| The port's hook on the Apollo4's CRC engine | 49.8 µs, 1.2 cycles a byte | 5.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:
nandlog_store_record()adds its record to the page's CRC as it copies it in, so a commit computes only the header's CRC.- Each page served, and each page
nandlog_read_span()totals, costs one payload CRC. - Boot and the time-bound seek check one header CRC per header read.
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:
| Call | Median | Longest |
|---|---|---|
store_record() that only buffers: 32-, 256-, 1,024-byte records | 16.8, 27.4, 50.2 µs | 17.2, 27.8, 51.2 µs |
store_record() that commits a full page | 2,790–2,974 µs | 2,978 µs |
store_record() that carries an erase-ahead | 988–1,022 µs | 3,017 µs |
flush(true) of a 156-byte partial page | 2,086 µs | 2,091 µs |
flush(false) with nothing due | 4.3 µs | 4.9 µs |
begin_epoch() | 8.2 ms, or 9.9 ms entering a ring block | 10.6 ms |
begin_epoch(), the first on a part with no slot | About 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.
| Loop | Bound |
|---|---|
| Walking to the next good block | One pass over the log region's blocks, in RAM |
| Placing a page that will not stay written | NANDLOG_PAGE_PLACEMENT_ATTEMPTS blocks in all, then nandlog_port_unwritable() |
| Waiting for the part to clear BUSY | NANDLOG_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 page | One PROGRAM EXECUTE, never repeated |
| Erasing one block | NANDLOG_BLOCK_ERRORS_BEFORE_REMOVAL attempts before the block is retired |
| Writing a metadata slot | Two passes over the ring's blocks, a pair of pages in each |
| Writing the Alliance driver's table | One pass over its table blocks, a pair of pages in each |
| Carrying on with epochs whose slots the ring lost | One 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.
| Object | RAM (bytes) | Code (bytes) |
|---|---|---|
nandlog.c | 10,972 | 21,905 |
nandlog_chip_AS5F18G04SND.c | 5,449 | 9,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.