Last active
September 23, 2022 20:08
-
-
Save GavinRay97/8ea0997693c00f8df61968a98ba30135 to your computer and use it in GitHub Desktop.
io_uring Panama draft
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IoUringExample { | |
private static int QUEUE_DEPTH = 8; | |
private static MemorySession session = MemorySession.global(); | |
private static GroupLayout myStructLayout = MemoryLayout.structLayout( | |
Constants.C_INT.withName("foo"), | |
Constants.C_INT.withName("bar")); | |
private static VarHandle myStruct$foo = myStructLayout.varHandle(MemoryLayout.PathElement.groupElement("foo")); | |
private static VarHandle myStruct$bar = myStructLayout.varHandle(MemoryLayout.PathElement.groupElement("bar")); | |
public static void run() { | |
SegmentAllocator allocator = session; | |
MemorySegment ring = allocator.allocate(io_uring.$LAYOUT()); | |
int ret = io_uring_queue_init(QUEUE_DEPTH, ring, 0); | |
if (ret < 0) { | |
System.out.printf("queue_init: %d", ret); | |
return; | |
} | |
MemoryAddress sqePtr = liburing_h.io_uring_get_sqe(ring); | |
if (sqePtr == MemoryAddress.NULL) { | |
System.out.printf("get_sqe failed"); | |
return; | |
} | |
io_uring_prep_nop(sqePtr); | |
MemorySegment myStruct = MemorySegment.allocateNative(myStructLayout, session); | |
myStruct$foo.set(myStruct, 1); | |
myStruct$bar.set(myStruct, 2); | |
liburing_h.io_uring_sqe_set_data(sqePtr, myStruct); | |
ret = liburing_h.io_uring_submit(ring); | |
if (ret < 0) { | |
System.out.printf("submit: %d", ret); | |
return; | |
} | |
MemorySegment cqes = io_uring_cqe.allocateArray(1, allocator); | |
ret = liburing_h.io_uring_wait_cqe(ring, cqes); | |
if (ret < 0) { | |
System.out.printf("wait_cqe: %d", ret); | |
return; | |
} | |
MemorySegment cqe = cqes.asSlice(0, io_uring_cqe.$LAYOUT().byteSize()); | |
MemoryAddress user_data = MemoryAddress.ofLong(io_uring_cqe.user_data$get(cqe)); | |
MemorySegment user_data_segment = MemorySegment.ofAddress(user_data, myStructLayout.byteSize(), session); | |
System.out.printf("user_data->foo=%d | user_data->bar=%d\nexpected: %d | %d \n", | |
myStruct$foo.get(user_data_segment), | |
myStruct$bar.get(user_data_segment), | |
myStruct$foo.get(myStruct), | |
myStruct$bar.get(myStruct)); | |
liburing_h.io_uring_cqe_seen(ring, cqe); | |
liburing_h.io_uring_queue_exit(ring); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "liburing.h" | |
#include <linux/io_uring.h> | |
#include <stdio.h> | |
#include <string.h> | |
struct MyStruct { | |
int a; | |
int b; | |
}; | |
int main(int argc, char* argv[]) | |
{ | |
struct io_uring ring; | |
int ret; | |
ret = io_uring_queue_init(8, &ring, 0); | |
if (ret < 0) { | |
printf("queue_init: %s (%d)", strerror(-ret), ret); | |
return 1; | |
} | |
struct io_uring_sqe* sqe = io_uring_get_sqe(&ring); | |
if (!sqe) { | |
printf("get_sqe failed"); | |
return 1; | |
} | |
io_uring_prep_nop(sqe); | |
struct MyStruct myStruct = { | |
.a = 1, .b = 2 | |
}; | |
io_uring_sqe_set_data(sqe, &myStruct); | |
ret = io_uring_submit(&ring); | |
if (ret < 0) { | |
printf("submit: %s (%d)", strerror(-ret), ret); | |
return 1; | |
} | |
struct io_uring_cqe* cqes[1]; | |
ret = io_uring_wait_cqe(&ring, cqes); | |
if (ret < 0) { | |
printf("wait_cqe: %s (%d)", strerror(-ret), ret); | |
return 1; | |
} | |
printf("cqe->user_data: %d %d; expected: %d %d", | |
((struct MyStruct*)cqes[0]->user_data)->a, | |
((struct MyStruct*)cqes[0]->user_data)->b, | |
myStruct.a, myStruct.b); | |
io_uring_cqe_seen(&ring, cqes[0]); | |
io_uring_queue_exit(&ring); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment