authorgravatar for joad.nacer@gmail.comjoadnacer <joad.nacer@gmail.com> 2023-10-27 00:31:55+01:00
committergravatar for joad.nacer@gmail.comjoadnacer <joad.nacer@gmail.com> 2023-10-27 00:31:55+01:00
log28b848e3f09f2774a06863438ffff59a8e67f780
treee9ab0f53b730c38b5647140e3fe6f7b5bc2eb24e
parent0005e7e70b96ecedee64b7259911ae857b8cf676

std.io_uring: Improve splice implementation


1 files changed, 10 insertions(+), 10 deletions(-)

lib/std/os/linux/io_uring.zig+10-10
......@@ -415,10 +415,10 @@ pub const IO_Uring = struct {
415415
416416 /// Queues (but does not submit) an SQE to perform a `splice(2)`
417417 /// Either `fd_in` or `fd_out` must be a pipe.
418 /// If `fd_in` refers to a pipe, `off_in` is ignored and must be set to -1.
419 /// If `fd_in` does not refer to a pipe and `off_in` is -1, then `len` are read
418 /// If `fd_in` refers to a pipe, `off_in` is ignored and must be set to std.math.maxInt(u64).
419 /// If `fd_in` does not refer to a pipe and `off_in` is maxInt(u64), then `len` are read
420420 /// from `fd_in` starting from the file offset, which is incremented by the number of bytes read.
421 /// If `fd_in` does not refer to a pipe and `off_in` is not -1, then the starting offset of `fd_in` will be `off_in`.
421 /// If `fd_in` does not refer to a pipe and `off_in` is not maxInt(u64), then the starting offset of `fd_in` will be `off_in`.
422422 /// This splice operation can be used to implement sendfile by splicing to an intermediate pipe first,
423423 /// then splice to the final destination. In fact, the implementation of sendfile in kernel uses splice internally.
424424 ///
......@@ -427,7 +427,7 @@ pub const IO_Uring = struct {
427427 /// See https://github.com/axboe/liburing/issues/291
428428 ///
429429 /// Returns a pointer to the SQE so that you can further modify the SQE for advanced use cases.
430 pub fn splice(self: *IO_Uring, user_data: u64, fd_in: os.fd_t, off_in: i64, fd_out: os.fd_t, off_out: i64, len: usize) !*linux.io_uring_sqe {
430 pub fn splice(self: *IO_Uring, user_data: u64, fd_in: os.fd_t, off_in: u64, fd_out: os.fd_t, off_out: u64, len: usize) !*linux.io_uring_sqe {
431431 const sqe = try self.get_sqe();
432432 io_uring_prep_splice(sqe, fd_in, off_in, fd_out, off_out, len);
433433 sqe.user_data = user_data;
......@@ -1268,9 +1268,9 @@ pub fn io_uring_prep_write(sqe: *linux.io_uring_sqe, fd: os.fd_t, buffer: []cons
12681268 io_uring_prep_rw(.WRITE, sqe, fd, @intFromPtr(buffer.ptr), buffer.len, offset);
12691269}
12701270
1271pub fn io_uring_prep_splice(sqe: *linux.io_uring_sqe, fd_in: os.fd_t, off_in: i64, fd_out: os.fd_t, off_out: i64, len: usize) void {
1272 io_uring_prep_rw(.SPLICE, sqe, fd_out, undefined, len, @bitCast(off_out));
1273 sqe.addr = @bitCast(off_in);
1271pub fn io_uring_prep_splice(sqe: *linux.io_uring_sqe, fd_in: os.fd_t, off_in: u64, fd_out: os.fd_t, off_out: u64, len: usize) void {
1272 io_uring_prep_rw(.SPLICE, sqe, fd_out, undefined, len, off_out);
1273 sqe.addr = off_in;
12741274 sqe.splice_fd_in = fd_in;
12751275}
12761276
......@@ -1888,17 +1888,17 @@ test "splice/read" {
18881888 _ = try file_src.write(&buffer_write);
18891889
18901890 var fds = try os.pipe();
1891 const pipe_offset: i64 = -1;
1891 const pipe_offset: u64 = std.math.maxInt(u64);
18921892
18931893 const sqe_splice_to_pipe = try ring.splice(0x11111111, fd_src, 0, fds[1], pipe_offset, buffer_write.len);
18941894 try testing.expectEqual(linux.IORING_OP.SPLICE, sqe_splice_to_pipe.opcode);
18951895 try testing.expectEqual(@as(u64, 0), sqe_splice_to_pipe.addr);
1896 try testing.expectEqual(@as(u64, @bitCast((pipe_offset))), sqe_splice_to_pipe.off);
1896 try testing.expectEqual(pipe_offset, sqe_splice_to_pipe.off);
18971897 sqe_splice_to_pipe.flags |= linux.IOSQE_IO_LINK;
18981898
18991899 const sqe_splice_from_pipe = try ring.splice(0x22222222, fds[0], pipe_offset, fd_dst, 10, buffer_write.len);
19001900 try testing.expectEqual(linux.IORING_OP.SPLICE, sqe_splice_from_pipe.opcode);
1901 try testing.expectEqual(@as(u64, @bitCast(pipe_offset)), sqe_splice_from_pipe.addr);
1901 try testing.expectEqual(pipe_offset, sqe_splice_from_pipe.addr);
19021902 try testing.expectEqual(@as(u64, 10), sqe_splice_from_pipe.off);
19031903 sqe_splice_from_pipe.flags |= linux.IOSQE_IO_LINK;
19041904