author | mchung |
Fri, 18 Jun 2010 09:35:22 -0700 | |
changeset 5808 | 3a1f603c5ca7 |
parent 5506 | 202f599c92aa |
child 7019 | f2f6b61d34ac |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2003, 2004, 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 4959889 |
|
27 |
* @summary Basic unit test of memory management testing: |
|
28 |
* 1) setCollectionUsageThreshold() and getCollectionUsageThreshold() |
|
29 |
* 2) test notification emitted for two different memory pools. |
|
30 |
* |
|
31 |
* @author Mandy Chung |
|
32 |
* |
|
33 |
* @build CollectionUsageThreshold MemoryUtil |
|
34 |
* @run main/timeout=300 CollectionUsageThreshold |
|
35 |
*/ |
|
36 |
||
37 |
import java.lang.management.*; |
|
38 |
import java.util.*; |
|
39 |
import javax.management.*; |
|
40 |
import javax.management.openmbean.CompositeData; |
|
41 |
||
42 |
public class CollectionUsageThreshold { |
|
43 |
private static MemoryMXBean mm = ManagementFactory.getMemoryMXBean(); |
|
5808
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
44 |
private static List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans(); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
45 |
private static List<MemoryManagerMXBean> managers = ManagementFactory.getMemoryManagerMXBeans(); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
46 |
private static Map<String, PoolRecord> result = new HashMap<>(); |
2 | 47 |
private static boolean trace = false; |
48 |
private static boolean testFailed = false; |
|
49 |
private static final int EXPECTED_NUM_POOLS = 2; |
|
50 |
private static final int NUM_GCS = 3; |
|
51 |
private static final int THRESHOLD = 10; |
|
52 |
private static Checker checker; |
|
53 |
private static int numGCs = 0; |
|
54 |
||
55 |
static class PoolRecord { |
|
56 |
private MemoryPoolMXBean pool; |
|
57 |
private int listenerInvoked = 0; |
|
58 |
private long notifCount = 0; |
|
59 |
PoolRecord(MemoryPoolMXBean p) { |
|
60 |
this.pool = p; |
|
61 |
} |
|
62 |
int getListenerInvokedCount() { |
|
63 |
return listenerInvoked; |
|
64 |
} |
|
65 |
long getNotifCount() { |
|
66 |
return notifCount; |
|
67 |
} |
|
68 |
MemoryPoolMXBean getPool() { |
|
69 |
return pool; |
|
70 |
} |
|
71 |
void addNotification(MemoryNotificationInfo minfo) { |
|
72 |
listenerInvoked++; |
|
73 |
notifCount = minfo.getCount(); |
|
74 |
} |
|
75 |
} |
|
76 |
||
77 |
static class SensorListener implements NotificationListener { |
|
78 |
private int numNotifs = 0; |
|
79 |
public void handleNotification(Notification notif, Object handback) { |
|
80 |
String type = notif.getType(); |
|
81 |
if (type.equals(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED) || |
|
82 |
type.equals(MemoryNotificationInfo. |
|
83 |
MEMORY_COLLECTION_THRESHOLD_EXCEEDED)) { |
|
84 |
MemoryNotificationInfo minfo = MemoryNotificationInfo. |
|
85 |
from((CompositeData) notif.getUserData()); |
|
86 |
||
87 |
MemoryUtil.printMemoryNotificationInfo(minfo, type); |
|
88 |
PoolRecord pr = (PoolRecord) result.get(minfo.getPoolName()); |
|
89 |
if (pr == null) { |
|
90 |
throw new RuntimeException("Pool " + minfo.getPoolName() + |
|
91 |
" is not selected"); |
|
92 |
} |
|
93 |
if (type != MemoryNotificationInfo. |
|
94 |
MEMORY_COLLECTION_THRESHOLD_EXCEEDED) { |
|
95 |
throw new RuntimeException("Pool " + minfo.getPoolName() + |
|
96 |
" got unexpected notification type: " + |
|
97 |
type); |
|
98 |
} |
|
99 |
pr.addNotification(minfo); |
|
100 |
synchronized (this) { |
|
101 |
numNotifs++; |
|
102 |
if (numNotifs > 0 && (numNotifs % EXPECTED_NUM_POOLS) == 0) { |
|
103 |
checker.goCheckResult(); |
|
104 |
} |
|
105 |
} |
|
106 |
} |
|
107 |
} |
|
108 |
} |
|
109 |
||
110 |
private static long newThreshold; |
|
111 |
public static void main(String args[]) throws Exception { |
|
112 |
if (args.length > 0 && args[0].equals("trace")) { |
|
113 |
trace = true; |
|
114 |
} |
|
115 |
||
116 |
if (trace) { |
|
117 |
MemoryUtil.printMemoryPools(pools); |
|
118 |
MemoryUtil.printMemoryManagers(managers); |
|
119 |
} |
|
120 |
||
121 |
// Find the Old generation which supports low memory detection |
|
5808
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
122 |
for (MemoryPoolMXBean p : pools) { |
2 | 123 |
MemoryUsage u = p.getUsage(); |
124 |
if (p.isUsageThresholdSupported() && p.isCollectionUsageThresholdSupported()) { |
|
125 |
PoolRecord pr = new PoolRecord(p); |
|
126 |
result.put(p.getName(), pr); |
|
127 |
if (result.size() == EXPECTED_NUM_POOLS) { |
|
128 |
break; |
|
129 |
} |
|
130 |
} |
|
131 |
} |
|
132 |
if (result.size() != EXPECTED_NUM_POOLS) { |
|
133 |
throw new RuntimeException("Unexpected number of selected pools"); |
|
134 |
} |
|
135 |
||
5808
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
136 |
try { |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
137 |
checker = new Checker("Checker thread"); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
138 |
checker.setDaemon(true); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
139 |
checker.start(); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
140 |
|
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
141 |
for (PoolRecord pr : result.values()) { |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
142 |
pr.getPool().setCollectionUsageThreshold(THRESHOLD); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
143 |
System.out.println("Collection usage threshold of " + |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
144 |
pr.getPool().getName() + " set to " + THRESHOLD); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
145 |
} |
2 | 146 |
|
5808
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
147 |
SensorListener listener = new SensorListener(); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
148 |
NotificationEmitter emitter = (NotificationEmitter) mm; |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
149 |
emitter.addNotificationListener(listener, null, null); |
2 | 150 |
|
5808
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
151 |
for (int i = 0; i < NUM_GCS; i++) { |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
152 |
invokeGC(); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
153 |
checker.waitForCheckResult(); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
154 |
} |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
155 |
} finally { |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
156 |
// restore the default |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
157 |
for (PoolRecord pr : result.values()) { |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
158 |
pr.getPool().setCollectionUsageThreshold(0); |
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
159 |
} |
2 | 160 |
} |
161 |
||
162 |
if (testFailed) |
|
163 |
throw new RuntimeException("TEST FAILED."); |
|
164 |
||
165 |
System.out.println("Test passed."); |
|
166 |
||
167 |
} |
|
168 |
||
169 |
private static void invokeGC() { |
|
170 |
System.out.println("Calling System.gc()"); |
|
171 |
numGCs++; |
|
172 |
mm.gc(); |
|
173 |
||
174 |
if (trace) { |
|
5808
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
175 |
for (PoolRecord pr : result.values()) { |
2 | 176 |
System.out.println("Usage after GC for: " + pr.getPool().getName()); |
177 |
MemoryUtil.printMemoryUsage(pr.getPool().getUsage()); |
|
178 |
} |
|
179 |
} |
|
180 |
} |
|
181 |
||
182 |
static class Checker extends Thread { |
|
183 |
private Object lock = new Object(); |
|
184 |
private Object go = new Object(); |
|
185 |
private boolean checkerReady = false; |
|
186 |
private int waiters = 0; |
|
187 |
private boolean readyToCheck = false; |
|
188 |
Checker(String name) { |
|
189 |
super(name); |
|
190 |
}; |
|
191 |
public void run() { |
|
192 |
while (true) { |
|
193 |
synchronized (lock) { |
|
194 |
checkerReady = true; |
|
195 |
try { |
|
196 |
lock.wait(); |
|
197 |
} catch (InterruptedException e) { |
|
198 |
// ignore |
|
199 |
} |
|
200 |
checkResult(); |
|
201 |
checkerReady = false; |
|
202 |
} |
|
203 |
} |
|
204 |
} |
|
205 |
private void checkResult() { |
|
5808
3a1f603c5ca7
6961894: TEST_BUG: jdk_lang tests fail in samevm mode
mchung
parents:
5506
diff
changeset
|
206 |
for (PoolRecord pr : result.values()) { |
2 | 207 |
if (pr.getListenerInvokedCount() != numGCs) { |
208 |
throw new RuntimeException("Listeners invoked count = " + |
|
209 |
pr.getListenerInvokedCount() + " expected to be " + |
|
210 |
numGCs); |
|
211 |
} |
|
212 |
if (pr.getNotifCount() != numGCs) { |
|
213 |
throw new RuntimeException("Notif Count = " + |
|
214 |
pr.getNotifCount() + " expected to be " + |
|
215 |
numGCs); |
|
216 |
} |
|
217 |
||
218 |
long count = pr.getPool().getCollectionUsageThresholdCount(); |
|
219 |
if (count != numGCs) { |
|
220 |
throw new RuntimeException("CollectionUsageThresholdCount = " + |
|
221 |
count + " expected to be " + numGCs); |
|
222 |
} |
|
223 |
if (!pr.getPool().isCollectionUsageThresholdExceeded()) { |
|
224 |
throw new RuntimeException("isCollectionUsageThresholdExceeded" + |
|
225 |
" expected to be true"); |
|
226 |
} |
|
227 |
} |
|
228 |
synchronized (go) { |
|
229 |
// wait until the main thread is waiting for notification |
|
230 |
while (waiters == 0) { |
|
231 |
try { |
|
232 |
go.wait(50); |
|
233 |
} catch (InterruptedException e) { |
|
234 |
// ignore |
|
235 |
} |
|
236 |
} |
|
237 |
||
238 |
System.out.println(Thread.currentThread().getName() + |
|
239 |
" notifying main thread to continue - result checking finished"); |
|
240 |
go.notify(); |
|
241 |
} |
|
242 |
} |
|
243 |
public void goCheckResult() { |
|
244 |
System.out.println(Thread.currentThread().getName() + |
|
245 |
" notifying to check result"); |
|
246 |
synchronized (lock) { |
|
247 |
while (!checkerReady) { |
|
248 |
try { |
|
249 |
lock.wait(50); |
|
250 |
} catch (InterruptedException e) { |
|
251 |
// ignore |
|
252 |
} |
|
253 |
} |
|
254 |
lock.notify(); |
|
255 |
} |
|
256 |
} |
|
257 |
||
258 |
public void waitForCheckResult() { |
|
259 |
System.out.println(Thread.currentThread().getName() + |
|
260 |
" waiting for result checking finishes"); |
|
261 |
synchronized (go) { |
|
262 |
waiters++; |
|
263 |
try { |
|
264 |
go.wait(); |
|
265 |
} catch (InterruptedException e) { |
|
266 |
// ignore |
|
267 |
} |
|
268 |
waiters--; |
|
269 |
} |
|
270 |
} |
|
271 |
} |
|
272 |
} |