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 
  62     // Hotspot VM 1.8+ after perm gen removal is expected to have only
  63     // one non-heap memory pool
  64     private static int[] expectedMinNumPools = {3, 1};
  65     private static int[] expectedMaxNumPools = {3, 1};
  66     private static int expectedNumGCMgrs = 2;
  67     private static int expectedNumMgrs = expectedNumGCMgrs + 1;
  68     private static String[] types = { "heap", "non-heap" };
  69 
  70     public static void main(String args[]) throws Exception {
  71         Integer value = new Integer(args[0]);
  72         expectedNumGCMgrs = value.intValue();
  73         expectedNumMgrs = expectedNumGCMgrs + 1;
  74 
  75         checkMemoryPools();
  76         checkMemoryManagers();
  77         if (testFailed)
  78             throw new RuntimeException("TEST FAILED.");
  79 
  80         System.out.println("Test passed.");
  81 
  82     }
  83 
  84     private static void checkMemoryPools() throws Exception {
  85         List pools = ManagementFactory.getMemoryPoolMXBeans();
  86         boolean hasPerm = false;
  87 
  88         int[] numPools = new int[NUM_TYPES];
  89         for (ListIterator iter = pools.listIterator(); iter.hasNext();) {
  90             MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();
  91             if (pool.getType() == MemoryType.HEAP) {
  92                 numPools[HEAP]++;
  93             }
  94             if (pool.getType() == MemoryType.NON_HEAP) {
  95                 numPools[NONHEAP]++;
  96             }
  97             if (pool.getName().toLowerCase().contains("perm")) {
  98                 hasPerm = true;
  99             }
 100         }
 101 
 102         if (hasPerm) {
 103             // If the VM has perm gen there will be between 2 and 4 non heap
 104             // pools (4 if class data sharing is used)
 105             expectedMinNumPools[NONHEAP] = 2;
 106             expectedMaxNumPools[NONHEAP] = 4;
 107         }
 108 
 109         // Check the number of Memory pools
 110         for (int i = 0; i < NUM_TYPES; i++) {
 111             if (numPools[i] < expectedMinNumPools[i] ||
 112                     numPools[i] > expectedMaxNumPools[i]) {
 113                 throw new RuntimeException("TEST FAILED: " +
 114                     "Number of " + types[i] + " pools = " + numPools[i] +
 115                     " but expected <= " + expectedMaxNumPools[i] +
 116                     " and >= " + expectedMinNumPools[i]);
 117             }
 118         }
 119     }
 120 
 121     private static void checkMemoryManagers() throws Exception {
 122         List mgrs = ManagementFactory.getMemoryManagerMXBeans();
 123 
 124         int numGCMgr = 0;
 125 
 126         // Check the number of Memory Managers
 127         for (ListIterator iter = mgrs.listIterator(); iter.hasNext();) {
 128             MemoryManagerMXBean mgr = (MemoryManagerMXBean) iter.next();
 129             String[] poolNames = mgr.getMemoryPoolNames();
 130             if (poolNames == null || poolNames.length == 0) {
 131                 throw new RuntimeException("TEST FAILED: " +
 132                     "Expected to have one or more pools for " +
 133                     mgr.getName() + "manager.");
 134             }
 135 
 136             if (mgr instanceof GarbageCollectorMXBean) {
 137                 numGCMgr++;
 138             } else {
 139                 for (int i = 0; i < poolNames.length; i++) {
 140                     checkPoolType(poolNames[i], MemoryType.NON_HEAP);
 141                 }
 142             }
 143         }
 144 
 145         if (mgrs.size() != expectedNumMgrs) {
 146             throw new RuntimeException("TEST FAILED: " +
 147                 "Number of memory managers = " + mgrs.size() +
 148                 " but expected = " + expectedNumMgrs);
 149         }
 150         if (numGCMgr != expectedNumGCMgrs) {
 151             throw new RuntimeException("TEST FAILED: " +
 152                 "Number of GC managers = " + numGCMgr + " but expected = " +
 153                 expectedNumGCMgrs);
 154         }
 155     }
 156     private static List pools = ManagementFactory.getMemoryPoolMXBeans();
 157     private static void checkPoolType(String name, MemoryType type)
 158         throws Exception {
 159         for (ListIterator iter = pools.listIterator(); iter.hasNext(); ) {
 160             MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();
 161             if (pool.getName().equals(name)) {
 162                 if (pool.getType() != type) {
 163                     throw new RuntimeException("TEST FAILED: " +
 164                         "Pool " + pool.getName() + " is of type " +
 165                         pool.getType() + " but expected to be " + type);
 166                 } else {
 167                     return;
 168                 }
 169             }
 170         }
 171         throw new RuntimeException("TEST FAILED: " +
 172             "Pool " + name + " is of type " + type +
 173             " not found");
 174     }
 175 }