Skip to content

Commit 11cdafb

Browse files
committed
8367297: Test com/sun/jdi/JdbStopInNotificationThreadTest.java can still fail after JDK-8366850
Reviewed-by: ayang, kevinw
1 parent f8ba02f commit 11cdafb

File tree

1 file changed

+49
-14
lines changed

1 file changed

+49
-14
lines changed

test/jdk/com/sun/jdi/JdbStopInNotificationThreadTest.java

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,14 @@ class JdbStopInNotificationThreadTestTarg {
4747

4848
private static volatile boolean done = false;
4949

50-
private static final MemoryPoolMXBean tenuredGenPool =
51-
findTenuredGenPool();
50+
private static final MemoryPoolMXBean tenuredGenPool = findTenuredGenPool();
5251

5352
public static void main(String[] args) throws Exception {
5453
test(); // @1 breakpoint
5554
}
5655

5756
private static void test() throws Exception {
58-
setPercentageUsageThreshold(0.1);
57+
setPercentageUsageThreshold(0.2);
5958
MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
6059
NotificationEmitter emitter = (NotificationEmitter) mbean;
6160
emitter.addNotificationListener(new NotificationListener() {
@@ -74,34 +73,70 @@ public void handleNotification(Notification n, Object hb) {
7473
}
7574
}, null, null);
7675

77-
Collection<Object[]> numbers = new LinkedList();
76+
Collection<int[]> numbers = new LinkedList();
7877
long counter = 0;
78+
System.out.println(tenuredGenPool.getName() + ": " + tenuredGenPool.getUsage());
7979
while (!done) {
80-
numbers.add(new Object[1000]);
80+
numbers.add(new int[1000]);
8181
counter++;
8282
if (counter % 1000 == 0) {
83-
Thread.sleep(100);
83+
System.gc(); // Encourage promotion into old/tenured generation
84+
Thread.sleep(100); // Give the notification a bit of time to happen
85+
MemoryUsage usage = tenuredGenPool.getUsage();
86+
long used = usage.getUsed();
87+
long max = usage.getMax();
88+
System.out.println(tenuredGenPool.getName() + ": " + tenuredGenPool.getUsage());
89+
if ((float)used / (float)max > .50) {
90+
// If we have allocated 50% of the heap pool, block here until the
91+
// notication arrives
92+
System.out.println("counter: " + counter);
93+
System.out.println(tenuredGenPool.getName() + ": " + tenuredGenPool.getUsage());
94+
System.out.println(">50% of heap pool allocated (" + used + "). Blocking...");
95+
while (!done) {
96+
Thread.sleep(100);
97+
}
98+
System.out.println("Finished blocking");
99+
}
84100
}
85101
}
102+
86103
System.out.println("Done");
87104
}
88105

89106
private static MemoryPoolMXBean findTenuredGenPool() {
90-
for (MemoryPoolMXBean pool :
91-
ManagementFactory.getMemoryPoolMXBeans()) {
92-
if (pool.getType() == MemoryType.HEAP &&
93-
pool.isUsageThresholdSupported()) {
94-
return pool;
107+
// List of supported GC pools
108+
String[] supportedPools = {
109+
"Tenured Gen", // Serial GC
110+
"PS Old Gen", // Parallel GC
111+
"G1 Old Gen", // G1 GC
112+
"ZGC Old Generation", // Z GC
113+
"Shenandoah", // Shenandoah GC
114+
"Shenandoah Old Gen" // Shenandoah generational mode GC
115+
};
116+
117+
for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
118+
if (pool.getType() == MemoryType.HEAP && pool.isUsageThresholdSupported()) {
119+
System.out.println("Verify pool: " + pool.getName());
120+
for (String str : supportedPools) {
121+
String poolName = pool.getName();
122+
if (str.equals(poolName)) {
123+
System.out.println("Pool Verified: " + pool.getName());
124+
return pool;
125+
}
126+
}
95127
}
96128
}
97-
throw new RuntimeException("Could not find tenured space");
129+
130+
RuntimeException ex = new RuntimeException("Could not find tenured space");
131+
ex.printStackTrace(System.out);
132+
throw ex;
98133
}
99134

100135
public static void setPercentageUsageThreshold(double percentage) {
101136
if (percentage <= 0.0 || percentage > 1.0) {
102137
throw new IllegalArgumentException("Percentage not in range");
103138
}
104-
System.out.println("Setting threashold for pool " + tenuredGenPool.getName() + " percentage:" + percentage);
139+
System.out.println("Setting threshold for pool " + tenuredGenPool.getName() + " percentage:" + percentage);
105140
long maxMemory = tenuredGenPool.getUsage().getMax();
106141
long warningThreshold = (long) (maxMemory * percentage);
107142
tenuredGenPool.setUsageThreshold(warningThreshold);
@@ -113,7 +148,7 @@ public class JdbStopInNotificationThreadTest extends JdbTest {
113148
private static final String DEBUGGEE_CLASS = JdbStopInNotificationThreadTestTarg.class.getName();
114149
private static final String PATTERN1_TEMPLATE = "^Breakpoint hit: \"thread=Notification Thread\", " +
115150
"JdbStopInNotificationThreadTestTarg\\$1\\.handleNotification\\(\\), line=%LINE_NUMBER.*\\R%LINE_NUMBER\\s+System\\.out\\.println\\(\"Memory usage low!!!\"\\);.*";
116-
private static final String[] DEBUGGEE_OPTIONS = {"-Xmx256M"};
151+
private static final String[] DEBUGGEE_OPTIONS = {"-Xmx128M"};
117152

118153
private JdbStopInNotificationThreadTest() {
119154
super(new LaunchOptions(DEBUGGEE_CLASS)

0 commit comments

Comments
 (0)