# disk_write_io_uring

How many writes a disk completes when one thread keeps `queue_depth` of them submitted,
`O_DIRECT` through `io_uring`, each durable when it completes (`RWF_DSYNC`) or not, with the
time of each write from submission to reap binned in an `op_histogram`.

## What a figure means

The write counterpart of `disk_read_io_uring`: the binary keeps `queue_depth` writes of
`request_bytes` submitted to a scratch file of its own. One repetition reaps one completed
write and submits one in its place, so a sample's time over its repetitions is the time per
completion: 10^9 divided by Method `runs` v1's figure is writes per second, and
`bytes_per_repetition` (`request_bytes`) times that is bytes per second. With `sync` `dsync`
every write carries `RWF_DSYNC`, so it completes only once the device holds it durably.

The `op_histogram` (`metric` `io_uring_write_latency`) holds, for every write reaped in the
counted samples, the time from just before the `io_uring_enter` that submitted it to just
after its completion was taken, binned as Method `requests` v1 bins times.

## What a figure does not show

- `queue_depth` counts writes submitted and not yet reaped, not writes the device held at any
  instant (as in `disk_read_io_uring`).
- A histogram time is submission to reap: it includes any time a completion waited in the
  ring while the thread handled another. It is not the device's own service time.
- Timing each write costs two clock reads per repetition inside the measured loop
  (`clock_read_ns` in `started` each).
- With `sync` `none` a write completes once the device has taken it, in a volatile cache if it
  has one.

## Parameters

The benchmark's Series and their figures, once it is scheduled, are listed at
[https://api.ledgermath.com/v1/benchmarks/disk_write_io_uring.json](https://api.ledgermath.com/v1/benchmarks/disk_write_io_uring.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_ACCESS` | `sequential`: consecutive blocks from the file's start, wrapping. `random`: a seeded permutation of its blocks (`disk_read_random`'s seed) |
| `LM_PARAM_REQUEST_BYTES` | Bytes per write; a multiple of 512, at most 16 MiB |
| `LM_PARAM_QUEUE_DEPTH` | Writes kept submitted and not yet reaped; 1 to 4096 |
| `LM_PARAM_FILE_BYTES` | The scratch file's size; at least `queue_depth` requests, a whole number of 4 KiB blocks |
| `LM_PARAM_SYNC` | `dsync` (`RWF_DSYNC` on every write) or `none` (`O_DIRECT` alone) |

`--quick` uses `/var/tmp/ledgermath/disk`, `sequential`, 4 KiB, 16, 1 GiB and `dsync` when
the variables are unset, and writes a scratch file of at most 64 MiB.

## 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, here the scratch file, and:
`access`, `sync_flag` (`RWF_DSYNC` or `none`), `request_bytes`, `queue_depth`, `file_blocks`,
`bytes_per_repetition`, `ring_sq_entries`, `ring_cq_entries` and `ring_features`.

## Findings

No Run on EC2 has been examined yet.

## How it works

The ring is `disk_read_io_uring`'s: raw `io_uring_setup` and `io_uring_enter`, no setup flags,
no liburing; each write an `IORING_OP_WRITE` from a buffer of the process's own. A refusal of
`io_uring_setup` with `ENOSYS` or `EPERM` exits 2, saying
`error: io_uring is not available here: ...`.

The scratch file, `ledgermath-disk_write_io_uring.scratch` in `dir`, is made as `disk_write`
makes its own: exclusively (`O_CREAT | O_EXCL | O_NOFOLLOW`), refusing any existing name with
exit 1 and touching nothing; written in full with `O_DIRECT` and `fdatasync`, checked by FIEMAP;
deleted at exit only if its name is still the file the binary made. Two writes in flight never
share a block.

The histogram holds every write reaped in the counted samples, timed from just before the
`io_uring_enter` that submitted it to the moment its completion was seen (before anything is
submitted in its place), so `count` is `LM_SAMPLES` x R. The samples are `lm_measure`'s, taken
as `disk_read_io_uring` takes them (the harness's `lm_deferred.c`): nothing is written to
stdout while writes are in flight, and every sample's events, then `verified`, come after the
drain.

Events of its own: `device_counters` (over `lm_measure`), `work` (as `disk_read_io_uring`'s,
with `bytes_written` and `file_passes`) and `op_histogram`. The elision guard: each write's
first word is a tag of its index, the rest the Fixture pattern; `verified` is the running sum
of the tags of the writes reaped. After the drain every write must have completed, the sum
must equal the tags of every index submitted, and the last writes (at least 64, one per
block) must read back as written; otherwise exit 1.

## Building and testing

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

---

Authorship: Written entirely by AI.
