Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/demo/management/FullThreadDump/ThreadMonitor.java
38829 views
1
/*
2
* Copyright (c) 2004, 2011, Oracle and/or its affiliates. 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 Oracle 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
* This source code is provided to illustrate the usage of a given feature
34
* or technique and has been deliberately simplified. Additional steps
35
* required for a production-quality application, such as security checks,
36
* input validation and proper error handling, might not be present in
37
* this sample code.
38
*/
39
40
41
/*
42
*/
43
44
import static java.lang.management.ManagementFactory.*;
45
import java.lang.management.ThreadMXBean;
46
import java.lang.management.ThreadInfo;
47
import java.lang.management.LockInfo;
48
import java.lang.management.MonitorInfo;
49
import javax.management.*;
50
import java.io.*;
51
52
/**
53
* Example of using the java.lang.management API to dump stack trace
54
* and to perform deadlock detection.
55
*
56
* @author Mandy Chung
57
*/
58
public class ThreadMonitor {
59
private MBeanServerConnection server;
60
private ThreadMXBean tmbean;
61
private ObjectName objname;
62
63
// default - JDK 6+ VM
64
private String findDeadlocksMethodName = "findDeadlockedThreads";
65
private boolean canDumpLocks = true;
66
67
/**
68
* Constructs a ThreadMonitor object to get thread information
69
* in a remote JVM.
70
*/
71
public ThreadMonitor(MBeanServerConnection server) throws IOException {
72
this.server = server;
73
this.tmbean = newPlatformMXBeanProxy(server,
74
THREAD_MXBEAN_NAME,
75
ThreadMXBean.class);
76
try {
77
objname = new ObjectName(THREAD_MXBEAN_NAME);
78
} catch (MalformedObjectNameException e) {
79
// should not reach here
80
InternalError ie = new InternalError(e.getMessage());
81
ie.initCause(e);
82
throw ie;
83
}
84
parseMBeanInfo();
85
}
86
87
/**
88
* Constructs a ThreadMonitor object to get thread information
89
* in the local JVM.
90
*/
91
public ThreadMonitor() {
92
this.tmbean = getThreadMXBean();
93
}
94
95
/**
96
* Prints the thread dump information to System.out.
97
*/
98
public void threadDump() {
99
if (canDumpLocks) {
100
if (tmbean.isObjectMonitorUsageSupported() &&
101
tmbean.isSynchronizerUsageSupported()) {
102
// Print lock info if both object monitor usage
103
// and synchronizer usage are supported.
104
// This sample code can be modified to handle if
105
// either monitor usage or synchronizer usage is supported.
106
dumpThreadInfoWithLocks();
107
}
108
} else {
109
dumpThreadInfo();
110
}
111
}
112
113
private void dumpThreadInfo() {
114
System.out.println("Full Java thread dump");
115
long[] tids = tmbean.getAllThreadIds();
116
ThreadInfo[] tinfos = tmbean.getThreadInfo(tids, Integer.MAX_VALUE);
117
for (ThreadInfo ti : tinfos) {
118
printThreadInfo(ti);
119
}
120
}
121
122
/**
123
* Prints the thread dump information with locks info to System.out.
124
*/
125
private void dumpThreadInfoWithLocks() {
126
System.out.println("Full Java thread dump with locks info");
127
128
ThreadInfo[] tinfos = tmbean.dumpAllThreads(true, true);
129
for (ThreadInfo ti : tinfos) {
130
printThreadInfo(ti);
131
LockInfo[] syncs = ti.getLockedSynchronizers();
132
printLockInfo(syncs);
133
}
134
System.out.println();
135
}
136
137
private static String INDENT = " ";
138
139
private void printThreadInfo(ThreadInfo ti) {
140
// print thread information
141
printThread(ti);
142
143
// print stack trace with locks
144
StackTraceElement[] stacktrace = ti.getStackTrace();
145
MonitorInfo[] monitors = ti.getLockedMonitors();
146
for (int i = 0; i < stacktrace.length; i++) {
147
StackTraceElement ste = stacktrace[i];
148
System.out.println(INDENT + "at " + ste.toString());
149
for (MonitorInfo mi : monitors) {
150
if (mi.getLockedStackDepth() == i) {
151
System.out.println(INDENT + " - locked " + mi);
152
}
153
}
154
}
155
System.out.println();
156
}
157
158
private void printThread(ThreadInfo ti) {
159
StringBuilder sb = new StringBuilder("\"" + ti.getThreadName() + "\"" +
160
" Id=" + ti.getThreadId() +
161
" in " + ti.getThreadState());
162
if (ti.getLockName() != null) {
163
sb.append(" on lock=" + ti.getLockName());
164
}
165
if (ti.isSuspended()) {
166
sb.append(" (suspended)");
167
}
168
if (ti.isInNative()) {
169
sb.append(" (running in native)");
170
}
171
System.out.println(sb.toString());
172
if (ti.getLockOwnerName() != null) {
173
System.out.println(INDENT + " owned by " + ti.getLockOwnerName() +
174
" Id=" + ti.getLockOwnerId());
175
}
176
}
177
178
private void printMonitorInfo(ThreadInfo ti) {
179
MonitorInfo[] monitors = ti.getLockedMonitors();
180
System.out.println(INDENT + "Locked monitors: count = " + monitors.length);
181
for (MonitorInfo mi : monitors) {
182
System.out.println(INDENT + " - " + mi + " locked at ");
183
System.out.println(INDENT + " " + mi.getLockedStackDepth() +
184
" " + mi.getLockedStackFrame());
185
}
186
}
187
188
private void printLockInfo(LockInfo[] locks) {
189
System.out.println(INDENT + "Locked synchronizers: count = " + locks.length);
190
for (LockInfo li : locks) {
191
System.out.println(INDENT + " - " + li);
192
}
193
System.out.println();
194
}
195
196
/**
197
* Checks if any threads are deadlocked. If any, print
198
* the thread dump information.
199
*/
200
public boolean findDeadlock() {
201
long[] tids;
202
if (findDeadlocksMethodName.equals("findDeadlockedThreads") &&
203
tmbean.isSynchronizerUsageSupported()) {
204
tids = tmbean.findDeadlockedThreads();
205
if (tids == null) {
206
return false;
207
}
208
209
System.out.println("Deadlock found :-");
210
ThreadInfo[] infos = tmbean.getThreadInfo(tids, true, true);
211
for (ThreadInfo ti : infos) {
212
printThreadInfo(ti);
213
printMonitorInfo(ti);
214
printLockInfo(ti.getLockedSynchronizers());
215
System.out.println();
216
}
217
} else {
218
tids = tmbean.findMonitorDeadlockedThreads();
219
if (tids == null) {
220
return false;
221
}
222
ThreadInfo[] infos = tmbean.getThreadInfo(tids, Integer.MAX_VALUE);
223
for (ThreadInfo ti : infos) {
224
// print thread information
225
printThreadInfo(ti);
226
}
227
}
228
229
return true;
230
}
231
232
233
private void parseMBeanInfo() throws IOException {
234
try {
235
MBeanOperationInfo[] mopis = server.getMBeanInfo(objname).getOperations();
236
237
// look for findDeadlockedThreads operations;
238
boolean found = false;
239
for (MBeanOperationInfo op : mopis) {
240
if (op.getName().equals(findDeadlocksMethodName)) {
241
found = true;
242
break;
243
}
244
}
245
if (!found) {
246
// if findDeadlockedThreads operation doesn't exist,
247
// the target VM is running on JDK 5 and details about
248
// synchronizers and locks cannot be dumped.
249
findDeadlocksMethodName = "findMonitorDeadlockedThreads";
250
canDumpLocks = false;
251
}
252
} catch (IntrospectionException e) {
253
InternalError ie = new InternalError(e.getMessage());
254
ie.initCause(e);
255
throw ie;
256
} catch (InstanceNotFoundException e) {
257
InternalError ie = new InternalError(e.getMessage());
258
ie.initCause(e);
259
throw ie;
260
} catch (ReflectionException e) {
261
InternalError ie = new InternalError(e.getMessage());
262
ie.initCause(e);
263
throw ie;
264
}
265
}
266
}
267
268