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