# Torn-bit logging: what the generation bit proves

A generation bit can distinguish a missing atomic word from the word intended
for the current pass. It cannot prove that a transaction is committed, that a
payload is uncorrupted, or that ring space was reused safely.

## Historical context

[Mnemosyne](https://hesela.dev/terms/mnemosyne/) is a persistent-memory system
presented at [ASPLOS 2011](https://asplos-conference.org/asplos2011/program.html)
by Haris Volos, Andres Jaan Tack and Michael M. Swift.
The [paper](https://pages.cs.wisc.edu/~swift/papers/asplos11_mnemosyne.pdf)
separates log append from flush (section 4.4) and evaluates emulated PCM
(section 6.1). Its numbers are not measurements of later persistent-memory DIMMs.

The [GCC port](https://github.com/snalli/mnemosyne-gcc/tree/f9fc03f6eba3529c26725e8e99512e5257d2fc53)
is a distinct artifact. Its README says 2012, conflicting with the original
paper and conference programme; use 2011 for the publication.
The reviewed code reserves the high bit, buffers writes in 64-byte chunks,
changes generation at ring wrap and scans until a generation mismatch.
Relevant files are linked in [sources.json](sources.json). No port code is
redistributed here, and no native port build or hardware test is claimed.

## Independent bounded model

[model.py](model.py) is an original Python teaching model, not a port or a
verification of Mnemosyne. It packs bytes into 63-bit payload words and adds
the generation bit within the same assumed atomic 64-bit write.

The caller supplies a trusted record length, ring head and expected generation.
Every destination initially has the opposite generation; surviving writes
replace whole words. No write crosses more than one physical wrap and no slot
is reused within the experiment. For a wrap, the expected generation changes
at physical position zero. These are preconditions, not properties proved by
the model.

For each record length from one to eight encoded words, the enumeration tries
all eight starting positions, both starting generations and every subset of
surviving words. This covers 8,160 states, including 128 complete writes.
All complete records round-trip; all 8,032 incomplete records are rejected.
These are counts of enumerated states, not failure frequencies or probabilities.

Example: a two-word record starts in slot 3 of a four-word ring. Its starting
generation is 1, so slot 3 expects 1 and slot 0 expects 0. If only the later word
survives, recovery must reject the record even though it finds a new word beyond
the missing one. Merely counting matching words elsewhere would be unsound.

## Deliberately broken assumptions

| Case                                                 | What the model demonstrates                                                |
| ---------------------------------------------------- | -------------------------------------------------------------------------- |
| Tag persists separately from payload                 | A new tag can falsely accept old payload.                                  |
| A slot survives two generation flips without rewrite | Its old tag can match again; safe reuse is a separate requirement.         |
| A payload bit changes but the tag does not           | Generation checking accepts corrupt content.                               |
| A separate commit marker arrives before payload      | Marker presence alone cannot establish completeness.                       |
| Head or length metadata is untrusted                 | Outside this model; framing and metadata recovery need their own protocol. |

The stale-generation case is a counterexample to using one bit without reuse
invariants, not a reported bug in Mnemosyne. The model excludes concurrent
writers, log truncation, transaction replay and checksums.

## Space accounting

For B payload bytes, the packed lower bound is
8 * ceil(8B / 63) bytes. The asymptotic tag fraction of encoded bits is 1/64;
the expansion relative to payload is 1/63. These denominators differ.

| Payload bytes | Encoded words | Minimum encoded bytes |
| ------------: | ------------: | --------------------: |
|             1 |             1 |                     8 |
|             7 |             1 |                     8 |
|             8 |             2 |                    16 |
|            63 |             8 |                    64 |
|            64 |             9 |                    72 |
|          4096 |           521 |                  4168 |

This lower bound excludes chunk padding, record headers, ring metadata and
transaction records. In particular, 72 bytes is not a claim about the GCC
port's actual allocation or write traffic for a 64-byte application update.

## Reproduce

With Python 3.10 or later, in this directory:

```sh
python3 -B model.py --check results.json
python3 -B -m unittest -v test_model.py
python3 -B -O -m unittest -v test_model.py
```

[results.json](results.json) is generated deterministically by model.py.
[Tests](test_model.py) cover enumeration, wraparound, missing words, packing,
counterexamples and invalid inputs. No randomness or external dependencies
are used. These checks establish the stated model behaviour only.

Related: [write-ahead log](https://hesela.dev/terms/write-ahead-log/),
[persistence ordering](https://hesela.dev/terms/persistence-ordering/) and
[persistent memory](https://hesela.dev/terms/persistent-memory/).
