diff --git a/test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpAllTest.java b/test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpAllTest.java index bc8bc1f1f7d..bcf301458c2 100644 --- a/test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpAllTest.java +++ b/test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpAllTest.java @@ -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 @@ -21,8 +21,6 @@ * questions. */ -import java.io.IOException; - import jdk.test.lib.dcmd.CommandExecutor; /* @@ -42,7 +40,7 @@ public class HeapDumpAllTest extends HeapDumpTest { } @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(); diff --git a/test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpCompressedTest.java b/test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpCompressedTest.java index 7cef89f8c16..1e6d99a5048 100644 --- a/test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpCompressedTest.java +++ b/test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpCompressedTest.java @@ -143,31 +143,7 @@ public class HeapDumpCompressedTest { 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 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()); - } - } } diff --git a/test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpParallelTest.java b/test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpParallelTest.java index 0dfdf54152d..c07e1161592 100644 --- a/test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpParallelTest.java +++ b/test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpParallelTest.java @@ -47,7 +47,7 @@ import jdk.test.lib.hprof.HprofParser; 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()); @@ -64,7 +64,7 @@ public class HeapDumpParallelTest { appOut.shouldNotContain("Dump heap objects in parallel"); appOut.shouldNotContain("Merge heap files complete"); } - verifyHeapDump(heapDumpFile); + HprofParser.parseAndVerify(heapDumpFile); if (heapDumpFile.exists()) { heapDumpFile.delete(); } @@ -125,23 +125,4 @@ public class HeapDumpParallelTest { 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 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()); - } - } } \ No newline at end of file diff --git a/test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpTest.java b/test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpTest.java index c01bfa5df98..5635e04f489 100644 --- a/test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpTest.java +++ b/test/hotspot/jtreg/serviceability/dcmd/gc/HeapDumpTest.java @@ -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 @@ -26,7 +26,6 @@ import org.testng.Assert; import java.io.File; import java.nio.file.Files; -import java.io.IOException; import java.util.List; import jdk.test.lib.hprof.HprofParser; @@ -50,7 +49,7 @@ import jdk.test.lib.dcmd.PidJcmdExecutor; 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(); @@ -61,37 +60,18 @@ public class HeapDumpTest { 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 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); } } diff --git a/test/hotspot/jtreg/serviceability/sa/TestHeapDumpForInvokeDynamic.java b/test/hotspot/jtreg/serviceability/sa/TestHeapDumpForInvokeDynamic.java index a4c3d15750a..8022d9a0090 100644 --- a/test/hotspot/jtreg/serviceability/sa/TestHeapDumpForInvokeDynamic.java +++ b/test/hotspot/jtreg/serviceability/sa/TestHeapDumpForInvokeDynamic.java @@ -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 @@ -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; @@ -35,9 +31,7 @@ import jdk.test.lib.process.ProcessTools; 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 @@ -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 { @@ -101,7 +71,7 @@ public class TestHeapDumpForInvokeDynamic { SAOutput.shouldContain(heapDumpFileName); System.out.println(SAOutput.getOutput()); - verifyHeapDump(heapDumpFileName); + HprofParser.parseAndVerify(new File(heapDumpFileName)); } public static void main (String... args) throws Exception { diff --git a/test/jdk/sun/tools/jmap/BasicJMapTest.java b/test/jdk/sun/tools/jmap/BasicJMapTest.java index 393a02692fe..d8a24ef05fa 100644 --- a/test/jdk/sun/tools/jmap/BasicJMapTest.java +++ b/test/jdk/sun/tools/jmap/BasicJMapTest.java @@ -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 @@ -26,9 +26,7 @@ import static jdk.test.lib.Asserts.assertFalse; 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; @@ -297,32 +295,11 @@ public class BasicJMapTest { 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 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()); diff --git a/test/lib/jdk/test/lib/hprof/HprofParser.java b/test/lib/jdk/test/lib/hprof/HprofParser.java index 88841f9e6d4..96b53c304b7 100644 --- a/test/lib/jdk/test/lib/hprof/HprofParser.java +++ b/test/lib/jdk/test/lib/hprof/HprofParser.java @@ -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 @@ -26,8 +26,13 @@ package jdk.test.lib.hprof; 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; @@ -48,17 +53,24 @@ public class HprofParser { } /** - * @see #parse(File, boolean, boolean, boolean) + * @see #parse(File, boolean, boolean, boolean, boolean) */ - public static File parse(File dump) throws Exception { - return parse(dump, false, true, true); + public static File parseAndVerify(File dump) throws Exception { + return parse(dump, false, true, true, true); } /** - * @see #parse(File, boolean, boolean, boolean) + * @see #parse(File, boolean, boolean, boolean, boolean) + */ + public static File parse(File dump) throws Exception { + return parse(dump, false, true, true, false); + } + + /** + * @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); } /** @@ -68,10 +80,12 @@ public class HprofParser { * @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(); @@ -87,11 +101,21 @@ public class HprofParser { 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 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.*).*")); + } + } }