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