nop.dhover×48all
#!/usr/bin/env dub
/+ dub.sdl:
    name "io_uring_nop"
    dependency "sparkles:event-horizon-sys" path="../../../../.."
    platforms "linux"
    targetPath "build"
+/
/**
 * `io_uring` — the minimal submit/complete cycle (`IORING_OP_NOP`, Linux 5.1).
 *
 * This is the "hello world" of `io_uring`: it sets up a ring, places one
 * `NOP` submission queue entry (SQE), submits it, waits for the matching
 * completion queue entry (CQE), and verifies the `user_data` cookie round-trips.
 * It exercises every part of the core machinery from the 5.1 introduction —
 * `io_uring_setup` (here via `Uring.setup`), the shared SQ/CQ rings, and the
 * `submit` / `wait` / `front` / `popFront` flow that every later op reuses.
 *
 * Companion to the io_uring chronology:
 * see docs/research/async-io/io-uring/timeline.md § "5.1 — The introduction".
 *
 * Run with: `dub run --single nop.d`
 *
 * Portability: if the running kernel has no `io_uring` (too old, or blocked by a
 * seccomp/container policy), the program prints a `SKIP:` line and exits 0 so it
 * stays green in CI regardless of the host kernel.
 */
module 
(module) io_uring_nop

io_uring — the minimal submit/complete cycle (IORING_OP_NOP, Linux 5.1).

This is the "hello world" of io_uring: it sets up a ring, places one NOP submission queue entry (SQE), submits it, waits for the matching completion queue entry (CQE), and verifies the user_data cookie round-trips. It exercises every part of the core machinery from the 5.1 introduction — io_uring_setup (here via Uring.setup), the shared SQ/CQ rings, and the submit / wait / front / popFront flow that every later op reuses.

Companion to the io_uring chronology: see docs/research/async-io/io-uring/timeline.md § "5.1 — The introduction".

Run with: dub run --single nop.d

Portability

if the running kernel has no io_uring (too old, or blocked by a seccomp/container policy), the program prints a SKIP: line and exits 0 so it stays green in CI regardless of the host kernel.

io_uring_nop
;
import
(package) sparkles
sparkles
.
(package) sparkles.event_horizon
event_horizon
.
(package) sparkles.event_horizon.sys
sys
.
(module) sparkles.event_horizon.sys.uring

