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
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
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
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
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
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
100106import java .lang .management .*;
101107import javax .management .openmbean .*;
102108
109+ import jdk .test .lib .Utils ;
110+
103111import com .sun .management .GarbageCollectionNotificationInfo ;
104112
105113public 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