author | michaelm |
Thu, 24 Oct 2013 20:39:21 +0100 | |
changeset 22339 | e91bfaf4360d |
parent 19582 | 1cab3f8c48c7 |
child 22094 | 1c154d8a9a96 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
12851
diff
changeset
|
2 |
* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.tools.jconsole; |
|
27 |
||
28 |
import java.awt.*; |
|
29 |
import java.awt.event.*; |
|
30 |
import java.io.*; |
|
31 |
import java.lang.management.*; |
|
32 |
import java.lang.reflect.*; |
|
33 |
||
34 |
import javax.swing.*; |
|
35 |
import javax.swing.border.*; |
|
36 |
import javax.swing.event.*; |
|
37 |
||
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
38 |
|
2 | 39 |
import java.util.*; |
40 |
import java.util.concurrent.*; |
|
41 |
import java.util.List; |
|
42 |
||
43 |
import static sun.tools.jconsole.Utilities.*; |
|
44 |
||
45 |
||
46 |
@SuppressWarnings("serial") |
|
47 |
class ThreadTab extends Tab implements ActionListener, DocumentListener, ListSelectionListener { |
|
48 |
PlotterPanel threadMeter; |
|
49 |
TimeComboBox timeComboBox; |
|
50 |
JTabbedPane threadListTabbedPane; |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
51 |
DefaultListModel<Long> listModel; |
2 | 52 |
JTextField filterTF; |
53 |
JLabel messageLabel; |
|
54 |
JSplitPane threadsSplitPane; |
|
55 |
HashMap<Long, String> nameCache = new HashMap<Long, String>(); |
|
56 |
||
57 |
private ThreadOverviewPanel overviewPanel; |
|
58 |
private boolean plotterListening = false; |
|
59 |
||
60 |
||
61 |
private static final String threadCountKey = "threadCount"; |
|
62 |
private static final String peakKey = "peak"; |
|
63 |
||
64 |
private static final Color threadCountColor = Plotter.defaultColor; |
|
65 |
private static final Color peakColor = Color.red; |
|
66 |
||
67 |
private static final Border thinEmptyBorder = new EmptyBorder(2, 2, 2, 2); |
|
68 |
||
69 |
private static final String infoLabelFormat = "ThreadTab.infoLabelFormat"; |
|
70 |
||
71 |
||
72 |
/* |
|
73 |
Hierarchy of panels and layouts for this tab: |
|
74 |
||
75 |
ThreadTab (BorderLayout) |
|
76 |
||
77 |
North: topPanel (BorderLayout) |
|
78 |
||
79 |
Center: controlPanel (FlowLayout) |
|
80 |
timeComboBox |
|
81 |
||
82 |
Center: plotterPanel (BorderLayout) |
|
83 |
||
84 |
Center: plotter |
|
85 |
||
86 |
*/ |
|
87 |
||
88 |
||
89 |
public static String getTabName() { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
90 |
return Messages.THREADS; |
2 | 91 |
} |
92 |
||
93 |
public ThreadTab(VMPanel vmPanel) { |
|
94 |
super(vmPanel, getTabName()); |
|
95 |
||
96 |
setLayout(new BorderLayout(0, 0)); |
|
97 |
setBorder(new EmptyBorder(4, 4, 3, 4)); |
|
98 |
||
99 |
JPanel topPanel = new JPanel(new BorderLayout()); |
|
100 |
JPanel plotterPanel = new JPanel(new VariableGridLayout(0, 1, 4, 4, true, true)); |
|
101 |
||
102 |
add(topPanel, BorderLayout.NORTH); |
|
103 |
add(plotterPanel, BorderLayout.CENTER); |
|
104 |
||
105 |
JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 5)); |
|
106 |
topPanel.add(controlPanel, BorderLayout.CENTER); |
|
107 |
||
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
108 |
threadMeter = new PlotterPanel(Messages.NUMBER_OF_THREADS, |
2 | 109 |
Plotter.Unit.NONE, true); |
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
110 |
threadMeter.plotter.createSequence(threadCountKey, Messages.LIVE_THREADS, threadCountColor, true); |
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
111 |
threadMeter.plotter.createSequence(peakKey, Messages.PEAK, peakColor, true); |
2 | 112 |
setAccessibleName(threadMeter.plotter, |
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
113 |
Messages.THREAD_TAB_THREAD_PLOTTER_ACCESSIBLE_NAME); |
2 | 114 |
|
115 |
plotterPanel.add(threadMeter); |
|
116 |
||
117 |
timeComboBox = new TimeComboBox(threadMeter.plotter); |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
118 |
controlPanel.add(new LabeledComponent(Messages.TIME_RANGE_COLON, |
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
119 |
Resources.getMnemonicInt(Messages.TIME_RANGE_COLON), |
2 | 120 |
timeComboBox)); |
121 |
||
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
122 |
listModel = new DefaultListModel<Long>(); |
2 | 123 |
|
124 |
JTextArea textArea = new JTextArea(); |
|
125 |
textArea.setBorder(thinEmptyBorder); |
|
126 |
textArea.setEditable(false); |
|
127 |
setAccessibleName(textArea, |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
128 |
Messages.THREAD_TAB_THREAD_INFO_ACCESSIBLE_NAME); |
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
129 |
ThreadJList list = new ThreadJList(listModel, textArea); |
2 | 130 |
|
131 |
Dimension di = new Dimension(super.getPreferredSize()); |
|
132 |
di.width = Math.min(di.width, 200); |
|
133 |
||
134 |
JScrollPane threadlistSP = new JScrollPane(list); |
|
135 |
threadlistSP.setPreferredSize(di); |
|
136 |
threadlistSP.setBorder(null); |
|
137 |
||
138 |
JScrollPane textAreaSP = new JScrollPane(textArea); |
|
139 |
textAreaSP.setBorder(null); |
|
140 |
||
141 |
threadListTabbedPane = new JTabbedPane(JTabbedPane.TOP); |
|
142 |
threadsSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, |
|
143 |
threadlistSP, textAreaSP); |
|
144 |
threadsSplitPane.setOneTouchExpandable(true); |
|
145 |
threadsSplitPane.setBorder(null); |
|
146 |
||
147 |
JPanel firstTabPanel = new JPanel(new BorderLayout()); |
|
148 |
firstTabPanel.setOpaque(false); |
|
149 |
||
150 |
JPanel firstTabToolPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 2)); |
|
151 |
firstTabToolPanel.setOpaque(false); |
|
152 |
||
153 |
filterTF = new PromptingTextField("Filter", 20); |
|
154 |
filterTF.getDocument().addDocumentListener(this); |
|
155 |
firstTabToolPanel.add(filterTF); |
|
156 |
||
157 |
JSeparator separator = new JSeparator(JSeparator.VERTICAL); |
|
158 |
separator.setPreferredSize(new Dimension(separator.getPreferredSize().width, |
|
159 |
filterTF.getPreferredSize().height)); |
|
160 |
firstTabToolPanel.add(separator); |
|
161 |
||
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
162 |
JButton detectDeadlockButton = new JButton(Messages.DETECT_DEADLOCK); |
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
163 |
detectDeadlockButton.setMnemonic(Resources.getMnemonicInt(Messages.DETECT_DEADLOCK)); |
2 | 164 |
detectDeadlockButton.setActionCommand("detectDeadlock"); |
165 |
detectDeadlockButton.addActionListener(this); |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
166 |
detectDeadlockButton.setToolTipText(Messages.DETECT_DEADLOCK_TOOLTIP); |
2 | 167 |
firstTabToolPanel.add(detectDeadlockButton); |
168 |
||
169 |
messageLabel = new JLabel(); |
|
170 |
firstTabToolPanel.add(messageLabel); |
|
171 |
||
172 |
firstTabPanel.add(threadsSplitPane, BorderLayout.CENTER); |
|
173 |
firstTabPanel.add(firstTabToolPanel, BorderLayout.SOUTH); |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
174 |
threadListTabbedPane.addTab(Messages.THREADS, firstTabPanel); |
2 | 175 |
|
176 |
plotterPanel.add(threadListTabbedPane); |
|
177 |
} |
|
178 |
||
179 |
private long oldThreads[] = new long[0]; |
|
180 |
||
181 |
public SwingWorker<?, ?> newSwingWorker() { |
|
182 |
final ProxyClient proxyClient = vmPanel.getProxyClient(); |
|
183 |
||
184 |
if (!plotterListening) { |
|
185 |
proxyClient.addWeakPropertyChangeListener(threadMeter.plotter); |
|
186 |
plotterListening = true; |
|
187 |
} |
|
188 |
||
189 |
return new SwingWorker<Boolean, Object>() { |
|
190 |
private int tlCount; |
|
191 |
private int tpCount; |
|
192 |
private long ttCount; |
|
193 |
private long[] threads; |
|
194 |
private long timeStamp; |
|
195 |
||
196 |
public Boolean doInBackground() { |
|
197 |
try { |
|
198 |
ThreadMXBean threadMBean = proxyClient.getThreadMXBean(); |
|
199 |
||
200 |
tlCount = threadMBean.getThreadCount(); |
|
201 |
tpCount = threadMBean.getPeakThreadCount(); |
|
202 |
if (overviewPanel != null) { |
|
203 |
ttCount = threadMBean.getTotalStartedThreadCount(); |
|
204 |
} else { |
|
205 |
ttCount = 0L; |
|
206 |
} |
|
207 |
||
208 |
threads = threadMBean.getAllThreadIds(); |
|
209 |
for (long newThread : threads) { |
|
210 |
if (nameCache.get(newThread) == null) { |
|
211 |
ThreadInfo ti = threadMBean.getThreadInfo(newThread); |
|
212 |
if (ti != null) { |
|
213 |
String name = ti.getThreadName(); |
|
214 |
if (name != null) { |
|
215 |
nameCache.put(newThread, name); |
|
216 |
} |
|
217 |
} |
|
218 |
} |
|
219 |
} |
|
220 |
timeStamp = System.currentTimeMillis(); |
|
221 |
return true; |
|
222 |
} catch (IOException e) { |
|
223 |
return false; |
|
224 |
} catch (UndeclaredThrowableException e) { |
|
225 |
return false; |
|
226 |
} |
|
227 |
} |
|
228 |
||
229 |
protected void done() { |
|
230 |
try { |
|
231 |
if (!get()) { |
|
232 |
return; |
|
233 |
} |
|
234 |
} catch (InterruptedException ex) { |
|
235 |
return; |
|
236 |
} catch (ExecutionException ex) { |
|
237 |
if (JConsole.isDebug()) { |
|
238 |
ex.printStackTrace(); |
|
239 |
} |
|
240 |
return; |
|
241 |
} |
|
242 |
||
243 |
threadMeter.plotter.addValues(timeStamp, tlCount, tpCount); |
|
244 |
threadMeter.setValueLabel(tlCount+""); |
|
245 |
||
246 |
if (overviewPanel != null) { |
|
247 |
overviewPanel.updateThreadsInfo(tlCount, tpCount, ttCount, timeStamp); |
|
248 |
} |
|
249 |
||
250 |
String filter = filterTF.getText().toLowerCase(Locale.ENGLISH); |
|
251 |
boolean doFilter = (filter.length() > 0); |
|
252 |
||
253 |
ArrayList<Long> l = new ArrayList<Long>(); |
|
254 |
for (long t : threads) { |
|
255 |
l.add(t); |
|
256 |
} |
|
257 |
Iterator<Long> iterator = l.iterator(); |
|
258 |
while (iterator.hasNext()) { |
|
259 |
long newThread = iterator.next(); |
|
260 |
String name = nameCache.get(newThread); |
|
261 |
if (doFilter && name != null && |
|
262 |
name.toLowerCase(Locale.ENGLISH).indexOf(filter) < 0) { |
|
263 |
||
264 |
iterator.remove(); |
|
265 |
} |
|
266 |
} |
|
267 |
long[] newThreads = threads; |
|
268 |
if (l.size() < threads.length) { |
|
269 |
newThreads = new long[l.size()]; |
|
270 |
for (int i = 0; i < newThreads.length; i++) { |
|
271 |
newThreads[i] = l.get(i); |
|
272 |
} |
|
273 |
} |
|
274 |
||
275 |
||
276 |
for (long oldThread : oldThreads) { |
|
277 |
boolean found = false; |
|
278 |
for (long newThread : newThreads) { |
|
279 |
if (newThread == oldThread) { |
|
280 |
found = true; |
|
281 |
break; |
|
282 |
} |
|
283 |
} |
|
284 |
if (!found) { |
|
285 |
listModel.removeElement(oldThread); |
|
286 |
if (!doFilter) { |
|
287 |
nameCache.remove(oldThread); |
|
288 |
} |
|
289 |
} |
|
290 |
} |
|
291 |
||
292 |
// Threads are in reverse chronological order |
|
293 |
for (int i = newThreads.length - 1; i >= 0; i--) { |
|
294 |
long newThread = newThreads[i]; |
|
295 |
boolean found = false; |
|
296 |
for (long oldThread : oldThreads) { |
|
297 |
if (newThread == oldThread) { |
|
298 |
found = true; |
|
299 |
break; |
|
300 |
} |
|
301 |
} |
|
302 |
if (!found) { |
|
303 |
listModel.addElement(newThread); |
|
304 |
} |
|
305 |
} |
|
306 |
oldThreads = newThreads; |
|
307 |
} |
|
308 |
}; |
|
309 |
} |
|
310 |
||
311 |
long lastSelected = -1; |
|
312 |
||
313 |
public void valueChanged(ListSelectionEvent ev) { |
|
314 |
ThreadJList list = (ThreadJList)ev.getSource(); |
|
315 |
final JTextArea textArea = list.textArea; |
|
316 |
||
317 |
Long selected = (Long)list.getSelectedValue(); |
|
318 |
if (selected == null) { |
|
319 |
if (lastSelected != -1) { |
|
320 |
selected = lastSelected; |
|
321 |
} |
|
322 |
} else { |
|
323 |
lastSelected = selected; |
|
324 |
} |
|
325 |
textArea.setText(""); |
|
326 |
if (selected != null) { |
|
327 |
final long threadID = selected; |
|
328 |
workerAdd(new Runnable() { |
|
329 |
public void run() { |
|
330 |
ProxyClient proxyClient = vmPanel.getProxyClient(); |
|
331 |
StringBuilder sb = new StringBuilder(); |
|
332 |
try { |
|
333 |
ThreadMXBean threadMBean = proxyClient.getThreadMXBean(); |
|
334 |
ThreadInfo ti = null; |
|
335 |
MonitorInfo[] monitors = null; |
|
336 |
if (proxyClient.isLockUsageSupported() && |
|
337 |
threadMBean.isObjectMonitorUsageSupported()) { |
|
338 |
// VMs that support the monitor usage monitoring |
|
339 |
ThreadInfo[] infos = threadMBean.dumpAllThreads(true, false); |
|
340 |
for (ThreadInfo info : infos) { |
|
341 |
if (info.getThreadId() == threadID) { |
|
342 |
ti = info; |
|
343 |
monitors = info.getLockedMonitors(); |
|
344 |
break; |
|
345 |
} |
|
346 |
} |
|
347 |
} else { |
|
348 |
// VM doesn't support monitor usage monitoring |
|
349 |
ti = threadMBean.getThreadInfo(threadID, Integer.MAX_VALUE); |
|
350 |
} |
|
351 |
if (ti != null) { |
|
352 |
if (ti.getLockName() == null) { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
353 |
sb.append(Resources.format(Messages.NAME_STATE, |
2 | 354 |
ti.getThreadName(), |
355 |
ti.getThreadState().toString())); |
|
356 |
} else if (ti.getLockOwnerName() == null) { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
357 |
sb.append(Resources.format(Messages.NAME_STATE_LOCK_NAME, |
2 | 358 |
ti.getThreadName(), |
359 |
ti.getThreadState().toString(), |
|
360 |
ti.getLockName())); |
|
361 |
} else { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
362 |
sb.append(Resources.format(Messages.NAME_STATE_LOCK_NAME_LOCK_OWNER, |
2 | 363 |
ti.getThreadName(), |
364 |
ti.getThreadState().toString(), |
|
365 |
ti.getLockName(), |
|
366 |
ti.getLockOwnerName())); |
|
367 |
} |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
368 |
sb.append(Resources.format(Messages.BLOCKED_COUNT_WAITED_COUNT, |
2 | 369 |
ti.getBlockedCount(), |
370 |
ti.getWaitedCount())); |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
371 |
sb.append(Messages.STACK_TRACE); |
2 | 372 |
int index = 0; |
373 |
for (StackTraceElement e : ti.getStackTrace()) { |
|
374 |
sb.append(e.toString()+"\n"); |
|
375 |
if (monitors != null) { |
|
376 |
for (MonitorInfo mi : monitors) { |
|
377 |
if (mi.getLockedStackDepth() == index) { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
378 |
sb.append(Resources.format(Messages.MONITOR_LOCKED, mi.toString())); |
2 | 379 |
} |
380 |
} |
|
381 |
} |
|
382 |
index++; |
|
383 |
} |
|
384 |
} |
|
385 |
} catch (IOException ex) { |
|
386 |
// Ignore |
|
387 |
} catch (UndeclaredThrowableException e) { |
|
388 |
proxyClient.markAsDead(); |
|
389 |
} |
|
390 |
final String text = sb.toString(); |
|
391 |
SwingUtilities.invokeLater(new Runnable() { |
|
392 |
public void run() { |
|
393 |
textArea.setText(text); |
|
394 |
textArea.setCaretPosition(0); |
|
395 |
} |
|
396 |
}); |
|
397 |
} |
|
398 |
}); |
|
399 |
} |
|
400 |
} |
|
401 |
||
402 |
private void doUpdate() { |
|
403 |
workerAdd(new Runnable() { |
|
404 |
public void run() { |
|
405 |
update(); |
|
406 |
} |
|
407 |
}); |
|
408 |
} |
|
409 |
||
410 |
||
411 |
private void detectDeadlock() { |
|
412 |
workerAdd(new Runnable() { |
|
413 |
public void run() { |
|
414 |
try { |
|
415 |
final Long[][] deadlockedThreads = getDeadlockedThreadIds(); |
|
416 |
||
417 |
if (deadlockedThreads == null || deadlockedThreads.length == 0) { |
|
418 |
// Display message for 30 seconds. Do it on a separate thread so |
|
419 |
// the sleep won't hold up the worker queue. |
|
420 |
// This will be replaced later by separate statusbar logic. |
|
421 |
new Thread() { |
|
422 |
public void run() { |
|
423 |
try { |
|
424 |
SwingUtilities.invokeAndWait(new Runnable() { |
|
425 |
public void run() { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
426 |
String msg = Messages.NO_DEADLOCK_DETECTED; |
2 | 427 |
messageLabel.setText(msg); |
428 |
threadListTabbedPane.revalidate(); |
|
429 |
} |
|
430 |
}); |
|
431 |
sleep(30 * 1000); |
|
432 |
} catch (InterruptedException ex) { |
|
433 |
// Ignore |
|
434 |
} catch (InvocationTargetException ex) { |
|
435 |
// Ignore |
|
436 |
} |
|
437 |
SwingUtilities.invokeLater(new Runnable() { |
|
438 |
public void run() { |
|
439 |
messageLabel.setText(""); |
|
440 |
} |
|
441 |
}); |
|
442 |
} |
|
443 |
}.start(); |
|
444 |
return; |
|
445 |
} |
|
446 |
||
447 |
SwingUtilities.invokeLater(new Runnable() { |
|
448 |
public void run() { |
|
449 |
// Remove old deadlock tabs |
|
450 |
while (threadListTabbedPane.getTabCount() > 1) { |
|
451 |
threadListTabbedPane.removeTabAt(1); |
|
452 |
} |
|
453 |
||
454 |
if (deadlockedThreads != null) { |
|
455 |
for (int i = 0; i < deadlockedThreads.length; i++) { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
456 |
DefaultListModel<Long> listModel = new DefaultListModel<Long>(); |
2 | 457 |
JTextArea textArea = new JTextArea(); |
458 |
textArea.setBorder(thinEmptyBorder); |
|
459 |
textArea.setEditable(false); |
|
460 |
setAccessibleName(textArea, |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
461 |
Messages.THREAD_TAB_THREAD_INFO_ACCESSIBLE_NAME); |
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
462 |
ThreadJList list = new ThreadJList(listModel, textArea); |
2 | 463 |
JScrollPane threadlistSP = new JScrollPane(list); |
464 |
JScrollPane textAreaSP = new JScrollPane(textArea); |
|
465 |
threadlistSP.setBorder(null); |
|
466 |
textAreaSP.setBorder(null); |
|
467 |
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, |
|
468 |
threadlistSP, textAreaSP); |
|
469 |
splitPane.setOneTouchExpandable(true); |
|
470 |
splitPane.setBorder(null); |
|
471 |
splitPane.setDividerLocation(threadsSplitPane.getDividerLocation()); |
|
472 |
String tabName; |
|
473 |
if (deadlockedThreads.length > 1) { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
474 |
tabName = Resources.format(Messages.DEADLOCK_TAB_N, i+1); |
2 | 475 |
} else { |
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
476 |
tabName = Messages.DEADLOCK_TAB; |
2 | 477 |
} |
478 |
threadListTabbedPane.addTab(tabName, splitPane); |
|
479 |
||
480 |
for (long t : deadlockedThreads[i]) { |
|
481 |
listModel.addElement(t); |
|
482 |
} |
|
483 |
} |
|
484 |
threadListTabbedPane.setSelectedIndex(1); |
|
485 |
} |
|
486 |
} |
|
487 |
}); |
|
488 |
} catch (IOException e) { |
|
489 |
// Ignore |
|
490 |
} catch (UndeclaredThrowableException e) { |
|
491 |
vmPanel.getProxyClient().markAsDead(); |
|
492 |
} |
|
493 |
} |
|
494 |
}); |
|
495 |
} |
|
496 |
||
497 |
||
498 |
// Return deadlocked threads or null |
|
499 |
public Long[][] getDeadlockedThreadIds() throws IOException { |
|
500 |
ProxyClient proxyClient = vmPanel.getProxyClient(); |
|
501 |
ThreadMXBean threadMBean = proxyClient.getThreadMXBean(); |
|
502 |
||
503 |
long[] ids = proxyClient.findDeadlockedThreads(); |
|
504 |
if (ids == null) { |
|
505 |
return null; |
|
506 |
} |
|
507 |
ThreadInfo[] infos = threadMBean.getThreadInfo(ids, Integer.MAX_VALUE); |
|
508 |
||
509 |
List<Long[]> dcycles = new ArrayList<Long[]>(); |
|
510 |
List<Long> cycle = new ArrayList<Long>(); |
|
511 |
||
512 |
// keep track of which thread is visited |
|
513 |
// one thread can only be in one cycle |
|
514 |
boolean[] visited = new boolean[ids.length]; |
|
515 |
||
516 |
int deadlockedThread = -1; // Index into arrays |
|
517 |
while (true) { |
|
518 |
if (deadlockedThread < 0) { |
|
519 |
if (cycle.size() > 0) { |
|
520 |
// a cycle found |
|
521 |
dcycles.add(cycle.toArray(new Long[0])); |
|
522 |
cycle = new ArrayList<Long>(); |
|
523 |
} |
|
524 |
// start a new cycle from a non-visited thread |
|
525 |
for (int j = 0; j < ids.length; j++) { |
|
526 |
if (!visited[j]) { |
|
527 |
deadlockedThread = j; |
|
528 |
visited[j] = true; |
|
529 |
break; |
|
530 |
} |
|
531 |
} |
|
532 |
if (deadlockedThread < 0) { |
|
533 |
// done |
|
534 |
break; |
|
535 |
} |
|
536 |
} |
|
537 |
||
538 |
cycle.add(ids[deadlockedThread]); |
|
539 |
long nextThreadId = infos[deadlockedThread].getLockOwnerId(); |
|
540 |
for (int j = 0; j < ids.length; j++) { |
|
541 |
ThreadInfo ti = infos[j]; |
|
542 |
if (ti.getThreadId() == nextThreadId) { |
|
543 |
if (visited[j]) { |
|
544 |
deadlockedThread = -1; |
|
545 |
} else { |
|
546 |
deadlockedThread = j; |
|
547 |
visited[j] = true; |
|
548 |
} |
|
549 |
break; |
|
550 |
} |
|
551 |
} |
|
552 |
} |
|
553 |
return dcycles.toArray(new Long[0][0]); |
|
554 |
} |
|
555 |
||
556 |
||
557 |
||
558 |
||
559 |
||
560 |
// ActionListener interface |
|
561 |
public void actionPerformed(ActionEvent evt) { |
|
562 |
String cmd = ((AbstractButton)evt.getSource()).getActionCommand(); |
|
563 |
||
564 |
if (cmd == "detectDeadlock") { |
|
565 |
messageLabel.setText(""); |
|
566 |
detectDeadlock(); |
|
567 |
} |
|
568 |
} |
|
569 |
||
570 |
||
571 |
||
572 |
// DocumentListener interface |
|
573 |
||
574 |
public void insertUpdate(DocumentEvent e) { |
|
575 |
doUpdate(); |
|
576 |
} |
|
577 |
||
578 |
public void removeUpdate(DocumentEvent e) { |
|
579 |
doUpdate(); |
|
580 |
} |
|
581 |
||
582 |
public void changedUpdate(DocumentEvent e) { |
|
583 |
doUpdate(); |
|
584 |
} |
|
585 |
||
586 |
||
587 |
||
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
588 |
private class ThreadJList extends JList<Long> { |
2 | 589 |
private JTextArea textArea; |
590 |
||
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
591 |
ThreadJList(DefaultListModel<Long> listModel, JTextArea textArea) { |
2 | 592 |
super(listModel); |
593 |
||
594 |
this.textArea = textArea; |
|
595 |
||
596 |
setBorder(thinEmptyBorder); |
|
597 |
||
19567
11f4bac9b4f3
6417721: Thread list should not allow multiple selection
egahlin
parents:
14342
diff
changeset
|
598 |
setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
19582
1cab3f8c48c7
6359971: Threads tab: Simple text to explain that one can click on a thread to get stack trace
egahlin
parents:
19567
diff
changeset
|
599 |
textArea.setText(Messages.THREAD_TAB_INITIAL_STACK_TRACE_MESSAGE); |
2 | 600 |
addListSelectionListener(ThreadTab.this); |
601 |
setCellRenderer(new DefaultListCellRenderer() { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
602 |
public Component getListCellRendererComponent(JList<?> list, Object value, int index, |
2 | 603 |
boolean isSelected, boolean cellHasFocus) { |
604 |
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
|
605 |
||
606 |
if (value != null) { |
|
607 |
String name = nameCache.get(value); |
|
608 |
if (name == null) { |
|
609 |
name = value.toString(); |
|
610 |
} |
|
611 |
setText(name); |
|
612 |
} |
|
613 |
return this; |
|
614 |
} |
|
615 |
}); |
|
616 |
} |
|
617 |
||
618 |
public Dimension getPreferredSize() { |
|
619 |
Dimension d = super.getPreferredSize(); |
|
620 |
d.width = Math.max(d.width, 100); |
|
621 |
return d; |
|
622 |
} |
|
623 |
} |
|
624 |
||
625 |
private class PromptingTextField extends JTextField implements FocusListener { |
|
626 |
private String prompt; |
|
627 |
boolean promptRemoved = false; |
|
628 |
Color fg; |
|
629 |
||
630 |
public PromptingTextField(String prompt, int columns) { |
|
631 |
super(prompt, columns); |
|
632 |
||
633 |
this.prompt = prompt; |
|
634 |
updateForeground(); |
|
635 |
addFocusListener(this); |
|
636 |
setAccessibleName(this, prompt); |
|
637 |
} |
|
638 |
||
639 |
@Override |
|
640 |
public void revalidate() { |
|
641 |
super.revalidate(); |
|
642 |
updateForeground(); |
|
643 |
} |
|
644 |
||
645 |
private void updateForeground() { |
|
646 |
this.fg = UIManager.getColor("TextField.foreground"); |
|
647 |
if (promptRemoved) { |
|
648 |
setForeground(fg); |
|
649 |
} else { |
|
650 |
setForeground(Color.gray); |
|
651 |
} |
|
652 |
} |
|
653 |
||
654 |
public String getText() { |
|
655 |
if (!promptRemoved) { |
|
656 |
return ""; |
|
657 |
} else { |
|
658 |
return super.getText(); |
|
659 |
} |
|
660 |
} |
|
661 |
||
662 |
public void focusGained(FocusEvent e) { |
|
663 |
if (!promptRemoved) { |
|
664 |
setText(""); |
|
665 |
setForeground(fg); |
|
666 |
promptRemoved = true; |
|
667 |
} |
|
668 |
} |
|
669 |
||
670 |
public void focusLost(FocusEvent e) { |
|
671 |
if (promptRemoved && getText().equals("")) { |
|
672 |
setText(prompt); |
|
673 |
setForeground(Color.gray); |
|
674 |
promptRemoved = false; |
|
675 |
} |
|
676 |
} |
|
677 |
||
678 |
} |
|
679 |
||
680 |
OverviewPanel[] getOverviewPanels() { |
|
681 |
if (overviewPanel == null) { |
|
682 |
overviewPanel = new ThreadOverviewPanel(); |
|
683 |
} |
|
684 |
return new OverviewPanel[] { overviewPanel }; |
|
685 |
} |
|
686 |
||
687 |
||
688 |
private static class ThreadOverviewPanel extends OverviewPanel { |
|
689 |
ThreadOverviewPanel() { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
690 |
super(Messages.THREADS, threadCountKey, Messages.LIVE_THREADS, null); |
2 | 691 |
} |
692 |
||
693 |
private void updateThreadsInfo(long tlCount, long tpCount, long ttCount, long timeStamp) { |
|
694 |
getPlotter().addValues(timeStamp, tlCount); |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
5506
diff
changeset
|
695 |
getInfoLabel().setText(Resources.format(infoLabelFormat, tlCount, tpCount, ttCount)); |
2 | 696 |
} |
697 |
} |
|
698 |
} |