Skip to content

Commit fbc2fa3

Browse files
committed
8271834: TestStringDeduplicationAgeThreshold intermittent failures on Shenandoah
Backport-of: f9b2507f3e86bcb91e8ccfd0a84f31712fd535c2
1 parent cab2c67 commit fbc2fa3

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

test/hotspot/jtreg/gc/stringdedup/TestStringDeduplicationTools.java

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@
2727
* Common code for string deduplication tests
2828
*/
2929

30+
import com.sun.management.GarbageCollectionNotificationInfo;
3031
import java.lang.reflect.*;
32+
import java.lang.management.*;
3133
import java.util.*;
34+
import javax.management.*;
35+
import javax.management.openmbean.*;
3236
import jdk.test.lib.process.ProcessTools;
3337
import jdk.test.lib.process.OutputAnalyzer;
3438
import sun.misc.*;
@@ -89,21 +93,58 @@ private static void doFullGc(int numberOfTimes) {
8993
}
9094
}
9195

96+
private static volatile int gcCount;
97+
private static NotificationListener listener = new NotificationListener() {
98+
@Override
99+
public void handleNotification(Notification n, Object o) {
100+
if (n.getType().equals(GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION)) {
101+
GarbageCollectionNotificationInfo info = GarbageCollectionNotificationInfo.from((CompositeData) n.getUserData());
102+
// Shenandoah and Z GC also report GC pauses, skip them
103+
if (info.getGcName().startsWith("Shenandoah") || info.getGcName().startsWith("ZGC")) {
104+
if ("end of GC cycle".equals(info.getGcAction())) {
105+
gcCount++;
106+
}
107+
} else {
108+
gcCount++;
109+
}
110+
}
111+
}
112+
};
113+
114+
private static void registerGCListener() {
115+
for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
116+
((NotificationEmitter)bean).addNotificationListener(listener, null, null);
117+
}
118+
}
119+
120+
private static void unregisterGCListener() {
121+
for (GarbageCollectorMXBean bean : ManagementFactory.getGarbageCollectorMXBeans()) {
122+
try {
123+
((NotificationEmitter) bean).removeNotificationListener(listener, null, null);
124+
} catch (Exception e) {
125+
}
126+
}
127+
}
128+
92129
private static void doYoungGc(int numberOfTimes) {
93-
// Provoke at least numberOfTimes young GCs
94130
final int objectSize = 128;
95-
final int maxObjectInYoung = (Xmn * MB) / objectSize;
96131
List<List<String>> newStrings = new ArrayList<List<String>>();
97-
for (int i = 0; i < numberOfTimes; i++) {
132+
133+
// Provoke at least numberOfTimes young GCs
134+
gcCount = 0;
135+
registerGCListener();
136+
while (gcCount < numberOfTimes) {
137+
int currentCount = gcCount;
98138
// Create some more strings for every collection, to ensure
99139
// there will be deduplication work that will be reported.
100140
newStrings.add(createStrings(SmallNumberOfStrings, SmallNumberOfStrings));
101-
System.out.println("Begin: Young GC " + (i + 1) + "/" + numberOfTimes);
102-
for (int j = 0; j < maxObjectInYoung + 1; j++) {
141+
System.out.println("Begin: Young GC " + (currentCount + 1) + "/" + numberOfTimes);
142+
while (currentCount == gcCount) {
103143
dummy = new byte[objectSize];
104144
}
105-
System.out.println("End: Young GC " + (i + 1) + "/" + numberOfTimes);
145+
System.out.println("End: Young GC " + (currentCount + 1) + "/" + numberOfTimes);
106146
}
147+
unregisterGCListener();
107148
}
108149

109150
private static void forceDeduplication(int ageThreshold, String gcType) {

0 commit comments

Comments
 (0)