author | ykantser |
Thu, 07 May 2015 09:11:49 +0200 | |
changeset 30376 | 2ccf2cf7ea48 |
parent 26867 | 904e243eac08 |
child 37890 | f6cb5112c878 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
30376
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
26867
diff
changeset
|
2 |
* Copyright (c) 2003, 2015, 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 MemoryMXBean.getMemoryPools() and |
|
28 |
* MemoryMXBean.getMemoryManager(). |
|
29 |
* @author Mandy Chung |
|
30 |
* |
|
30376
2ccf2cf7ea48
8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents:
26867
diff
changeset
|
31 |
* @modules java.management |
2 | 32 |
* @run main MemoryTest 2 |
33 |
*/ |
|
34 |
||
35 |
/* |
|
36 |
* NOTE: This expected result is hardcoded in this test and this test |
|
37 |
* will be affected if the heap memory layout is changed in |
|
38 |
* the future implementation. |
|
39 |
*/ |
|
40 |
||
41 |
import java.lang.management.*; |
|
42 |
import java.util.*; |
|
43 |
||
44 |
public class MemoryTest { |
|
45 |
private static boolean testFailed = false; |
|
46 |
private static MemoryMXBean mm = ManagementFactory.getMemoryMXBean(); |
|
47 |
private static final int HEAP = 0; |
|
48 |
private static final int NONHEAP = 1; |
|
49 |
private static final int NUM_TYPES = 2; |
|
50 |
||
51 |
// WARNING: if the number of pools changes in the future, |
|
52 |
// this test needs to be modified to handle different version of VMs. |
|
53 |
||
54 |
// Hotspot VM 1.5 expected to have |
|
55 |
// heap memory pools = 3 (Eden, Survivor spaces, Old gen) |
|
56 |
// non-heap memory pools = 2 (Perm gen, Code cache) |
|
57 |
// or 4 if Class Sharing is enabled. |
|
58 |
// Number of memory managers = 3 |
|
59 |
// They are: Copy/Scavenger + MSC + CodeCache manager |
|
60 |
// (or equivalent for other collectors) |
|
61 |
// Number of GC memory managers = 2 |
|
13784 | 62 |
|
26867 | 63 |
// Hotspot VM 1.8+ after perm gen removal is expected to have between two |
64 |
// or five non-heap memory pools: |
|
65 |
// - Code cache (between one and three depending on the -XX:SegmentedCodeCache option) |
|
18785
92a4cdaeca0f
8010734: NPG: The test MemoryTest.java needs to be updated to support metaspace
ehelin
parents:
14342
diff
changeset
|
66 |
// - Metaspace |
92a4cdaeca0f
8010734: NPG: The test MemoryTest.java needs to be updated to support metaspace
ehelin
parents:
14342
diff
changeset
|
67 |
// - Compressed Class Space (if compressed class pointers are used) |
92a4cdaeca0f
8010734: NPG: The test MemoryTest.java needs to be updated to support metaspace
ehelin
parents:
14342
diff
changeset
|
68 |
private static int[] expectedMinNumPools = {3, 2}; |
26867 | 69 |
private static int[] expectedMaxNumPools = {3, 5}; |
2 | 70 |
private static int expectedNumGCMgrs = 2; |
18785
92a4cdaeca0f
8010734: NPG: The test MemoryTest.java needs to be updated to support metaspace
ehelin
parents:
14342
diff
changeset
|
71 |
private static int expectedNumMgrs = expectedNumGCMgrs + 2; |
2 | 72 |
private static String[] types = { "heap", "non-heap" }; |
73 |
||
74 |
public static void main(String args[]) throws Exception { |
|
75 |
Integer value = new Integer(args[0]); |
|
76 |
expectedNumGCMgrs = value.intValue(); |
|
18785
92a4cdaeca0f
8010734: NPG: The test MemoryTest.java needs to be updated to support metaspace
ehelin
parents:
14342
diff
changeset
|
77 |
expectedNumMgrs = expectedNumGCMgrs + 2; |
2 | 78 |
|
79 |
checkMemoryPools(); |
|
80 |
checkMemoryManagers(); |
|
81 |
if (testFailed) |
|
82 |
throw new RuntimeException("TEST FAILED."); |
|
83 |
||
84 |
System.out.println("Test passed."); |
|
85 |
||
86 |
} |
|
87 |
||
88 |
private static void checkMemoryPools() throws Exception { |
|
89 |
List pools = ManagementFactory.getMemoryPoolMXBeans(); |
|
13784 | 90 |
boolean hasPerm = false; |
2 | 91 |
|
92 |
int[] numPools = new int[NUM_TYPES]; |
|
93 |
for (ListIterator iter = pools.listIterator(); iter.hasNext();) { |
|
94 |
MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next(); |
|
95 |
if (pool.getType() == MemoryType.HEAP) { |
|
96 |
numPools[HEAP]++; |
|
97 |
} |
|
98 |
if (pool.getType() == MemoryType.NON_HEAP) { |
|
99 |
numPools[NONHEAP]++; |
|
100 |
} |
|
13784 | 101 |
if (pool.getName().toLowerCase().contains("perm")) { |
102 |
hasPerm = true; |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
if (hasPerm) { |
|
107 |
// If the VM has perm gen there will be between 2 and 4 non heap |
|
108 |
// pools (4 if class data sharing is used) |
|
109 |
expectedMinNumPools[NONHEAP] = 2; |
|
110 |
expectedMaxNumPools[NONHEAP] = 4; |
|
2 | 111 |
} |
112 |
||
113 |
// Check the number of Memory pools |
|
114 |
for (int i = 0; i < NUM_TYPES; i++) { |
|
115 |
if (numPools[i] < expectedMinNumPools[i] || |
|
116 |
numPools[i] > expectedMaxNumPools[i]) { |
|
117 |
throw new RuntimeException("TEST FAILED: " + |
|
118 |
"Number of " + types[i] + " pools = " + numPools[i] + |
|
119 |
" but expected <= " + expectedMaxNumPools[i] + |
|
120 |
" and >= " + expectedMinNumPools[i]); |
|
121 |
} |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
private static void checkMemoryManagers() throws Exception { |
|
126 |
List mgrs = ManagementFactory.getMemoryManagerMXBeans(); |
|
127 |
||
128 |
int numGCMgr = 0; |
|
129 |
||
130 |
// Check the number of Memory Managers |
|
131 |
for (ListIterator iter = mgrs.listIterator(); iter.hasNext();) { |
|
132 |
MemoryManagerMXBean mgr = (MemoryManagerMXBean) iter.next(); |
|
133 |
String[] poolNames = mgr.getMemoryPoolNames(); |
|
134 |
if (poolNames == null || poolNames.length == 0) { |
|
135 |
throw new RuntimeException("TEST FAILED: " + |
|
136 |
"Expected to have one or more pools for " + |
|
137 |
mgr.getName() + "manager."); |
|
138 |
} |
|
139 |
||
140 |
if (mgr instanceof GarbageCollectorMXBean) { |
|
141 |
numGCMgr++; |
|
142 |
} else { |
|
143 |
for (int i = 0; i < poolNames.length; i++) { |
|
144 |
checkPoolType(poolNames[i], MemoryType.NON_HEAP); |
|
145 |
} |
|
146 |
} |
|
147 |
} |
|
148 |
||
149 |
if (mgrs.size() != expectedNumMgrs) { |
|
150 |
throw new RuntimeException("TEST FAILED: " + |
|
151 |
"Number of memory managers = " + mgrs.size() + |
|
152 |
" but expected = " + expectedNumMgrs); |
|
153 |
} |
|
154 |
if (numGCMgr != expectedNumGCMgrs) { |
|
155 |
throw new RuntimeException("TEST FAILED: " + |
|
156 |
"Number of GC managers = " + numGCMgr + " but expected = " + |
|
157 |
expectedNumGCMgrs); |
|
158 |
} |
|
159 |
} |
|
160 |
private static List pools = ManagementFactory.getMemoryPoolMXBeans(); |
|
161 |
private static void checkPoolType(String name, MemoryType type) |
|
162 |
throws Exception { |
|
163 |
for (ListIterator iter = pools.listIterator(); iter.hasNext(); ) { |
|
164 |
MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next(); |
|
165 |
if (pool.getName().equals(name)) { |
|
166 |
if (pool.getType() != type) { |
|
167 |
throw new RuntimeException("TEST FAILED: " + |
|
168 |
"Pool " + pool.getName() + " is of type " + |
|
169 |
pool.getType() + " but expected to be " + type); |
|
170 |
} else { |
|
171 |
return; |
|
172 |
} |
|
173 |
} |
|
174 |
} |
|
175 |
throw new RuntimeException("TEST FAILED: " + |
|
176 |
"Pool " + name + " is of type " + type + |
|
177 |
" not found"); |
|
178 |
} |
|
179 |
} |