Skip to content

Commit 519894a

Browse files
committed
8269478: Shenandoah: gc/shenandoah/mxbeans tests should be more resilient
Backport-of: 23d2996
1 parent 2d6c976 commit 519894a

File tree

2 files changed

+79
-29
lines changed

2 files changed

+79
-29
lines changed

test/hotspot/jtreg/gc/shenandoah/mxbeans/TestChurnNotifications.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* @test TestChurnNotifications
2626
* @summary Check that MX notifications are reported for all cycles
2727
* @requires vm.gc.Shenandoah & !vm.graal.enabled
28+
* @library /test/lib /
2829
*
2930
* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
3031
* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
@@ -41,6 +42,7 @@
4142
* @test TestChurnNotifications
4243
* @summary Check that MX notifications are reported for all cycles
4344
* @requires vm.gc.Shenandoah & !vm.graal.enabled
45+
* @library /test/lib /
4446
*
4547
* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
4648
* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
@@ -52,6 +54,7 @@
5254
* @test TestChurnNotifications
5355
* @summary Check that MX notifications are reported for all cycles
5456
* @requires vm.gc.Shenandoah & !vm.graal.enabled
57+
* @library /test/lib /
5558
*
5659
* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
5760
* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive
@@ -63,6 +66,7 @@
6366
* @test TestChurnNotifications
6467
* @summary Check that MX notifications are reported for all cycles
6568
* @requires vm.gc.Shenandoah & !vm.graal.enabled
69+
* @library /test/lib /
6670
*
6771
* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
6872
* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=static
@@ -74,6 +78,7 @@
7478
* @test TestChurnNotifications
7579
* @summary Check that MX notifications are reported for all cycles
7680
* @requires vm.gc.Shenandoah & !vm.graal.enabled
81+
* @library /test/lib /
7782
*
7883
* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
7984
* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact
@@ -85,6 +90,7 @@
8590
* @test TestChurnNotifications
8691
* @summary Check that MX notifications are reported for all cycles
8792
* @requires vm.gc.Shenandoah & !vm.graal.enabled
93+
* @library /test/lib /
8894
*
8995
* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
9096
* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
@@ -103,6 +109,8 @@
103109
import java.lang.management.*;
104110
import javax.management.openmbean.*;
105111

112+
import jdk.test.lib.Utils;
113+
106114
import com.sun.management.GarbageCollectionNotificationInfo;
107115

