# disk_write_latency

The time of one durable write to a disk: a buffered `pwrite` followed by `fdatasync`, one at a
time, with every write timed on its own: each counted sample is exactly one write, so a Run
gives the spread of single-write latencies (its `op_histogram`) as well as their median.

## What a figure means

The latency of one `pwrite` of `request_bytes` at the next offset of a scratch file of the
binary's own, then `fdatasync` in the `fdatasync` sets: what a database's commit to a log
waits for. Each counted sample is one repetition, one write: the harness reads the clock at
the sample's two edges and nowhere inside. Method `runs` v1's figure, the median of a Run's
counted samples' times per repetition, is therefore the median single-write latency in
nanoseconds. The same samples' times, binned as Method `requests` v1 bins times, make the
`op_histogram` event (`metric` `write_latency`).

## What a figure does not show

- With `sync` `none` the figure is the page cache's, not the device's: `device_counters`
  shows how few writes reached the device.
- Throughput at depth: one write is outstanding at a time. `disk_write_io_uring` keeps many.
- A tail beyond the counted samples: a Run times `LM_SAMPLES` writes.
- The file system's journal and metadata writes are in each `fdatasync`'s time, as they are
  in an application's.

## Parameters

The benchmark's Series and their figures, once it is scheduled, are listed at
[https://api.ledgermath.com/v1/benchmarks/disk_write_latency.json](https://api.ledgermath.com/v1/benchmarks/disk_write_latency.json).

| Variable | Meaning |
|---|---|
| `LM_PARAM_DISK` | `root_volume`, `instance_store`, or a target's EBS volume (`gp3_16k`, `io2_16k`, `io2_32k`, at `/mnt/lm/<name>`): the Series' name for the disk; never read here |
| `LM_PARAM_DIR` | The directory the scratch file is made in |
| `LM_PARAM_REQUEST_BYTES` | Bytes per write; a multiple of 8, at most 64 MiB |
| `LM_PARAM_SYNC` | `fdatasync`: `pwrite`, then `fdatasync`. `none`: `pwrite` alone |
| `LM_PARAM_FILE_BYTES` | The scratch file's size; writes wrap at its end. A whole number of requests and of 4 KiB blocks |
| `LM_SAMPLES` | Counted samples, so writes timed; the histogram's count |

`--quick` uses `/var/tmp/ledgermath/disk`, 8 KiB, `fdatasync`, 256 MiB and 32 samples when the
variables are unset, and writes a scratch file of at most 16 MiB.

## Observed values

Everything [`disk_write`'s README](https://ledgermath.com/docs/benchmarks/disk_write) lists
under "Observed values", and `warmup_writes` (the writes of the one warmup sample, 64).

## Findings

No Run on EC2 has been examined yet. On a developer's local NVMe disk, `--quick` writes of
8 KiB with `fdatasync` took 34 to 38 µs.

## How it works

The scratch file, `ledgermath-disk_write_latency.scratch` in `dir`, is made as `disk_write`
makes its own: exclusively (`O_CREAT | O_EXCL | O_NOFOLLOW`, relative to `dir`), so any
existing name, a symbolic link included, is refused with exit 1 and left untouched; written in
full with `O_DIRECT` and `fdatasync`, and checked by FIEMAP to have no unwritten extent;
deleted at exit only if its name is still the file the binary made (same device and inode).

One write is checked by reading it back before measuring. Then one warmup sample of 64 writes,
and `LM_SAMPLES` counted samples of one write each, by `lm_hist_measure`
(`benchmarks/harness/c/lm_hist.h`). Events of its own, after measuring: `device_counters`
(over `lm_hist_measure`), `work` (`writes`, `writes_checked_before_measuring`,
`writes_timed`, `fdatasyncs`, `bytes_written`, `file_passes`) and `op_histogram`.

The elision guard is `disk_write`'s: the first word of each write is a tag of its offset;
`verified` is their running sum, recomputed after measuring, and the last block written must
read back as written. Either mismatch exits 1.

## Building and testing

```bash
make -C benchmarks/disk_write_latency             # build/static/disk_write_latency
LM_PARAM_DIR=/var/tmp/ledgermath-test ./benchmarks/disk_write_latency/build/static/disk_write_latency --quick
bun test tests/disk-latency.test.ts
```

---

Authorship: Written entirely by AI.
