1 /* 2 * Copyright (c) 2013, 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 import java.lang.management.ThreadInfo; 24 import java.lang.management.ThreadMXBean; 25 import java.lang.Thread.State; 26 import java.io.IOException; 27 import java.lang.management.ManagementFactory; 28 import java.util.logging.LogManager; 29 import java.util.logging.Logger; 30 import java.util.Map; 31 32 /** 33 * @test 34 * @bug 8010939 35 * @summary check for deadlock between findLogger() and drainLoggerRefQueueBounded() 36 * @author jim.gish@oracle.com 37 * @build DrainFindDeadlockTest 38 * @run main/othervm/timeout=10 DrainFindDeadlockTest 39 */ 40 41 /** 42 * This test is checking for a deadlock between 43 * LogManager$LoggerContext.findLogger() and 44 * LogManager.drainLoggerRefQueueBounded() (which could happen by calling 45 * Logger.getLogger() and LogManager.readConfiguration() in different threads) 46 */ 47 public class DrainFindDeadlockTest { 48 private LogManager mgr = LogManager.getLogManager(); 49 private final static int MAX_ITERATIONS = 100; 50 51 // Get a ThreadMXBean so we can check for deadlock. N.B. this may 52 // not be supported on all platforms, which means we will have to 53 // resort to the traditional test timeout method. However, if 54 // we have the support we'll get the deadlock details if one 55 // is detected. 56 private final static ThreadMXBean threadMXBean = 57 ManagementFactory.getThreadMXBean(); 58 private final boolean threadMXBeanDeadlockSupported = 59 threadMXBean.isSynchronizerUsageSupported(); 60 61 public static void main(String... args) throws IOException, Exception { 62 new DrainFindDeadlockTest().testForDeadlock(); 63 } 64 65 public static void randomDelay() { 66 int runs = (int) Math.random() * 1000000; 67 int c = 0; 68 69 for (int i=0; i<runs; ++i) { 70 c=c+i; 71 } 72 } 73 74 public void testForDeadlock() throws IOException, Exception { 75 System.out.println("Deadlock detection " 76 + (threadMXBeanDeadlockSupported ? "is" : "is not") + 77 " available."); 78 Thread setup = new Thread(new SetupLogger(), "SetupLogger"); 79 Thread readConfig = new Thread(new ReadConfig(), "ReadConfig"); 80 Thread check = new Thread(new DeadlockChecker(setup, readConfig), 81 "DeadlockChecker"); 82 83 // make the threads daemon threads so they will go away when the 84 // test exits 85 setup.setDaemon(true); 86 readConfig.setDaemon(true); 87 check.setDaemon(true); 88 89 check.start(); setup.start(); readConfig.start(); 90 try { 91 check.join(); 92 } catch (InterruptedException ex) { 93 ex.printStackTrace(); 94 } 95 try { 96 readConfig.join(); 97 setup.join(); 98 } catch (InterruptedException ex) { 99 ex.printStackTrace(); 100 } 101 System.out.println("Test passed"); 102 } 103 104 class SetupLogger implements Runnable { 105 Logger logger = null; 106 107 @Override 108 public void run() { 109 System.out.println("Running " + Thread.currentThread().getName()); 110 111 for (int i=0; i < MAX_ITERATIONS; i++) { 112 logger = Logger.getLogger("DrainFindDeadlockTest"+i); 113 DrainFindDeadlockTest.randomDelay(); 114 } 115 } 116 } 117 118 class ReadConfig implements Runnable { 119 @Override 120 public void run() { 121 System.out.println("Running " + Thread.currentThread().getName()); 122 for (int i=0; i < MAX_ITERATIONS; i++) { 123 try { 124 mgr.readConfiguration(); 125 } catch (IOException | SecurityException ex) { 126 throw new RuntimeException("FAILED: test setup problem", ex); 127 } 128 DrainFindDeadlockTest.randomDelay(); 129 } 130 } 131 } 132 133 class DeadlockChecker implements Runnable { 134 Thread t1, t2; 135 136 DeadlockChecker(Thread t1, Thread t2) { 137 this.t1 = t1; 138 this.t2 = t2; 139 } 140 141 void checkState(Thread x, Thread y) { 142 // System.out.println("checkstate"); 143 boolean isXblocked = x.getState().equals(State.BLOCKED); 144 boolean isYblocked = y.getState().equals(State.BLOCKED); 145 long[] deadlockedThreads = null; 146 147 if (isXblocked && isYblocked) { 148 System.out.println("threads blocked"); 149 // they are both blocked, but this doesn't necessarily mean 150 // they are deadlocked 151 if (threadMXBeanDeadlockSupported) { 152 System.out.println("checking for deadlock"); 153 deadlockedThreads = threadMXBean.findDeadlockedThreads(); 154 } else { 155 System.out.println("Can't check for deadlock"); 156 } 157 if (deadlockedThreads != null) { 158 System.out.println("We detected a deadlock! "); 159 ThreadInfo[] threadInfos = threadMXBean.getThreadInfo( 160 deadlockedThreads, true, true); 161 for (ThreadInfo threadInfo: threadInfos) { 162 System.out.println(threadInfo); 163 } 164 throw new RuntimeException("TEST FAILED: Deadlock detected"); 165 } 166 System.out.println("We may have a deadlock"); 167 Map<Thread, StackTraceElement[]> threadMap = 168 Thread.getAllStackTraces(); 169 dumpStack(threadMap.get(x), x); 170 dumpStack(threadMap.get(y), y); 171 } 172 } 173 174 private void dumpStack(StackTraceElement[] aStackElt, Thread aThread) { 175 if (aStackElt != null) { 176 System.out.println("Thread:" + aThread.getName() + ": " + 177 aThread.getState()); 178 for (StackTraceElement element: aStackElt) { 179 System.out.println(" " + element); 180 } 181 } 182 } 183 184 @Override 185 public void run() { 186 System.out.println("Running " + Thread.currentThread().getName()); 187 for (int i=0; i < MAX_ITERATIONS*2; i++) { 188 checkState(t1, t2); 189 try { 190 Thread.sleep(10); 191 } catch (InterruptedException ex) { 192 }; 193 } 194 } 195 } 196 }