1 /*
   2  * Copyright (c) 2003, 2016, 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 /*
  25  * @test
  26  * @bug     4530538
  27  * @summary Basic unit test of MemoryMXBean.getMemoryPools() and
  28  *          MemoryMXBean.getMemoryManager().
  29  * @requires vm.gc != "Z"
  30  * @author  Mandy Chung
  31  *
  32  * @modules jdk.management
  33  * @run main MemoryTest 2 3
  34  */
  35 
  36 /*
  37  * NOTE: This expected result is hardcoded in this test and this test
  38  *       will be affected if the heap memory layout is changed in
  39  *       the future implementation.
  40  */
  41 
  42 import java.lang.management.*;
  43 import java.util.*;
  44 
  45 public class MemoryTest {
  46     private static boolean testFailed = false;
  47     private static MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
  48     private static final int HEAP = 0;
  49     private static final int NONHEAP = 1;
  50     private static final int NUM_TYPES = 2;
  51 
  52     // WARNING: if the number of pools changes in the future,
  53     // this test needs to be modified to handle different version of VMs.
  54 
  55     // Hotspot VM 1.5 expected to have
  56     //   heap memory pools     = 3 (Eden, Survivor spaces, Old gen)
  57     //   non-heap memory pools = 2 (Perm gen, Code cache)
  58     //                           or 4 if Class Sharing is enabled.
  59     // Number of memory managers = 3
  60     // They are: Copy/Scavenger + MSC + CodeCache manager
  61     // (or equivalent for other collectors)
  62     // Number of GC memory managers = 2
  63 
  64     // Hotspot VM 1.8+ after perm gen removal is expected to have between two
  65     // or five non-heap memory pools:
  66     // - Code cache (between one and three depending on the -XX:SegmentedCodeCache option)
  67     // - Metaspace
  68     // - Compressed Class Space (if compressed class pointers are used)
  69 
  70     private static int[] expectedMinNumPools = new int[2];
  71     private static int[] expectedMaxNumPools = new int[2];
  72     private static int expectedNumGCMgrs;
  73     private static int expectedNumMgrs;
  74     private static String[] types = { "heap", "non-heap" };
  75 
  76     public static void main(String args[]) throws Exception {
  77         expectedNumGCMgrs = Integer.valueOf(args[0]);
  78         expectedNumMgrs = expectedNumGCMgrs + 2;
  79 
  80         int expectedNumPools = Integer.valueOf(args[1]);
  81         expectedMinNumPools[HEAP] = expectedNumPools;
  82         expectedMaxNumPools[HEAP] = expectedNumPools;
  83 
  84         expectedMinNumPools[NONHEAP] = 2;
  85         expectedMaxNumPools[NONHEAP] = 5;
  86 
  87         checkMemoryPools();
  88         checkMemoryManagers();
  89         if (testFailed)
  90             throw new RuntimeException("TEST FAILED.");
  91 
  92         System.out.println("Test passed.");
  93 
  94     }
  95 
  96     private static void checkMemoryPools() throws Exception {
  97         List pools = ManagementFactory.getMemoryPoolMXBeans();
  98         boolean hasPerm = false;
  99 
 100         int[] numPools = new int[NUM_TYPES];
 101         for (ListIterator iter = pools.listIterator(); iter.hasNext();) {
 102             MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();
 103             if (pool.getType() == MemoryType.HEAP) {
 104                 numPools[HEAP]++;
 105             }
 106             if (pool.getType() == MemoryType.NON_HEAP) {
 107                 numPools[NONHEAP]++;
 108             }
 109             if (pool.getName().toLowerCase().contains("perm")) {
 110                 hasPerm = true;
 111             }
 112         }
 113 
 114         if (hasPerm) {
 115             // If the VM has perm gen there will be between 2 and 4 non heap
 116             // pools (4 if class data sharing is used)
 117             expectedMinNumPools[NONHEAP] = 2;
 118             expectedMaxNumPools[NONHEAP] = 4;
 119         }
 120 
 121         // Check the number of Memory pools
 122         for (int i = 0; i < NUM_TYPES; i++) {
 123             if (numPools[i] < expectedMinNumPools[i] ||
 124                     numPools[i] > expectedMaxNumPools[i]) {
 125                 throw new RuntimeException("TEST FAILED: " +
 126                     "Number of " + types[i] + " pools = " + numPools[i] +
 127                     " but expected <= " + expectedMaxNumPools[i] +
 128                     " and >= " + expectedMinNumPools[i]);
 129             }
 130         }
 131     }
 132 
 133     private static void checkMemoryManagers() throws Exception {
 134         List mgrs = ManagementFactory.getMemoryManagerMXBeans();
 135 
 136         int numGCMgr = 0;
 137 
 138         // Check the number of Memory Managers
 139         for (ListIterator iter = mgrs.listIterator(); iter.hasNext();) {
 140             MemoryManagerMXBean mgr = (MemoryManagerMXBean) iter.next();
 141             String[] poolNames = mgr.getMemoryPoolNames();
 142             if (poolNames == null || poolNames.length == 0) {
 143                 throw new RuntimeException("TEST FAILED: " +
 144                     "Expected to have one or more pools for " +
 145                     mgr.getName() + "manager.");
 146             }
 147 
 148             if (mgr instanceof GarbageCollectorMXBean) {
 149                 numGCMgr++;
 150             } else {
 151                 for (int i = 0; i < poolNames.length; i++) {
 152                     checkPoolType(poolNames[i], MemoryType.NON_HEAP);
 153                 }
 154             }
 155         }
 156 
 157         if (mgrs.size() != expectedNumMgrs) {
 158             throw new RuntimeException("TEST FAILED: " +
 159                 "Number of memory managers = " + mgrs.size() +
 160                 " but expected = " + expectedNumMgrs);
 161         }
 162         if (numGCMgr != expectedNumGCMgrs) {
 163             throw new RuntimeException("TEST FAILED: " +
 164                 "Number of GC managers = " + numGCMgr + " but expected = " +
 165                 expectedNumGCMgrs);
 166         }
 167     }
 168     private static List pools = ManagementFactory.getMemoryPoolMXBeans();
 169     private static void checkPoolType(String name, MemoryType type)
 170         throws Exception {
 171         for (ListIterator iter = pools.listIterator(); iter.hasNext(); ) {
 172             MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();
 173             if (pool.getName().equals(name)) {
 174                 if (pool.getType() != type) {
 175                     throw new RuntimeException("TEST FAILED: " +
 176                         "Pool " + pool.getName() + " is of type " +
 177                         pool.getType() + " but expected to be " + type);
 178                 } else {
 179                     return;
 180                 }
 181             }
 182         }
 183         throw new RuntimeException("TEST FAILED: " +
 184             "Pool " + name + " is of type " + type +
 185             " not found");
 186     }
 187 }