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