Skip to content

Commit

Permalink
io_uring: optimize the instance flags
Browse files Browse the repository at this point in the history
We can use IORING_SETUP_SINGLE_ISSUER and IORING_SETUP_DEFER_TASKRUN no
problem.
  • Loading branch information
Cloudef committed Jun 21, 2024
1 parent 4c2c314 commit 98a543e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
7 changes: 7 additions & 0 deletions docs/pages/aio-dynamic.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ const res = try work.complete(.blocking);
// returns immediately
const res = try work.complete(.nonblocking);
```

To complete all operations within the dynamic instance, use `completeAll`.
This blocks until all the operations are complete and returns the number of errors, if any.

```zig
const num_errors = try work.completeAll();
```
17 changes: 14 additions & 3 deletions src/aio.zig
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ pub const Dynamic = struct {
pub inline fn complete(self: *@This(), mode: CompletionMode) Error!CompletionResult {
return self.io.complete(mode);
}

/// Block until all opreations are complete
/// Returns the number of errors occured, 0 if there were no errors
pub inline fn completeAll(self: *@This()) Error!u16 {
var num_errors: u16 = 0;
while (true) {
const res = try self.io.complete(.blocking);
num_errors += res.num_errors;
if (res.num_completed == 0) break;
}
return num_errors;
}
};

/// Completes a list of operations immediately, blocks until complete
Expand Down Expand Up @@ -352,9 +364,8 @@ test "Cancel" {
try std.testing.expectEqual(0, tmp.num_errors);
try std.testing.expectEqual(0, tmp.num_completed);
try dynamic.queue(Cancel{ .id = id });
const res = try dynamic.complete(.blocking);
try std.testing.expectEqual(1, res.num_errors);
try std.testing.expectEqual(2, res.num_completed);
const num_errors = try dynamic.completeAll();
try std.testing.expectEqual(1, num_errors);
try std.testing.expectEqual(error.OperationCanceled, err);
try std.testing.expect(timer.lap() < std.time.ns_per_s);
}
Expand Down
23 changes: 20 additions & 3 deletions src/aio/IoUring.zig
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const ProbeOpsResult = struct {
};

inline fn uring_probe_ops(mem: *ProbeOpsBuffer) !ProbeOpsResult {
var io = try std.os.linux.IoUring.init(2, 0);
var io = try uring_init(2);
defer io.deinit();
var fba = std.heap.FixedBufferAllocator.init(mem);
var pbuf = fba.allocator().alloc(u8, @sizeOf(std.os.linux.io_uring_probe) + 256 * @sizeOf(std.os.linux.io_uring_probe_op)) catch unreachable;
Expand All @@ -155,18 +155,35 @@ inline fn uring_probe_ops(mem: *ProbeOpsBuffer) !ProbeOpsResult {
return .{ .last_op = probe.last_op, .ops = ops[0..probe.ops_len] };
}

inline fn uring_init(n: u16) aio.Error!std.os.linux.IoUring {
return std.os.linux.IoUring.init(n, 0) catch |err| switch (err) {
inline fn uring_init_inner(n: u16, flags: u32) !std.os.linux.IoUring {
return std.os.linux.IoUring.init(n, flags) catch |err| switch (err) {
error.PermissionDenied,
error.SystemResources,
error.SystemOutdated,
error.ProcessFdQuotaExceeded,
error.SystemFdQuotaExceeded,
=> |e| e,
error.ArgumentsInvalid => error.ArgumentsInvalid,
else => error.Unexpected,
};
}

inline fn uring_init(n: u16) aio.Error!std.os.linux.IoUring {
const flags: []const u32 = &.{
std.os.linux.IORING_SETUP_SINGLE_ISSUER | std.os.linux.IORING_SETUP_DEFER_TASKRUN, // 6.1
std.os.linux.IORING_SETUP_SINGLE_ISSUER | std.os.linux.IORING_SETUP_COOP_TASKRUN, // 6.0
std.os.linux.IORING_SETUP_COOP_TASKRUN, // 5.9
0, // 5.4
};
for (flags) |f| {
return uring_init_inner(n, f) catch |err| switch (err) {
error.ArgumentsInvalid => continue,
else => |e| return e,
};
}
return error.SystemOutdated;
}

inline fn uring_queue(io: *std.os.linux.IoUring, op: anytype, user_data: u64) aio.Error!void {
debug("queue: {}: {}", .{ user_data, comptime Operation.tagFromPayloadType(@TypeOf(op.*)) });
const Trash = struct {
Expand Down

0 comments on commit 98a543e

Please sign in to comment.