1 /*
   2  * Copyright (c) 2013, 2017, 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 public class TestMetaspacePerfCounters {
  57     public static Class fooClass = null;
  58     private static final String[] counterNames = {"minCapacity", "maxCapacity", "capacity", "used"};
  59     private static final List<GarbageCollectorMXBean> gcBeans = ManagementFactoryHelper.getGarbageCollectorMXBeans();
  60 
  61     public static void main(String[] args) throws Exception {
  62         String metaspace = "sun.gc.metaspace";
  63         String ccs = "sun.gc.compressedclassspace";
  64 
  65         checkPerfCounters(metaspace);
  66 
  67         if (isUsingCompressedClassPointers()) {
  68             checkPerfCounters(ccs);
  69             checkUsedIncreasesWhenLoadingClass(ccs);
  70         } else {
  71             checkEmptyPerfCounters(ccs);
  72             checkUsedIncreasesWhenLoadingClass(metaspace);
  73         }
  74     }
  75 
  76     private static void checkPerfCounters(String ns) throws Exception {
  77         long gcCountBefore;
  78         long gcCountAfter;
  79         long minCapacity;
  80         long maxCapacity;
  81         long capacity;
  82         long used;
  83 
  84         // The perf counter values are updated during GC and to be able to
  85         // do the assertions below we need to ensure that the values are from
  86         // the same GC cycle.
  87         do {
  88             gcCountBefore = currentGCCount();
  89 
  90             minCapacity = getMinCapacity(ns);
  91             maxCapacity = getMaxCapacity(ns);
  92             capacity = getCapacity(ns);
  93             used = getUsed(ns);
  94 
  95             gcCountAfter = currentGCCount();
  96             assertGTE(gcCountAfter, gcCountBefore);
  97         } while(gcCountAfter > gcCountBefore);
  98 
  99         assertGTE(minCapacity, 0L);
 100         assertGTE(used, minCapacity);
 101         assertGTE(capacity, used);
 102         assertGTE(maxCapacity, capacity);
 103     }
 104 
 105     private static void checkEmptyPerfCounters(String ns) throws Exception {
 106         for (PerfCounter counter : countersInNamespace(ns)) {
 107             String msg = "Expected " + counter.getName() + " to equal 0";
 108             assertEQ(counter.longValue(), 0L, msg);
 109         }
 110     }
 111 
 112     private static void checkUsedIncreasesWhenLoadingClass(String ns) throws Exception {
 113         // Need to ensure that used is up to date and that all unreachable
 114         // classes are unloaded before doing this check.
 115         System.gc();
 116         long before = getUsed(ns);
 117         fooClass = compileAndLoad("Foo", "public class Foo { }");
 118         System.gc();
 119         long after = getUsed(ns);
 120 
 121         assertGT(after, before);
 122     }
 123 
 124     private static List<PerfCounter> countersInNamespace(String ns) throws Exception {
 125         List<PerfCounter> counters = new ArrayList<>();
 126         for (String name : counterNames) {
 127             counters.add(PerfCounters.findByName(ns + "." + name));
 128         }
 129         return counters;
 130     }
 131 
 132     private static Class<?> compileAndLoad(String name, String source) throws Exception {
 133         byte[] byteCode = InMemoryJavaCompiler.compile(name, source);
 134         return ByteCodeLoader.load(name, byteCode);
 135     }
 136 
 137     private static boolean isUsingCompressedClassPointers() {
 138         return Platform.is64bit() && InputArguments.contains("-XX:+UseCompressedClassPointers");
 139     }
 140 
 141     private static long getMinCapacity(String ns) throws Exception {
 142         return PerfCounters.findByName(ns + ".minCapacity").longValue();
 143     }
 144 
 145     private static long getCapacity(String ns) throws Exception {
 146         return PerfCounters.findByName(ns + ".capacity").longValue();
 147     }
 148 
 149     private static long getMaxCapacity(String ns) throws Exception {
 150         return PerfCounters.findByName(ns + ".maxCapacity").longValue();
 151     }
 152 
 153     private static long getUsed(String ns) throws Exception {
 154         return PerfCounters.findByName(ns + ".used").longValue();
 155     }
 156 
 157     private static long currentGCCount() {
 158         long gcCount = 0;
 159         for (GarbageCollectorMXBean bean : gcBeans) {
 160             gcCount += bean.getCollectionCount();
 161         }
 162         return gcCount;
 163     }
 164 }