-
Notifications
You must be signed in to change notification settings - Fork 255
Mpsc scheduler #227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
franz1981
wants to merge
7
commits into
openjdk:fibers
Choose a base branch
from
franz1981:mpsc-scheduler
base: fibers
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Mpsc scheduler #227
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e7d76ee
Add stickyAffinity() to Thread.Builder.OfVirtual
franz1981 bbd44c5
Sticky VTs skip externalSubmit in afterYield
franz1981 6e7e35e
Restore useLazyUnpark dispatch, limit stickyAffinity to mode 3 sub-po…
franz1981 fc701ec
Merge upstream/fibers, adapt to lazyUnpark removal
franz1981 1d9a237
Add MPSC virtual thread scheduler
franz1981 081d380
Add carrier-local poller (Mode 4)
franz1981 e20e4a6
Fail-safe: pollerMode=4 falls back to Mode 2 without MPSC scheduler
franz1981 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
src/java.base/linux/classes/sun/nio/ch/CarrierLocalPoller.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /* | ||
| * Copyright (c) 2026, 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 | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. Oracle designates this | ||
| * particular file as subject to the "Classpath" exception as provided | ||
| * by Oracle in the LICENSE file that accompanied this code. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| */ | ||
| package sun.nio.ch; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.concurrent.locks.LockSupport; | ||
| import static sun.nio.ch.EPoll.*; | ||
|
|
||
| /** | ||
| * A carrier-local I/O poller using EPOLLONESHOT. Each carrier thread owns | ||
| * one instance. VTs that park on I/O register their fd directly with the | ||
| * carrier's epoll fd. No sub-pollers, no master poller. | ||
| * | ||
| * <p>The carrier calls {@link #poll(int)} when idle. When I/O arrives, | ||
| * the blocked VTs are unparked and enqueued to the carrier's own queue. | ||
| * External task submissions wake the carrier via the eventfd. | ||
| */ | ||
| public final class CarrierLocalPoller { | ||
|
|
||
| private static final int ENOENT = 2; | ||
| private static final int MAX_EVENTS = 64; | ||
|
|
||
| private final int epfd; | ||
| private final long pollAddress; | ||
| private final EventFD eventfd; | ||
| private final HashMap<Integer, Thread> fdToThread = new HashMap<>(); | ||
|
|
||
| public CarrierLocalPoller() throws IOException { | ||
| this.epfd = EPoll.create(); | ||
| this.pollAddress = EPoll.allocatePollArray(MAX_EVENTS); | ||
| this.eventfd = new EventFD(); | ||
| IOUtil.configureBlocking(eventfd.efd(), false); | ||
| EPoll.ctl(epfd, EPOLL_CTL_ADD, eventfd.efd(), EPOLLIN); | ||
| } | ||
|
|
||
| /** | ||
| * Register a file descriptor for read or write polling. Called by VTs | ||
| * on this carrier before parking. The VT is unparked when the fd is ready. | ||
| */ | ||
| public void register(int fdVal, int event, Thread thread) throws IOException { | ||
| fdToThread.put(fdVal, thread); | ||
| int err = EPoll.ctl(epfd, EPOLL_CTL_MOD, fdVal, (event | EPOLLONESHOT)); | ||
| if (err == ENOENT) { | ||
| err = EPoll.ctl(epfd, EPOLL_CTL_ADD, fdVal, (event | EPOLLONESHOT)); | ||
| } | ||
| if (err != 0) { | ||
| fdToThread.remove(fdVal); | ||
| throw new IOException("epoll_ctl failed: " + err); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Deregister a file descriptor. Called if the VT was unparked by | ||
| * something other than I/O readiness (e.g. interrupt, timeout). | ||
| */ | ||
| public void deregister(int fdVal) { | ||
| if (fdToThread.remove(fdVal) != null) { | ||
| EPoll.ctl(epfd, EPOLL_CTL_DEL, fdVal, 0); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Poll for I/O events. Returns the number of VTs unparked. | ||
| * | ||
| * @param timeout milliseconds: -1 to block, 0 for non-blocking | ||
| */ | ||
| public int poll(int timeout) throws IOException { | ||
| if (timeout == 0 && fdToThread.isEmpty()) { | ||
| return 0; | ||
| } | ||
| int n = EPoll.wait(epfd, pollAddress, MAX_EVENTS, timeout); | ||
| int unparked = 0; | ||
| for (int i = 0; i < n; i++) { | ||
| long eventAddress = EPoll.getEvent(pollAddress, i); | ||
| int fd = EPoll.getDescriptor(eventAddress); | ||
| if (fd == eventfd.efd()) { | ||
| eventfd.reset(); | ||
| } else { | ||
| Thread vt = fdToThread.remove(fd); | ||
| if (vt != null) { | ||
| LockSupport.unpark(vt); | ||
| unparked++; | ||
| } | ||
| } | ||
| } | ||
| return unparked; | ||
| } | ||
|
|
||
| /** | ||
| * Wake the carrier from a blocking {@link #poll} call. | ||
| * Called by external threads submitting tasks to this carrier. | ||
| */ | ||
| public void wakeup() throws IOException { | ||
| eventfd.set(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if there are fds registered for polling. | ||
| */ | ||
| public boolean hasPendingFds() { | ||
| return !fdToThread.isEmpty(); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have an IntMap here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it was my first choice tbh but thanks to:
The numbers were low enough that boxing was using the int cache.
But yes, in a final version would be better to use a primitive map