1 /* 2 * Copyright (c) 2013, 2019, 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 package gc.metaspace; 25 26 import java.lang.management.GarbageCollectorMXBean; 27 import java.util.List; 28 import java.util.ArrayList; 29 30 import jdk.test.lib.ByteCodeLoader; 31 import jdk.test.lib.compiler.InMemoryJavaCompiler; 32 import jdk.test.lib.Platform; 33 34 import sun.management.ManagementFactoryHelper; 35 36 import static jdk.test.lib.Asserts.*; 37 import gc.testlibrary.PerfCounter; 38 import gc.testlibrary.PerfCounters; 39 40 /* @test TestMetaspacePerfCounters 41 * @bug 8014659 42 * @requires vm.gc=="null" 43 * @library /test/lib / 44 * @summary Tests that performance counters for metaspace and compressed class 45 * space exists and works. 46 * @modules java.base/jdk.internal.misc 47 * java.compiler 48 * java.management/sun.management 49 * jdk.internal.jvmstat/sun.jvmstat.monitor 50 * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UseSerialGC gc.metaspace.TestMetaspacePerfCounters 51 * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UseParallelGC -XX:+UseParallelOldGC gc.metaspace.TestMetaspacePerfCounters 52 * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UseG1GC gc.metaspace.TestMetaspacePerfCounters 53 * 54 * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UsePerfData -XX:+UseSerialGC gc.metaspace.TestMetaspacePerfCounters 55 * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UsePerfData -XX:+UseParallelGC -XX:+UseParallelOldGC gc.metaspace.TestMetaspacePerfCounters 56 * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UsePerfData -XX:+UseG1GC gc.metaspace.TestMetaspacePerfCounters 57 */ 58 59 /* @test TestMetaspacePerfCountersShenandoah 60 * @bug 8014659 61 * @requires vm.gc.Shenandoah 62 * @library /test/lib / 63 * @summary Tests that performance counters for metaspace and compressed class 64 * space exists and works. 65 * @modules java.base/jdk.internal.misc 66 * java.compiler 67 * java.management/sun.management 68 * jdk.internal.jvmstat/sun.jvmstat.monitor 69 * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC gc.metaspace.TestMetaspacePerfCounters 70 * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UsePerfData -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC gc.metaspace.TestMetaspacePerfCounters 71 */ 72 public class TestMetaspacePerfCounters { 73 public static Class<?> fooClass = null; 74 private static final String[] counterNames = {"minCapacity", "maxCapacity", "capacity", "used"}; 75 private static final List<GarbageCollectorMXBean> gcBeans = ManagementFactoryHelper.getGarbageCollectorMXBeans(); 76 77 public static void main(String[] args) throws Exception { 78 String metaspace = "sun.gc.metaspace"; 79 String ccs = "sun.gc.compressedclassspace"; 80 81 checkPerfCounters(metaspace); 82 83 if (isUsingCompressedClassPointers()) { 84 checkPerfCounters(ccs); 85 checkUsedIncreasesWhenLoadingClass(ccs); 86 } else { 87 checkEmptyPerfCounters(ccs); 88 checkUsedIncreasesWhenLoadingClass(metaspace); 89 } 90 } 91 92 private static void checkPerfCounters(String ns) throws Exception { 93 long gcCountBefore; 94 long gcCountAfter; 95 long minCapacity; 96 long maxCapacity; 97 long capacity; 98 long used; 99 100 // The perf counter values are updated during GC and to be able to 101 // do the assertions below we need to ensure that the values are from 102 // the same GC cycle. 103 do { 104 gcCountBefore = currentGCCount(); 105 106 minCapacity = getMinCapacity(ns); 107 maxCapacity = getMaxCapacity(ns); 108 capacity = getCapacity(ns); 109 used = getUsed(ns); 110 111 gcCountAfter = currentGCCount(); 112 assertGTE(gcCountAfter, gcCountBefore); 113 } while(gcCountAfter > gcCountBefore); 114 115 assertGTE(minCapacity, 0L); 116 assertGTE(used, minCapacity); 117 assertGTE(capacity, used); 118 assertGTE(maxCapacity, capacity); 119 } 120 121 private static void checkEmptyPerfCounters(String ns) throws Exception { 122 for (PerfCounter counter : countersInNamespace(ns)) { 123 String msg = "Expected " + counter.getName() + " to equal 0"; 124 assertEQ(counter.longValue(), 0L, msg); 125 } 126 } 127 128 private static void checkUsedIncreasesWhenLoadingClass(String ns) throws Exception { 129 // Need to ensure that used is up to date and that all unreachable 130 // classes are unloaded before doing this check. 131 System.gc(); 132 long before = getUsed(ns); 133 fooClass = compileAndLoad("Foo", "public class Foo { }"); 134 System.gc(); 135 long after = getUsed(ns); 136 137 assertGT(after, before); 138 } 139 140 private static List<PerfCounter> countersInNamespace(String ns) throws Exception { 141 List<PerfCounter> counters = new ArrayList<>(); 142 for (String name : counterNames) { 143 counters.add(PerfCounters.findByName(ns + "." + name)); 144 } 145 return counters; 146 } 147 148 private static Class<?> compileAndLoad(String name, String source) throws Exception { 149 byte[] byteCode = InMemoryJavaCompiler.compile(name, source); 150 return ByteCodeLoader.load(name, byteCode); 151 } 152 153 private static boolean isUsingCompressedClassPointers() { 154 return Platform.is64bit() && InputArguments.contains("-XX:+UseCompressedClassPointers"); 155 } 156 157 private static long getMinCapacity(String ns) throws Exception { 158 return PerfCounters.findByName(ns + ".minCapacity").longValue(); 159 } 160 161 private static long getCapacity(String ns) throws Exception { 162 return PerfCounters.findByName(ns + ".capacity").longValue(); 163 } 164 165 private static long getMaxCapacity(String ns) throws Exception { 166 return PerfCounters.findByName(ns + ".maxCapacity").longValue(); 167 } 168 169 private static long getUsed(String ns) throws Exception { 170 return PerfCounters.findByName(ns + ".used").longValue(); 171 } 172 173 private static long currentGCCount() { 174 long gcCount = 0; 175 for (GarbageCollectorMXBean bean : gcBeans) { 176 gcCount += bean.getCollectionCount(); 177 } 178 return gcCount; 179 } 180 }