jdk/test/sun/tools/jstack/DeadlockDetectionTest.java
author dsamersoff
Mon, 09 May 2016 23:41:59 +0300
changeset 38360 fb63be22ffa6
parent 33832 7dde7a271492
child 40686 1b855fa5c9c5
permissions -rw-r--r--
8155091: Remove SA related functions from tmtools Summary: Remove options that enables support for non-cooperative mode Reviewed-by: alanb, mchung, sla
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33263
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
     1
/*
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
     4
 *
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
     8
 *
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    13
 * accompanied this code).
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    14
 *
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    18
 *
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    21
 * questions.
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    22
 */
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    23
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    24
import java.util.ArrayList;
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    25
import java.util.List;
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    26
import java.util.stream.Collectors;
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    27
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    28
import jdk.test.lib.apps.LingeredApp;
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    29
import jdk.test.lib.apps.LingeredAppWithDeadlock;
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    30
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    31
import jdk.testlibrary.Utils;
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    32
import jdk.testlibrary.Platform;
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    33
import jdk.testlibrary.JDKToolLauncher;
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    34
import jdk.testlibrary.OutputAnalyzer;
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    35
import jdk.testlibrary.ProcessTools;
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    36
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    37
/*
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    38
 * @test
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    39
 * @summary Test deadlock detection
33832
7dde7a271492 8140189: [TESTBUG] Get rid of "@library /../../test/lib" in jtreg tests
cjplummer
parents: 33263
diff changeset
    40
 * @library /test/lib/share/classes
33263
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    41
 * @library /lib/testlibrary
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    42
 * @build jdk.testlibrary.*
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    43
 * @build jdk.test.lib.apps.*
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    44
 * @build DeadlockDetectionTest
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    45
 * @run main DeadlockDetectionTest
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    46
 */
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    47
public class DeadlockDetectionTest {
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    48
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    49
    private static LingeredAppWithDeadlock theApp = null;
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    50
    private static ProcessBuilder processBuilder = new ProcessBuilder();
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    51
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    52
    private static OutputAnalyzer jstack(String... toolArgs) throws Exception {
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    53
        JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jstack");
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    54
        launcher.addVMArg("-XX:+UsePerfData");
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    55
        if (toolArgs != null) {
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    56
            for (String toolArg : toolArgs) {
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    57
                launcher.addToolArg(toolArg);
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    58
            }
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    59
        }
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    60
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    61
        processBuilder.command(launcher.getCommand());
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    62
        System.out.println(processBuilder.command().stream().collect(Collectors.joining(" ")));
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    63
        OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    64
        System.out.println(output.getOutput());
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    65
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    66
        return output;
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    67
    }
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    68
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    69
    public static void main(String[] args) throws Exception {
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    70
        System.out.println("Starting DeadlockDetectionTest");
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    71
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    72
        if (!Platform.shouldSAAttach()) {
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    73
            // Silently skip the test if we don't have enough permissions to attach
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    74
            System.err.println("Error! Insufficient permissions to attach.");
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    75
            return;
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    76
        }
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    77
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    78
        if (!LingeredApp.isLastModifiedWorking()) {
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    79
            // Exact behaviour of the test depends on operating system and the test nature,
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    80
            // so just print the warning and continue
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    81
            System.err.println("Warning! Last modified time doesn't work.");
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    82
        }
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    83
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    84
        try {
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    85
            List<String> vmArgs = new ArrayList<String>();
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    86
            vmArgs.add("-XX:+UsePerfData");
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    87
            vmArgs.addAll(Utils.getVmOptions());
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    88
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    89
            theApp = new LingeredAppWithDeadlock();
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    90
            LingeredApp.startApp(vmArgs, theApp);
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    91
            OutputAnalyzer output = jstack(Long.toString(theApp.getPid()));
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    92
            System.out.println(output.getOutput());
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    93
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    94
            if (output.getExitValue() == 3) {
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    95
                System.out.println("Test can't run for some reason. Skipping");
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    96
            }
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    97
            else {
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    98
                output.shouldHaveExitValue(0);
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
    99
                output.shouldContain("Found 1 deadlock.");
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
   100
            }
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
   101
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
   102
        } finally {
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
   103
            LingeredApp.stopApp(theApp);
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
   104
        }
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
   105
    }
c91d948ab638 8086134: Deadlock detection fails to attach to core file
dsamersoff
parents:
diff changeset
   106
}