Skip to content

Commit 222ada9

Browse files
committed
8344577: Virtual thread tests are timing out on some macOS systems
8344143: Test jdk/java/lang/Thread/virtual/stress/GetStackTraceALotWhenPinned.java timed out on macosx-x64 Reviewed-by: mbaesken Backport-of: a032de2904baf83143415858ed7191549c659035
1 parent d3facda commit 222ada9

File tree

3 files changed

+51
-18
lines changed

3 files changed

+51
-18
lines changed

test/jdk/java/lang/Thread/virtual/stress/GetStackTraceALotWhenPinned.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@
3636
* @requires vm.debug == true
3737
* @modules java.base/java.lang:+open
3838
* @library /test/lib
39-
* @run main/othervm/timeout=300 GetStackTraceALotWhenPinned 200000
39+
* @run main/othervm/timeout=300 GetStackTraceALotWhenPinned 50000
4040
*/
4141

4242
import java.time.Instant;
4343
import java.util.concurrent.atomic.AtomicInteger;
4444
import java.util.concurrent.locks.LockSupport;
45+
import jdk.test.lib.Platform;
4546
import jdk.test.lib.thread.VThreadRunner;
4647
import jdk.test.lib.thread.VThreadPinner;
4748

@@ -53,7 +54,15 @@ public static void main(String[] args) throws Exception {
5354
VThreadRunner.ensureParallelism(2);
5455
}
5556

56-
int iterations = Integer.parseInt(args[0]);
57+
int iterations;
58+
int value = Integer.parseInt(args[0]);
59+
if (Platform.isOSX() && Platform.isX64()) {
60+
// reduced iterations on macosx-x64
61+
iterations = Math.max(value / 4, 1);
62+
} else {
63+
iterations = value;
64+
}
65+
5766
var barrier = new Barrier(2);
5867

5968
// Start a virtual thread that loops doing Thread.yield and parking while pinned.
@@ -78,18 +87,23 @@ public static void main(String[] args) throws Exception {
7887
}
7988
});
8089

81-
long lastTimestamp = System.currentTimeMillis();
82-
for (int i = 0; i < iterations; i++) {
90+
long lastTime = System.nanoTime();
91+
for (int i = 1; i <= iterations; i++) {
8392
// wait for virtual thread to arrive
8493
barrier.await();
8594

8695
thread.getStackTrace();
8796
LockSupport.unpark(thread);
8897

89-
long currentTime = System.currentTimeMillis();
90-
if ((currentTime - lastTimestamp) > 500) {
91-
System.out.format("%s %d remaining ...%n", Instant.now(), (iterations - i));
92-
lastTimestamp = currentTime;
98+
long currentTime = System.nanoTime();
99+
if (i == iterations || ((currentTime - lastTime) > 1_000_000_000L)) {
100+
System.out.format("%s => %d of %d%n", Instant.now(), i, iterations);
101+
lastTime = currentTime;
102+
}
103+
104+
if (Thread.currentThread().isInterrupted()) {
105+
// fail quickly if interrupted by jtreg
106+
throw new RuntimeException("interrupted");
93107
}
94108
}
95109
}

test/jdk/java/lang/Thread/virtual/stress/ParkALot.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,38 +25,54 @@
2525
* @test
2626
* @summary Stress test parking and unparking
2727
* @requires vm.debug != true
28-
* @run main/othervm ParkALot 500000
28+
* @library /test/lib
29+
* @run main/othervm/timeout=300 ParkALot 300000
2930
*/
3031

3132
/*
3233
* @test
3334
* @requires vm.debug == true
34-
* @run main/othervm ParkALot 100000
35+
* @library /test/lib
36+
* @run main/othervm/timeout=300 ParkALot 100000
3537
*/
3638

3739
import java.time.Instant;
3840
import java.util.concurrent.Executors;
3941
import java.util.concurrent.ThreadFactory;
42+
import java.util.concurrent.TimeUnit;
43+
import java.util.concurrent.atomic.AtomicInteger;
4044
import java.util.concurrent.locks.LockSupport;
45+
import jdk.test.lib.Platform;
4146

4247
public class ParkALot {
43-
private static final int ITERATIONS = 1_000_000;
4448

45-
public static void main(String[] args) {
49+
public static void main(String[] args) throws Exception {
4650
int iterations;
47-
if (args.length > 0) {
48-
iterations = Integer.parseInt(args[0]);
51+
int value = Integer.parseInt(args[0]);
52+
if (Platform.isOSX() && Platform.isX64()) {
53+
// reduced iterations on macosx-x64
54+
iterations = Math.max(value / 4, 1);
4955
} else {
50-
iterations = ITERATIONS;
56+
iterations = value;
5157
}
5258

5359
int maxThreads = Math.clamp(Runtime.getRuntime().availableProcessors() / 2, 1, 4);
5460
for (int nthreads = 1; nthreads <= maxThreads; nthreads++) {
5561
System.out.format("%s %d thread(s) ...%n", Instant.now(), nthreads);
5662
ThreadFactory factory = Thread.ofPlatform().factory();
5763
try (var executor = Executors.newThreadPerTaskExecutor(factory)) {
64+
var totalIterations = new AtomicInteger();
5865
for (int i = 0; i < nthreads; i++) {
59-
executor.submit(() -> parkALot(iterations));
66+
executor.submit(() -> parkALot(iterations, totalIterations::incrementAndGet));
67+
}
68+
69+
// shutdown, await for all threads to finish with progress output
70+
executor.shutdown();
71+
boolean terminated = false;
72+
while (!terminated) {
73+
terminated = executor.awaitTermination(1, TimeUnit.SECONDS);
74+
System.out.format("%s => %d of %d%n",
75+
Instant.now(), totalIterations.get(), iterations * nthreads);
6076
}
6177
}
6278
System.out.format("%s %d thread(s) done%n", Instant.now(), nthreads);
@@ -66,8 +82,10 @@ public static void main(String[] args) {
6682
/**
6783
* Creates a virtual thread that alternates between untimed and timed parking.
6884
* A platform thread spins unparking the virtual thread.
85+
* @param iterations number of iterations
86+
* @param afterIteration the task to run after each iteration
6987
*/
70-
private static void parkALot(int iterations) {
88+
private static void parkALot(int iterations, Runnable afterIteration) {
7189
Thread vthread = Thread.ofVirtual().start(() -> {
7290
int i = 0;
7391
boolean timed = false;
@@ -80,6 +98,7 @@ private static void parkALot(int iterations) {
8098
timed = true;
8199
}
82100
i++;
101+
afterIteration.run();
83102
}
84103
});
85104

test/jdk/java/lang/Thread/virtual/stress/SleepALot.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* @test
2626
* @summary Stress test Thread.sleep
2727
* @requires vm.debug != true & vm.continuations
28-
* @run main/othervm SleepALot 500000
28+
* @run main SleepALot 500000
2929
*/
3030

3131
/*

0 commit comments

Comments
 (0)