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  * @library /testlibrary
  33  * @summary Tests that performance counters for metaspace and compressed class
  34  *          space exists and works.
  35  *
  36  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UseSerialGC TestMetaspacePerfCounters
  37  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UseParallelGC -XX:+UseParallelOldGC TestMetaspacePerfCounters
  38  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UsePerfData -XX:+UseG1GC TestMetaspacePerfCounters
  39  *
  40  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UsePerfData -XX:+UseSerialGC TestMetaspacePerfCounters
  41  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UsePerfData -XX:+UseParallelGC -XX:+UseParallelOldGC TestMetaspacePerfCounters
  42  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UsePerfData -XX:+UseG1GC TestMetaspacePerfCounters
  43  */
  44 public class TestMetaspacePerfCounters {
  45     public static Class fooClass = null;
  46     private static final String[] counterNames = {"minCapacity", "maxCapacity", "capacity", "used"};
  47 
  48     public static void main(String[] args) throws Exception {
  49         String metaspace = "sun.gc.metaspace";
  50         String ccs = "sun.gc.compressedclassspace";
  51 
  52         checkPerfCounters(metaspace);
  53 
  54         if (isUsingCompressedClassPointers()) {
  55             checkPerfCounters(ccs);
  56             checkUsedIncreasesWhenLoadingClass(ccs);
  57         } else {
  58             checkEmptyPerfCounters(ccs);
  59             checkUsedIncreasesWhenLoadingClass(metaspace);
  60         }
  61     }
  62 
  63     private static void checkPerfCounters(String ns) throws Exception {
  64         for (PerfCounter counter : countersInNamespace(ns)) {
  65             String msg = "Expected " + counter.getName() + " to be larger than 0";
  66             assertGT(counter.longValue(), 0L, msg);
  67         }
  68     }
  69 
  70     private static void checkEmptyPerfCounters(String ns) throws Exception {
  71         for (PerfCounter counter : countersInNamespace(ns)) {
  72             String msg = "Expected " + counter.getName() + " to equal 0";
  73             assertEQ(counter.longValue(), 0L, msg);
  74         }
  75     }
  76 
  77     private static void checkUsedIncreasesWhenLoadingClass(String ns) throws Exception {
  78         PerfCounter used = PerfCounters.findByName(ns + ".used");
  79 
  80         long before = used.longValue();
  81         fooClass = compileAndLoad("Foo", "public class Foo { }");
  82         System.gc();
  83         long after = used.longValue();
  84 
  85         assertGT(after, before);
  86     }
  87 
  88     private static List<PerfCounter> countersInNamespace(String ns) throws Exception {
  89         List<PerfCounter> counters = new ArrayList<>();
  90         for (String name : counterNames) {
  91             counters.add(PerfCounters.findByName(ns + "." + name));
  92         }
  93         return counters;
  94     }
  95 
  96     private static Class<?> compileAndLoad(String name, String source) throws Exception {
  97         byte[] byteCode = InMemoryJavaCompiler.compile(name, source);
  98         return ByteCodeLoader.load(name, byteCode);
  99     }
 100 
 101     private static boolean isUsingCompressedClassPointers() {
 102         return Platform.is64bit() && InputArguments.contains("-XX:+UseCompressedClassPointers");
 103     }
 104 }