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