Path: blob/master/test/hotspot/jtreg/serviceability/sa/ClhsdbDumpheap.java
41152 views
/*1* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.HashMap;24import java.util.List;25import java.util.Map;26import java.io.File;27import java.io.FileInputStream;28import java.io.FileOutputStream;2930import static jdk.test.lib.Asserts.assertTrue;31import static jdk.test.lib.Asserts.assertFalse;32import jdk.test.lib.hprof.HprofParser;33import jdk.test.lib.apps.LingeredApp;34import jdk.test.lib.hprof.parser.HprofReader;35import jtreg.SkippedException;3637/**38* @test39* @bug 824098940* @summary Test clhsdb dumpheap command41* @requires vm.hasSA42* @library /test/lib43* @run main/othervm/timeout=240 ClhsdbDumpheap44*/4546public class ClhsdbDumpheap {47// The default heap dump file name defined in JDK.48private static final String HEAP_DUMP_FILENAME_DEFAULT = "heap.bin";49private static final String HEAP_DUMP_GZIPED_FILENAME_DEFAULT = "heap.bin.gz";5051public static void printStackTraces(String file) {52try {53System.out.println("HprofReader.getStack() output:");54String output = HprofReader.getStack(file, 0);55if (!output.contains("LingeredApp.steadyState")) {56throw new RuntimeException("'LingeredApp.steadyState' missing from stdout/stderr");57}58} catch (Exception ex) {59throw new RuntimeException("Test ERROR " + ex, ex);60}61}6263private static void verifyDumpFile(File dump) throws Exception {64assertTrue(dump.exists() && dump.isFile(), "Could not create dump file " + dump.getAbsolutePath());65printStackTraces(dump.getAbsolutePath());66}6768private static class SubTest {69private String cmd;70private String fileName;71private String expectedOutput;72boolean compression;73boolean needVerify;7475public SubTest(String comm, String fName, boolean isComp, boolean verify, String expected) {76cmd = comm;77fileName = fName;78expectedOutput = expected;79compression = isComp;80needVerify = verify;81}8283public String getCmd() { return cmd; }84public String getFileName() { return fileName; }85public String getExpectedOutput() { return expectedOutput; }86public boolean isCompression() { return compression; }87public boolean needVerify() { return needVerify; }88}8990private static void runTest(long appPid, SubTest subtest) throws Exception {91ClhsdbLauncher test = new ClhsdbLauncher();92String fileName = subtest.getFileName();93String cmd = subtest.getCmd();94String expectedOutput = subtest.getExpectedOutput();95boolean compression = subtest.isCompression();96/* The expected generated file, used to distinguish with fileName in case fileName is blank or null */97String expectedFileName = fileName;98if (fileName == null || fileName.length() == 0) {99if (!compression) {100expectedFileName = HEAP_DUMP_FILENAME_DEFAULT;101} else {102expectedFileName = HEAP_DUMP_GZIPED_FILENAME_DEFAULT;103}104}105assertTrue (expectedFileName != null && expectedFileName.length() > 0,106"Expected generated file name must have value");107File file = new File(expectedFileName);108if (file.exists()) {109file.delete();110}111String command = cmd + fileName;112List<String> cmds = List.of(command);113Map<String, List<String>> expStrMap = new HashMap<>();114expStrMap.put(command, List.of(expectedOutput));115test.run(appPid, cmds, expStrMap, null);116if (subtest.needVerify()) {117verifyDumpFile(file);118}119file.delete();120}121122public static void main(String[] args) throws Exception {123System.out.println("Starting ClhsdbDumpheap test");124125LingeredApp theApp = null;126try {127// Use file name different with JDK's default value "heap.bin".128String heapDumpFileName = "heapdump.bin";129String heapDumpFileNameGz = "heapdump.bin.gz";130131theApp = new LingeredApp();132LingeredApp.startApp(theApp);133System.out.println("Started LingeredApp with pid " + theApp.getPid());134135SubTest[] subtests = new SubTest[] {136new SubTest("dumpheap ", heapDumpFileName, false/*compression*/, true,/*verify*/137"heap written to " + heapDumpFileName),138new SubTest("dumpheap gz=1 ", heapDumpFileNameGz, true, true,139"heap written to " + heapDumpFileNameGz),140new SubTest("dumpheap gz=9 ", heapDumpFileNameGz, true, true,141"heap written to " + heapDumpFileNameGz),142new SubTest("dumpheap gz=0 ", heapDumpFileNameGz, true, false,143"Usage: dumpheap \\[gz=<1-9>\\] \\[filename\\]"),144new SubTest("dumpheap gz=100 ", heapDumpFileNameGz, true, false,145"Usage: dumpheap \\[gz=<1-9>\\] \\[filename\\]"),146new SubTest("dumpheap gz= ", heapDumpFileNameGz, true, false,147"Usage: dumpheap \\[gz=<1-9>\\] \\[filename\\]"),148new SubTest("dumpheap gz ", heapDumpFileNameGz, true, false,149"Usage: dumpheap \\[gz=<1-9>\\] \\[filename\\]"),150new SubTest("dumpheap", "", false, true,151"heap written to " + HEAP_DUMP_FILENAME_DEFAULT),152new SubTest("dumpheap gz=1", "", true, true,153"heap written to " + HEAP_DUMP_GZIPED_FILENAME_DEFAULT),154new SubTest("dumpheap gz=9", "", true, true,155"heap written to " + HEAP_DUMP_GZIPED_FILENAME_DEFAULT),156new SubTest("dumpheap gz=0", "", true, false,157"Usage: dumpheap \\[gz=<1-9>\\] \\[filename\\]"),158new SubTest("dumpheap gz=100", "", true, false,159"Usage: dumpheap \\[gz=<1-9>\\] \\[filename\\]"),160// Command "dumpheap gz=".161new SubTest("dumpheap ", "gz=", true, false,162"Usage: dumpheap \\[gz=<1-9>\\] \\[filename\\]"),163// Command "dumpheap gz".164new SubTest("dumpheap ", "gz", false, true, "heap written to gz"),165// Command "dump heap gz=1 gz=2".166new SubTest("dumpheap gz=1", "gz=2", true, false,167"Usage: dumpheap \\[gz=<1-9>\\] \\[filename\\]")168};169// Run subtests170for (int i = 0; i < subtests.length;i++) {171runTest(theApp.getPid(), subtests[i]);172}173} catch (SkippedException se) {174throw se;175} catch (Exception ex) {176throw new RuntimeException("Test ERROR " + ex, ex);177} finally {178LingeredApp.stopApp(theApp);179}180System.out.println("Test PASSED");181}182}183184185