Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/management/MemoryMXBean/MemoryTest.java
41155 views
1
/*
2
* Copyright (c) 2003, 2021, 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
* @requires vm.gc != "Z"
30
* @author Mandy Chung
31
*
32
* @modules jdk.management
33
* @run main MemoryTest 2 3
34
*/
35
36
/*
37
* @test
38
* @bug 4530538
39
* @summary Basic unit test of MemoryMXBean.getMemoryPools() and
40
* MemoryMXBean.getMemoryManager().
41
* @requires vm.gc == "Z"
42
* @author Mandy Chung
43
*
44
* @modules jdk.management
45
* @run main MemoryTest 2 1
46
*/
47
48
/*
49
* NOTE: This expected result is hardcoded in this test and this test
50
* will be affected if the heap memory layout is changed in
51
* the future implementation.
52
*/
53
54
import java.lang.management.*;
55
import java.util.*;
56
57
public class MemoryTest {
58
private static boolean testFailed = false;
59
private static MemoryMXBean mm = ManagementFactory.getMemoryMXBean();
60
private static final int HEAP = 0;
61
private static final int NONHEAP = 1;
62
private static final int NUM_TYPES = 2;
63
64
// WARNING: if the number of pools changes in the future,
65
// this test needs to be modified to handle different version of VMs.
66
67
// Hotspot VM 1.5 expected to have
68
// heap memory pools = 3 (Eden, Survivor spaces, Old gen)
69
// non-heap memory pools = 2 (Perm gen, Code cache)
70
// or 4 if Class Sharing is enabled.
71
// Number of memory managers = 3
72
// They are: Copy/Scavenger + MSC + CodeCache manager
73
// (or equivalent for other collectors)
74
// Number of GC memory managers = 2
75
76
// Hotspot VM 1.8+ after perm gen removal is expected to have between two
77
// or five non-heap memory pools:
78
// - Code cache (between one and three depending on the -XX:SegmentedCodeCache option)
79
// - Metaspace
80
// - Compressed Class Space (if compressed class pointers are used)
81
82
private static int[] expectedMinNumPools = new int[2];
83
private static int[] expectedMaxNumPools = new int[2];
84
private static int expectedNumGCMgrs;
85
private static int expectedNumMgrs;
86
private static String[] types = { "heap", "non-heap" };
87
88
public static void main(String args[]) throws Exception {
89
expectedNumGCMgrs = Integer.valueOf(args[0]);
90
expectedNumMgrs = expectedNumGCMgrs + 2;
91
92
int expectedNumPools = Integer.valueOf(args[1]);
93
expectedMinNumPools[HEAP] = expectedNumPools;
94
expectedMaxNumPools[HEAP] = expectedNumPools;
95
96
expectedMinNumPools[NONHEAP] = 2;
97
expectedMaxNumPools[NONHEAP] = 5;
98
99
checkMemoryPools();
100
checkMemoryManagers();
101
if (testFailed)
102
throw new RuntimeException("TEST FAILED.");
103
104
System.out.println("Test passed.");
105
106
}
107
108
private static void checkMemoryPools() throws Exception {
109
List pools = ManagementFactory.getMemoryPoolMXBeans();
110
boolean hasPerm = false;
111
112
int[] numPools = new int[NUM_TYPES];
113
for (ListIterator iter = pools.listIterator(); iter.hasNext();) {
114
MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();
115
if (pool.getType() == MemoryType.HEAP) {
116
numPools[HEAP]++;
117
}
118
if (pool.getType() == MemoryType.NON_HEAP) {
119
numPools[NONHEAP]++;
120
}
121
if (pool.getName().toLowerCase().contains("perm")) {
122
hasPerm = true;
123
}
124
}
125
126
if (hasPerm) {
127
// If the VM has perm gen there will be between 2 and 4 non heap
128
// pools (4 if class data sharing is used)
129
expectedMinNumPools[NONHEAP] = 2;
130
expectedMaxNumPools[NONHEAP] = 4;
131
}
132
133
// Check the number of Memory pools
134
for (int i = 0; i < NUM_TYPES; i++) {
135
if (numPools[i] < expectedMinNumPools[i] ||
136
numPools[i] > expectedMaxNumPools[i]) {
137
throw new RuntimeException("TEST FAILED: " +
138
"Number of " + types[i] + " pools = " + numPools[i] +
139
" but expected <= " + expectedMaxNumPools[i] +
140
" and >= " + expectedMinNumPools[i]);
141
}
142
}
143
}
144
145
private static void checkMemoryManagers() throws Exception {
146
List mgrs = ManagementFactory.getMemoryManagerMXBeans();
147
148
int numGCMgr = 0;
149
150
// Check the number of Memory Managers
151
for (ListIterator iter = mgrs.listIterator(); iter.hasNext();) {
152
MemoryManagerMXBean mgr = (MemoryManagerMXBean) iter.next();
153
String[] poolNames = mgr.getMemoryPoolNames();
154
if (poolNames == null || poolNames.length == 0) {
155
throw new RuntimeException("TEST FAILED: " +
156
"Expected to have one or more pools for " +
157
mgr.getName() + "manager.");
158
}
159
160
if (mgr instanceof GarbageCollectorMXBean) {
161
numGCMgr++;
162
} else {
163
for (int i = 0; i < poolNames.length; i++) {
164
checkPoolType(poolNames[i], MemoryType.NON_HEAP);
165
}
166
}
167
}
168
169
if (mgrs.size() != expectedNumMgrs) {
170
throw new RuntimeException("TEST FAILED: " +
171
"Number of memory managers = " + mgrs.size() +
172
" but expected = " + expectedNumMgrs);
173
}
174
if (numGCMgr != expectedNumGCMgrs) {
175
throw new RuntimeException("TEST FAILED: " +
176
"Number of GC managers = " + numGCMgr + " but expected = " +
177
expectedNumGCMgrs);
178
}
179
}
180
private static List pools = ManagementFactory.getMemoryPoolMXBeans();
181
private static void checkPoolType(String name, MemoryType type)
182
throws Exception {
183
for (ListIterator iter = pools.listIterator(); iter.hasNext(); ) {
184
MemoryPoolMXBean pool = (MemoryPoolMXBean) iter.next();
185
if (pool.getName().equals(name)) {
186
if (pool.getType() != type) {
187
throw new RuntimeException("TEST FAILED: " +
188
"Pool " + pool.getName() + " is of type " +
189
pool.getType() + " but expected to be " + type);
190
} else {
191
return;
192
}
193
}
194
}
195
throw new RuntimeException("TEST FAILED: " +
196
"Pool " + name + " is of type " + type +
197
" not found");
198
}
199
}
200
201