1 /*
   2  * Copyright (c) 2013, 2014, 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.util.List;
  25 import java.util.ArrayList;
  26 
  27 import com.oracle.java.testlibrary.*;
  28 import static com.oracle.java.testlibrary.Asserts.*;
  29 
  30 /* @test TestMetaspacePerfCounters
  31  * @bug 8014659
  32  * @requires vm.gc=="null"
  33  * @library /testlibrary
  34  * @summary Tests that performance counters for metaspace and compressed class
  35  *          space exists and works.
  36  *
  37  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UseSerialGC TestMetaspacePerfCounters
  38  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UseParallelGC -XX:+UseParallelOldGC TestMetaspacePerfCounters
  39  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UseG1GC TestMetaspacePerfCounters
  40  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC TestMetaspacePerfCounters
  41  *
  42  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UsePerfData -XX:+UseSerialGC TestMetaspacePerfCounters
  43  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UsePerfData -XX:+UseParallelGC -XX:+UseParallelOldGC TestMetaspacePerfCounters
  44  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UsePerfData -XX:+UseG1GC TestMetaspacePerfCounters
  45  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UsePerfData -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC TestMetaspacePerfCounters
  46  */
  47 public class TestMetaspacePerfCounters {
  48     public static Class fooClass = null;
  49     private static final String[] counterNames = {"minCapacity", "maxCapacity", "capacity", "used"};
  50 
  51     public static void main(String[] args) throws Exception {
  52         String metaspace = "sun.gc.metaspace";
  53         String ccs = "sun.gc.compressedclassspace";
  54 
  55         checkPerfCounters(metaspace);
  56 
  57         if (isUsingCompressedClassPointers()) {
  58             checkPerfCounters(ccs);
  59             checkUsedIncreasesWhenLoadingClass(ccs);
  60         } else {
  61             checkEmptyPerfCounters(ccs);
  62             checkUsedIncreasesWhenLoadingClass(metaspace);
  63         }
  64     }
  65 
  66     private static void checkPerfCounters(String ns) throws Exception {
  67         long minCapacity = getMinCapacity(ns);
  68         long maxCapacity = getMaxCapacity(ns);
  69         long capacity = getCapacity(ns);
  70         long used = getUsed(ns);
  71 
  72         assertGTE(minCapacity, 0L);
  73         assertGTE(used, minCapacity);
  74         assertGTE(capacity, used);
  75         assertGTE(maxCapacity, capacity);
  76     }
  77 
  78     private static void checkEmptyPerfCounters(String ns) throws Exception {
  79         for (PerfCounter counter : countersInNamespace(ns)) {
  80             String msg = "Expected " + counter.getName() + " to equal 0";
  81             assertEQ(counter.longValue(), 0L, msg);
  82         }
  83     }
  84 
  85     private static void checkUsedIncreasesWhenLoadingClass(String ns) throws Exception {
  86         long before = getUsed(ns);
  87         fooClass = compileAndLoad("Foo", "public class Foo { }");
  88         System.gc();
  89         long after = getUsed(ns);
  90 
  91         assertGT(after, before);
  92     }
  93 
  94     private static List<PerfCounter> countersInNamespace(String ns) throws Exception {
  95         List<PerfCounter> counters = new ArrayList<>();
  96         for (String name : counterNames) {
  97             counters.add(PerfCounters.findByName(ns + "." + name));
  98         }
  99         return counters;
 100     }
 101 
 102     private static Class<?> compileAndLoad(String name, String source) throws Exception {
 103         byte[] byteCode = InMemoryJavaCompiler.compile(name, source);
 104         return ByteCodeLoader.load(name, byteCode);
 105     }
 106 
 107     private static boolean isUsingCompressedClassPointers() {
 108         return Platform.is64bit() && InputArguments.contains("-XX:+UseCompressedClassPointers");
 109     }
 110 
 111     private static long getMinCapacity(String ns) throws Exception {
 112         return PerfCounters.findByName(ns + ".minCapacity").longValue();
 113     }
 114 
 115     private static long getCapacity(String ns) throws Exception {
 116         return PerfCounters.findByName(ns + ".capacity").longValue();
 117     }
 118 
 119     private static long getMaxCapacity(String ns) throws Exception {
 120         return PerfCounters.findByName(ns + ".maxCapacity").longValue();
 121     }
 122 
 123     private static long getUsed(String ns) throws Exception {
 124         return PerfCounters.findByName(ns + ".used").longValue();
 125     }
 126 }
--- EOF ---