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 
  84         int[] numPools = new int[NUM_TYPES];
  85         for (ListIterator iter = pools.listIterator(); iter.hasNext();) {
  86             MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();
  87             if (pool.getType() == MemoryType.HEAP) {
  88                 numPools[HEAP]++;
  89             }
  90             if (pool.getType() == MemoryType.NON_HEAP) {
  91                 numPools[NONHEAP]++;
  92             }
  93         }
  94 
  95         // Check the number of Memory pools
  96         for (int i = 0; i < NUM_TYPES; i++) {
  97             if (numPools[i] < expectedMinNumPools[i] ||
  98                     numPools[i] > expectedMaxNumPools[i]) {
  99                 throw new RuntimeException("TEST FAILED: " +
 100                     "Number of " + types[i] + " pools = " + numPools[i] +
 101                     " but expected <= " + expectedMaxNumPools[i] +
 102                     " and >= " + expectedMinNumPools[i]);
 103             }
 104         }
 105     }
 106 
 107     private static void checkMemoryManagers() throws Exception {
 108         List mgrs = ManagementFactory.getMemoryManagerMXBeans();
 109 
 110         int numGCMgr = 0;
 111 
 112         // Check the number of Memory Managers
 113         for (ListIterator iter = mgrs.listIterator(); iter.hasNext();) {
 114             MemoryManagerMXBean mgr = (MemoryManagerMXBean) iter.next();
 115             String[] poolNames = mgr.getMemoryPoolNames();
 116             if (poolNames == null || poolNames.length == 0) {
 117                 throw new RuntimeException("TEST FAILED: " +
 118                     "Expected to have one or more pools for " +
 119                     mgr.getName() + "manager.");
 120             }
 121 
 122             if (mgr instanceof GarbageCollectorMXBean) {
 123                 numGCMgr++;
 124             } else {
 125                 for (int i = 0; i < poolNames.length; i++) {
 126                     checkPoolType(poolNames[i], MemoryType.NON_HEAP);
 127                 }
 128             }
 129         }
 130 
 131         if (mgrs.size() != expectedNumMgrs) {
 132             throw new RuntimeException("TEST FAILED: " +
 133                 "Number of memory managers = " + mgrs.size() +
 134                 " but expected = " + expectedNumMgrs);
 135         }
 136         if (numGCMgr != expectedNumGCMgrs) {
 137             throw new RuntimeException("TEST FAILED: " +
 138                 "Number of GC managers = " + numGCMgr + " but expected = " +
 139                 expectedNumGCMgrs);
 140         }
 141     }
 142     private static List pools = ManagementFactory.getMemoryPoolMXBeans();
 143     private static void checkPoolType(String name, MemoryType type)
 144         throws Exception {
 145         for (ListIterator iter = pools.listIterator(); iter.hasNext(); ) {
 146             MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();
 147             if (pool.getName().equals(name)) {
 148                 if (pool.getType() != type) {
 149                     throw new RuntimeException("TEST FAILED: " +
 150                         "Pool " + pool.getName() + " is of type " +
 151                         pool.getType() + " but expected to be " + type);
 152                 } else {
 153                     return;
 154                 }
 155             }
 156         }
 157         throw new RuntimeException("TEST FAILED: " +
 158             "Pool " + name + " is of type " + type +
 159             " not found");
 160     }
 161 }