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