Path: blob/jdk8u272-b10-aarch32-20201026/hotspot/test/gc/metaspace/TestMetaspacePerfCounters.java
48799 views
/*1* Copyright (c) 2013, 2014, 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*/2223import java.util.List;24import java.util.ArrayList;2526import com.oracle.java.testlibrary.*;27import static com.oracle.java.testlibrary.Asserts.*;2829/* @test TestMetaspacePerfCounters30* @bug 801465931* @requires vm.gc=="null"32* @library /testlibrary33* @summary Tests that performance counters for metaspace and compressed class34* space exists and works.35*36* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UseSerialGC TestMetaspacePerfCounters37* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UseParallelGC -XX:+UseParallelOldGC TestMetaspacePerfCounters38* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UseG1GC TestMetaspacePerfCounters39*40* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UsePerfData -XX:+UseSerialGC TestMetaspacePerfCounters41* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UsePerfData -XX:+UseParallelGC -XX:+UseParallelOldGC TestMetaspacePerfCounters42* @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UsePerfData -XX:+UseG1GC TestMetaspacePerfCounters43*/44public class TestMetaspacePerfCounters {45public static Class fooClass = null;46private static final String[] counterNames = {"minCapacity", "maxCapacity", "capacity", "used"};4748public static void main(String[] args) throws Exception {49String metaspace = "sun.gc.metaspace";50String ccs = "sun.gc.compressedclassspace";5152checkPerfCounters(metaspace);5354if (isUsingCompressedClassPointers()) {55checkPerfCounters(ccs);56checkUsedIncreasesWhenLoadingClass(ccs);57} else {58checkEmptyPerfCounters(ccs);59checkUsedIncreasesWhenLoadingClass(metaspace);60}61}6263private static void checkPerfCounters(String ns) throws Exception {64long minCapacity = getMinCapacity(ns);65long maxCapacity = getMaxCapacity(ns);66long capacity = getCapacity(ns);67long used = getUsed(ns);6869assertGTE(minCapacity, 0L);70assertGTE(used, minCapacity);71assertGTE(capacity, used);72assertGTE(maxCapacity, capacity);73}7475private static void checkEmptyPerfCounters(String ns) throws Exception {76for (PerfCounter counter : countersInNamespace(ns)) {77String msg = "Expected " + counter.getName() + " to equal 0";78assertEQ(counter.longValue(), 0L, msg);79}80}8182private static void checkUsedIncreasesWhenLoadingClass(String ns) throws Exception {83long before = getUsed(ns);84fooClass = compileAndLoad("Foo", "public class Foo { }");85System.gc();86long after = getUsed(ns);8788assertGT(after, before);89}9091private static List<PerfCounter> countersInNamespace(String ns) throws Exception {92List<PerfCounter> counters = new ArrayList<>();93for (String name : counterNames) {94counters.add(PerfCounters.findByName(ns + "." + name));95}96return counters;97}9899private static Class<?> compileAndLoad(String name, String source) throws Exception {100byte[] byteCode = InMemoryJavaCompiler.compile(name, source);101return ByteCodeLoader.load(name, byteCode);102}103104private static boolean isUsingCompressedClassPointers() {105return Platform.is64bit() && InputArguments.contains("-XX:+UseCompressedClassPointers");106}107108private static long getMinCapacity(String ns) throws Exception {109return PerfCounters.findByName(ns + ".minCapacity").longValue();110}111112private static long getCapacity(String ns) throws Exception {113return PerfCounters.findByName(ns + ".capacity").longValue();114}115116private static long getMaxCapacity(String ns) throws Exception {117return PerfCounters.findByName(ns + ".maxCapacity").longValue();118}119120private static long getUsed(String ns) throws Exception {121return PerfCounters.findByName(ns + ".used").longValue();122}123}124125126