Path: blob/master/test/hotspot/jtreg/gc/metaspace/TestMetaspacePerfCounters.java
40942 views
/*1* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package gc.metaspace;2425import java.lang.management.GarbageCollectorMXBean;26import java.util.List;27import java.util.ArrayList;2829import jdk.test.lib.ByteCodeLoader;30import jdk.test.lib.compiler.InMemoryJavaCompiler;31import jdk.test.lib.Platform;3233import sun.management.ManagementFactoryHelper;3435import static jdk.test.lib.Asserts.*;36import gc.testlibrary.PerfCounter;37import gc.testlibrary.PerfCounters;3839/* @test TestMetaspacePerfCountersSerial40* @bug 801465941* @requires vm.gc.Serial42* @library /test/lib /43* @summary Tests that performance counters for metaspace and compressed class44* space exists and works.45* @modules java.base/jdk.internal.misc46* java.compiler47* java.management/sun.management48* jdk.internal.jvmstat/sun.jvmstat.monitor49* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UseSerialGC gc.metaspace.TestMetaspacePerfCounters50* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UsePerfData -XX:+UseSerialGC gc.metaspace.TestMetaspacePerfCounters51*/5253/* @test TestMetaspacePerfCountersParallel54* @bug 801465955* @requires vm.gc.Parallel56* @library /test/lib /57* @summary Tests that performance counters for metaspace and compressed class58* space exists and works.59* @modules java.base/jdk.internal.misc60* java.compiler61* java.management/sun.management62* jdk.internal.jvmstat/sun.jvmstat.monitor63* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UseParallelGC gc.metaspace.TestMetaspacePerfCounters64* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UsePerfData -XX:+UseParallelGC gc.metaspace.TestMetaspacePerfCounters65*/6667/* @test TestMetaspacePerfCountersG168* @bug 801465969* @requires vm.gc.G170* @library /test/lib /71* @summary Tests that performance counters for metaspace and compressed class72* space exists and works.73* @modules java.base/jdk.internal.misc74* java.compiler75* java.management/sun.management76* jdk.internal.jvmstat/sun.jvmstat.monitor77* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UseG1GC gc.metaspace.TestMetaspacePerfCounters78* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UsePerfData -XX:+UseG1GC gc.metaspace.TestMetaspacePerfCounters79*/8081/* @test TestMetaspacePerfCountersShenandoah82* @bug 801465983* @requires vm.gc.Shenandoah84* @library /test/lib /85* @summary Tests that performance counters for metaspace and compressed class86* space exists and works.87* @modules java.base/jdk.internal.misc88* java.compiler89* java.management/sun.management90* jdk.internal.jvmstat/sun.jvmstat.monitor91* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC gc.metaspace.TestMetaspacePerfCounters92* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UsePerfData -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC gc.metaspace.TestMetaspacePerfCounters93*/94public class TestMetaspacePerfCounters {95public static Class<?> fooClass = null;96private static final String[] counterNames = {"minCapacity", "maxCapacity", "capacity", "used"};97private static final List<GarbageCollectorMXBean> gcBeans = ManagementFactoryHelper.getGarbageCollectorMXBeans();9899public static void main(String[] args) throws Exception {100String metaspace = "sun.gc.metaspace";101String ccs = "sun.gc.compressedclassspace";102103checkPerfCounters(metaspace);104105if (isUsingCompressedClassPointers()) {106checkPerfCounters(ccs);107checkUsedIncreasesWhenLoadingClass(ccs);108} else {109checkEmptyPerfCounters(ccs);110checkUsedIncreasesWhenLoadingClass(metaspace);111}112}113114private static void checkPerfCounters(String ns) throws Exception {115long gcCountBefore;116long gcCountAfter;117long minCapacity;118long maxCapacity;119long capacity;120long used;121122// The perf counter values are updated during GC and to be able to123// do the assertions below we need to ensure that the values are from124// the same GC cycle.125do {126gcCountBefore = currentGCCount();127128minCapacity = getMinCapacity(ns);129maxCapacity = getMaxCapacity(ns);130capacity = getCapacity(ns);131used = getUsed(ns);132133gcCountAfter = currentGCCount();134assertGTE(gcCountAfter, gcCountBefore);135} while(gcCountAfter > gcCountBefore);136137assertGTE(minCapacity, 0L);138assertGTE(used, minCapacity);139assertGTE(capacity, used);140assertGTE(maxCapacity, capacity);141}142143private static void checkEmptyPerfCounters(String ns) throws Exception {144for (PerfCounter counter : countersInNamespace(ns)) {145String msg = "Expected " + counter.getName() + " to equal 0";146assertEQ(counter.longValue(), 0L, msg);147}148}149150private static void checkUsedIncreasesWhenLoadingClass(String ns) throws Exception {151// Need to ensure that used is up to date and that all unreachable152// classes are unloaded before doing this check.153System.gc();154long before = getUsed(ns);155fooClass = compileAndLoad("Foo", "public class Foo { }");156System.gc();157long after = getUsed(ns);158159assertGT(after, before);160}161162private static List<PerfCounter> countersInNamespace(String ns) throws Exception {163List<PerfCounter> counters = new ArrayList<>();164for (String name : counterNames) {165counters.add(PerfCounters.findByName(ns + "." + name));166}167return counters;168}169170private static Class<?> compileAndLoad(String name, String source) throws Exception {171byte[] byteCode = InMemoryJavaCompiler.compile(name, source);172return ByteCodeLoader.load(name, byteCode);173}174175private static boolean isUsingCompressedClassPointers() {176return Platform.is64bit() && InputArguments.contains("-XX:+UseCompressedClassPointers");177}178179private static long getMinCapacity(String ns) throws Exception {180return PerfCounters.findByName(ns + ".minCapacity").longValue();181}182183private static long getCapacity(String ns) throws Exception {184return PerfCounters.findByName(ns + ".capacity").longValue();185}186187private static long getMaxCapacity(String ns) throws Exception {188return PerfCounters.findByName(ns + ".maxCapacity").longValue();189}190191private static long getUsed(String ns) throws Exception {192return PerfCounters.findByName(ns + ".used").longValue();193}194195private static long currentGCCount() {196long gcCount = 0;197for (GarbageCollectorMXBean bean : gcBeans) {198gcCount += bean.getCollectionCount();199}200return gcCount;201}202}203204205