From a032de2904baf83143415858ed7191549c659035 Mon Sep 17 00:00:00 2001 From: Alan Bateman Date: Mon, 25 Nov 2024 15:34:13 +0000 Subject: [PATCH] 8344577: Virtual thread tests are timing out on some macOS systems Reviewed-by: jpai --- .../stress/GetStackTraceALotWhenBlocking.java | 10 ++++- .../stress/GetStackTraceALotWhenPinned.java | 11 +++++- .../GetStackTraceALotWithTimedWait.java | 4 +- .../stress/LotsOfContendedMonitorEnter.java | 2 +- .../stress/LotsOfUncontendedMonitorEnter.java | 2 +- .../lang/Thread/virtual/stress/ParkALot.java | 37 ++++++++++++++----- .../lang/Thread/virtual/stress/SleepALot.java | 2 +- .../Thread/virtual/stress/TimedWaitALot.java | 8 ++-- 8 files changed, 56 insertions(+), 20 deletions(-) diff --git a/test/jdk/java/lang/Thread/virtual/stress/GetStackTraceALotWhenBlocking.java b/test/jdk/java/lang/Thread/virtual/stress/GetStackTraceALotWhenBlocking.java index b68496bc7db..17b63573d85 100644 --- a/test/jdk/java/lang/Thread/virtual/stress/GetStackTraceALotWhenBlocking.java +++ b/test/jdk/java/lang/Thread/virtual/stress/GetStackTraceALotWhenBlocking.java @@ -42,6 +42,7 @@ import java.time.Instant; import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.atomic.AtomicBoolean; +import jdk.test.lib.Platform; import jdk.test.lib.thread.VThreadRunner; // ensureParallelism requires jdk.management public class GetStackTraceALotWhenBlocking { @@ -50,7 +51,14 @@ public class GetStackTraceALotWhenBlocking { // need at least two carriers VThreadRunner.ensureParallelism(2); - int iterations = args.length > 0 ? Integer.parseInt(args[0]) : 100_000; + int iterations; + int value = Integer.parseInt(args[0]); + if (Platform.isOSX() && Platform.isX64()) { + // reduced iterations on macosx-x64 + iterations = Math.max(value / 4, 1); + } else { + iterations = value; + } var done = new AtomicBoolean(); var lock = new Object(); diff --git a/test/jdk/java/lang/Thread/virtual/stress/GetStackTraceALotWhenPinned.java b/test/jdk/java/lang/Thread/virtual/stress/GetStackTraceALotWhenPinned.java index 9d9d3eddce1..0b5cabab7d5 100644 --- a/test/jdk/java/lang/Thread/virtual/stress/GetStackTraceALotWhenPinned.java +++ b/test/jdk/java/lang/Thread/virtual/stress/GetStackTraceALotWhenPinned.java @@ -42,6 +42,7 @@ import java.time.Instant; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.LockSupport; +import jdk.test.lib.Platform; import jdk.test.lib.thread.VThreadRunner; // ensureParallelism requires jdk.management import jdk.test.lib.thread.VThreadPinner; @@ -53,7 +54,15 @@ public class GetStackTraceALotWhenPinned { VThreadRunner.ensureParallelism(2); } - int iterations = Integer.parseInt(args[0]); + int iterations; + int value = Integer.parseInt(args[0]); + if (Platform.isOSX() && Platform.isX64()) { + // reduced iterations on macosx-x64 + iterations = Math.max(value / 4, 1); + } else { + iterations = value; + } + var barrier = new Barrier(2); // Start a virtual thread that loops doing Thread.yield and parking while pinned. diff --git a/test/jdk/java/lang/Thread/virtual/stress/GetStackTraceALotWithTimedWait.java b/test/jdk/java/lang/Thread/virtual/stress/GetStackTraceALotWithTimedWait.java index 3f8f1c464a6..ad3fbdf0565 100644 --- a/test/jdk/java/lang/Thread/virtual/stress/GetStackTraceALotWithTimedWait.java +++ b/test/jdk/java/lang/Thread/virtual/stress/GetStackTraceALotWithTimedWait.java @@ -25,13 +25,13 @@ * @test * @summary Stress test Thread.getStackTrace on a virtual thread in timed-Object.wait * @requires vm.debug != true - * @run main/othervm GetStackTraceALotWithTimedWait 100000 + * @run main GetStackTraceALotWithTimedWait 100000 */ /* * @test * @requires vm.debug == true - * @run main/othervm GetStackTraceALotWithTimedWait 50000 + * @run main GetStackTraceALotWithTimedWait 50000 */ import java.time.Instant; diff --git a/test/jdk/java/lang/Thread/virtual/stress/LotsOfContendedMonitorEnter.java b/test/jdk/java/lang/Thread/virtual/stress/LotsOfContendedMonitorEnter.java index 86096156b91..c63a4b0c947 100644 --- a/test/jdk/java/lang/Thread/virtual/stress/LotsOfContendedMonitorEnter.java +++ b/test/jdk/java/lang/Thread/virtual/stress/LotsOfContendedMonitorEnter.java @@ -26,7 +26,7 @@ * @summary Test virtual threads entering a lot of monitors with contention * @requires vm.opt.LockingMode != 1 * @library /test/lib - * @run main/othervm LotsOfContendedMonitorEnter + * @run main LotsOfContendedMonitorEnter */ /* diff --git a/test/jdk/java/lang/Thread/virtual/stress/LotsOfUncontendedMonitorEnter.java b/test/jdk/java/lang/Thread/virtual/stress/LotsOfUncontendedMonitorEnter.java index e72fc12eb88..da5fdd1161d 100644 --- a/test/jdk/java/lang/Thread/virtual/stress/LotsOfUncontendedMonitorEnter.java +++ b/test/jdk/java/lang/Thread/virtual/stress/LotsOfUncontendedMonitorEnter.java @@ -25,7 +25,7 @@ * @test id=default * @summary Test virtual thread entering (and reentering) a lot of monitors with no contention * @library /test/lib - * @run main/othervm LotsOfUncontendedMonitorEnter + * @run main LotsOfUncontendedMonitorEnter */ /* diff --git a/test/jdk/java/lang/Thread/virtual/stress/ParkALot.java b/test/jdk/java/lang/Thread/virtual/stress/ParkALot.java index ccba3fe1b4d..882e0b881bc 100644 --- a/test/jdk/java/lang/Thread/virtual/stress/ParkALot.java +++ b/test/jdk/java/lang/Thread/virtual/stress/ParkALot.java @@ -25,29 +25,35 @@ * @test * @summary Stress test parking and unparking * @requires vm.debug != true - * @run main/othervm ParkALot 500000 + * @library /test/lib + * @run main/othervm/timeout=300 ParkALot 300000 */ /* * @test * @requires vm.debug == true - * @run main/othervm ParkALot 100000 + * @library /test/lib + * @run main/othervm/timeout=300 ParkALot 100000 */ import java.time.Instant; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.LockSupport; +import jdk.test.lib.Platform; public class ParkALot { - private static final int ITERATIONS = 1_000_000; - public static void main(String[] args) { + public static void main(String[] args) throws Exception { int iterations; - if (args.length > 0) { - iterations = Integer.parseInt(args[0]); + int value = Integer.parseInt(args[0]); + if (Platform.isOSX() && Platform.isX64()) { + // reduced iterations on macosx-x64 + iterations = Math.max(value / 4, 1); } else { - iterations = ITERATIONS; + iterations = value; } int maxThreads = Math.clamp(Runtime.getRuntime().availableProcessors() / 2, 1, 4); @@ -55,8 +61,18 @@ public class ParkALot { System.out.format("%s %d thread(s) ...%n", Instant.now(), nthreads); ThreadFactory factory = Thread.ofPlatform().factory(); try (var executor = Executors.newThreadPerTaskExecutor(factory)) { + var totalIterations = new AtomicInteger(); for (int i = 0; i < nthreads; i++) { - executor.submit(() -> parkALot(iterations)); + executor.submit(() -> parkALot(iterations, totalIterations::incrementAndGet)); + } + + // shutdown, await for all threads to finish with progress output + executor.shutdown(); + boolean terminated = false; + while (!terminated) { + terminated = executor.awaitTermination(1, TimeUnit.SECONDS); + System.out.format("%s => %d of %d%n", + Instant.now(), totalIterations.get(), iterations * nthreads); } } System.out.format("%s %d thread(s) done%n", Instant.now(), nthreads); @@ -66,8 +82,10 @@ public class ParkALot { /** * Creates a virtual thread that alternates between untimed and timed parking. * A platform thread spins unparking the virtual thread. + * @param iterations number of iterations + * @param afterIteration the task to run after each iteration */ - private static void parkALot(int iterations) { + private static void parkALot(int iterations, Runnable afterIteration) { Thread vthread = Thread.ofVirtual().start(() -> { int i = 0; boolean timed = false; @@ -80,6 +98,7 @@ public class ParkALot { timed = true; } i++; + afterIteration.run(); } }); diff --git a/test/jdk/java/lang/Thread/virtual/stress/SleepALot.java b/test/jdk/java/lang/Thread/virtual/stress/SleepALot.java index ed1c985ba7c..d10897629c4 100644 --- a/test/jdk/java/lang/Thread/virtual/stress/SleepALot.java +++ b/test/jdk/java/lang/Thread/virtual/stress/SleepALot.java @@ -25,7 +25,7 @@ * @test * @summary Stress test Thread.sleep * @requires vm.debug != true & vm.continuations - * @run main/othervm SleepALot 500000 + * @run main SleepALot 500000 */ /* diff --git a/test/jdk/java/lang/Thread/virtual/stress/TimedWaitALot.java b/test/jdk/java/lang/Thread/virtual/stress/TimedWaitALot.java index 2a2fce62d62..6a81a7c5fee 100644 --- a/test/jdk/java/lang/Thread/virtual/stress/TimedWaitALot.java +++ b/test/jdk/java/lang/Thread/virtual/stress/TimedWaitALot.java @@ -24,28 +24,28 @@ /* * @test id=timeout * @summary Stress test timed-Object.wait - * @run main/othervm TimedWaitALot 200 + * @run main TimedWaitALot 200 */ /* * @test id=timeout-notify * @summary Test timed-Object.wait where the waiting thread is awakened with Object.notify * at around the same time that the timeout expires. - * @run main/othervm TimedWaitALot 200 true false + * @run main TimedWaitALot 150 true false */ /* * @test id=timeout-interrupt * @summary Test timed-Object.wait where the waiting thread is awakened with Thread.interrupt * at around the same time that the timeout expires. - * @run main/othervm TimedWaitALot 200 false true + * @run main TimedWaitALot 150 false true */ /* * @test id=timeout-notify-interrupt * @summary Test timed-Object.wait where the waiting thread is awakened with Object.notify * and Thread.interrupt at around the same time that the timeout expires. - * @run main/othervm TimedWaitALot 100 true true + * @run main TimedWaitALot 100 true true */ import java.time.Instant;