Skip to content

Commit 75d4ac2

Browse files
committed
8311775: [TEST] duplicate verifyHeapDump in several tests
Reviewed-by: kevinw, amenkov, cjplummer
1 parent 4f90aba commit 75d4ac2

File tree

7 files changed

+49
-143
lines changed

7 files changed

+49
-143
lines changed

test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpAllTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,8 +21,6 @@
2121
* questions.
2222
*/
2323

24-
import java.io.IOException;
25-
2624
import jdk.test.lib.dcmd.CommandExecutor;
2725

2826
/*
@@ -42,7 +40,7 @@ public HeapDumpAllTest() {
4240
}
4341

4442
@Override
45-
public void run(CommandExecutor executor, boolean overwrite) throws IOException {
43+
public void run(CommandExecutor executor, boolean overwrite) throws Exception {
4644
// Trigger gc by hand, so the created heap dump isnt't too large and
4745
// takes too long to parse.
4846
System.gc();

test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpCompressedTest.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -143,31 +143,7 @@ public static void main(String[] args) throws Exception {
143143
output = executor.execute("GC.heap_dump -gz=1 " + dump.getAbsolutePath());
144144
output.shouldContain("Unable to create ");
145145

146-
verifyHeapDump(dump);
146+
HprofParser.parseAndVerify(dump);
147147
dump.delete();
148148
}
149-
150-
private static void verifyHeapDump(File dump) throws Exception {
151-
152-
Asserts.assertTrue(dump.exists() && dump.isFile(),
153-
"Could not create dump file " + dump.getAbsolutePath());
154-
155-
try {
156-
File out = HprofParser.parse(dump);
157-
158-
Asserts.assertTrue(out != null && out.exists() && out.isFile(),
159-
"Could not find hprof parser output file");
160-
List<String> lines = Files.readAllLines(out.toPath());
161-
Asserts.assertTrue(lines.size() > 0, "hprof parser output file is empty");
162-
for (String line : lines) {
163-
Asserts.assertFalse(line.matches(".*WARNING(?!.*Failed to resolve " +
164-
"object.*constantPoolOop.*).*"));
165-
}
166-
167-
out.delete();
168-
} catch (Exception e) {
169-
e.printStackTrace();
170-
Asserts.fail("Could not parse dump file " + dump.getAbsolutePath());
171-
}
172-
}
173149
}

test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpParallelTest.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
public class HeapDumpParallelTest {
4949

50-
private static void checkAndVerify(OutputAnalyzer dcmdOut, LingeredApp app, File heapDumpFile, boolean expectSerial) throws IOException {
50+
private static void checkAndVerify(OutputAnalyzer dcmdOut, LingeredApp app, File heapDumpFile, boolean expectSerial) throws Exception {
5151
dcmdOut.shouldHaveExitValue(0);
5252
dcmdOut.shouldContain("Heap dump file created");
5353
OutputAnalyzer appOut = new OutputAnalyzer(app.getProcessStdout());
@@ -64,7 +64,7 @@ private static void checkAndVerify(OutputAnalyzer dcmdOut, LingeredApp app, File
6464
appOut.shouldNotContain("Dump heap objects in parallel");
6565
appOut.shouldNotContain("Merge heap files complete");
6666
}
67-
verifyHeapDump(heapDumpFile);
67+
HprofParser.parseAndVerify(heapDumpFile);
6868
if (heapDumpFile.exists()) {
6969
heapDumpFile.delete();
7070
}
@@ -125,23 +125,4 @@ private static OutputAnalyzer attachJcmdHeapDump(File heapDumpFile, long lingere
125125
PidJcmdExecutor executor = new PidJcmdExecutor("" + lingeredAppPid);
126126
return executor.execute("GC.heap_dump " + arg + " " + heapDumpFile.getAbsolutePath());
127127
}
128-
129-
private static void verifyHeapDump(File dump) {
130-
Asserts.assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());
131-
try {
132-
File out = HprofParser.parse(dump);
133-
134-
Asserts.assertTrue(out != null && out.exists() && out.isFile(), "Could not find hprof parser output file");
135-
List<String> lines = Files.readAllLines(out.toPath());
136-
Asserts.assertTrue(lines.size() > 0, "hprof parser output file is empty");
137-
for (String line : lines) {
138-
Asserts.assertFalse(line.matches(".*WARNING(?!.*Failed to resolve object.*constantPoolOop.*).*"));
139-
}
140-
141-
out.delete();
142-
} catch (Exception e) {
143-
e.printStackTrace();
144-
Asserts.fail("Could not parse dump file " + dump.getAbsolutePath());
145-
}
146-
}
147128
}

test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpTest.java

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,7 +26,6 @@
2626

2727
import java.io.File;
2828
import java.nio.file.Files;
29-
import java.io.IOException;
3029
import java.util.List;
3130

3231
import jdk.test.lib.hprof.HprofParser;
@@ -50,7 +49,7 @@
5049
public class HeapDumpTest {
5150
protected String heapDumpArgs = "";
5251

53-
public void run(CommandExecutor executor, boolean overwrite) throws IOException {
52+
public void run(CommandExecutor executor, boolean overwrite) throws Exception {
5453
File dump = new File("jcmd.gc.heap_dump." + System.currentTimeMillis() + ".hprof");
5554
if (!overwrite && dump.exists()) {
5655
dump.delete();
@@ -61,37 +60,18 @@ public void run(CommandExecutor executor, boolean overwrite) throws IOException
6160
String cmd = "GC.heap_dump " + (overwrite ? "-overwrite " : "") + heapDumpArgs + " " + dump.getAbsolutePath();
6261
executor.execute(cmd);
6362

64-
verifyHeapDump(dump);
63+
HprofParser.parseAndVerify(dump);
6564
dump.delete();
6665
}
6766

68-
private void verifyHeapDump(File dump) {
69-
Assert.assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());
70-
try {
71-
File out = HprofParser.parse(dump);
72-
73-
Assert.assertTrue(out != null && out.exists() && out.isFile(), "Could not find hprof parser output file");
74-
List<String> lines = Files.readAllLines(out.toPath());
75-
Assert.assertTrue(lines.size() > 0, "hprof parser output file is empty");
76-
for (String line : lines) {
77-
Assert.assertFalse(line.matches(".*WARNING(?!.*Failed to resolve object.*constantPoolOop.*).*"));
78-
}
79-
80-
out.delete();
81-
} catch (Exception e) {
82-
e.printStackTrace();
83-
Assert.fail("Could not parse dump file " + dump.getAbsolutePath());
84-
}
85-
}
86-
8767
/* GC.heap_dump is not available over JMX, running jcmd pid executor instead */
8868
@Test
89-
public void pid() throws IOException {
69+
public void pid() throws Exception {
9070
run(new PidJcmdExecutor(), false);
9171
}
9272

9373
@Test
94-
public void pidRewrite() throws IOException {
74+
public void pidRewrite() throws Exception {
9575
run(new PidJcmdExecutor(), true);
9676
}
9777
}

test/hotspot/jtreg/serviceability/sa/TestHeapDumpForInvokeDynamic.java

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -22,11 +22,7 @@
2222
*/
2323

2424
import java.io.File;
25-
import java.io.IOException;
26-
import java.io.BufferedInputStream;
2725
import java.util.stream.Collectors;
28-
import java.io.FileInputStream;
29-
3026

3127
import jdk.test.lib.apps.LingeredApp;
3228
import jdk.test.lib.Asserts;
@@ -35,9 +31,7 @@
3531
import jdk.test.lib.process.OutputAnalyzer;
3632
import jdk.test.lib.SA.SATestUtils;
3733
import jdk.test.lib.Utils;
38-
import jdk.test.lib.hprof.parser.HprofReader;
39-
import jdk.test.lib.hprof.parser.PositionDataInputStream;
40-
import jdk.test.lib.hprof.model.Snapshot;
34+
import jdk.test.lib.hprof.HprofParser;
4135

4236
/**
4337
* @test
@@ -55,30 +49,6 @@ public class TestHeapDumpForInvokeDynamic {
5549

5650
private static LingeredAppWithInvokeDynamic theApp = null;
5751

58-
private static void verifyHeapDump(String heapFile) {
59-
60-
File heapDumpFile = new File(heapFile);
61-
Asserts.assertTrue(heapDumpFile.exists() && heapDumpFile.isFile(),
62-
"Could not create dump file " + heapDumpFile.getAbsolutePath());
63-
try (PositionDataInputStream in = new PositionDataInputStream(
64-
new BufferedInputStream(new FileInputStream(heapFile)))) {
65-
int i = in.readInt();
66-
if (HprofReader.verifyMagicNumber(i)) {
67-
Snapshot sshot;
68-
HprofReader r = new HprofReader(heapFile, in, 0,
69-
false, 0);
70-
sshot = r.read();
71-
} else {
72-
throw new IOException("Unrecognized magic number: " + i);
73-
}
74-
} catch (Exception e) {
75-
e.printStackTrace();
76-
Asserts.fail("Could not read dump file " + heapFile);
77-
} finally {
78-
heapDumpFile.delete();
79-
}
80-
}
81-
8252
private static void attachDumpAndVerify(String heapDumpFileName,
8353
long lingeredAppPid) throws Exception {
8454

@@ -101,7 +71,7 @@ private static void attachDumpAndVerify(String heapDumpFileName,
10171
SAOutput.shouldContain(heapDumpFileName);
10272
System.out.println(SAOutput.getOutput());
10373

104-
verifyHeapDump(heapDumpFileName);
74+
HprofParser.parseAndVerify(new File(heapDumpFileName));
10575
}
10676

10777
public static void main (String... args) throws Exception {

test/jdk/sun/tools/jmap/BasicJMapTest.java

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,9 +26,7 @@
2626
import static jdk.test.lib.Asserts.fail;
2727

2828
import java.io.File;
29-
import java.nio.file.Files;
3029
import java.util.Arrays;
31-
import java.util.List;
3230

3331
import jdk.test.lib.JDKToolLauncher;
3432
import jdk.test.lib.Utils;
@@ -297,32 +295,11 @@ private static void dump(boolean live,
297295
output.shouldHaveExitValue(expExitValue);
298296
output.shouldContain(expOutput);
299297
if (expExitValue == 0) {
300-
verifyDumpFile(file);
298+
HprofParser.parseAndVerify(file);
301299
}
302300
file.delete();
303301
}
304302

305-
private static void verifyDumpFile(File dump) {
306-
assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());
307-
try {
308-
File out = HprofParser.parse(dump);
309-
310-
assertTrue(out != null && out.exists() && out.isFile(),
311-
"Could not find hprof parser output file");
312-
List<String> lines = Files.readAllLines(out.toPath());
313-
assertTrue(lines.size() > 0, "hprof parser output file is empty");
314-
for (String line : lines) {
315-
assertFalse(line.matches(".*WARNING(?!.*Failed to resolve " +
316-
"object.*constantPoolOop.*).*"));
317-
}
318-
319-
out.delete();
320-
} catch (Exception e) {
321-
e.printStackTrace();
322-
fail("Could not parse dump file " + dump.getAbsolutePath());
323-
}
324-
}
325-
326303
private static OutputAnalyzer jmap(String... toolArgs) throws Exception {
327304
JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jmap");
328305
launcher.addVMArgs(Utils.getTestJavaOpts());

test/lib/jdk/test/lib/hprof/HprofParser.java

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,8 +26,13 @@
2626
import java.io.BufferedOutputStream;
2727
import java.io.File;
2828
import java.io.FileOutputStream;
29+
import java.io.IOException;
2930
import java.io.PrintStream;
31+
import java.nio.file.Files;
32+
import java.nio.file.Paths;
33+
import java.util.List;
3034

35+
import jdk.test.lib.Asserts;
3136
import jdk.test.lib.hprof.model.Snapshot;
3237
import jdk.test.lib.hprof.parser.Reader;
3338

@@ -48,17 +53,24 @@ public static void main(String[] args) throws Exception {
4853
}
4954

5055
/**
51-
* @see #parse(File, boolean, boolean, boolean)
56+
* @see #parse(File, boolean, boolean, boolean, boolean)
57+
*/
58+
public static File parseAndVerify(File dump) throws Exception {
59+
return parse(dump, false, true, true, true);
60+
}
61+
62+
/**
63+
* @see #parse(File, boolean, boolean, boolean, boolean)
5264
*/
5365
public static File parse(File dump) throws Exception {
54-
return parse(dump, false, true, true);
66+
return parse(dump, false, true, true, false);
5567
}
5668

5769
/**
58-
* @see #parse(File, boolean, boolean, boolean)
70+
* @see #parse(File, boolean, boolean, boolean, boolean)
5971
*/
6072
public static File parseWithDebugInfo(File dump) throws Exception {
61-
return parse(dump, true, true, true);
73+
return parse(dump, true, true, true, false);
6274
}
6375

6476
/**
@@ -68,10 +80,12 @@ public static File parseWithDebugInfo(File dump) throws Exception {
6880
* @param debug Turn on/off debug file parsing
6981
* @param callStack Turn on/off tracking of object allocation call stack
7082
* @param calculateRefs Turn on/off tracking object allocation call stack
83+
* @param verifyParse Verify output of parse process and fail if error occurred
7184
* @throws Exception
7285
* @return File containing output from the parser
7386
*/
74-
public static File parse(File dump, boolean debug, boolean callStack, boolean calculateRefs) throws Exception {
87+
public static File parse(File dump, boolean debug, boolean callStack,
88+
boolean calculateRefs, boolean verifyParse) throws Exception {
7589
File out = new File("hprof." + System.currentTimeMillis() + ".out");
7690
if (out.exists()) {
7791
out.delete();
@@ -87,11 +101,21 @@ public static File parse(File dump, boolean debug, boolean callStack, boolean ca
87101
snapshot.resolve(calculateRefs);
88102
System.out.println("Snapshot resolved.");
89103
}
90-
} finally {
91-
System.setOut(psSystemOut);
92-
}
93-
104+
} finally {
105+
System.setOut(psSystemOut);
106+
}
107+
if (verifyParse) {
108+
verifyParse(out);
109+
}
94110
return out;
95111
}
96112

113+
private static void verifyParse(File out) throws IOException {
114+
Asserts.assertTrue(out != null && out.exists() && out.isFile(), "Could not find hprof parser output file");
115+
List<String> lines = Files.readAllLines(out.toPath());
116+
Asserts.assertTrue(lines.size() > 0, "hprof parser output file is empty");
117+
for (String line : lines) {
118+
Asserts.assertFalse(line.matches(".*WARNING(?!.*Failed to resolve object.*constantPoolOop.*).*"));
119+
}
120+
}
97121
}

0 commit comments

Comments
 (0)