Path: blob/master/test/hotspot/jtreg/serviceability/sa/TestHeapDumpForInvokeDynamic.java
41149 views
/*1* Copyright (c) 2016, 2020, 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.io.File;24import java.io.IOException;25import java.io.BufferedInputStream;26import java.util.stream.Collectors;27import java.io.FileInputStream;282930import jdk.test.lib.apps.LingeredApp;31import jdk.test.lib.Asserts;32import jdk.test.lib.JDKToolLauncher;33import jdk.test.lib.process.ProcessTools;34import jdk.test.lib.process.OutputAnalyzer;35import jdk.test.lib.SA.SATestUtils;36import jdk.test.lib.Utils;37import jdk.test.lib.hprof.parser.HprofReader;38import jdk.test.lib.hprof.parser.PositionDataInputStream;39import jdk.test.lib.hprof.model.Snapshot;4041/**42* @test43* @library /test/lib44* @requires vm.hasSA45* @modules java.base/jdk.internal.misc46* jdk.hotspot.agent/sun.jvm.hotspot47* jdk.hotspot.agent/sun.jvm.hotspot.utilities48* jdk.hotspot.agent/sun.jvm.hotspot.oops49* jdk.hotspot.agent/sun.jvm.hotspot.debugger50* @run main/othervm TestHeapDumpForInvokeDynamic51*/5253public class TestHeapDumpForInvokeDynamic {5455private static LingeredAppWithInvokeDynamic theApp = null;5657private static void verifyHeapDump(String heapFile) {5859File heapDumpFile = new File(heapFile);60Asserts.assertTrue(heapDumpFile.exists() && heapDumpFile.isFile(),61"Could not create dump file " + heapDumpFile.getAbsolutePath());62try (PositionDataInputStream in = new PositionDataInputStream(63new BufferedInputStream(new FileInputStream(heapFile)))) {64int i = in.readInt();65if (HprofReader.verifyMagicNumber(i)) {66Snapshot sshot;67HprofReader r = new HprofReader(heapFile, in, 0,68false, 0);69sshot = r.read();70} else {71throw new IOException("Unrecognized magic number: " + i);72}73} catch (Exception e) {74e.printStackTrace();75Asserts.fail("Could not read dump file " + heapFile);76} finally {77heapDumpFile.delete();78}79}8081private static void attachDumpAndVerify(String heapDumpFileName,82long lingeredAppPid) throws Exception {8384JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");85launcher.addVMArgs(Utils.getTestJavaOpts());86launcher.addToolArg("jmap");87launcher.addToolArg("--binaryheap");88launcher.addToolArg("--dumpfile");89launcher.addToolArg(heapDumpFileName);90launcher.addToolArg("--pid");91launcher.addToolArg(Long.toString(lingeredAppPid));9293ProcessBuilder processBuilder = SATestUtils.createProcessBuilder(launcher);94System.out.println(95processBuilder.command().stream().collect(Collectors.joining(" ")));9697OutputAnalyzer SAOutput = ProcessTools.executeProcess(processBuilder);98SAOutput.shouldHaveExitValue(0);99SAOutput.shouldContain("heap written to");100SAOutput.shouldContain(heapDumpFileName);101System.out.println(SAOutput.getOutput());102103verifyHeapDump(heapDumpFileName);104}105106public static void main (String... args) throws Exception {107SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.108String heapDumpFileName = "lambdaHeapDump.bin";109110File heapDumpFile = new File(heapDumpFileName);111if (heapDumpFile.exists()) {112heapDumpFile.delete();113}114115try {116theApp = new LingeredAppWithInvokeDynamic();117LingeredApp.startApp(theApp, "-XX:+UsePerfData", "-Xmx512m");118attachDumpAndVerify(heapDumpFileName, theApp.getPid());119} finally {120LingeredApp.stopApp(theApp);121}122}123}124125126