Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/tools/jmap/BasicJMapTest.java
41149 views
1
/*
2
* Copyright (c) 2005, 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
import static jdk.test.lib.Asserts.assertTrue;
25
import static jdk.test.lib.Asserts.assertFalse;
26
import static jdk.test.lib.Asserts.fail;
27
28
import java.io.File;
29
import java.nio.file.Files;
30
import java.util.Arrays;
31
import java.util.List;
32
33
import jdk.test.lib.JDKToolLauncher;
34
import jdk.test.lib.Utils;
35
import jdk.test.lib.hprof.HprofParser;
36
import jdk.test.lib.process.OutputAnalyzer;
37
import jdk.test.lib.process.ProcessTools;
38
39
/*
40
* @test id=Serial
41
* @requires vm.gc.Serial
42
* @summary Unit test for jmap utility (Serial GC)
43
* @key intermittent
44
* @library /test/lib
45
* @build jdk.test.lib.hprof.*
46
* @build jdk.test.lib.hprof.model.*
47
* @build jdk.test.lib.hprof.parser.*
48
* @build jdk.test.lib.hprof.util.*
49
* @run main/othervm/timeout=240 -XX:+UseSerialGC BasicJMapTest
50
*/
51
52
/*
53
* @test id=Parallel
54
* @requires vm.gc.Parallel
55
* @summary Unit test for jmap utility (Parallel GC)
56
* @key intermittent
57
* @library /test/lib
58
* @build jdk.test.lib.hprof.*
59
* @build jdk.test.lib.hprof.model.*
60
* @build jdk.test.lib.hprof.parser.*
61
* @build jdk.test.lib.hprof.util.*
62
* @run main/othervm/timeout=240 -XX:+UseParallelGC BasicJMapTest
63
*/
64
65
/*
66
* @test id=G1
67
* @requires vm.gc.G1
68
* @summary Unit test for jmap utility (G1 GC)
69
* @key intermittent
70
* @library /test/lib
71
* @build jdk.test.lib.hprof.*
72
* @build jdk.test.lib.hprof.model.*
73
* @build jdk.test.lib.hprof.parser.*
74
* @build jdk.test.lib.hprof.util.*
75
* @run main/othervm/timeout=240 -XX:+UseG1GC BasicJMapTest
76
*/
77
78
/*
79
* @test id=Shenandoah
80
* @requires vm.gc.Shenandoah
81
* @summary Unit test for jmap utility (Shenandoah GC)
82
* @key intermittent
83
* @library /test/lib
84
* @build jdk.test.lib.hprof.*
85
* @build jdk.test.lib.hprof.model.*
86
* @build jdk.test.lib.hprof.parser.*
87
* @build jdk.test.lib.hprof.util.*
88
* @run main/othervm/timeout=240 -XX:+UseShenandoahGC BasicJMapTest
89
*/
90
91
/*
92
* @test id=Z
93
* @requires vm.gc.Z
94
* @summary Unit test for jmap utility (Z GC)
95
* @key intermittent
96
* @library /test/lib
97
* @build jdk.test.lib.hprof.*
98
* @build jdk.test.lib.hprof.model.*
99
* @build jdk.test.lib.hprof.parser.*
100
* @build jdk.test.lib.hprof.util.*
101
* @run main/othervm/timeout=240 -XX:+UseZGC BasicJMapTest
102
*/
103
104
public class BasicJMapTest {
105
106
private static ProcessBuilder processBuilder = new ProcessBuilder();
107
108
public static void main(String[] args) throws Exception {
109
testHisto();
110
testHistoLive();
111
testHistoAll();
112
testHistoParallelZero();
113
testHistoParallel();
114
testHistoNonParallel();
115
testHistoToFile();
116
testHistoLiveToFile();
117
testHistoAllToFile();
118
testFinalizerInfo();
119
testClstats();
120
testDump();
121
testDumpLive();
122
testDumpAll();
123
testDumpCompressed();
124
testDumpIllegalCompressedArgs();
125
}
126
127
private static void testHisto() throws Exception {
128
OutputAnalyzer output = jmap("-histo:");
129
output.shouldHaveExitValue(0);
130
OutputAnalyzer output1 = jmap("-histo");
131
output1.shouldHaveExitValue(0);
132
}
133
134
private static void testHistoLive() throws Exception {
135
OutputAnalyzer output = jmap("-histo:live");
136
output.shouldHaveExitValue(0);
137
}
138
139
private static void testHistoAll() throws Exception {
140
OutputAnalyzer output = jmap("-histo:all");
141
output.shouldHaveExitValue(0);
142
}
143
144
private static void testHistoParallelZero() throws Exception {
145
OutputAnalyzer output = jmap("-histo:parallel=0");
146
output.shouldHaveExitValue(0);
147
}
148
149
private static void testHistoParallel() throws Exception {
150
OutputAnalyzer output = jmap("-histo:parallel=2");
151
output.shouldHaveExitValue(0);
152
}
153
154
private static void testHistoNonParallel() throws Exception {
155
OutputAnalyzer output = jmap("-histo:parallel=1");
156
output.shouldHaveExitValue(0);
157
}
158
159
private static void testHistoToFile() throws Exception {
160
histoToFile(false, false, 1);
161
}
162
163
private static void testHistoLiveToFile() throws Exception {
164
histoToFile(true, false, 1);
165
}
166
167
private static void testHistoAllToFile() throws Exception {
168
histoToFile(false, true, 1);
169
}
170
171
private static void testHistoFileParallelZero() throws Exception {
172
histoToFile(false, false, 0);
173
}
174
175
private static void testHistoFileParallel() throws Exception {
176
histoToFile(false, false, 2);
177
}
178
179
private static void histoToFile(boolean live,
180
boolean explicitAll,
181
int parallelThreadNum) throws Exception {
182
String liveArg = "";
183
String fileArg = "";
184
String parArg = "parallel=" + parallelThreadNum;
185
String allArgs = "-histo:";
186
187
if (live && explicitAll) {
188
fail("Illegal argument setting for jmap -histo");
189
}
190
if (live) {
191
liveArg = "live,";
192
}
193
if (explicitAll) {
194
liveArg = "all,";
195
}
196
197
File file = new File("jmap.histo.file" + System.currentTimeMillis() + ".histo");
198
if (file.exists()) {
199
file.delete();
200
}
201
fileArg = "file=" + file.getName();
202
203
OutputAnalyzer output;
204
allArgs = allArgs + liveArg + fileArg + ',' + parArg;
205
output = jmap(allArgs);
206
output.shouldHaveExitValue(0);
207
output.shouldContain("Heap inspection file created");
208
file.delete();
209
}
210
211
private static void testFinalizerInfo() throws Exception {
212
OutputAnalyzer output = jmap("-finalizerinfo");
213
output.shouldHaveExitValue(0);
214
}
215
216
private static void testClstats() throws Exception {
217
OutputAnalyzer output = jmap("-clstats");
218
output.shouldHaveExitValue(0);
219
}
220
221
private static void testDump() throws Exception {
222
dump(false, false, false);
223
}
224
225
private static void testDumpLive() throws Exception {
226
dump(true, false, false);
227
}
228
229
private static void testDumpAll() throws Exception {
230
dump(false, true, false);
231
}
232
233
private static void testDumpCompressed() throws Exception {
234
dump(true, false, true);
235
}
236
237
private static void testDumpIllegalCompressedArgs() throws Exception{
238
dump(true, false, true, "0", 1, "Compression level out of range");
239
dump(true, false, true, "100", 1, "Compression level out of range");
240
dump(true, false, true, "abc", 1, "Invalid compress level");
241
dump(true, false, true, "", 1, "Fail: no number provided in option:");
242
}
243
244
private static void dump(boolean live, boolean explicitAll, boolean compressed) throws Exception {
245
dump(live, explicitAll, compressed, "1", 0, "Heap dump file created");
246
}
247
248
private static void dump(boolean live,
249
boolean explicitAll,
250
boolean compressed,
251
String compressLevel,
252
int expExitValue,
253
String expOutput) throws Exception {
254
String liveArg = "";
255
String fileArg = "";
256
String compressArg = "";
257
String allArgs = "-dump:";
258
259
if (live && explicitAll) {
260
fail("Illegal argument setting for jmap -dump");
261
}
262
if (live) {
263
liveArg = "live,";
264
}
265
if (explicitAll) {
266
liveArg = "all,";
267
}
268
269
String filePath = "jmap.dump" + System.currentTimeMillis() + ".hprof";
270
if (compressed) {
271
compressArg = "gz=" + compressLevel;
272
filePath = filePath + ".gz";
273
}
274
275
File file = new File(filePath);
276
if (file.exists()) {
277
file.delete();
278
}
279
fileArg = "file=" + file.getName() + ",";
280
281
OutputAnalyzer output;
282
allArgs = allArgs + liveArg + "format=b," + fileArg + compressArg;
283
output = jmap(allArgs);
284
output.shouldHaveExitValue(expExitValue);
285
output.shouldContain(expOutput);
286
if (expExitValue == 0) {
287
verifyDumpFile(file);
288
}
289
file.delete();
290
}
291
292
private static void verifyDumpFile(File dump) {
293
assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());
294
try {
295
File out = HprofParser.parse(dump);
296
297
assertTrue(out != null && out.exists() && out.isFile(),
298
"Could not find hprof parser output file");
299
List<String> lines = Files.readAllLines(out.toPath());
300
assertTrue(lines.size() > 0, "hprof parser output file is empty");
301
for (String line : lines) {
302
assertFalse(line.matches(".*WARNING(?!.*Failed to resolve " +
303
"object.*constantPoolOop.*).*"));
304
}
305
306
out.delete();
307
} catch (Exception e) {
308
e.printStackTrace();
309
fail("Could not parse dump file " + dump.getAbsolutePath());
310
}
311
}
312
313
private static OutputAnalyzer jmap(String... toolArgs) throws Exception {
314
JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jmap");
315
launcher.addVMArgs(Utils.getTestJavaOpts());
316
if (toolArgs != null) {
317
for (String toolArg : toolArgs) {
318
launcher.addToolArg(toolArg);
319
}
320
}
321
launcher.addToolArg(Long.toString(ProcessTools.getProcessId()));
322
323
processBuilder.command(launcher.getCommand());
324
System.out.println(Arrays.toString(processBuilder.command().toArray()));
325
OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
326
System.out.println(output.getOutput());
327
328
return output;
329
}
330
}
331
332