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