Path: blob/master/test/jdk/sun/tools/jhsdb/HeapDumpTest.java
41152 views
/*1* Copyright (c) 2016, 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*/2223/**24* @test25* @bug 816334626* @summary Test hashing of extended characters in Serviceability Agent.27* @requires vm.hasSA28* @library /test/lib29* @compile -encoding utf8 HeapDumpTest.java30* @run main/timeout=240 HeapDumpTest31*/3233import static jdk.test.lib.Asserts.assertTrue;3435import java.io.IOException;36import java.io.File;37import java.util.List;38import java.util.Arrays;3940import jdk.test.lib.Utils;41import jdk.test.lib.apps.LingeredApp;42import jdk.test.lib.hprof.parser.HprofReader;43import jdk.test.lib.JDKToolLauncher;44import jdk.test.lib.process.OutputAnalyzer;45import jdk.test.lib.process.ProcessTools;46import jdk.test.lib.SA.SATestUtils;4748public class HeapDumpTest {4950private static LingeredAppWithExtendedChars theApp = null;51private static final String SUCCESS_STRING = "heap written to";52/**53*54* @param vmArgs - tool arguments to launch jhsdb55* @return exit code of tool56*/57public static void launch(int expectedExitValue, List<String> toolArgs)58throws IOException {5960System.out.println("Starting LingeredApp");61try {62theApp = new LingeredAppWithExtendedChars();63LingeredApp.startApp(theApp, "-Xmx256m");6465System.out.println(theApp.\u00CB);66System.out.println("Starting " + toolArgs.get(0) + " against " + theApp.getPid());67JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");68launcher.addVMArgs(Utils.getFilteredTestJavaOpts("-Xcomp"));6970for (String cmd : toolArgs) {71launcher.addToolArg(cmd);72}7374launcher.addToolArg("--pid=" + Long.toString(theApp.getPid()));7576ProcessBuilder processBuilder = SATestUtils.createProcessBuilder(launcher);77processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);78OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);79System.out.println("stdout:");80System.out.println(output.getStdout());81System.out.println("stderr:");82System.out.println(output.getStderr());83output.shouldHaveExitValue(expectedExitValue);84if (expectedExitValue == 0) {85output.shouldContain(SUCCESS_STRING);86} else {87output.stdoutShouldNotContain(SUCCESS_STRING);88}89} catch (Exception ex) {90throw new RuntimeException("Test ERROR " + ex, ex);91} finally {92LingeredApp.stopApp(theApp);93}94}9596public static void launch(int expectedExitValue, String... toolArgs)97throws IOException {98launch(expectedExitValue, Arrays.asList(toolArgs));99}100101public static void printStackTraces(String file) throws IOException {102try {103String output = HprofReader.getStack(file, 0);104if (!output.contains("LingeredApp.steadyState")) {105throw new RuntimeException("'LingeredApp.steadyState' missing from stdout/stderr");106}107} catch (Exception ex) {108throw new RuntimeException("Test ERROR " + ex, ex);109}110}111112public static void testHeapDump(SubTest subtest) throws IOException {113String gzOption = subtest.getGzOption();114boolean checkSuccess = subtest.needCheckSuccess();115int expectedExitValue = checkSuccess ? 0 : 1;116117File dump = new File("jhsdb.jmap.heap." +118System.currentTimeMillis() + ".hprof");119if (dump.exists()) {120dump.delete();121}122if (gzOption == null || gzOption.length() == 0) {123launch(expectedExitValue, "jmap",124"--binaryheap", "--dumpfile=" + dump.getAbsolutePath());125} else {126launch(expectedExitValue, "jmap",127"--binaryheap", gzOption, "--dumpfile=" + dump.getAbsolutePath());128}129130if (checkSuccess) {131assertTrue(dump.exists() && dump.isFile(),132"Could not create dump file " + dump.getAbsolutePath());133134printStackTraces(dump.getAbsolutePath());135dump.delete();136} else {137assertTrue(!dump.exists(), "Unexpected file created: " + dump.getAbsolutePath());138}139}140141public static void main(String[] args) throws Exception {142SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.143SubTest[] subtests = new SubTest[] {144new SubTest("", true/*checkSuccess*/),145new SubTest("--gz=1", true),146new SubTest("--gz=9", true),147new SubTest("--gz=0", false),148new SubTest("--gz=100", false),149new SubTest("--gz=", false),150new SubTest("--gz", false),151};152// Run subtests153for (int i = 0; i < subtests.length;i++) {154testHeapDump(subtests[i]);155}156// The test throws RuntimeException on error.157// IOException is thrown if LingeredApp can't start because of some bad158// environment condition159System.out.println("Test PASSED");160}161162private static class SubTest {163private String gzOption;164boolean needCheckSuccess;165166public SubTest(String gzOpt, boolean checkSuccess) {167gzOption = gzOpt;168needCheckSuccess = checkSuccess;169}170171public String getGzOption() { return gzOption; }172public boolean needCheckSuccess() { return needCheckSuccess; }173}174}175176177