Skip to content
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

8319447: Improve performance of delayed task handling #23702

Closed
wants to merge 51 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
cb1aedf
In-progress snapshot
DougLea Jan 6, 2025
5c4ca21
Better conform to STPE
DougLea Jan 7, 2025
bb0e6a6
Refactorings
DougLea Jan 9, 2025
f683d7f
Use pendingRemval queue
DougLea Jan 9, 2025
3801ba0
Reduce unparks
DougLea Jan 10, 2025
d630b40
Better pending queues
DougLea Jan 16, 2025
0dd4ab7
Merge branch 'openjdk:master' into JDK-8319447
DougLea Jan 16, 2025
7e04af5
Conform to default STPE policies
DougLea Jan 19, 2025
7288e32
Comment out racy test
DougLea Jan 19, 2025
b14e31c
Merge branch 'openjdk:master' into JDK-8319447
DougLea Jan 19, 2025
cb202b6
Reduce memory contention
DougLea Jan 21, 2025
1f0a5cf
Use nanoTimeOrigin
DougLea Jan 21, 2025
97a2920
Reduce nanoTime usage; extend tck tests
DougLea Jan 23, 2025
f9aa135
Simplify scheduler state tracking
DougLea Jan 23, 2025
798fe64
Refactor delay scheduler pool submissions
DougLea Jan 24, 2025
d083e91
improve removal cost balance
DougLea Jan 25, 2025
1a7f77c
Ensure negative nanotime offset
DougLea Jan 26, 2025
f832335
Merge branch 'openjdk:master' into JDK-8319447
DougLea Feb 2, 2025
3da4fd7
Solidify design; add documentation
DougLea Feb 2, 2025
49b1699
Separate out DelayScheduler.java
DougLea Feb 3, 2025
229da14
Rework FJP-DS connections
DougLea Feb 4, 2025
9fad8d4
Deal with commonPool parallelism zero; use in other juc classes; remo…
DougLea Feb 7, 2025
2e60dc9
Refactor schedule methods
DougLea Feb 8, 2025
a0db427
Isolate screening
DougLea Feb 8, 2025
d0f4af1
Support STPE policy methods
DougLea Feb 9, 2025
a6290ab
Reduce memory accesses
DougLea Feb 9, 2025
c839299
Simplify policy methods; improve layout
DougLea Feb 10, 2025
f1394c4
Rename DelayedTask to ScheduledForkJoinTask; misc other improvements
DougLea Feb 12, 2025
0e13955
Better accommodate CompletableFuture; use 4-ary heap; add javadocs; o…
DougLea Feb 15, 2025
14a7a6f
Reduce garbage retention; use trailing padding; add tests
DougLea Feb 16, 2025
bd58f41
Merge branch 'openjdk:master' into JDK-8319447
DougLea Feb 16, 2025
93aac79
Merge remote-tracking branch 'refs/remotes/origin/JDK-8319447' into J…
DougLea Feb 16, 2025
753d0e0
Add optional SubmitWithTimeout action
DougLea Feb 17, 2025
53516e9
Misc minor improvements and renamings for clarity
DougLea Feb 19, 2025
16815cc
Address feedback
DougLea Feb 21, 2025
84eaab0
Address review comments; ensure new internal methods can't clash with…
DougLea Feb 22, 2025
c9bc41a
Standardize parameter checking
DougLea Feb 23, 2025
b40513f
Address review comments; reactivation tweak
DougLea Feb 28, 2025
0c5d22a
Reduce volatile reads
DougLea Mar 1, 2025
5c0355b
Associate probes with carriers if Virtual (no doc updates yet)
DougLea Mar 8, 2025
f670910
Merge branch 'openjdk:master' into JDK-8319447
DougLea Mar 8, 2025
6fe1a3b
Disambiguate caller-runs vs Interruptible
DougLea Mar 9, 2025
9cc670b
Use SharedSecrets for ThreadLocalRandomProbe; other tweaks
DougLea Mar 11, 2025
172a235
Reword javadoc
DougLea Mar 13, 2025
9b51b7a
Use TC_MASK in accord with https://bugs.openjdk.org/browse/JDK-833001…
DougLea Mar 14, 2025
24422e4
Match indent of naster changes
DougLea Mar 22, 2025
b552c22
Merge branch 'openjdk:master' into JDK-8319447
DougLea Mar 22, 2025
9cf0a75
Address review comments
DougLea Mar 25, 2025
4aabe6b
Merge branch 'openjdk:master' into JDK-8319447
DougLea Mar 25, 2025
3237cc7
Address review comments
DougLea Mar 27, 2025
b872713
Typo
DougLea Mar 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 18 additions & 25 deletions src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -387,18 +387,18 @@ public class ForkJoinPool extends AbstractExecutorService
* WorkQueues are also used in a similar way for tasks submitted
* to the pool. We cannot mix these tasks in the same queues used
* by workers. Instead, we randomly associate submission queues
* with submitting threads, using a form of hashing. The
* ThreadLocalRandom probe value serves as a hash code for
* choosing existing queues, and may be randomly repositioned upon
* contention with other submitters. In essence, submitters act
* like workers except that they are restricted to executing local
* tasks that they submitted (or when known, subtasks thereof).
* Insertion of tasks in shared mode requires a lock. We use only
* a simple spinlock (as one role of field "phase") because
* submitters encountering a busy queue move to a different
* position to use or create other queues. They (spin) block when
* registering new queues, or indirectly elsewhere, by revisiting
* later.
* with submitting threads (or carriers when using VirtualThreads)
* using a form of hashing. The ThreadLocalRandom probe value
* serves as a hash code for choosing existing queues, and may be
* randomly repositioned upon contention with other submitters.
* In essence, submitters act like workers except that they are
* restricted to executing local tasks that they submitted (or
* when known, subtasks thereof). Insertion of tasks in shared
* mode requires a lock. We use only a simple spinlock (as one
* role of field "phase") because submitters encountering a busy
* queue move to a different position to use or create other
* queues. They (spin) block when registering new queues, or
* indirectly elsewhere, by revisiting later.
*
* Management
* ==========
Expand Down Expand Up @@ -885,7 +885,8 @@ public class ForkJoinPool extends AbstractExecutorService
* To comply with ExecutorService specs, we use subclasses of
* abstract class InterruptibleTask for tasks that require
* stronger interruption and cancellation guarantees. External
* submitters never run these tasks, even if in the common pool.
* submitters never run these tasks, even if in the common pool
* (as indicated by ForkJoinTask.noUserHelp status bit).
* InterruptibleTasks include a "runner" field (implemented
* similarly to FutureTask) to support cancel(true). Upon pool
* shutdown, runners are interrupted so they can cancel. Since
Expand Down Expand Up @@ -1295,10 +1296,9 @@ final void push(ForkJoinTask<?> task, ForkJoinPool pool,
unlockPhase();
if (room < 0)
throw new RejectedExecutionException("Queue capacity exceeded");
if ((room == 0 || // pad for InterruptibleTasks
if ((room == 0 || // pad if no caller-run
a[m & (s - ((internal || task == null ||
task.getClass().getSuperclass() !=
interruptibleTaskClass) ? 1 : 2))] == null) &&
task.noUserHelp() == 0) ? 1 : 2))] == null) &&
pool != null)
pool.signalWork(); // may have appeared empty
}
Expand Down Expand Up @@ -1637,11 +1637,6 @@ final boolean isApparentlyUnblocked() {
*/
static volatile RuntimePermission modifyThreadPermission;

/**
* Cached for faster type tests.
*/
static final Class<?> interruptibleTaskClass;

/**
* For VirtualThread intrinsics
*/
Expand Down Expand Up @@ -2023,12 +2018,11 @@ final void runWorker(WorkQueue w) {
boolean propagate;
int nb = q.base = b + 1;
w.nsteals = ++nsteals;
int ts = t.status;
w.source = j; // volatile
rescan = true;
if (propagate =
((src != (src = j) ||
t.getClass().getSuperclass() ==
interruptibleTaskClass) &&
((src != (src = j) || t.noUserHelp() != 0) &&
a[nb & m] != null))
signalWork();
w.topLevelExec(t, fifo);
Expand Down Expand Up @@ -4406,7 +4400,6 @@ protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
if ((scale & (scale - 1)) != 0)
throw new Error("array index scale not a power of two");

interruptibleTaskClass = ForkJoinTask.InterruptibleTask.class;
Class<?> dep = LockSupport.class; // ensure loaded
// allow access to non-public methods
JLA = SharedSecrets.getJavaLangAccess();
Expand Down
16 changes: 13 additions & 3 deletions src/java.base/share/classes/java/util/concurrent/ForkJoinTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ final boolean casNext(Aux c, Aux v) { // used only in cancellation
static final int ABNORMAL = 1 << 16;
static final int THROWN = 1 << 17;
static final int HAVE_EXCEPTION = DONE | ABNORMAL | THROWN;
static final int NO_USER_HELP = 1 << 24; // no external caller-run helping
static final int MARKER = 1 << 30; // utility marker
static final int SMASK = 0xffff; // short bits for tags
static final int UNCOMPENSATE = 1 << 16; // helpJoin sentinel
Expand All @@ -292,6 +293,12 @@ private int getAndBitwiseOrStatus(int v) {
private boolean casStatus(int c, int v) {
return U.compareAndSetInt(this, STATUS, c, v);
}
final int noUserHelp() { // nonvolatile read
return U.getInt(this, STATUS) & NO_USER_HELP;
}
final void setNoUserHelp() { // for use in constructors only
U.putInt(this, STATUS, NO_USER_HELP);
}

// Support for waiting and signalling

Expand Down Expand Up @@ -476,7 +483,7 @@ else if (casAux(a, next))
*/
private int awaitDone(boolean interruptible, long deadline) {
ForkJoinWorkerThread wt; ForkJoinPool p; ForkJoinPool.WorkQueue q;
Thread t; boolean internal; int s;
Thread t; boolean internal; int s, ss;
if (internal =
(t = Thread.currentThread()) instanceof ForkJoinWorkerThread) {
p = (wt = (ForkJoinWorkerThread)t).pool;
Expand All @@ -487,7 +494,7 @@ private int awaitDone(boolean interruptible, long deadline) {
return (((s = (p == null) ? 0 :
((this instanceof CountedCompleter) ?
p.helpComplete(this, q, internal) :
(this instanceof InterruptibleTask) && !internal ? status :
!internal && ((ss = status) & NO_USER_HELP) != 0 ? ss :
p.helpJoin(this, q, internal))) < 0)) ? s :
awaitDone(internal ? p : null, s, interruptible, deadline);
}
Expand Down Expand Up @@ -1155,7 +1162,7 @@ public static void helpQuiesce() {
*/
public void reinitialize() {
aux = null;
status = 0;
status &= NO_USER_HELP;
}

/**
Expand Down Expand Up @@ -1634,6 +1641,9 @@ public String toString() {
abstract static class InterruptibleTask<T> extends ForkJoinTask<T>
implements RunnableFuture<T> {
transient volatile Thread runner;
InterruptibleTask() {
setNoUserHelp();
}
abstract T compute() throws Exception;
public final boolean exec() {
Thread.interrupted();
Expand Down