authorgravatar for joran@ronomon.comJoran Dirk Greef <joran@ronomon.com> 2020-12-09 19:06:28+02:00
committergravatar for joran@ronomon.comJoran Dirk Greef <joran@ronomon.com> 2020-12-09 19:06:28+02:00
log349d32dc2c810525e840bcae3cb7c1ca923195c9
tree26459a138e7ad0dcf0b1de58873f720438f14586
parent3599fb9bfc221c37cdd59d3b19a10eff63a4170e

Add io_uring TIMEOUT and TIMEOUT_REMOVE operations:

ring.timeout() to queue a IORING_OP_TIMEOUT operation ring.timeout_remove() to queue a IORING_OP_TIMEOUT_REMOVE operation io_uring_prep_timeout() to prep a IORING_OP_TIMEOUT sqe io_uring_prep_timeout_remove() to prep a IORING_OP_TIMEOUT_REMOVE sqe

1 files changed, 176 insertions(+), 0 deletions(-)

lib/std/os/linux/io_uring.zig+176
......@@ -512,6 +512,52 @@ pub const IO_Uring = struct {
512512 return sqe;
513513 }
514514
515 /// Queues (but does not submit) an SQE to register a timeout operation.
516 /// Returns a pointer to the SQE.
517 ///
518 /// The timeout will complete when either the timeout expires, or after the specified number of
519 /// events complete (if `count` is greater than `0`).
520 ///
521 /// `flags` may be `0` for a relative timeout, or `IORING_TIMEOUT_ABS` for an absolute timeout.
522 ///
523 /// The completion event result will be `-ETIME` if the timeout completed through expiration,
524 /// `0` if the timeout completed after the specified number of events, or `-ECANCELED` if the
525 /// timeout was removed before it expired.
526 ///
527 /// io_uring timeouts use the `CLOCK_MONOTONIC` clock source.
528 pub fn timeout(
529 self: *IO_Uring,
530 user_data: u64,
531 ts: *const os.timespec,
532 count: u32,
533 flags: u32,
534 ) !*io_uring_sqe {
535 const sqe = try self.get_sqe();
536 io_uring_prep_timeout(sqe, ts, count, flags);
537 sqe.user_data = user_data;
538 return sqe;
539 }
540
541 /// Queues (but does not submit) an SQE to remove an existing timeout operation.
542 /// Returns a pointer to the SQE.
543 ///
544 /// The timeout is identified by its `user_data`.
545 ///
546 /// The completion event result will be `0` if the timeout was found and cancelled successfully,
547 /// `-EBUSY` if the timeout was found but expiration was already in progress, or
548 /// `-ENOENT` if the timeout was not found.
549 pub fn timeout_remove(
550 self: *IO_Uring,
551 user_data: u64,
552 timeout_user_data: u64,
553 flags: u32,
554 ) !*io_uring_sqe {
555 const sqe = try self.get_sqe();
556 io_uring_prep_timeout_remove(sqe, timeout_user_data, flags);
557 sqe.user_data = user_data;
558 return sqe;
559 }
560
515561 /// Registers an array of file descriptors.
516562 /// Every time a file descriptor is put in an SQE and submitted to the kernel, the kernel must
517563 /// retrieve a reference to the file, and once I/O has completed the file reference must be
......@@ -824,6 +870,34 @@ pub fn io_uring_prep_close(sqe: *io_uring_sqe, fd: os.fd_t) void {
824870 };
825871}
826872
873pub fn io_uring_prep_timeout(
874 sqe: *io_uring_sqe,
875 ts: *const os.timespec,
876 count: u32,
877 flags: u32,
878) void {
879 io_uring_prep_rw(.TIMEOUT, sqe, -1, ts, 1, count);
880 sqe.rw_flags = flags;
881}
882
883pub fn io_uring_prep_timeout_remove(sqe: *io_uring_sqe, timeout_user_data: u64, flags: u32) void {
884 sqe.* = .{
885 .opcode = .TIMEOUT_REMOVE,
886 .flags = 0,
887 .ioprio = 0,
888 .fd = -1,
889 .off = 0,
890 .addr = timeout_user_data,
891 .len = 0,
892 .rw_flags = flags,
893 .user_data = 0,
894 .buf_index = 0,
895 .personality = 0,
896 .splice_fd_in = 0,
897 .__pad2 = [2]u64{ 0, 0 },
898 };
899}
900
827901test "structs/offsets/entries" {
828902 if (builtin.os.tag != .linux) return error.SkipZigTest;
829903
......@@ -1216,3 +1290,105 @@ test "accept/connect/send/recv" {
12161290
12171291 testing.expectEqualSlices(u8, buffer_send[0..buffer_recv.len], buffer_recv[0..]);
12181292}
1293
1294test "timeout (after a relative time)" {
1295 if (builtin.os.tag != .linux) return error.SkipZigTest;
1296
1297 var ring = IO_Uring.init(1, 0) catch |err| switch (err) {
1298 error.SystemOutdated => return error.SkipZigTest,
1299 error.PermissionDenied => return error.SkipZigTest,
1300 else => return err,
1301 };
1302 defer ring.deinit();
1303
1304 const ms = 10;
1305 const margin = 5;
1306 const ts = os.timespec{ .tv_sec = 0, .tv_nsec = ms * 1000000 };
1307
1308 const started = std.time.milliTimestamp();
1309 const sqe = try ring.timeout(0x55555555, &ts, 0, 0);
1310 testing.expectEqual(linux.IORING_OP.TIMEOUT, sqe.opcode);
1311 testing.expectEqual(@as(u32, 1), try ring.submit());
1312 const cqe = try ring.copy_cqe();
1313 const stopped = std.time.milliTimestamp();
1314
1315 testing.expectEqual(linux.io_uring_cqe{
1316 .user_data = 0x55555555,
1317 .res = -linux.ETIME,
1318 .flags = 0,
1319 }, cqe);
1320 testing.expectWithinMargin(@intToFloat(f64, ms), @intToFloat(f64, stopped - started), margin);
1321}
1322
1323test "timeout (after a number of completions)" {
1324 if (builtin.os.tag != .linux) return error.SkipZigTest;
1325
1326 var ring = IO_Uring.init(2, 0) catch |err| switch (err) {
1327 error.SystemOutdated => return error.SkipZigTest,
1328 error.PermissionDenied => return error.SkipZigTest,
1329 else => return err,
1330 };
1331 defer ring.deinit();
1332
1333 const ts = os.timespec{ .tv_sec = 3, .tv_nsec = 0 };
1334 const count_completions: u64 = 1;
1335 const sqe_timeout = try ring.timeout(0x66666666, &ts, count_completions, 0);
1336 testing.expectEqual(linux.IORING_OP.TIMEOUT, sqe_timeout.opcode);
1337 testing.expectEqual(count_completions, sqe_timeout.off);
1338 _ = try ring.nop(0x77777777);
1339 testing.expectEqual(@as(u32, 2), try ring.submit());
1340
1341 const cqe_nop = try ring.copy_cqe();
1342 testing.expectEqual(linux.io_uring_cqe{
1343 .user_data = 0x77777777,
1344 .res = 0,
1345 .flags = 0,
1346 }, cqe_nop);
1347
1348 const cqe_timeout = try ring.copy_cqe();
1349 testing.expectEqual(linux.io_uring_cqe{
1350 .user_data = 0x66666666,
1351 .res = 0,
1352 .flags = 0,
1353 }, cqe_timeout);
1354}
1355
1356test "timeout_remove" {
1357 if (builtin.os.tag != .linux) return error.SkipZigTest;
1358
1359 var ring = IO_Uring.init(2, 0) catch |err| switch (err) {
1360 error.SystemOutdated => return error.SkipZigTest,
1361 error.PermissionDenied => return error.SkipZigTest,
1362 else => return err,
1363 };
1364 defer ring.deinit();
1365
1366 const ts = os.timespec{ .tv_sec = 3, .tv_nsec = 0 };
1367 const sqe_timeout = try ring.timeout(0x88888888, &ts, 0, 0);
1368 testing.expectEqual(linux.IORING_OP.TIMEOUT, sqe_timeout.opcode);
1369 testing.expectEqual(@as(u64, 0x88888888), sqe_timeout.user_data);
1370
1371 const sqe_timeout_remove = try ring.timeout_remove(0x99999999, 0x88888888, 0);
1372 testing.expectEqual(linux.IORING_OP.TIMEOUT_REMOVE, sqe_timeout_remove.opcode);
1373 testing.expectEqual(@as(u64, 0x88888888), sqe_timeout_remove.addr);
1374 testing.expectEqual(@as(u64, 0x99999999), sqe_timeout_remove.user_data);
1375
1376 testing.expectEqual(@as(u32, 2), try ring.submit());
1377
1378 const cqe_timeout = try ring.copy_cqe();
1379 if (cqe_timeout.user_data == 0x99999999 and cqe_timeout.res == -linux.EINVAL) {
1380 return error.SkipZigTest;
1381 }
1382 testing.expectEqual(linux.io_uring_cqe{
1383 .user_data = 0x88888888,
1384 .res = -linux.ECANCELED,
1385 .flags = 0,
1386 }, cqe_timeout);
1387
1388 const cqe_timeout_remove = try ring.copy_cqe();
1389 testing.expectEqual(linux.io_uring_cqe{
1390 .user_data = 0x99999999,
1391 .res = 0,
1392 .flags = 0,
1393 }, cqe_timeout_remove);
1394}