Skip to content

Commit

Permalink
8318422: Allow poller threads be virtual threads
Browse files Browse the repository at this point in the history
Reviewed-by: michaelm
  • Loading branch information
Alan Bateman committed Nov 4, 2023
1 parent 29cf2c4 commit c099cf5
Show file tree
Hide file tree
Showing 13 changed files with 347 additions and 378 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class DefaultPollerProvider extends PollerProvider {
DefaultPollerProvider() { }

@Override
Poller readPoller() throws IOException {
Poller readPoller(boolean subPoller) throws IOException {
return new PollsetPoller(true);
}

@Override
Poller writePoller() throws IOException {
Poller writePoller(boolean subPoller) throws IOException {
return new PollsetPoller(false);
}
}
6 changes: 3 additions & 3 deletions src/java.base/aix/classes/sun/nio/ch/PollsetPoller.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ class PollsetPoller extends Poller {
MAX_EVENTS_TO_POLL = 512;
}

private final int event;
private final int setid;
private final long pollBuffer;

PollsetPoller(boolean read) throws IOException {
super(read);
this.event = (read) ? Net.POLLIN : Net.POLLOUT;
this.setid = Pollset.pollsetCreate();
this.pollBuffer = Pollset.allocatePollArray(MAX_EVENTS_TO_POLL);
}
Expand All @@ -58,8 +59,7 @@ int fdVal() {

@Override
void implRegister(int fd) throws IOException {
int ret = Pollset.pollsetCtl(setid, Pollset.PS_MOD, fd,
Pollset.PS_POLLPRI | (this.reading() ? Net.POLLIN : Net.POLLOUT));
int ret = Pollset.pollsetCtl(setid, Pollset.PS_MOD, fd, Pollset.PS_POLLPRI | event);
if (ret != 0) {
throw new IOException("Unable to register fd " + fd);
}
Expand Down
30 changes: 25 additions & 5 deletions src/java.base/linux/classes/sun/nio/ch/DefaultPollerProvider.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,6 +25,7 @@
package sun.nio.ch;

import java.io.IOException;
import jdk.internal.vm.ContinuationSupport;

/**
* Default PollerProvider for Linux.
Expand All @@ -33,12 +34,31 @@ class DefaultPollerProvider extends PollerProvider {
DefaultPollerProvider() { }

@Override
Poller readPoller() throws IOException {
return new EPollPoller(true);
Poller.Mode defaultPollerMode() {
if (ContinuationSupport.isSupported()) {
return Poller.Mode.VTHREAD_POLLERS;
} else {
return Poller.Mode.SYSTEM_THREADS;
}
}

@Override
Poller writePoller() throws IOException {
return new EPollPoller(false);
int defaultReadPollers(Poller.Mode mode) {
int ncpus = Runtime.getRuntime().availableProcessors();
if (mode == Poller.Mode.VTHREAD_POLLERS) {
return Math.min(Integer.highestOneBit(ncpus), 32);
} else {
return Math.max(Integer.highestOneBit(ncpus / 4), 1);
}
}

@Override
Poller readPoller(boolean subPoller) throws IOException {
return new EPollPoller(subPoller, true);
}

@Override
Poller writePoller(boolean subPoller) throws IOException {
return new EPollPoller(subPoller, false);
}
}
10 changes: 5 additions & 5 deletions src/java.base/linux/classes/sun/nio/ch/EPollPoller.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@
*/

class EPollPoller extends Poller {
private static final int MAX_EVENTS_TO_POLL = 512;
private static final int ENOENT = 2;

private final int epfd;
private final int event;
private final int maxEvents;
private final long address;

EPollPoller(boolean read) throws IOException {
super(read);
EPollPoller(boolean subPoller, boolean read) throws IOException {
this.epfd = EPoll.create();
this.event = (read) ? EPOLLIN : EPOLLOUT;
this.address = EPoll.allocatePollArray(MAX_EVENTS_TO_POLL);
this.maxEvents = (subPoller) ? 64 : 512;
this.address = EPoll.allocatePollArray(maxEvents);
}

@Override
Expand All @@ -68,7 +68,7 @@ void implDeregister(int fdVal) {

@Override
int poll(int timeout) throws IOException {
int n = EPoll.wait(epfd, address, MAX_EVENTS_TO_POLL, timeout);
int n = EPoll.wait(epfd, address, maxEvents, timeout);
int i = 0;
while (i < n) {
long eventAddress = EPoll.getEvent(address, i);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -33,12 +33,12 @@ class DefaultPollerProvider extends PollerProvider {
DefaultPollerProvider() { }

@Override
Poller readPoller() throws IOException {
return new KQueuePoller(true);
Poller readPoller(boolean subPoller) throws IOException {
return new KQueuePoller(subPoller, true);
}

@Override
Poller writePoller() throws IOException {
return new KQueuePoller(false);
Poller writePoller(boolean subPoller) throws IOException {
return new KQueuePoller(subPoller, false);
}
}
11 changes: 5 additions & 6 deletions src/java.base/macosx/classes/sun/nio/ch/KQueuePoller.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@
* Poller implementation based on the kqueue facility.
*/
class KQueuePoller extends Poller {
private static final int MAX_EVENTS_TO_POLL = 512;

private final int kqfd;
private final int filter;
private final int maxEvents;
private final long address;

KQueuePoller(boolean read) throws IOException {
super(read);
KQueuePoller(boolean subPoller, boolean read) throws IOException {
this.kqfd = KQueue.create();
this.filter = (read) ? EVFILT_READ : EVFILT_WRITE;
this.address = KQueue.allocatePollArray(MAX_EVENTS_TO_POLL);
this.maxEvents = (subPoller) ? 64 : 512;
this.address = KQueue.allocatePollArray(maxEvents);
}

@Override
Expand All @@ -63,7 +62,7 @@ void implDeregister(int fdVal) {

@Override
int poll(int timeout) throws IOException {
int n = KQueue.poll(kqfd, address, MAX_EVENTS_TO_POLL, timeout);
int n = KQueue.poll(kqfd, address, maxEvents, timeout);
int i = 0;
while (i < n) {
long keventAddress = KQueue.getEvent(address, i);
Expand Down
Loading

0 comments on commit c099cf5

Please sign in to comment.