1 /* 2 * Copyright (c) 2013, 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 import java.util.List; 25 import java.lang.management.ManagementFactory; 26 import java.lang.management.MemoryManagerMXBean; 27 import java.lang.management.MemoryPoolMXBean; 28 import java.lang.management.MemoryUsage; 29 30 import java.lang.management.RuntimeMXBean; 31 import java.lang.management.ManagementFactory; 32 33 /* @test TestMetaspaceMemoryPool 34 * @bug 8000754 35 * @summary Tests that a MemoryPoolMXBeans is created for metaspace and that a 36 * MemoryManagerMXBean is created. 37 * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops TestMetaspaceMemoryPool 38 * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:MaxMetaspaceSize=60m TestMetaspaceMemoryPool 39 * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedKlassPointers TestMetaspaceMemoryPool 40 * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedKlassPointers -XX:ClassMetaspaceSize=60m TestMetaspaceMemoryPool 41 */ 42 public class TestMetaspaceMemoryPool { 43 public static void main(String[] args) { 44 verifyThatMetaspaceMemoryManagerExists(); 45 verifyMemoryPool(getMemoryPool("Metaspace"), isFlagDefined("MaxMetaspaceSize")); 46 47 if (runsOn64bit()) { 48 MemoryPoolMXBean cksPool = getMemoryPool("Compressed Klass Space"); 49 if (usesCompressedOops()) { 50 verifyMemoryPool(cksPool, true); 51 } else { 52 verifyEmptyMemoryPool(cksPool); 53 } 54 } 55 } 56 57 private static boolean runsOn64bit() { 58 return !System.getProperty("sun.arch.data.model").equals("32"); 59 } 60 61 private static boolean usesCompressedOops() { 62 return isFlagDefined("+UseCompressedOops"); 63 } 64 65 private static boolean isFlagDefined(String name) { 66 RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean(); 67 List<String> args = runtimeMxBean.getInputArguments(); 68 for (String arg : args) { 69 if (arg.startsWith("-XX:" + name)) { 70 return true; 71 } 72 } 73 return false; 74 } 75 76 private static void verifyThatMetaspaceMemoryManagerExists() { 77 List<MemoryManagerMXBean> managers = ManagementFactory.getMemoryManagerMXBeans(); 78 for (MemoryManagerMXBean manager : managers) { 79 if (manager.getName().equals("Metaspace Manager")) { 80 return; 81 } 82 } 83 84 throw new RuntimeException("Expected to find a metaspace memory manager"); 85 } 86 87 private static MemoryPoolMXBean getMemoryPool(String name) { 88 List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans(); 89 for (MemoryPoolMXBean pool : pools) { 90 if (pool.getName().equals(name)) { 91 return pool; 92 } 93 } 94 95 throw new RuntimeException("Expected to find a memory pool with name " + name); 96 } 97 98 private static void verifyMemoryPool(MemoryPoolMXBean pool, boolean isMaxDefined) { 99 MemoryUsage mu = pool.getUsage(); 100 assertDefined(mu.getInit(), "init"); 101 assertDefined(mu.getUsed(), "used"); 102 assertDefined(mu.getCommitted(), "committed"); 103 104 if (isMaxDefined) { 105 assertDefined(mu.getMax(), "max"); 106 } else { 107 assertUndefined(mu.getMax(), "max"); 108 } 109 } 110 111 private static void verifyEmptyMemoryPool(MemoryPoolMXBean pool) { 112 MemoryUsage mu = pool.getUsage(); 113 assertUndefined(mu.getInit(), "init"); 114 assertUndefined(mu.getMax(), "max"); 115 116 assertEquals(mu.getUsed(), 0, "Expected used to be 0"); 117 assertEquals(mu.getCommitted(), 0, "Expected committed to be 0"); 118 } 119 120 121 private static void assertDefined(long value, String name) { 122 assertTrue(value != -1, "Expected " + name + " to be defined"); 123 } 124 125 private static void assertUndefined(long value, String name) { 126 assertEquals(value, -1, "Expected " + name + " to be undefined"); 127 } 128 129 private static void assertEquals(long actual, long expected, String msg) { 130 assertTrue(actual == expected, msg); 131 } 132 133 private static void assertTrue(boolean condition, String msg) { 134 if (!condition) { 135 throw new RuntimeException(msg); 136 } 137 } 138 }