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