Path: blob/master/test/jdk/sun/tools/jstack/DeadlockDetectionTest.java
41149 views
/*1* Copyright (c) 2015, 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.util.ArrayList;24import java.util.List;25import java.util.stream.Collectors;2627import jdk.test.lib.apps.LingeredApp;28import jdk.test.lib.apps.LingeredAppWithDeadlock;29import jdk.test.lib.JDKToolLauncher;30import jdk.test.lib.process.OutputAnalyzer;31import jdk.test.lib.process.ProcessTools;32import jdk.test.lib.Utils;3334import jtreg.SkippedException;3536/**37* @test38* @summary Test deadlock detection39* @requires vm.hasSA40* @library /test/lib41* @build jdk.test.lib.apps.*42* @build DeadlockDetectionTest43* @run main DeadlockDetectionTest44*/45public class DeadlockDetectionTest {4647private static LingeredAppWithDeadlock theApp = null;48private static ProcessBuilder processBuilder = new ProcessBuilder();4950private static OutputAnalyzer jstack(String... toolArgs) throws Exception {51JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jstack");52launcher.addVMArgs(Utils.getFilteredTestJavaOpts("-XX:+UsePerfData"));53launcher.addVMArg("-XX:+UsePerfData");54if (toolArgs != null) {55for (String toolArg : toolArgs) {56launcher.addToolArg(toolArg);57}58}5960processBuilder.command(launcher.getCommand());61System.out.println(processBuilder.command().stream().collect(Collectors.joining(" ")));62OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);63System.out.println(output.getOutput());6465return output;66}6768public static void main(String[] args) throws Exception {69System.out.println("Starting DeadlockDetectionTest");7071if (!LingeredApp.isLastModifiedWorking()) {72// Exact behaviour of the test depends on operating system and the test nature,73// so just print the warning and continue74System.err.println("Warning! Last modified time doesn't work.");75}7677try {78String[] vmArgs = Utils.appendTestJavaOpts("-XX:+UsePerfData");7980theApp = new LingeredAppWithDeadlock();81LingeredApp.startApp(theApp, vmArgs);82OutputAnalyzer output = jstack(Long.toString(theApp.getPid()));83System.out.println(output.getOutput());8485if (output.getExitValue() == 3) {86throw new SkippedException("Test can't run for some reason");87} else {88output.shouldHaveExitValue(0);89output.shouldContain("Found 1 deadlock.");90}91} finally {92LingeredApp.stopApp(theApp);93}94}95}969798