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  * @modules java.management
  35  * @build MonitorDeadlock
  36  * @build SynchronizerDeadlock
  37  * @build ThreadDump
  38  * @run main/othervm FindDeadlocks
  39  */
  40 
  41 import java.lang.management.*;
  42 import java.util.*;
  43 
  44 public class FindDeadlocks {
  45     static ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
  46     public static void main(String[] argv) {
  47         ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
  48         // create deadlocked threads
  49         MonitorDeadlock md = new MonitorDeadlock();
  50 
  51         // no deadlock
  52         if (findDeadlocks() != null) {
  53             throw new RuntimeException("TEST FAILED: Should return null.");
  54         }
  55 
  56         // Let the threads to proceed
  57         md.goDeadlock();
  58         // wait until the deadlock is ready
  59         md.waitUntilDeadlock();
  60 
  61         long[] mthreads = findDeadlocks();
  62         if (mthreads == null) {
  63             ThreadDump.dumpStacks();
  64             throw new RuntimeException("TEST FAILED: Deadlock not detected.");
  65         }
  66         md.checkResult(mthreads);
  67 
  68         // create deadlocked threads on synchronizers
  69         SynchronizerDeadlock sd = new SynchronizerDeadlock();
  70 
  71         // Let the threads to proceed
  72         sd.goDeadlock();
  73         // wait until the deadlock is ready
  74         sd.waitUntilDeadlock();
  75 
  76         // Find Deadlock
  77         long[] threads = findDeadlocks();
  78         if (threads == null) {
  79             ThreadDump.dumpStacks();
  80             throw new RuntimeException("TEST FAILED: Deadlock not detected.");
  81         }
  82 
  83         // form a list of newly deadlocked threads
  84         long[] newList = new long[threads.length - mthreads.length];
  85         int count = 0;
  86         for (int i = 0; i < threads.length; i++) {
  87             long id = threads[i];
  88             boolean isNew = true;
  89             for (int j = 0; j < mthreads.length; j++) {
  90                 if (mthreads[j] == id) {
  91                     isNew = false;
  92                     break;
  93                 }
  94             }
  95             if (isNew) {
  96                 newList[count++] = id;
  97             }
  98         }
  99 
 100         if (mbean.isSynchronizerUsageSupported()) {
 101             sd.checkResult(newList);
 102         } else {
 103             // monitoring of synchronizer usage not supported
 104             if (count != 0) {
 105                 throw new RuntimeException("TEST FAILED: NewList should be empty.");
 106             }
 107         }
 108 
 109         // Print Deadlock stack trace
 110         System.out.println("Found threads that are in deadlock:-");
 111         ThreadInfo[] infos = mbean.getThreadInfo(threads, Integer.MAX_VALUE);
 112         for (int i = 0; i < infos.length; i++) {
 113             ThreadDump.printThreadInfo(infos[i]);
 114         }
 115 
 116         System.out.println("Test passed");
 117     }
 118     static long[] findDeadlocks() {
 119         long[] threads;
 120         if (mbean.isSynchronizerUsageSupported()) {
 121             threads = mbean.findDeadlockedThreads();
 122         } else {
 123             threads = mbean.findMonitorDeadlockedThreads();
 124         }
 125         return threads;
 126     }
 127 
 128 }