Skip to content

Commit

Permalink
8311775: [TEST] duplicate verifyHeapDump in several tests
Browse files Browse the repository at this point in the history
Reviewed-by: kevinw, amenkov, cjplummer
  • Loading branch information
y1yang0 committed Sep 4, 2023
1 parent 4f90aba commit 75d4ac2
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 143 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -21,8 +21,6 @@
* questions.
*/

import java.io.IOException;

import jdk.test.lib.dcmd.CommandExecutor;

/*
Expand All @@ -42,7 +40,7 @@ public HeapDumpAllTest() {
}

@Override
public void run(CommandExecutor executor, boolean overwrite) throws IOException {
public void run(CommandExecutor executor, boolean overwrite) throws Exception {
// Trigger gc by hand, so the created heap dump isnt't too large and
// takes too long to parse.
System.gc();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,31 +143,7 @@ public static void main(String[] args) throws Exception {
output = executor.execute("GC.heap_dump -gz=1 " + dump.getAbsolutePath());
output.shouldContain("Unable to create ");

verifyHeapDump(dump);
HprofParser.parseAndVerify(dump);
dump.delete();
}

private static void verifyHeapDump(File dump) throws Exception {

Asserts.assertTrue(dump.exists() && dump.isFile(),
"Could not create dump file " + dump.getAbsolutePath());

try {
File out = HprofParser.parse(dump);

Asserts.assertTrue(out != null && out.exists() && out.isFile(),
"Could not find hprof parser output file");
List<String> lines = Files.readAllLines(out.toPath());
Asserts.assertTrue(lines.size() > 0, "hprof parser output file is empty");
for (String line : lines) {
Asserts.assertFalse(line.matches(".*WARNING(?!.*Failed to resolve " +
"object.*constantPoolOop.*).*"));
}

out.delete();
} catch (Exception e) {
e.printStackTrace();
Asserts.fail("Could not parse dump file " + dump.getAbsolutePath());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

public class HeapDumpParallelTest {

private static void checkAndVerify(OutputAnalyzer dcmdOut, LingeredApp app, File heapDumpFile, boolean expectSerial) throws IOException {
private static void checkAndVerify(OutputAnalyzer dcmdOut, LingeredApp app, File heapDumpFile, boolean expectSerial) throws Exception {
dcmdOut.shouldHaveExitValue(0);
dcmdOut.shouldContain("Heap dump file created");
OutputAnalyzer appOut = new OutputAnalyzer(app.getProcessStdout());
Expand All @@ -64,7 +64,7 @@ private static void checkAndVerify(OutputAnalyzer dcmdOut, LingeredApp app, File
appOut.shouldNotContain("Dump heap objects in parallel");
appOut.shouldNotContain("Merge heap files complete");
}
verifyHeapDump(heapDumpFile);
HprofParser.parseAndVerify(heapDumpFile);
if (heapDumpFile.exists()) {
heapDumpFile.delete();
}
Expand Down Expand Up @@ -125,23 +125,4 @@ private static OutputAnalyzer attachJcmdHeapDump(File heapDumpFile, long lingere
PidJcmdExecutor executor = new PidJcmdExecutor("" + lingeredAppPid);
return executor.execute("GC.heap_dump " + arg + " " + heapDumpFile.getAbsolutePath());
}

private static void verifyHeapDump(File dump) {
Asserts.assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());
try {
File out = HprofParser.parse(dump);

Asserts.assertTrue(out != null && out.exists() && out.isFile(), "Could not find hprof parser output file");
List<String> lines = Files.readAllLines(out.toPath());
Asserts.assertTrue(lines.size() > 0, "hprof parser output file is empty");
for (String line : lines) {
Asserts.assertFalse(line.matches(".*WARNING(?!.*Failed to resolve object.*constantPoolOop.*).*"));
}

out.delete();
} catch (Exception e) {
e.printStackTrace();
Asserts.fail("Could not parse dump file " + dump.getAbsolutePath());
}
}
}
30 changes: 5 additions & 25 deletions test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -26,7 +26,6 @@

import java.io.File;
import java.nio.file.Files;
import java.io.IOException;
import java.util.List;

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

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

verifyHeapDump(dump);
HprofParser.parseAndVerify(dump);
dump.delete();
}

private void verifyHeapDump(File dump) {
Assert.assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());
try {
File out = HprofParser.parse(dump);

Assert.assertTrue(out != null && out.exists() && out.isFile(), "Could not find hprof parser output file");
List<String> lines = Files.readAllLines(out.toPath());
Assert.assertTrue(lines.size() > 0, "hprof parser output file is empty");
for (String line : lines) {
Assert.assertFalse(line.matches(".*WARNING(?!.*Failed to resolve object.*constantPoolOop.*).*"));
}

out.delete();
} catch (Exception e) {
e.printStackTrace();
Assert.fail("Could not parse dump file " + dump.getAbsolutePath());
}
}

/* GC.heap_dump is not available over JMX, running jcmd pid executor instead */
@Test
public void pid() throws IOException {
public void pid() throws Exception {
run(new PidJcmdExecutor(), false);
}

@Test
public void pidRewrite() throws IOException {
public void pidRewrite() throws Exception {
run(new PidJcmdExecutor(), true);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -22,11 +22,7 @@
*/

import java.io.File;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.util.stream.Collectors;
import java.io.FileInputStream;


import jdk.test.lib.apps.LingeredApp;
import jdk.test.lib.Asserts;
Expand All @@ -35,9 +31,7 @@
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.SA.SATestUtils;
import jdk.test.lib.Utils;
import jdk.test.lib.hprof.parser.HprofReader;
import jdk.test.lib.hprof.parser.PositionDataInputStream;
import jdk.test.lib.hprof.model.Snapshot;
import jdk.test.lib.hprof.HprofParser;

/**
* @test
Expand All @@ -55,30 +49,6 @@ public class TestHeapDumpForInvokeDynamic {

private static LingeredAppWithInvokeDynamic theApp = null;

private static void verifyHeapDump(String heapFile) {

File heapDumpFile = new File(heapFile);
Asserts.assertTrue(heapDumpFile.exists() && heapDumpFile.isFile(),
"Could not create dump file " + heapDumpFile.getAbsolutePath());
try (PositionDataInputStream in = new PositionDataInputStream(
new BufferedInputStream(new FileInputStream(heapFile)))) {
int i = in.readInt();
if (HprofReader.verifyMagicNumber(i)) {
Snapshot sshot;
HprofReader r = new HprofReader(heapFile, in, 0,
false, 0);
sshot = r.read();
} else {
throw new IOException("Unrecognized magic number: " + i);
}
} catch (Exception e) {
e.printStackTrace();
Asserts.fail("Could not read dump file " + heapFile);
} finally {
heapDumpFile.delete();
}
}

private static void attachDumpAndVerify(String heapDumpFileName,
long lingeredAppPid) throws Exception {

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

verifyHeapDump(heapDumpFileName);
HprofParser.parseAndVerify(new File(heapDumpFileName));
}

public static void main (String... args) throws Exception {
Expand Down
27 changes: 2 additions & 25 deletions test/jdk/sun/tools/jmap/BasicJMapTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -26,9 +26,7 @@
import static jdk.test.lib.Asserts.fail;

import java.io.File;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;

import jdk.test.lib.JDKToolLauncher;
import jdk.test.lib.Utils;
Expand Down Expand Up @@ -297,32 +295,11 @@ private static void dump(boolean live,
output.shouldHaveExitValue(expExitValue);
output.shouldContain(expOutput);
if (expExitValue == 0) {
verifyDumpFile(file);
HprofParser.parseAndVerify(file);
}
file.delete();
}

private static void verifyDumpFile(File dump) {
assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());
try {
File out = HprofParser.parse(dump);

assertTrue(out != null && out.exists() && out.isFile(),
"Could not find hprof parser output file");
List<String> lines = Files.readAllLines(out.toPath());
assertTrue(lines.size() > 0, "hprof parser output file is empty");
for (String line : lines) {
assertFalse(line.matches(".*WARNING(?!.*Failed to resolve " +
"object.*constantPoolOop.*).*"));
}

out.delete();
} catch (Exception e) {
e.printStackTrace();
fail("Could not parse dump file " + dump.getAbsolutePath());
}
}

private static OutputAnalyzer jmap(String... toolArgs) throws Exception {
JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jmap");
launcher.addVMArgs(Utils.getTestJavaOpts());
Expand Down
44 changes: 34 additions & 10 deletions test/lib/jdk/test/lib/hprof/HprofParser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -26,8 +26,13 @@
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import jdk.test.lib.Asserts;
import jdk.test.lib.hprof.model.Snapshot;
import jdk.test.lib.hprof.parser.Reader;

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

/**
* @see #parse(File, boolean, boolean, boolean)
* @see #parse(File, boolean, boolean, boolean, boolean)
*/
public static File parseAndVerify(File dump) throws Exception {
return parse(dump, false, true, true, true);
}

/**
* @see #parse(File, boolean, boolean, boolean, boolean)
*/
public static File parse(File dump) throws Exception {
return parse(dump, false, true, true);
return parse(dump, false, true, true, false);
}

/**
* @see #parse(File, boolean, boolean, boolean)
* @see #parse(File, boolean, boolean, boolean, boolean)
*/
public static File parseWithDebugInfo(File dump) throws Exception {
return parse(dump, true, true, true);
return parse(dump, true, true, true, false);
}

/**
Expand All @@ -68,10 +80,12 @@ public static File parseWithDebugInfo(File dump) throws Exception {
* @param debug Turn on/off debug file parsing
* @param callStack Turn on/off tracking of object allocation call stack
* @param calculateRefs Turn on/off tracking object allocation call stack
* @param verifyParse Verify output of parse process and fail if error occurred
* @throws Exception
* @return File containing output from the parser
*/
public static File parse(File dump, boolean debug, boolean callStack, boolean calculateRefs) throws Exception {
public static File parse(File dump, boolean debug, boolean callStack,
boolean calculateRefs, boolean verifyParse) throws Exception {
File out = new File("hprof." + System.currentTimeMillis() + ".out");
if (out.exists()) {
out.delete();
Expand All @@ -87,11 +101,21 @@ public static File parse(File dump, boolean debug, boolean callStack, boolean ca
snapshot.resolve(calculateRefs);
System.out.println("Snapshot resolved.");
}
} finally {
System.setOut(psSystemOut);
}

} finally {
System.setOut(psSystemOut);
}
if (verifyParse) {
verifyParse(out);
}
return out;
}

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

1 comment on commit 75d4ac2

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.