1 /*
  2  * Copyright (c) 2013, 2019, 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 package gc.metaspace;
 25 
 26 import java.util.List;
 27 import java.lang.management.*;
 28 
 29 import jdk.test.lib.Platform;
 30 import static jdk.test.lib.Asserts.*;
 31 import gc.testlibrary.PerfCounters;
 32 
 33 /* @test TestPerfCountersAndMemoryPools
 34  * @bug 8023476
 35  * @library /test/lib /
 36  * @requires vm.gc.Serial
 37  * @summary Tests that a MemoryPoolMXBeans and PerfCounters for metaspace
 38  *          report the same data.
 39  * @modules java.base/jdk.internal.misc
 40  *          java.management
 41  *          jdk.internal.jvmstat/sun.jvmstat.monitor
 42  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedClassPointers -XX:+UseSerialGC -XX:+UsePerfData -Xint gc.metaspace.TestPerfCountersAndMemoryPools
 43  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:+UseSerialGC -XX:+UsePerfData -Xint gc.metaspace.TestPerfCountersAndMemoryPools
 44  */
 45 public class TestPerfCountersAndMemoryPools {
 46     public static void main(String[] args) throws Exception {
 47         checkMemoryUsage("Metaspace", "sun.gc.metaspace");
 48 
 49         if (InputArguments.contains("-XX:+UseCompressedClassPointers") && Platform.is64bit()) {
 50             checkMemoryUsage("Compressed Class Space", "sun.gc.compressedclassspace");
 51         }
 52     }
 53 
 54     private static MemoryPoolMXBean getMemoryPool(String memoryPoolName) {
 55         List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
 56         for (MemoryPoolMXBean pool : pools) {
 57             if (pool.getName().equals(memoryPoolName)) {
 58                 return pool;
 59             }
 60         }
 61 
 62         throw new RuntimeException("Excpted to find a memory pool with name " +
 63                                    memoryPoolName);
 64     }
 65 
 66     private static void checkMemoryUsage(String memoryPoolName, String perfNS)
 67         throws Exception {
 68         MemoryPoolMXBean pool = getMemoryPool(memoryPoolName);
 69 
 70         // First, call all the methods to let them allocate their own slab of metadata
 71         getMinCapacity(perfNS);
 72         getCapacity(perfNS);
 73         getUsed(perfNS);
 74         pool.getUsage().getInit();
 75         pool.getUsage().getUsed();
 76         pool.getUsage().getCommitted();
 77         assertEQ(1L, 1L, "Make assert load");
 78 
 79         // Must do a GC to update performance counters
 80         System.gc();
 81         assertEQ(getMinCapacity(perfNS), pool.getUsage().getInit(), "MinCapacity out of sync");
 82 
 83         // Adding a second GC due to metadata allocations caused by getting the
 84         // initial size from the pool. This is needed when running with -Xcomp.
 85         System.gc();
 86         assertEQ(getUsed(perfNS), pool.getUsage().getUsed(), "Used out of sync");
 87         assertEQ(getCapacity(perfNS), pool.getUsage().getCommitted(), "Committed out of sync");
 88     }
 89 
 90     private static long getMinCapacity(String ns) throws Exception {
 91         return PerfCounters.findByName(ns + ".minCapacity").longValue();
 92     }
 93 
 94     private static long getCapacity(String ns) throws Exception {
 95         return PerfCounters.findByName(ns + ".capacity").longValue();
 96     }
 97 
 98     private static long getUsed(String ns) throws Exception {
 99         return PerfCounters.findByName(ns + ".used").longValue();
100     }
101 }