1 /* 2 * Copyright (c) 2003, 2016, 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 * @author Mandy Chung 30 * 31 * @modules jdk.management 32 * @run main MemoryTest 2 3 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 62 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) 66 // - Metaspace 67 // - Compressed Class Space (if compressed class pointers are used) 68 69 private static int[] expectedMinNumPools = new int[2]; 70 private static int[] expectedMaxNumPools = new int[2]; 71 private static int expectedNumGCMgrs; 72 private static int expectedNumMgrs; 73 private static String[] types = { "heap", "non-heap" }; 74 75 public static void main(String args[]) throws Exception { 76 expectedNumGCMgrs = Integer.valueOf(args[0]); 77 expectedNumMgrs = expectedNumGCMgrs + 2; 78 79 int expectedNumPools = Integer.valueOf(args[1]); 80 expectedMinNumPools[HEAP] = expectedNumPools; 81 expectedMaxNumPools[HEAP] = expectedNumPools; 82 83 expectedMinNumPools[NONHEAP] = 2; 84 expectedMaxNumPools[NONHEAP] = 5; 85 86 checkMemoryPools(); 87 checkMemoryManagers(); 88 if (testFailed) 89 throw new RuntimeException("TEST FAILED."); 90 91 System.out.println("Test passed."); 92 93 } 94 95 private static void checkMemoryPools() throws Exception { 96 List pools = ManagementFactory.getMemoryPoolMXBeans(); 97 boolean hasPerm = false; 98 99 int[] numPools = new int[NUM_TYPES]; 100 for (ListIterator iter = pools.listIterator(); iter.hasNext();) { 101 MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next(); 102 if (pool.getType() == MemoryType.HEAP) { 103 numPools[HEAP]++; 104 } 105 if (pool.getType() == MemoryType.NON_HEAP) { 106 numPools[NONHEAP]++; 107 } 108 if (pool.getName().toLowerCase().contains("perm")) { 109 hasPerm = true; 110 } 111 } 112 113 if (hasPerm) { 114 // If the VM has perm gen there will be between 2 and 4 non heap 115 // pools (4 if class data sharing is used) 116 expectedMinNumPools[NONHEAP] = 2; 117 expectedMaxNumPools[NONHEAP] = 4; 118 } 119 120 // Check the number of Memory pools 121 for (int i = 0; i < NUM_TYPES; i++) { 122 if (numPools[i] < expectedMinNumPools[i] || 123 numPools[i] > expectedMaxNumPools[i]) { 124 throw new RuntimeException("TEST FAILED: " + 125 "Number of " + types[i] + " pools = " + numPools[i] + 126 " but expected <= " + expectedMaxNumPools[i] + 127 " and >= " + expectedMinNumPools[i]); 128 } 129 } 130 } 131 132 private static void checkMemoryManagers() throws Exception { 133 List mgrs = ManagementFactory.getMemoryManagerMXBeans(); 134 135 int numGCMgr = 0; 136 137 // Check the number of Memory Managers 138 for (ListIterator iter = mgrs.listIterator(); iter.hasNext();) { 139 MemoryManagerMXBean mgr = (MemoryManagerMXBean) iter.next(); 140 String[] poolNames = mgr.getMemoryPoolNames(); 141 if (poolNames == null || poolNames.length == 0) { 142 throw new RuntimeException("TEST FAILED: " + 143 "Expected to have one or more pools for " + 144 mgr.getName() + "manager."); 145 } 146 147 if (mgr instanceof GarbageCollectorMXBean) { 148 numGCMgr++; 149 } else { 150 for (int i = 0; i < poolNames.length; i++) { 151 checkPoolType(poolNames[i], MemoryType.NON_HEAP); 152 } 153 } 154 } 155 156 if (mgrs.size() != expectedNumMgrs) { 157 throw new RuntimeException("TEST FAILED: " + 158 "Number of memory managers = " + mgrs.size() + 159 " but expected = " + expectedNumMgrs); 160 } 161 if (numGCMgr != expectedNumGCMgrs) { 162 throw new RuntimeException("TEST FAILED: " + 163 "Number of GC managers = " + numGCMgr + " but expected = " + 164 expectedNumGCMgrs); 165 } 166 } 167 private static List pools = ManagementFactory.getMemoryPoolMXBeans(); 168 private static void checkPoolType(String name, MemoryType type) 169 throws Exception { 170 for (ListIterator iter = pools.listIterator(); iter.hasNext(); ) { 171 MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next(); 172 if (pool.getName().equals(name)) { 173 if (pool.getType() != type) { 174 throw new RuntimeException("TEST FAILED: " + 175 "Pool " + pool.getName() + " is of type " + 176 pool.getType() + " but expected to be " + type); 177 } else { 178 return; 179 } 180 } 181 } 182 throw new RuntimeException("TEST FAILED: " + 183 "Pool " + name + " is of type " + type + 184 " not found"); 185 } 186 }