1/*
2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25/*
26 * @test
27 * @bug     5086470
28 * @key intermittent
29 * @summary Basic Test for the following methods:
30 *          - ThreadMXBean.findDeadlockedThreads()
31 *          - ThreadMXBean.findMonitorDeadlockedThreads()
32 * @author  Mandy Chung
33 *
34 * @build MonitorDeadlock
35 * @build SynchronizerDeadlock
36 * @build ThreadDump
37 * @run main/othervm FindDeadlocks
38 */
39
40import java.lang.management.*;
41import java.util.*;
42
43public class FindDeadlocks {
44    static ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
45    public static void main(String[] argv) {
46        ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
47        // create deadlocked threads
48        MonitorDeadlock md = new MonitorDeadlock();
49
50        // no deadlock
51        if (findDeadlocks() != null) {
52            throw new RuntimeException("TEST FAILED: Should return null.");
53        }
54
55        // Let the threads to proceed
56        md.goDeadlock();
57        // wait until the deadlock is ready
58        md.waitUntilDeadlock();
59
60        long[] mthreads = findDeadlocks();
61        if (mthreads == null) {
62            ThreadDump.dumpStacks();
63            throw new RuntimeException("TEST FAILED: Deadlock not detected.");
64        }
65        md.checkResult(mthreads);
66
67        // create deadlocked threads on synchronizers
68        SynchronizerDeadlock sd = new SynchronizerDeadlock();
69
70        // Let the threads to proceed
71        sd.goDeadlock();
72        // wait until the deadlock is ready
73        sd.waitUntilDeadlock();
74
75        // Find Deadlock
76        long[] threads = findDeadlocks();
77        if (threads == null) {
78            ThreadDump.dumpStacks();
79            throw new RuntimeException("TEST FAILED: Deadlock not detected.");
80        }
81
82        // form a list of newly deadlocked threads
83        long[] newList = new long[threads.length - mthreads.length];
84        int count = 0;
85        for (int i = 0; i < threads.length; i++) {
86            long id = threads[i];
87            boolean isNew = true;
88            for (int j = 0; j < mthreads.length; j++) {
89                if (mthreads[j] == id) {
90                    isNew = false;
91                    break;
92                }
93            }
94            if (isNew) {
95                newList[count++] = id;
96            }
97        }
98
99        if (mbean.isSynchronizerUsageSupported()) {
100            sd.checkResult(newList);
101        } else {
102            // monitoring of synchronizer usage not supported
103            if (count != 0) {
104                throw new RuntimeException("TEST FAILED: NewList should be empty.");
105            }
106        }
107
108        // Print Deadlock stack trace
109        System.out.println("Found threads that are in deadlock:-");
110        ThreadInfo[] infos = mbean.getThreadInfo(threads, Integer.MAX_VALUE);
111        for (int i = 0; i < infos.length; i++) {
112            ThreadDump.printThreadInfo(infos[i]);
113        }
114
115        System.out.println("Test passed");
116    }
117    static long[] findDeadlocks() {
118        long[] threads;
119        if (mbean.isSynchronizerUsageSupported()) {
120            threads = mbean.findDeadlockedThreads();
121        } else {
122            threads = mbean.findMonitorDeadlockedThreads();
123        }
124        return threads;
125    }
126
127}
128