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