Skip to content
baskingspotPublic

About

An io_uring completion loop for Zig: one ring, one tick, and a user_data type you define.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Repository files navigation

uring

An io_uring completion loop for Zig: one ring, one tick, and a user_data type you define.

Zig's standard library already encodes every io_uring operation. What it does not have is the loop around them: when to submit, when to enter, and what runs when. uring is that loop, and nothing else.

It is parameterized by your tag, the value user_data carries home, so a completion means whatever you say it means. The handler is known at comptime, which makes every completion a direct call. Nothing allocates after init, and the loop never closes a descriptor, never frees a byte, and never rearms an operation.

const std = @import("std");
const uring = @import("uring");

const Tag = packed struct(u64) { op: u8, slot: u16, gen: u16, spare: u24 };

var cqes: [64]std.os.linux.io_uring_cqe = undefined;
var loop: uring.Loop(Tag) = try .init(.{
    .cqes = &cqes,
    .setup = .{ .single_issuer = true, .defer_taskrun = true },
});
defer loop.deinit();

_ = try loop.recv(.{ .op = 1, .slot = 7, .gen = 1 }, fd, .{ .buffer = buf }, 0);

// One pass per call. The deadline is the caller's, computed from the loop's
// clock every tick, because a moving deadline is application state.
while (!loop.stopped) {
    const next = server.nextDeadline(loop.now());
    try loop.tick(&server, Server.onCompletion, .{ .until = next });
}

A program with no deadlines of its own calls run instead, which ticks until stop.

Deadlines ride the enter, not an operation: io_uring_enter takes a timeout through IORING_ENTER_EXT_ARG, so moving a deadline costs no submission and no cancel.

What is in it

  • The loop: Loop(Tag).tick, run, post, stop, and now. One pass publishes, reaps, enters, dispatches, and submits, in that order.
  • The tag: any 64-bit type. ping, a packed struct, a pointer. Cqe decodes it once and hands it to a comptime handler.
  • Waiting: Wait.poll, no_wait, once, and until, and what each costs.
  • Deadlines: a deadline through the enter with no submitted operation, and timeout when you want the kernel to own the clock.
  • Operations: every opcode the standard library encodes, each taking your tag and returning the SQE.
  • Registered resources: files, buffers, eventfds, NAPI, and the probe.
  • Provided buffers: BufferGroup with a multishot receive that fills ring buffers the kernel picks. std requests incremental consumption (a completion is a chunk); initBufferGroup(..., .{ .incremental = false }) registers whole-buffer mode, where a completion is one message on a message socket.
  • The ring: Ring, its setup struct, and the submit discipline that keeps the queue from filling.
  • Performance: the loop against the equivalent hand-written loop, measured: about 3 ns per completion and two clock reads per idle pass.

Not implemented:

  • msg_ring, futex operations, tee, madvise, openat2, timeout_update, and a generic URING_CMD for non-socket commands: the standard library does not encode them. Build the SQE through Loop.sqe if you need one.
  • Registration for personalities, restrictions, io-wq worker limits, sync_cancel, clamp, pbuf_status, and files_update2: same reason.

Install

zig fetch --save https://baskingspot.dev/uring/v0.4.0.tar.gz

Add the module to your build:

const uring = b.dependency("uring", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("uring", uring.module("uring"));

uring builds with Zig 0.17, on Linux only, because io_uring is Linux.

Documentation

  • The guide says which piece to use for a task, with examples that compile.
  • The API reference documents every symbol.

Work on it

just ci

nix develop provides the toolchain. just lists the recipes, and just ci runs what CI runs. just bench compares the loop against the equivalent hand-written loop.

License

MIT. See LICENSE. The logo files in docs/static/ are baskingspot marks and are not covered by it. See docs/static/LICENSE.

How it's made

Built with LLM assistance. How, in detail.

baskingspot

About

An io_uring completion loop for Zig: one ring, one tick, and a user_data type you define.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages