author | mchung |
Thu, 19 Dec 2013 13:43:30 -0800 | |
changeset 22075 | 4c204174a8d9 |
parent 7668 | d4a77089c587 |
child 23933 | 754ee146b623 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 4530538 |
|
27 |
* @summary Basic unit test of memory management testing: |
|
28 |
* 1) setUsageThreshold() and getUsageThreshold() |
|
29 |
* 2) test low memory detection on the old generation. |
|
30 |
* |
|
31 |
* @author Mandy Chung |
|
32 |
* |
|
33 |
* @build LowMemoryTest MemoryUtil |
|
5808
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
34 |
* @run main/othervm/timeout=600 LowMemoryTest |
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
35 |
* @run main/othervm/timeout=600 -XX:+UseConcMarkSweepGC LowMemoryTest |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
36 |
* @run main/othervm/timeout=600 -XX:+UseParallelGC LowMemoryTest |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
37 |
* @run main/othervm/timeout=600 -XX:+UseSerialGC LowMemoryTest |
2 | 38 |
*/ |
39 |
||
40 |
import java.lang.management.*; |
|
41 |
import java.util.*; |
|
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
42 |
import java.util.concurrent.Phaser; |
2 | 43 |
import javax.management.*; |
44 |
import javax.management.openmbean.CompositeData; |
|
45 |
||
46 |
public class LowMemoryTest { |
|
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
47 |
private static final MemoryMXBean mm = ManagementFactory.getMemoryMXBean(); |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
48 |
private static final List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans(); |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
49 |
private static final Phaser phaser = new Phaser(2); |
2 | 50 |
private static MemoryPoolMXBean mpool = null; |
51 |
private static boolean trace = false; |
|
52 |
private static boolean testFailed = false; |
|
53 |
private static final int NUM_TRIGGERS = 5; |
|
54 |
private static final int NUM_CHUNKS = 2; |
|
55 |
private static long chunkSize; |
|
56 |
||
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
57 |
private static volatile boolean listenerInvoked = false; |
2 | 58 |
static class SensorListener implements NotificationListener { |
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
59 |
@Override |
2 | 60 |
public void handleNotification(Notification notif, Object handback) { |
61 |
String type = notif.getType(); |
|
62 |
if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) || |
|
63 |
type.equals(MemoryNotificationInfo. |
|
64 |
MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) { |
|
65 |
||
66 |
MemoryNotificationInfo minfo = MemoryNotificationInfo. |
|
67 |
from((CompositeData) notif.getUserData()); |
|
68 |
||
69 |
MemoryUtil.printMemoryNotificationInfo(minfo, type); |
|
70 |
listenerInvoked = true; |
|
71 |
} |
|
72 |
} |
|
73 |
} |
|
74 |
||
75 |
static class TestListener implements NotificationListener { |
|
76 |
private int triggers = 0; |
|
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
77 |
private final long[] count = new long[NUM_TRIGGERS * 2]; |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
78 |
private final long[] usedMemory = new long[NUM_TRIGGERS * 2]; |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
79 |
@Override |
2 | 80 |
public void handleNotification(Notification notif, Object handback) { |
81 |
MemoryNotificationInfo minfo = MemoryNotificationInfo. |
|
82 |
from((CompositeData) notif.getUserData()); |
|
83 |
count[triggers] = minfo.getCount(); |
|
84 |
usedMemory[triggers] = minfo.getUsage().getUsed(); |
|
85 |
triggers++; |
|
86 |
} |
|
87 |
public void checkResult() throws Exception { |
|
88 |
if (triggers != NUM_TRIGGERS) { |
|
89 |
throw new RuntimeException("Unexpected number of triggers = " + |
|
90 |
triggers + " but expected to be " + NUM_TRIGGERS); |
|
91 |
} |
|
92 |
||
93 |
for (int i = 0; i < triggers; i++) { |
|
94 |
if (count[i] != i+1) { |
|
95 |
throw new RuntimeException("Unexpected count of" + |
|
96 |
" notification #" + i + |
|
97 |
" count = " + count[i] + |
|
98 |
" but expected to be " + (i+1)); |
|
99 |
} |
|
100 |
if (usedMemory[i] < newThreshold) { |
|
101 |
throw new RuntimeException("Used memory = " + |
|
102 |
usedMemory[i] + " is less than the threshold = " + |
|
103 |
newThreshold); |
|
104 |
} |
|
105 |
} |
|
106 |
} |
|
107 |
} |
|
108 |
||
109 |
private static long newThreshold; |
|
110 |
public static void main(String args[]) throws Exception { |
|
111 |
if (args.length > 0 && args[0].equals("trace")) { |
|
112 |
trace = true; |
|
113 |
} |
|
114 |
||
115 |
// Find the Old generation which supports low memory detection |
|
116 |
ListIterator iter = pools.listIterator(); |
|
117 |
while (iter.hasNext()) { |
|
118 |
MemoryPoolMXBean p = (MemoryPoolMXBean) iter.next(); |
|
119 |
if (p.getType() == MemoryType.HEAP && |
|
120 |
p.isUsageThresholdSupported()) { |
|
121 |
mpool = p; |
|
122 |
if (trace) { |
|
123 |
System.out.println("Selected memory pool for low memory " + |
|
124 |
"detection."); |
|
125 |
MemoryUtil.printMemoryPool(mpool); |
|
126 |
} |
|
127 |
break; |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
TestListener listener = new TestListener(); |
|
132 |
SensorListener l2 = new SensorListener(); |
|
133 |
NotificationEmitter emitter = (NotificationEmitter) mm; |
|
134 |
emitter.addNotificationListener(listener, null, null); |
|
135 |
emitter.addNotificationListener(l2, null, null); |
|
136 |
||
137 |
Thread allocator = new AllocatorThread(); |
|
138 |
Thread sweeper = new SweeperThread(); |
|
139 |
||
140 |
// Now set threshold |
|
141 |
MemoryUsage mu = mpool.getUsage(); |
|
142 |
chunkSize = (mu.getMax() - mu.getUsed()) / 20; |
|
143 |
newThreshold = mu.getUsed() + (chunkSize * NUM_CHUNKS); |
|
144 |
||
145 |
System.out.println("Setting threshold for " + mpool.getName() + |
|
146 |
" from " + mpool.getUsageThreshold() + " to " + newThreshold + |
|
147 |
". Current used = " + mu.getUsed()); |
|
148 |
mpool.setUsageThreshold(newThreshold); |
|
149 |
||
150 |
if (mpool.getUsageThreshold() != newThreshold) { |
|
151 |
throw new RuntimeException("TEST FAILED: " + |
|
152 |
"Threshold for Memory pool " + mpool.getName() + |
|
153 |
"is " + mpool.getUsageThreshold() + " but expected to be" + |
|
154 |
newThreshold); |
|
155 |
} |
|
156 |
||
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
157 |
|
2 | 158 |
allocator.start(); |
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
159 |
// Force Allocator start first |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
160 |
phaser.arriveAndAwaitAdvance(); |
2 | 161 |
sweeper.start(); |
162 |
||
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
163 |
|
2 | 164 |
try { |
165 |
allocator.join(); |
|
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
166 |
// Wait until AllocatorThread's done |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
167 |
phaser.arriveAndAwaitAdvance(); |
2 | 168 |
sweeper.join(); |
169 |
} catch (InterruptedException e) { |
|
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
170 |
System.out.println("Unexpected exception:" + e); |
2 | 171 |
testFailed = true; |
172 |
} |
|
173 |
||
174 |
listener.checkResult(); |
|
175 |
||
176 |
if (testFailed) |
|
177 |
throw new RuntimeException("TEST FAILED."); |
|
178 |
||
179 |
System.out.println("Test passed."); |
|
180 |
||
181 |
} |
|
182 |
||
183 |
private static void goSleep(long ms) { |
|
184 |
try { |
|
185 |
Thread.sleep(ms); |
|
186 |
} catch (InterruptedException e) { |
|
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
187 |
System.out.println("Unexpected exception:" + e); |
2 | 188 |
testFailed = true; |
189 |
} |
|
190 |
} |
|
191 |
||
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
192 |
private static final List<Object> objectPool = new ArrayList<>(); |
2 | 193 |
static class AllocatorThread extends Thread { |
194 |
public void doTask() { |
|
195 |
int iterations = 0; |
|
196 |
int numElements = (int) (chunkSize / 4); // minimal object size |
|
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
197 |
while (!listenerInvoked || mpool.getUsage().getUsed() < mpool.getUsageThreshold()) { |
2 | 198 |
iterations++; |
199 |
if (trace) { |
|
200 |
System.out.println(" Iteration " + iterations + |
|
201 |
": before allocation " + |
|
202 |
mpool.getUsage().getUsed()); |
|
203 |
} |
|
204 |
||
205 |
Object[] o = new Object[numElements]; |
|
206 |
if (iterations <= NUM_CHUNKS) { |
|
207 |
// only hold a reference to the first NUM_CHUNKS |
|
208 |
// allocated objects |
|
209 |
objectPool.add(o); |
|
210 |
} |
|
211 |
||
212 |
if (trace) { |
|
213 |
System.out.println(" " + |
|
214 |
" after allocation " + |
|
215 |
mpool.getUsage().getUsed()); |
|
216 |
} |
|
217 |
goSleep(100); |
|
218 |
} |
|
219 |
} |
|
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
220 |
@Override |
2 | 221 |
public void run() { |
222 |
for (int i = 1; i <= NUM_TRIGGERS; i++) { |
|
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
223 |
// Sync with SweeperThread's second phase. |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
224 |
phaser.arriveAndAwaitAdvance(); |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
225 |
System.out.println("AllocatorThread is doing task " + i + |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
226 |
" phase " + phaser.getPhase()); |
2 | 227 |
doTask(); |
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
228 |
// Sync with SweeperThread's first phase. |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
229 |
phaser.arriveAndAwaitAdvance(); |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
230 |
System.out.println("AllocatorThread done task " + i + |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
231 |
" phase " + phaser.getPhase()); |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
232 |
if (testFailed) { |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
233 |
return; |
2 | 234 |
} |
235 |
} |
|
236 |
} |
|
237 |
} |
|
238 |
||
239 |
static class SweeperThread extends Thread { |
|
240 |
private void doTask() { |
|
241 |
for (; mpool.getUsage().getUsed() >= |
|
242 |
mpool.getUsageThreshold();) { |
|
243 |
// clear all allocated objects and invoke GC |
|
244 |
objectPool.clear(); |
|
245 |
mm.gc(); |
|
246 |
goSleep(100); |
|
247 |
} |
|
248 |
} |
|
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
249 |
@Override |
2 | 250 |
public void run() { |
251 |
for (int i = 1; i <= NUM_TRIGGERS; i++) { |
|
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
252 |
// Sync with AllocatorThread's first phase. |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
253 |
phaser.arriveAndAwaitAdvance(); |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
254 |
System.out.println("SweepThread is doing task " + i + |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
255 |
" phase " + phaser.getPhase()); |
2 | 256 |
doTask(); |
257 |
||
258 |
listenerInvoked = false; |
|
259 |
||
22075
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
260 |
// Sync with AllocatorThread's second phase. |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
261 |
phaser.arriveAndAwaitAdvance(); |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
262 |
System.out.println("SweepThread done task " + i + |
4c204174a8d9
8029346: LowMemoryTestConcMarkSweepGC.sh fails intermittently with timeout
mchung
parents:
7668
diff
changeset
|
263 |
" phase " + phaser.getPhase()); |
2 | 264 |
if (testFailed) return; |
265 |
} |
|
266 |
} |
|
267 |
} |
|
268 |
} |