Simple idiomatic dlang wrapper around linux io_uring (see: https://kernel.dk/io_uring.pdf) asynchronous API.

Vendored from tchaloupka/during by Tomáš Chaloupka: release 0.6.0, commit 3cd81909a4204b94da7de9f7b9d8e707a7360e74, which is also upstream's latest, so no untagged fixes are left behind. The three modules during, during.io_uring and during.openat2 are sparkles.event_horizon.sys.uring`` and its io_uring and openat2 submodules; all three compile to nothing outside Linux. Upstream's own test suite (tests/, run by silly) is not vendored: this package's suite tests what Sparkles changes or relies on.

Changed from upstream:

  • the module names;

  • openat2 is version (linux) like the other two;

  • the io_uring_* system-call wrappers call druntime's core.sys.linux.unistd.syscall (c_long syscall(c_long, ...)) instead of declaring their own int syscall(int, ...): compiled into one LDC module set with druntime's, the two prototypes clash;

  • Uring.submitAndWait(want, args) with nothing to submit waits with args: upstream dropped them there, so a wait with a deadline blocked until a completion arrived.

  • prepCancel64 (liburing's io_uring_prep_cancel64) cancels by a raw user_data value: prepCancel cancels by the address of a variable, which cannot name a request tagged with setUserDataRaw.

  • prepCmdGetsockname notes Linux 6.19, where the kernel added the command, not 6.7.

  • eight RegisterOpCode members note the release that added them, from the kernel's history: CLOCK and CLONE_BUFFERS 6.12, SEND_MSG_RING and RESIZE_RINGS 6.13, ZCRX_IFQ 6.15, QUERY 6.18, ZCRX_CTRL 6.19 and BPF_FILTER 7.0;

  • the query interface (linux/io_uring/query.h, Linux 6.18), which upstream does not bind: io_uring_query and the io_uring_query_* structs.

Why vendored: Sparkles' io_uring backend (sparkles:event-horizon) stands on this binding and worked around two of its defects in its own code instead of fixing them here; owning the copy fixes them at the source and lets new kernel operations land as the kernel ships them. As a dub package it also put during and, through its unittest configuration, silly (which Sparkles does not use) into the dub.selections.json of every package that reaches the loop and into the Nix dub locks. It is the raw kernel interface, with no event loop, so it belongs to the loop-free system-call layer.

@copyrightCopyright © 2019, Tomáš Chaloupka@authorsTomáš Chaloupka, with contributions from Petar Kirov, Baruch Even and Sönke Ludwig (the upstream history to the vendored commit)@licenseBSL-1.0
uring
;
import
(package) std
std
.
(module) std.stdio
Category Symbols
File handles _popen File isFileHandle openNetwork stderr stdin stdout
Reading chunks lines readf readfln readln
Writing toFile write writef writefln writeln
Misc KeepTerminator LockType StdioException

Standard I/O functions that extend core.stdc.stdio. core.stdc.stdio is publically imported when importing std.stdio.

There are three layers of I/O:

  1. The lowest layer is the operating system layer. The two main schemes are Windows and Posix.

  2. C's stdio.h which unifies the two operating system schemes.

  3. std.stdio, this module, unifies the various stdio.h implementations into a high level package for D programs.

Source

std/stdio.d

@copyrightCopyright The D Language Foundation 2007-.@licenseBoost License 1.0.@authorsWalter Bright, Andrei Alexandrescu, Alex Rønne Petersen
stdio
:
(alias template) io_uring_nop.writefln = std.stdio.writefln(alias fmt, A...)(A args) if (isSomeString!(typeof(fmt)))

Equivalent to writef(fmt, args, '\n').

writefln
, stderr;
int
int D main()
main
()
{ // `IORING_OP_NOP` carries this cookie through the kernel and hands it back // on the CQE — the mechanism every op uses to correlate completions. enum ulong
(constant) ulong io_uring_nop.main.cookie = 12648430LU
cookie
= 0xC0FFEE;
(struct) sparkles.event_horizon.sys.uring.Uring

Main entry point to work with io_uring.

It hides SubmissionQueue and CompletionQueue behind standard range interface. We put in SubmissionEntry entries and take out CompletionEntry entries.

Use predefined prepXX methods to fill required fields of SubmissionEntry before put or during putWith.

Note

prepXX functions doesn't touch previous entry state, just fills in operation properties. This is because for less error prone interface it is cleared automatically when prepared using putWith. So when using on own SubmissionEntry (outside submission queue), that would be added to the submission queue using put, be sure its cleared if it's reused for multiple operations.

Uring
(local variable) sparkles.event_horizon.sys.uring.Uring io
io
;
const
(local variable) const(int) setupRet
setupRet
=
(local variable) sparkles.event_horizon.sys.uring.Uring io
io
.
int sparkles.event_horizon.sys.uring.setup(ref sparkles.event_horizon.sys.uring.Uring uring, uint entries = 128u, sparkles.event_horizon.sys.uring.io_uring.SetupFlags flags = SetupFlags.NONE) nothrow @nogc @safe

Setup new instance of io_uring into provided Uring structure.

@paramuring Uring structure to be initialized (must not be already initialized)@paramentries Number of entries to initialize uring with@paramflags SetupFlags to use to initialize uring.@returnsOn succes it returns 0, -errno otherwise.
setup
(8);
if (
(local variable) const(int) setupRet
setupRet
< 0)
{
void std.stdio.writefln!(char, const(int))(in char[] fmt, const(int) __param_1) @safe

Equivalent to writef(fmt, args, '\n').

writefln
("SKIP: io_uring_setup failed (errno %d) — io_uring unavailable on this host", -
(local variable) const(int) setupRet
setupRet
);
return 0; } // Place a single NOP SQE. `putWith` clears the entry, runs the prep callback, // and advances the submission queue tail in one step.
(local variable) sparkles.event_horizon.sys.uring.Uring io
io
.putWith!((ref SubmissionEntry e) {
e.prepNop(); e.user_data = cookie; })(); const
(local variable) const(int) submitted
submitted
=
(local variable) sparkles.event_horizon.sys.uring.Uring io
io
.
int sparkles.event_horizon.sys.uring.Uring.submit(uint want) nothrow @nogc @safe

Submits qued SubmissionEntry to be processed by kernel.

@paramwant number of CompletionEntries to wait for. If 0, this just submits queued entries and returns. If > 0, it blocks until at least wanted number of entries were completed.@paramsig See io_uring_enter(2) man page@returnsNumber of submitted entries on success, -errno on error
submit
(1);
if (
(local variable) const(int) submitted
submitted
< 0)
{ stderr.
std.stdio.File std.stdio.makeGlobal!"core.stdc.stdio.stderr"() nothrow @nogc @property ref @system
writefln
("submit failed: errno %d", -
(local variable) const(int) submitted
submitted
);
return 1; } // Block until at least one completion is ready, then consume it.
(local variable) sparkles.event_horizon.sys.uring.Uring io
io
.
int sparkles.event_horizon.sys.uring.Uring.wait(uint want = 1u) nothrow @nogc

Simmilar to submit but with this method we just wait for required number of CompletionEntries.

@returns0 on success, -errno on error
wait
(1);
const
(local variable) const(int) res
res
=
(local variable) sparkles.event_horizon.sys.uring.Uring io
io
.
sparkles.event_horizon.sys.uring.io_uring.CompletionEntry sparkles.event_horizon.sys.uring.Uring.front() pure nothrow @nogc return ref @safe

Get first CompletionEntry from cq ring

front
.
(field) int sparkles.event_horizon.sys.uring.io_uring.CompletionEntry.res

result code for this event

res
;
const
(local variable) const(ulong) echoed
echoed
=
(local variable) sparkles.event_horizon.sys.uring.Uring io
io
.
sparkles.event_horizon.sys.uring.io_uring.CompletionEntry sparkles.event_horizon.sys.uring.Uring.front() pure nothrow @nogc return ref @safe

Get first CompletionEntry from cq ring

front
.
(field) ulong sparkles.event_horizon.sys.uring.io_uring.CompletionEntry.user_data

sqe->data submission passed back

user_data
;
(local variable) sparkles.event_horizon.sys.uring.Uring io
io
.
void sparkles.event_horizon.sys.uring.Uring.popFront() pure nothrow @nogc @safe

Move to next CompletionEntry

popFront
();
if (
(local variable) const(int) res
res
< 0)
{ stderr.
std.stdio.File std.stdio.makeGlobal!"core.stdc.stdio.stderr"() nothrow @nogc @property ref @system
writefln
("NOP completed with error: errno %d", -
(local variable) const(int) res
res
);
return 1; } if (
(local variable) const(ulong) echoed
echoed
!=
(constant) ulong io_uring_nop.main.cookie = 12648430LU
cookie
)
{ stderr.
std.stdio.File std.stdio.makeGlobal!"core.stdc.stdio.stderr"() nothrow @nogc @property ref @system
writefln
("user_data mismatch: expected 0x%X, got 0x%X",
(constant) ulong io_uring_nop.main.cookie = 12648430LU
cookie
,
(local variable) const(ulong) echoed
echoed
);
return 1; }
void std.stdio.writefln!(char, const(int), const(ulong))(in char[] fmt, const(int) __param_1, const(ulong) __param_2) @safe

Equivalent to writef(fmt, args, '\n').

writefln
("ok: NOP completed (res=%d), user_data 0x%X round-tripped through the ring",
(local variable) const(int) res
res
,
(local variable) const(ulong) echoed
echoed
);
return 0; }