test/gc/metaspace/TestMetaspacePerfCounters.java

changeset 5531
1a8fb39bdbc4
child 5694
7944aba7ba41
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/gc/metaspace/TestMetaspacePerfCounters.java	Wed Aug 07 16:47:32 2013 +0200
     1.3 @@ -0,0 +1,104 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +import java.util.List;
    1.28 +import java.util.ArrayList;
    1.29 +
    1.30 +import com.oracle.java.testlibrary.*;
    1.31 +import static com.oracle.java.testlibrary.Asserts.*;
    1.32 +
    1.33 +/* @test TestMetaspacePerfCounters
    1.34 + * @bug 8014659
    1.35 + * @library /testlibrary
    1.36 + * @summary Tests that performance counters for metaspace and compressed class
    1.37 + *          space exists and works.
    1.38 + *
    1.39 + * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedKlassPointers -XX:+UsePerfData -XX:+UseSerialGC TestMetaspacePerfCounters
    1.40 + * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedKlassPointers -XX:+UsePerfData -XX:+UseParallelGC -XX:+UseParallelOldGC TestMetaspacePerfCounters
    1.41 + * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedKlassPointers -XX:+UsePerfData -XX:+UseG1GC TestMetaspacePerfCounters
    1.42 + *
    1.43 + * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedKlassPointers -XX:+UsePerfData -XX:+UseSerialGC TestMetaspacePerfCounters
    1.44 + * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedKlassPointers -XX:+UsePerfData -XX:+UseParallelGC -XX:+UseParallelOldGC TestMetaspacePerfCounters
    1.45 + * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedKlassPointers -XX:+UsePerfData -XX:+UseG1GC TestMetaspacePerfCounters
    1.46 + */
    1.47 +public class TestMetaspacePerfCounters {
    1.48 +    public static Class fooClass = null;
    1.49 +    private static final String[] counterNames = {"minCapacity", "maxCapacity", "capacity", "used"};
    1.50 +
    1.51 +    public static void main(String[] args) throws Exception {
    1.52 +        String metaspace = "sun.gc.metaspace";
    1.53 +        String ccs = "sun.gc.compressedclassspace";
    1.54 +
    1.55 +        checkPerfCounters(metaspace);
    1.56 +
    1.57 +        if (isUsingCompressedClassPointers()) {
    1.58 +            checkPerfCounters(ccs);
    1.59 +            checkUsedIncreasesWhenLoadingClass(ccs);
    1.60 +        } else {
    1.61 +            checkEmptyPerfCounters(ccs);
    1.62 +            checkUsedIncreasesWhenLoadingClass(metaspace);
    1.63 +        }
    1.64 +    }
    1.65 +
    1.66 +    private static void checkPerfCounters(String ns) throws Exception {
    1.67 +        for (PerfCounter counter : countersInNamespace(ns)) {
    1.68 +            String msg = "Expected " + counter.getName() + " to be larger than 0";
    1.69 +            assertGT(counter.longValue(), 0L, msg);
    1.70 +        }
    1.71 +    }
    1.72 +
    1.73 +    private static void checkEmptyPerfCounters(String ns) throws Exception {
    1.74 +        for (PerfCounter counter : countersInNamespace(ns)) {
    1.75 +            String msg = "Expected " + counter.getName() + " to equal 0";
    1.76 +            assertEQ(counter.longValue(), 0L, msg);
    1.77 +        }
    1.78 +    }
    1.79 +
    1.80 +    private static void checkUsedIncreasesWhenLoadingClass(String ns) throws Exception {
    1.81 +        PerfCounter used = PerfCounters.findByName(ns + ".used");
    1.82 +
    1.83 +        long before = used.longValue();
    1.84 +        fooClass = compileAndLoad("Foo", "public class Foo { }");
    1.85 +        System.gc();
    1.86 +        long after = used.longValue();
    1.87 +
    1.88 +        assertGT(after, before);
    1.89 +    }
    1.90 +
    1.91 +    private static List<PerfCounter> countersInNamespace(String ns) throws Exception {
    1.92 +        List<PerfCounter> counters = new ArrayList<>();
    1.93 +        for (String name : counterNames) {
    1.94 +            counters.add(PerfCounters.findByName(ns + "." + name));
    1.95 +        }
    1.96 +        return counters;
    1.97 +    }
    1.98 +
    1.99 +    private static Class<?> compileAndLoad(String name, String source) throws Exception {
   1.100 +        byte[] byteCode = InMemoryJavaCompiler.compile(name, source);
   1.101 +        return ByteCodeLoader.load(name, byteCode);
   1.102 +    }
   1.103 +
   1.104 +    private static boolean isUsingCompressedClassPointers() {
   1.105 +        return Platform.is64bit() && InputArguments.contains("-XX:+UseCompressedKlassPointers");
   1.106 +    }
   1.107 +}

mercurial