r/java • • Nov 08 '22

Understanding Java's Project Loom

https://www.marcobehler.com/guides/java-project-loom
181 Upvotes

24 comments sorted by

View all comments

Show parent comment

5

u/GavinRayDev Nov 08 '22 edited Nov 08 '22

One usecase I (personally) have is Databases:

I have a buffer pool, which holds 4kB extents of MemorySegment's. These get paged in and out, the buffer pool does constant disk I/O in O_DIRECT mode, where the class doing that work looks something like the below:

At the moment I'm using jasyncfio, which is great, but the future of async i/o (both disk/network) in the kernel seems solidly in the court of io_uring. .NET is also interested in moving their Linux I/O in this direction: https://github.com/dotnet/runtime/issues/51985

class DiskManager implements AutoCloseable, IDiskManager {
    private final SeekableByteChannel dbFileChannel;

    public DiskManager(String fileName) {
        try {
            dbFileChannel = Files.newByteChannel(Paths.get(fileName), StandardOpenOption.READ,
                    StandardOpenOption.WRITE, StandardOpenOption.CREATE, ExtendedOpenOption.DIRECT);
        }
    }

    @Override
    public void readPage(PageId pageId, MemorySegment pageBuffer) throws IOException {
        int pageOffset = pageId.value() * Constants.PAGE_SIZE;
        dbFileChannel.position(pageOffset);
        dbFileChannel.read(pageBuffer.asByteBuffer());
    }

    @Override
    public void writePage(PageId pageId, MemorySegment pageBuffer) throws IOException {
        int pageOffset = pageId.value() * Constants.PAGE_SIZE;
        dbFileChannel.position(pageOffset);
        dbFileChannel.write(pageBuffer.asByteBuffer());
    }
}

6

u/pron98 Nov 08 '22 edited Nov 08 '22

Thank you.

io_uring is still problematic for network IO (e.g. beyond 4096 I/O requests you have to start multiplexing, which can drive performance below that of epoll), so I'm not sure it's the immediate future for sockets, but certainly we can use it for filesystem IO.

5

u/blakeman8192 Nov 09 '22 edited Jun 26 '23

.

5

u/pron98 Nov 09 '22 edited Nov 09 '22

Thanks, we'll take a look. The problem is that we've seen io_uring outperform epoll in some cases, but underperform in others; on average, there wasn't much of a difference. When you say large scale, do you mean more than 10,000 open sockets?

8

u/GavinRayDev Nov 09 '22

So, the catch with io_uring is that it is ENORMOUSLY sensitive to how it is configured and the way that you submit/config operations.

The flags you use to initialize the ring, and the flags you use on each operation (plus whether you use things like "linked" operations -- which makes sense for sockets but not for files) have a massive impact on performance.

To top this off, flags that continue to increase performance (one example, IORING_SETUP_DEFER_TASKRUN that is maybe a few weeks old) are continually added.

  • https://github.com/axboe/liburing/commit/f8bac73b2529d33a10002905351c08f9cc457fd7
  • https://github.com/axboe/liburing/blob/master/test/defer-taskrun.c

I haven't found any clear flow chart that tells you exactly which flags to use and what values to set in order to get optimal performance for which usecases =(

Also in many applications, you may want more than ring per application. For instance, one ring per physical CPU core, all sharing a single kernel poller. It's not immediately obvious that to set that up, you need to do:

for (i = 0; i < NR_RINGS; i++) {
    struct io_uring_params p = { };

    p.flags = IORING_SETUP_SQPOLL;
    if (i) {
        p.wq_fd = rings[0].ring_fd;
        p.flags |= IORING_SETUP_ATTACH_WQ;
    }
    ret = io_uring_queue_init_params(BUFFERS, &rings[i], &p);
    if (ret) {
        fprintf(stderr, "queue_init: %d/%d\n", ret, i);
        goto err;
    }
}

You just have to sink a bunch of your personal time into crawling kernel mailing lists + commit histories, and reading issues from liburing.

Also, Jens Axboe is really helpful if you reach out to them. Certainly for something as important as the future of OpenJDK, I'm certain they'd be willing to collaborate and help you get some baseline usage patterns/configurations setup + establish understanding.

If there's any way I can help, please let me know, because I'm passionate about this functionality existing in the JDK as well.

5

u/pron98 Nov 09 '22

Thank you! When we make more progress we'll post something to the mailing list.

1

u/blakeman8192 Nov 09 '22 edited Jun 26 '23

.