@@ -107,17 +107,17 @@ void gcTimingIsCorrectForPauseCycleCollectors() {
107107 // get initial GC timing metrics from JMX, if any
108108 // GC could have happened before this test due to testing infrastructure
109109 // If it did, it will not be captured in the metrics
110- long initialPausePhaseCount = 0 ;
110+ long initialPauseCount = 0 ;
111111 long initialPauseTimeMs = 0 ;
112- long initialConcurrentPhaseCount = 0 ;
112+ long initialConcurrentCount = 0 ;
113113 long initialConcurrentTimeMs = 0 ;
114114 for (GarbageCollectorMXBean mbean : ManagementFactory .getGarbageCollectorMXBeans ()) {
115115 if (mbean .getName ().contains ("Pauses" )) {
116- initialPausePhaseCount += mbean .getCollectionCount ();
116+ initialPauseCount += mbean .getCollectionCount ();
117117 initialPauseTimeMs += mbean .getCollectionTime ();
118118 }
119119 else if (mbean .getName ().contains ("Cycles" )) {
120- initialConcurrentPhaseCount += mbean .getCollectionCount ();
120+ initialConcurrentCount += mbean .getCollectionCount ();
121121 initialConcurrentTimeMs += mbean .getCollectionTime ();
122122 }
123123 }
@@ -127,33 +127,11 @@ else if (mbean.getName().contains("Cycles")) {
127127 // cause GC to record new metrics
128128 System .gc ();
129129
130- // get metrics from JMX again to obtain difference
131- long pausePhaseCount = 0 ;
132- long pauseTimeMs = 0 ;
133- long concurrentPhaseCount = 0 ;
134- long concurrentTimeMs = 0 ;
135- for (GarbageCollectorMXBean mbean : ManagementFactory .getGarbageCollectorMXBeans ()) {
136- if (mbean .getName ().contains ("Pauses" )) {
137- pausePhaseCount += mbean .getCollectionCount ();
138- pauseTimeMs += mbean .getCollectionTime ();
139- }
140- else if (mbean .getName ().contains ("Cycles" )) {
141- concurrentPhaseCount += mbean .getCollectionCount ();
142- concurrentTimeMs += mbean .getCollectionTime ();
143- }
144- }
145-
146- // subtract any difference
147- pausePhaseCount -= initialPausePhaseCount ;
148- pauseTimeMs -= initialPauseTimeMs ;
149- concurrentPhaseCount -= initialConcurrentPhaseCount ;
150- concurrentTimeMs -= initialConcurrentTimeMs ;
151-
152- checkPhaseCount (pausePhaseCount , concurrentPhaseCount );
153- checkCollectionTime (pauseTimeMs , concurrentTimeMs );
130+ checkPhaseCount (initialPauseCount , initialConcurrentCount );
131+ checkCollectionTime (initialPauseTimeMs , initialConcurrentTimeMs );
154132 }
155133
156- boolean isPauseCyclesGc () {
134+ static boolean isPauseCyclesGc () {
157135 return ManagementFactory .getGarbageCollectorMXBeans ()
158136 .stream ()
159137 .map (MemoryManagerMXBean ::getName )
@@ -225,8 +203,24 @@ public void handleNotification(Notification notification, Object handback) {
225203
226204 }
227205
228- private void checkPhaseCount (long expectedPauseCount , long expectedConcurrentCount ) {
206+ private void checkPhaseCount (long initialPauseCount , long initialConcurrentCount ) {
229207 await ().atMost (200 , TimeUnit .MILLISECONDS ).untilAsserted (() -> {
208+ long pauseCount = 0 ;
209+ long concurrentCount = 0 ;
210+
211+ // get metrics from JMX again to obtain the difference
212+ for (GarbageCollectorMXBean mbean : ManagementFactory .getGarbageCollectorMXBeans ()) {
213+ if (mbean .getName ().contains ("Pauses" )) {
214+ pauseCount += mbean .getCollectionCount ();
215+ }
216+ else if (mbean .getName ().contains ("Cycles" )) {
217+ concurrentCount += mbean .getCollectionCount ();
218+ }
219+ }
220+
221+ long expectedPauseCount = pauseCount - initialPauseCount ;
222+ long expectedConcurrentCount = concurrentCount - initialConcurrentCount ;
223+
230224 long observedPauseCount = registry .find ("jvm.gc.pause" ).timers ().stream ().mapToLong (Timer ::count ).sum ();
231225 long observedConcurrentCount = registry .find ("jvm.gc.concurrent.phase.time" )
232226 .timers ()
@@ -238,8 +232,24 @@ private void checkPhaseCount(long expectedPauseCount, long expectedConcurrentCou
238232 });
239233 }
240234
241- private void checkCollectionTime (long expectedPauseTimeMs , long expectedConcurrentTimeMs ) {
235+ private void checkCollectionTime (long initialPauseTimeMs , long initialConcurrentTimeMs ) {
242236 await ().atMost (200 , TimeUnit .MILLISECONDS ).untilAsserted (() -> {
237+ long pauseTimeMs = 0 ;
238+ long concurrentTimeMs = 0 ;
239+
240+ // get metrics from JMX again to obtain the difference
241+ for (GarbageCollectorMXBean mbean : ManagementFactory .getGarbageCollectorMXBeans ()) {
242+ if (mbean .getName ().contains ("Pauses" )) {
243+ pauseTimeMs += mbean .getCollectionTime ();
244+ }
245+ else if (mbean .getName ().contains ("Cycles" )) {
246+ concurrentTimeMs += mbean .getCollectionTime ();
247+ }
248+ }
249+
250+ long expectedPauseTimeMs = pauseTimeMs - initialPauseTimeMs ;
251+ long expectedConcurrentTimeMs = concurrentTimeMs - initialConcurrentTimeMs ;
252+
243253 double observedPauseTimeMs = registry .find ("jvm.gc.pause" )
244254 .timers ()
245255 .stream ()
0 commit comments