< prev index next >

test/gc/metaspace/TestPerfCountersAndMemoryPools.java

Print this page
rev 10588 : 8151460: Metaspace counters can have inconsistent values
Reviewed-by:
rev 10589 : [mq]: meta-rev


  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.lang.management.*;
  26 
  27 import jdk.test.lib.*;
  28 import static jdk.test.lib.Asserts.*;
  29 
  30 /* @test TestPerfCountersAndMemoryPools
  31  * @bug 8023476
  32  * @library /testlibrary
  33  * @requires vm.gc=="Serial" | vm.gc=="null"
  34  * @summary Tests that a MemoryPoolMXBeans and PerfCounters for metaspace
  35  *          report the same data.
  36  * @modules java.base/sun.misc
  37  *          java.management
  38  *          jdk.jvmstat/sun.jvmstat.monitor
  39  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:-UseCompressedKlassPointers -XX:+UseSerialGC -XX:+UsePerfData -Xint TestPerfCountersAndMemoryPools
  40  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedKlassPointers -XX:+UseSerialGC -XX:+UsePerfData -Xint TestPerfCountersAndMemoryPools
  41  */
  42 public class TestPerfCountersAndMemoryPools {
  43     public static void main(String[] args) throws Exception {
  44         checkMemoryUsage("Metaspace", "sun.gc.metaspace");
  45 
  46         if (InputArguments.contains("-XX:+UseCompressedKlassPointers") && Platform.is64bit()) {
  47             checkMemoryUsage("Compressed Class Space", "sun.gc.compressedclassspace");
  48         }
  49     }
  50 
  51     private static MemoryPoolMXBean getMemoryPool(String memoryPoolName) {
  52         List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
  53         for (MemoryPoolMXBean pool : pools) {
  54             if (pool.getName().equals(memoryPoolName)) {
  55                 return pool;
  56             }
  57         }
  58 
  59         throw new RuntimeException("Excpted to find a memory pool with name " +
  60                                    memoryPoolName);
  61     }
  62 
  63     private static void checkMemoryUsage(String memoryPoolName, String perfNS)
  64         throws Exception {
  65         MemoryPoolMXBean pool = getMemoryPool(memoryPoolName);
  66 
  67         // First, call all the methods to let them allocate their own slab of metadata
  68         getMinCapacity(perfNS);
  69         getCapacity(perfNS);
  70         getUsed(perfNS);
  71         pool.getUsage().getInit();
  72         pool.getUsage().getUsed();
  73         pool.getUsage().getCommitted();
  74         assertEQ(1L, 1L);
  75 
  76         // Must do a GC to update performance counters
  77         System.gc();
  78         assertEQ(getMinCapacity(perfNS), pool.getUsage().getInit());
  79         assertEQ(getUsed(perfNS), pool.getUsage().getUsed());
  80         assertEQ(getCapacity(perfNS), pool.getUsage().getCommitted());




  81     }
  82 
  83     private static long getMinCapacity(String ns) throws Exception {
  84         return PerfCounters.findByName(ns + ".minCapacity").longValue();
  85     }
  86 
  87     private static long getCapacity(String ns) throws Exception {
  88         return PerfCounters.findByName(ns + ".capacity").longValue();
  89     }
  90 
  91     private static long getUsed(String ns) throws Exception {
  92         return PerfCounters.findByName(ns + ".used").longValue();
  93     }
  94 }


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