2
|
1 |
/*
|
|
2 |
* Copyright 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
*
|
|
4 |
* Redistribution and use in source and binary forms, with or without
|
|
5 |
* modification, are permitted provided that the following conditions
|
|
6 |
* are met:
|
|
7 |
*
|
|
8 |
* - Redistributions of source code must retain the above copyright
|
|
9 |
* notice, this list of conditions and the following disclaimer.
|
|
10 |
*
|
|
11 |
* - Redistributions in binary form must reproduce the above copyright
|
|
12 |
* notice, this list of conditions and the following disclaimer in the
|
|
13 |
* documentation and/or other materials provided with the distribution.
|
|
14 |
*
|
|
15 |
* - Neither the name of Sun Microsystems nor the names of its
|
|
16 |
* contributors may be used to endorse or promote products derived
|
|
17 |
* from this software without specific prior written permission.
|
|
18 |
*
|
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
30 |
*/
|
|
31 |
|
|
32 |
/*
|
|
33 |
*/
|
|
34 |
|
|
35 |
import static java.lang.management.ManagementFactory.*;
|
|
36 |
import java.lang.management.ThreadMXBean;
|
|
37 |
import java.lang.management.ThreadInfo;
|
|
38 |
import java.lang.management.LockInfo;
|
|
39 |
import java.lang.management.MonitorInfo;
|
|
40 |
import javax.management.*;
|
|
41 |
import java.io.*;
|
|
42 |
import java.util.*;
|
|
43 |
|
|
44 |
/**
|
|
45 |
* Example of using the java.lang.management API to dump stack trace
|
|
46 |
* and to perform deadlock detection.
|
|
47 |
*
|
|
48 |
* @author Mandy Chung
|
|
49 |
*/
|
|
50 |
public class ThreadMonitor {
|
|
51 |
private MBeanServerConnection server;
|
|
52 |
private ThreadMXBean tmbean;
|
|
53 |
private ObjectName objname;
|
|
54 |
|
|
55 |
// default - JDK 6+ VM
|
|
56 |
private String findDeadlocksMethodName = "findDeadlockedThreads";
|
|
57 |
private boolean canDumpLocks = true;
|
|
58 |
|
|
59 |
/**
|
|
60 |
* Constructs a ThreadMonitor object to get thread information
|
|
61 |
* in a remote JVM.
|
|
62 |
*/
|
|
63 |
public ThreadMonitor(MBeanServerConnection server) throws IOException {
|
|
64 |
this.server = server;
|
|
65 |
this.tmbean = newPlatformMXBeanProxy(server,
|
|
66 |
THREAD_MXBEAN_NAME,
|
|
67 |
ThreadMXBean.class);
|
|
68 |
try {
|
|
69 |
objname = new ObjectName(THREAD_MXBEAN_NAME);
|
|
70 |
} catch (MalformedObjectNameException e) {
|
|
71 |
// should not reach here
|
|
72 |
InternalError ie = new InternalError(e.getMessage());
|
|
73 |
ie.initCause(e);
|
|
74 |
throw ie;
|
|
75 |
}
|
|
76 |
parseMBeanInfo();
|
|
77 |
}
|
|
78 |
|
|
79 |
/**
|
|
80 |
* Constructs a ThreadMonitor object to get thread information
|
|
81 |
* in the local JVM.
|
|
82 |
*/
|
|
83 |
public ThreadMonitor() {
|
|
84 |
this.tmbean = getThreadMXBean();
|
|
85 |
}
|
|
86 |
|
|
87 |
/**
|
|
88 |
* Prints the thread dump information to System.out.
|
|
89 |
*/
|
|
90 |
public void threadDump() {
|
|
91 |
if (canDumpLocks) {
|
|
92 |
if (tmbean.isObjectMonitorUsageSupported() &&
|
|
93 |
tmbean.isSynchronizerUsageSupported()) {
|
|
94 |
// Print lock info if both object monitor usage
|
|
95 |
// and synchronizer usage are supported.
|
|
96 |
// This sample code can be modified to handle if
|
|
97 |
// either monitor usage or synchronizer usage is supported.
|
|
98 |
dumpThreadInfoWithLocks();
|
|
99 |
}
|
|
100 |
} else {
|
|
101 |
dumpThreadInfo();
|
|
102 |
}
|
|
103 |
}
|
|
104 |
|
|
105 |
private void dumpThreadInfo() {
|
|
106 |
System.out.println("Full Java thread dump");
|
|
107 |
long[] tids = tmbean.getAllThreadIds();
|
|
108 |
ThreadInfo[] tinfos = tmbean.getThreadInfo(tids, Integer.MAX_VALUE);
|
|
109 |
for (ThreadInfo ti : tinfos) {
|
|
110 |
printThreadInfo(ti);
|
|
111 |
}
|
|
112 |
}
|
|
113 |
|
|
114 |
/**
|
|
115 |
* Prints the thread dump information with locks info to System.out.
|
|
116 |
*/
|
|
117 |
private void dumpThreadInfoWithLocks() {
|
|
118 |
System.out.println("Full Java thread dump with locks info");
|
|
119 |
|
|
120 |
ThreadInfo[] tinfos = tmbean.dumpAllThreads(true, true);
|
|
121 |
for (ThreadInfo ti : tinfos) {
|
|
122 |
printThreadInfo(ti);
|
|
123 |
LockInfo[] syncs = ti.getLockedSynchronizers();
|
|
124 |
printLockInfo(syncs);
|
|
125 |
}
|
|
126 |
System.out.println();
|
|
127 |
}
|
|
128 |
|
|
129 |
private static String INDENT = " ";
|
|
130 |
|
|
131 |
private void printThreadInfo(ThreadInfo ti) {
|
|
132 |
// print thread information
|
|
133 |
printThread(ti);
|
|
134 |
|
|
135 |
// print stack trace with locks
|
|
136 |
StackTraceElement[] stacktrace = ti.getStackTrace();
|
|
137 |
MonitorInfo[] monitors = ti.getLockedMonitors();
|
|
138 |
for (int i = 0; i < stacktrace.length; i++) {
|
|
139 |
StackTraceElement ste = stacktrace[i];
|
|
140 |
System.out.println(INDENT + "at " + ste.toString());
|
|
141 |
for (MonitorInfo mi : monitors) {
|
|
142 |
if (mi.getLockedStackDepth() == i) {
|
|
143 |
System.out.println(INDENT + " - locked " + mi);
|
|
144 |
}
|
|
145 |
}
|
|
146 |
}
|
|
147 |
System.out.println();
|
|
148 |
}
|
|
149 |
|
|
150 |
private void printThread(ThreadInfo ti) {
|
|
151 |
StringBuilder sb = new StringBuilder("\"" + ti.getThreadName() + "\"" +
|
|
152 |
" Id=" + ti.getThreadId() +
|
|
153 |
" in " + ti.getThreadState());
|
|
154 |
if (ti.getLockName() != null) {
|
|
155 |
sb.append(" on lock=" + ti.getLockName());
|
|
156 |
}
|
|
157 |
if (ti.isSuspended()) {
|
|
158 |
sb.append(" (suspended)");
|
|
159 |
}
|
|
160 |
if (ti.isInNative()) {
|
|
161 |
sb.append(" (running in native)");
|
|
162 |
}
|
|
163 |
System.out.println(sb.toString());
|
|
164 |
if (ti.getLockOwnerName() != null) {
|
|
165 |
System.out.println(INDENT + " owned by " + ti.getLockOwnerName() +
|
|
166 |
" Id=" + ti.getLockOwnerId());
|
|
167 |
}
|
|
168 |
}
|
|
169 |
|
|
170 |
private void printMonitorInfo(ThreadInfo ti, MonitorInfo[] monitors) {
|
|
171 |
System.out.println(INDENT + "Locked monitors: count = " + monitors.length);
|
|
172 |
for (MonitorInfo mi : monitors) {
|
|
173 |
System.out.println(INDENT + " - " + mi + " locked at ");
|
|
174 |
System.out.println(INDENT + " " + mi.getLockedStackDepth() +
|
|
175 |
" " + mi.getLockedStackFrame());
|
|
176 |
}
|
|
177 |
}
|
|
178 |
|
|
179 |
private void printLockInfo(LockInfo[] locks) {
|
|
180 |
System.out.println(INDENT + "Locked synchronizers: count = " + locks.length);
|
|
181 |
for (LockInfo li : locks) {
|
|
182 |
System.out.println(INDENT + " - " + li);
|
|
183 |
}
|
|
184 |
System.out.println();
|
|
185 |
}
|
|
186 |
|
|
187 |
/**
|
|
188 |
* Checks if any threads are deadlocked. If any, print
|
|
189 |
* the thread dump information.
|
|
190 |
*/
|
|
191 |
public boolean findDeadlock() {
|
|
192 |
long[] tids;
|
|
193 |
if (findDeadlocksMethodName.equals("findDeadlockedThreads") &&
|
|
194 |
tmbean.isSynchronizerUsageSupported()) {
|
|
195 |
tids = tmbean.findDeadlockedThreads();
|
|
196 |
if (tids == null) {
|
|
197 |
return false;
|
|
198 |
}
|
|
199 |
|
|
200 |
System.out.println("Deadlock found :-");
|
|
201 |
ThreadInfo[] infos = tmbean.getThreadInfo(tids, true, true);
|
|
202 |
for (ThreadInfo ti : infos) {
|
|
203 |
printThreadInfo(ti);
|
|
204 |
printLockInfo(ti.getLockedSynchronizers());
|
|
205 |
System.out.println();
|
|
206 |
}
|
|
207 |
} else {
|
|
208 |
tids = tmbean.findMonitorDeadlockedThreads();
|
|
209 |
if (tids == null) {
|
|
210 |
return false;
|
|
211 |
}
|
|
212 |
ThreadInfo[] infos = tmbean.getThreadInfo(tids, Integer.MAX_VALUE);
|
|
213 |
for (ThreadInfo ti : infos) {
|
|
214 |
// print thread information
|
|
215 |
printThreadInfo(ti);
|
|
216 |
}
|
|
217 |
}
|
|
218 |
|
|
219 |
return true;
|
|
220 |
}
|
|
221 |
|
|
222 |
|
|
223 |
private void parseMBeanInfo() throws IOException {
|
|
224 |
try {
|
|
225 |
MBeanOperationInfo[] mopis = server.getMBeanInfo(objname).getOperations();
|
|
226 |
|
|
227 |
// look for findDeadlockedThreads operations;
|
|
228 |
boolean found = false;
|
|
229 |
for (MBeanOperationInfo op : mopis) {
|
|
230 |
if (op.getName().equals(findDeadlocksMethodName)) {
|
|
231 |
found = true;
|
|
232 |
break;
|
|
233 |
}
|
|
234 |
}
|
|
235 |
if (!found) {
|
|
236 |
// if findDeadlockedThreads operation doesn't exist,
|
|
237 |
// the target VM is running on JDK 5 and details about
|
|
238 |
// synchronizers and locks cannot be dumped.
|
|
239 |
findDeadlocksMethodName = "findMonitorDeadlockedThreads";
|
|
240 |
canDumpLocks = false;
|
|
241 |
}
|
|
242 |
} catch (IntrospectionException e) {
|
|
243 |
InternalError ie = new InternalError(e.getMessage());
|
|
244 |
ie.initCause(e);
|
|
245 |
throw ie;
|
|
246 |
} catch (InstanceNotFoundException e) {
|
|
247 |
InternalError ie = new InternalError(e.getMessage());
|
|
248 |
ie.initCause(e);
|
|
249 |
throw ie;
|
|
250 |
} catch (ReflectionException e) {
|
|
251 |
InternalError ie = new InternalError(e.getMessage());
|
|
252 |
ie.initCause(e);
|
|
253 |
throw ie;
|
|
254 |
}
|
|
255 |
}
|
|
256 |
}
|