108116
public class TestChurnNotifications {
@@ -120,6 +128,8 @@ public class TestChurnNotifications {
120128
static volatile Object sink;
121129

122130
public static void main(String[] args) throws Exception {
131+
final long startTime = System.currentTimeMillis();
132+
123133
final AtomicLong churnBytes = new AtomicLong();
124134

125135
NotificationListener listener = new NotificationListener() {
@@ -158,17 +168,28 @@ public void handleNotification(Notification n, Object o) {
158168

159169
System.gc();
160170

161-
// Wait until notifications start arriving, and then wait some more
162-
// to catch the ones arriving late.
163-
while (churnBytes.get() == 0) {
164-
Thread.sleep(1000);
165-
}
166-
Thread.sleep(5000);
167-
168-
long actual = churnBytes.get();
169-
170171
long minExpected = PRECISE ? (mem - HEAP_MB * 1024 * 1024) : 1;
171172
long maxExpected = mem + HEAP_MB * 1024 * 1024;
173+
long actual = 0;
174+
175+
// Look at test timeout to figure out how long we can wait without breaking into timeout.
176+
// Default to 1/4 of the remaining time in 1s steps.
177+
final long STEP_MS = 1000;
178+
long spentTime = System.currentTimeMillis() - startTime;
179+
long maxTries = (Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT) - spentTime) / STEP_MS / 4;
180+
181+
// Wait until enough notifications are accrued to match minimum boundary.
182+
long tries = 0;
183+
while (tries++ < maxTries) {
184+
actual = churnBytes.get();
185+
if (minExpected <= actual) {
186+
// Wait some more to test if we are breaking the maximum boundary.
187+
Thread.sleep(5000);
188+
actual = churnBytes.get();
189+
break;
190+
}
191+
Thread.sleep(STEP_MS);
192+
}
172193

173194
String msg = "Expected = [" + minExpected / M + "; " + maxExpected / M + "] (" + mem / M + "), actual = " + actual / M;
174195
if (minExpected <= actual && actual <= maxExpected) {

test/hotspot/jtreg/gc/shenandoah/mxbeans/TestPauseNotifications.java

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* @summary Check that MX notifications are reported for all cycles
2727
* @key gc
2828
* @requires vm.gc.Shenandoah & !vm.graal.enabled
29+
* @library /test/lib /
2930
*
3031
* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
3132
* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=passive
@@ -43,6 +44,7 @@
4344
* @summary Check that MX notifications are reported for all cycles
4445
* @key gc
4546
* @requires vm.gc.Shenandoah & !vm.graal.enabled
47+
* @library /test/lib /
4648
*
4749
* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
4850
* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=aggressive
@@ -53,6 +55,7 @@
5355
* @test TestPauseNotifications
5456
* @summary Check that MX notifications are reported for all cycles
5557
* @requires vm.gc.Shenandoah & !vm.graal.enabled
58+
* @library /test/lib /
5659
*
5760
* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
5861
* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=adaptive
@@ -63,6 +66,7 @@
6366
* @test TestPauseNotifications
6467
* @summary Check that MX notifications are reported for all cycles
6568
* @requires vm.gc.Shenandoah & !vm.graal.enabled
69+
* @library /test/lib /
6670
*
6771
* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
6872
* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=static
@@ -73,6 +77,7 @@
7377
* @test TestPauseNotifications
7478
* @summary Check that MX notifications are reported for all cycles
7579
* @requires vm.gc.Shenandoah & !vm.graal.enabled
80+
* @library /test/lib /
7681
*
7782
* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
7883
* -XX:+UseShenandoahGC -XX:ShenandoahGCHeuristics=compact
@@ -84,6 +89,7 @@
8489
* @summary Check that MX notifications are reported for all cycles
8590
* @key gc
8691
* @requires vm.gc.Shenandoah & !vm.graal.enabled
92+
* @library /test/lib /
8793
*
8894
* @run main/othervm -Xmx128m -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions
8995
* -XX:+UseShenandoahGC -XX:ShenandoahGCMode=iu -XX:ShenandoahGCHeuristics=aggressive
@@ -100,6 +106,8 @@
100106
import java.lang.management.*;
101107
import javax.management.openmbean.*;
102108

109+
import jdk.test.lib.Utils;
110+
103111
import com.sun.management.GarbageCollectionNotificationInfo;
104112

105113
public class TestPauseNotifications {
@@ -110,26 +118,30 @@ public class TestPauseNotifications {
110118
static volatile Object sink;
111119

112120
public static void main(String[] args) throws Exception {
121+
final long startTime = System.currentTimeMillis();
122+
113123
final AtomicLong pausesDuration = new AtomicLong();
114124
final AtomicLong cyclesDuration = new AtomicLong();
125+
final AtomicLong pausesCount = new AtomicLong();
126+
final AtomicLong cyclesCount = new AtomicLong();
115127

116128
NotificationListener listener = new NotificationListener() {
117129
@Override
118130
public void handleNotification(Notification n, Object o) {
119131
if (n.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
120132
GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) n.getUserData());
121133

122-
System.out.println(info.getGcInfo().toString());
123-
System.out.println(info.getGcName());
124-
System.out.println();
134+
System.out.println("Received: " + info.getGcName());
125135

126136
long d = info.getGcInfo().getDuration();
127137

128138
String name = info.getGcName();
129139
if (name.contains("Shenandoah")) {
130140
if (name.equals("Shenandoah Pauses")) {
141+
pausesCount.incrementAndGet();
131142
pausesDuration.addAndGet(d);
132143
} else if (name.equals("Shenandoah Cycles")) {
144+
cyclesCount.incrementAndGet();
133145
cyclesDuration.addAndGet(d);
134146
} else {
135147
throw new IllegalStateException("Unknown name: " + name);
@@ -150,40 +162,57 @@ public void handleNotification(Notification n, Object o) {
150162
sink = new int[size];
151163
}
152164

153-
// Wait until notifications start arriving, and then wait some more
154-
// to catch the ones arriving late.
155-
while (pausesDuration.get() == 0) {
156-
Thread.sleep(1000);
165+
// Look at test timeout to figure out how long we can wait without breaking into timeout.
166+
// Default to 1/4 of the remaining time in 1s steps.
167+
final long STEP_MS = 1000;
168+
long spentTime = System.currentTimeMillis() - startTime;
169+
long maxTries = (Utils.adjustTimeout(Utils.DEFAULT_TEST_TIMEOUT) - spentTime) / STEP_MS / 4;
170+
171+
long actualPauses = 0;
172+
long actualCycles = 0;
173+
174+
// Wait until enough notifications are accrued to match minimum boundary.
175+
long minExpected = 10;
176+
177+
long tries = 0;
178+
while (tries++ < maxTries) {
179+
actualPauses = pausesCount.get();
180+
actualCycles = cyclesCount.get();
181+
if (minExpected <= actualPauses && minExpected <= actualCycles) {
182+
// Wait a little bit to catch the lingering notifications.
183+
Thread.sleep(5000);
184+
actualPauses = pausesCount.get();
185+
actualCycles = cyclesCount.get();
186+
break;
187+
}
188+
Thread.sleep(STEP_MS);
157189
}
158-
Thread.sleep(5000);
159-
160-
long pausesActual = pausesDuration.get();
161-
long cyclesActual = cyclesDuration.get();
162-
163-
long minExpected = 1;
164-
long maxExpected = Long.MAX_VALUE;
165190

166191
{
167-
String msg = "Pauses expected = [" + minExpected + "; " + maxExpected + "], actual = " + pausesActual;
168-
if (minExpected <= pausesActual && pausesActual <= maxExpected) {
192+
String msg = "Pauses expected = [" + minExpected + "; +inf], actual = " + actualPauses;
193+
if (minExpected <= actualPauses) {
169194
System.out.println(msg);
170195
} else {
171196
throw new IllegalStateException(msg);
172197
}
173198
}
174199

175200
{
176-
String msg = "Cycles expected = [" + minExpected + "; " + maxExpected + "], actual = " + cyclesActual;
177-
if (minExpected <= cyclesActual && cyclesActual <= maxExpected) {
201+
String msg = "Cycles expected = [" + minExpected + "; +inf], actual = " + actualCycles;
202+
if (minExpected <= actualCycles) {
178203
System.out.println(msg);
179204
} else {
180205
throw new IllegalStateException(msg);
181206
}
182207
}
183208

184209
{
185-
String msg = "Cycle duration (" + cyclesActual + "), pause duration (" + pausesActual + ")";
186-
if (pausesActual <= cyclesActual) {
210+
long actualPauseDuration = pausesDuration.get();
211+
long actualCycleDuration = cyclesDuration.get();
212+
213+
String msg = "Pauses duration (" + actualPauseDuration + ") is expected to be not larger than cycles duration (" + actualCycleDuration + ")";
214+
215+
if (actualPauseDuration <= actualCycleDuration) {
187216
System.out.println(msg);
188217
} else {
189218
throw new IllegalStateException(msg);

0 commit comments

Comments
 (0)