Path: blob/master/test/hotspot/jtreg/serviceability/sa/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*/2223/**24* @test25* @summary Test deadlock detection26* @requires vm.hasSA27* @library /test/lib28* @modules java.base/jdk.internal.misc29* @modules java.management30* @run main DeadlockDetectionTest31*/3233import java.util.stream.Collectors;3435import jdk.test.lib.apps.LingeredApp;36import jdk.test.lib.apps.LingeredAppWithDeadlock;37import jdk.test.lib.JDKToolLauncher;38import jdk.test.lib.process.OutputAnalyzer;39import jdk.test.lib.process.ProcessTools;40import jdk.test.lib.SA.SATestUtils;41import jdk.test.lib.Utils;4243import jtreg.SkippedException;4445public class DeadlockDetectionTest {4647private static LingeredAppWithDeadlock theApp = null;4849private static OutputAnalyzer jstack(String... toolArgs) throws Exception {50JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jhsdb");51launcher.addVMArgs(Utils.getTestJavaOpts());52launcher.addToolArg("jstack");53if (toolArgs != null) {54for (String toolArg : toolArgs) {55launcher.addToolArg(toolArg);56}57}5859ProcessBuilder processBuilder = SATestUtils.createProcessBuilder(launcher);60System.out.println(processBuilder.command().stream().collect(Collectors.joining(" ")));61OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);62System.out.println(output.getOutput());6364return output;65}6667public static void main(String[] args) throws Exception {68SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.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 {78theApp = new LingeredAppWithDeadlock();79LingeredApp.startApp(theApp, "-XX:+UsePerfData");80OutputAnalyzer output = jstack("--pid", Long.toString(theApp.getPid()));81System.out.println(output.getOutput());8283if (output.getExitValue() == 3) {84throw new SkippedException("Test can't run for some reason");85} else {86output.shouldHaveExitValue(0);87output.shouldContain("Found a total of 1 deadlock.");88}89} finally {90LingeredApp.stopApp(theApp);91}92}93}949596