test/gc/metaspace/TestPerfCountersAndMemoryPools.java

Wed, 03 Sep 2014 09:25:44 +0200

author
tschatzl
date
Wed, 03 Sep 2014 09:25:44 +0200
changeset 7097
14b8221771dc
parent 5816
6e22e7042433
child 6876
710a3c8b516e
child 7785
f967da7f0c3c
permissions
-rw-r--r--

Merge

     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  */
    24 import java.util.List;
    25 import java.lang.management.*;
    27 import com.oracle.java.testlibrary.*;
    28 import static com.oracle.java.testlibrary.Asserts.*;
    30 /* @test TestPerfCountersAndMemoryPools
    31  * @bug 8023476
    32  * @library /testlibrary
    33  * @summary Tests that a MemoryPoolMXBeans and PerfCounters for metaspace
    34  *          report the same data.
    35  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedKlassPointers -XX:+UseSerialGC -XX:+UsePerfData -Xint TestPerfCountersAndMemoryPools
    36  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedKlassPointers -XX:+UseSerialGC -XX:+UsePerfData -Xint TestPerfCountersAndMemoryPools
    37  */
    38 public class TestPerfCountersAndMemoryPools {
    39     public static void main(String[] args) throws Exception {
    40         checkMemoryUsage("Metaspace", "sun.gc.metaspace");
    42         if (InputArguments.contains("-XX:+UseCompressedKlassPointers") && Platform.is64bit()) {
    43             checkMemoryUsage("Compressed Class Space", "sun.gc.compressedclassspace");
    44         }
    45     }
    47     private static MemoryPoolMXBean getMemoryPool(String memoryPoolName) {
    48         List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
    49         for (MemoryPoolMXBean pool : pools) {
    50             if (pool.getName().equals(memoryPoolName)) {
    51                 return pool;
    52             }
    53         }
    55         throw new RuntimeException("Excpted to find a memory pool with name " +
    56                                    memoryPoolName);
    57     }
    59     private static void checkMemoryUsage(String memoryPoolName, String perfNS)
    60         throws Exception {
    61         MemoryPoolMXBean pool = getMemoryPool(memoryPoolName);
    63         // Must do a GC to update performance counters
    64         System.gc();
    65         assertEQ(getMinCapacity(perfNS), pool.getUsage().getInit());
    67         // Must do a second GC to update the perfomance counters again, since
    68         // the call pool.getUsage().getInit() could have allocated some
    69         // metadata.
    70         System.gc();
    71         assertEQ(getUsed(perfNS), pool.getUsage().getUsed());
    72         assertEQ(getCapacity(perfNS), pool.getUsage().getCommitted());
    73     }
    75     private static long getMinCapacity(String ns) throws Exception {
    76         return PerfCounters.findByName(ns + ".minCapacity").longValue();
    77     }
    79     private static long getCapacity(String ns) throws Exception {
    80         return PerfCounters.findByName(ns + ".capacity").longValue();
    81     }
    83     private static long getUsed(String ns) throws Exception {
    84         return PerfCounters.findByName(ns + ".used").longValue();
    85     }
    86 }

mercurial