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