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.lang.management.ManagementFactory;
  26 import java.lang.management.MemoryManagerMXBean;
  27 import java.lang.management.MemoryPoolMXBean;
  28 import java.lang.management.MemoryUsage;
  29 
  30 import java.lang.management.RuntimeMXBean;
  31 import java.lang.management.ManagementFactory;
  32 
  33 /* @test TestMetaspaceMemoryPool
  34  * @bug 8000754
  35  * @summary Tests that a MemoryPoolMXBeans is created for metaspace and that a
  36  *          MemoryManagerMXBean is created.
  37  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops TestMetaspaceMemoryPool
  38  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:-UseCompressedOops -XX:MaxMetaspaceSize=60m TestMetaspaceMemoryPool
  39  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers TestMetaspaceMemoryPool
  40  * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:+UseCompressedClassPointers -XX:CompressedClassSpaceSize=60m TestMetaspaceMemoryPool
  41  */
  42 public class TestMetaspaceMemoryPool {
  43     public static void main(String[] args) {
  44         verifyThatMetaspaceMemoryManagerExists();
  45         verifyMemoryPool(getMemoryPool("Metaspace"), isFlagDefined("MaxMetaspaceSize"));
  46 
  47         if (runsOn64bit()) {
  48             if (usesCompressedOops()) {
  49                 MemoryPoolMXBean cksPool = getMemoryPool("Compressed Class Space");
  50                 verifyMemoryPool(cksPool, true);
  51             }
  52         }
  53     }
  54 
  55     private static boolean runsOn64bit() {
  56         return !System.getProperty("sun.arch.data.model").equals("32");
  57     }
  58 
  59     private static boolean usesCompressedOops() {
  60         return isFlagDefined("+UseCompressedOops");
  61     }
  62 
  63     private static boolean isFlagDefined(String name) {
  64         RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
  65         List<String> args = runtimeMxBean.getInputArguments();
  66         for (String arg : args) {
  67             if (arg.startsWith("-XX:" + name)) {
  68                 return true;
  69             }
  70         }
  71         return false;
  72     }
  73 
  74     private static void verifyThatMetaspaceMemoryManagerExists() {
  75         List<MemoryManagerMXBean> managers = ManagementFactory.getMemoryManagerMXBeans();
  76         for (MemoryManagerMXBean manager : managers) {
  77             if (manager.getName().equals("Metaspace Manager")) {
  78                 return;
  79             }
  80         }
  81 
  82         throw new RuntimeException("Expected to find a metaspace memory manager");
  83     }
  84 
  85     private static MemoryPoolMXBean getMemoryPool(String name) {
  86         List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
  87         for (MemoryPoolMXBean pool : pools) {
  88             if (pool.getName().equals(name)) {
  89                 return pool;
  90             }
  91         }
  92 
  93         throw new RuntimeException("Expected to find a memory pool with name " + name);
  94     }
  95 
  96     private static void verifyMemoryPool(MemoryPoolMXBean pool, boolean isMaxDefined) {
  97         MemoryUsage mu = pool.getUsage();
  98         assertDefined(mu.getInit(), "init");
  99         assertDefined(mu.getUsed(), "used");
 100         assertDefined(mu.getCommitted(), "committed");
 101 
 102         if (isMaxDefined) {
 103             assertDefined(mu.getMax(), "max");
 104         } else {
 105             assertUndefined(mu.getMax(), "max");
 106         }
 107     }
 108 
 109     private static void assertDefined(long value, String name) {
 110         assertTrue(value != -1, "Expected " + name + " to be defined");
 111     }
 112 
 113     private static void assertUndefined(long value, String name) {
 114         assertEquals(value, -1, "Expected " + name + " to be undefined");
 115     }
 116 
 117     private static void assertEquals(long actual, long expected, String msg) {
 118         assertTrue(actual == expected, msg);
 119     }
 120 
 121     private static void assertTrue(boolean condition, String msg) {
 122         if (!condition) {
 123             throw new RuntimeException(msg);
 124         }
 125     }
 126 }