# disk_read_latency

The time of one `O_DIRECT` random read from a disk, one read outstanding at a time, with every
read timed on its own: each counted sample is exactly one read, so a Run gives the spread of
single-read latencies (its `op_histogram`) as well as their median.

## What a figure means

The latency of one `pread` of `request_bytes` with `O_DIRECT` from the Fixture the box made at
boot, at the next offset of `disk_read_random`'s seeded permutation of the span's blocks.
Each counted sample is one repetition, one read: 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-read latency in nanoseconds.
The same samples' times, binned as Method `requests` v1 bins times (64 bins per doubling,
each within 1/64 of its time), make the `op_histogram` event (`metric` `read_latency`),
from which any percentile can be read to within that precision.

## What a figure does not show

- Throughput at depth: one read is outstanding at a time. `disk_read_io_uring` keeps many.
- A tail beyond the counted samples: a Run of `LM_SAMPLES` samples times `LM_SAMPLES`
  reads, so its 99.9th percentile means something only when `LM_SAMPLES` is in the
  thousands.
- Each sample's time includes the two clock reads at its edges (`clock_read_ns` in
  `started`, about 20 ns), and the `write` of the previous sample's two events precedes it.

## Parameters

The benchmark's Series and their figures, once it is scheduled, are listed at
[https://api.ledgermath.com/v1/benchmarks/disk_read_latency.json](https://api.ledgermath.com/v1/benchmarks/disk_read_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 holding the Fixture |
| `LM_PARAM_REQUEST_BYTES` | Bytes per read; a multiple of 512, at most 64 MiB |
| `LM_PARAM_SPAN_BYTES` | Bytes of the Fixture the reads cover, from its start; a whole number of requests |
| `LM_SAMPLES` | Counted samples, so reads timed; the histogram's count |

`--quick` uses `/var/tmp/ledgermath/disk`, 4 KiB and 32 samples when the variables are unset,
and reads at most 64 MiB of the span.

## Observed values

Everything [`disk_read_random`'s README](https://ledgermath.com/docs/benchmarks/disk_read_random)
lists under "Observed values" about the file and its device, and: `request_bytes`,
`queue_depth` (1), `span_bytes`, `span_blocks`, `bytes_per_repetition` (`request_bytes`) and
`warmup_reads` (the reads of the one warmup sample, 64).

## Findings

No Run on EC2 has been examined yet. On a developer's local NVMe disk, `--quick` reads of 4 KiB
took 240 to 290 µs.

## How it works

One warmup sample of 64 reads, then `LM_SAMPLES` counted samples of one read each, by
`lm_hist_measure` (`benchmarks/harness/c/lm_hist.h`), which adds each counted sample's time,
from the clock reads at its edges, to the histogram. Events of its own, after measuring:
`device_counters` (as `disk_read_random`'s, over `lm_hist_measure`), `work` (`reads`,
`reads_timed`, `bytes_read`, `distinct_blocks`, `permutation_passes`) and `op_histogram`
(`count`, `min_ns`, `max_ns`, `sum_ns`, and `bins` as `[lower_ns, upper_ns, count]` with
`upper_ns` exclusive, empty bins left out).

The elision guard is `disk_read_random`'s: `verified` is the running sum of the first word of
every block read; after measuring it is recomputed from the Fixture's pattern at the offsets
read, and a mismatch exits 1. The Fixture's checks before measuring are also
`disk_read_random`'s.

## Building and testing

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

(After making a Fixture there, as `disk_read_random`'s README does; the directory is an example.)

---

Authorship: Written entirely by AI.
