提交 cb29f42e authored 作者: noelgrandin@gmail.com's avatar noelgrandin@gmail.com

simplify sorting of monitors, and fix the stack trace output so we don't skip frames

上级 ae0e5a94
...@@ -10,6 +10,8 @@ import java.lang.management.MonitorInfo; ...@@ -10,6 +10,8 @@ import java.lang.management.MonitorInfo;
import java.lang.management.ThreadInfo; import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean; import java.lang.management.ThreadMXBean;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -105,10 +107,13 @@ public class AbbaLockingDetector implements Runnable { ...@@ -105,10 +107,13 @@ public class AbbaLockingDetector implements Runnable {
*/ */
private static void generateOrdering(final List<String> lockOrder, ThreadInfo info) { private static void generateOrdering(final List<String> lockOrder, ThreadInfo info) {
final MonitorInfo[] lockedMonitors = info.getLockedMonitors(); final MonitorInfo[] lockedMonitors = info.getLockedMonitors();
final StackTraceElement[] stackTrace = info.getStackTrace(); Arrays.sort(lockedMonitors, new Comparator<MonitorInfo>() {
for (int i = stackTrace.length - 1; i >= 0; i--) { @Override
public int compare(MonitorInfo a, MonitorInfo b) {
return b.getLockedStackDepth() - a.getLockedStackDepth();
}
});
for (MonitorInfo mi : lockedMonitors) { for (MonitorInfo mi : lockedMonitors) {
if (mi.getLockedStackDepth() == i) {
String lockName = getObjectName(mi); String lockName = getObjectName(mi);
if (lockName.equals("sun.misc.Launcher$AppClassLoader")) { if (lockName.equals("sun.misc.Launcher$AppClassLoader")) {
// ignore, it shows up everywhere // ignore, it shows up everywhere
...@@ -121,8 +126,6 @@ public class AbbaLockingDetector implements Runnable { ...@@ -121,8 +126,6 @@ public class AbbaLockingDetector implements Runnable {
} }
} }
} }
}
}
private void markHigher(List<String> lockOrder, ThreadInfo threadInfo) { private void markHigher(List<String> lockOrder, ThreadInfo threadInfo) {
String topLock = lockOrder.get(lockOrder.size() - 1); String topLock = lockOrder.get(lockOrder.size() - 1);
...@@ -179,20 +182,23 @@ public class AbbaLockingDetector implements Runnable { ...@@ -179,20 +182,23 @@ public class AbbaLockingDetector implements Runnable {
sb.append(" (in native)"); sb.append(" (in native)");
} }
sb.append('\n'); sb.append('\n');
StackTraceElement[] stackTrace = info.getStackTrace(); final StackTraceElement[] stackTrace = info.getStackTrace();
final MonitorInfo[] lockedMonitors = info.getLockedMonitors();
boolean startDumping = false;
for (int i = 0; i < stackTrace.length; i++) { for (int i = 0; i < stackTrace.length; i++) {
boolean dumpedStackTraceElement = false;
StackTraceElement ste = stackTrace[i]; StackTraceElement ste = stackTrace[i];
if (startDumping) {
dumpStackTraceElement(info, sb, i, ste);
}
MonitorInfo[] lockedMonitors = info.getLockedMonitors();
for (MonitorInfo mi : lockedMonitors) { for (MonitorInfo mi : lockedMonitors) {
if (mi.getLockedStackDepth() == i) { if (mi.getLockedStackDepth() == i) {
// Only start dumping the stack from the first time we lock // Only start dumping the stack from the first time we lock
// something. // something.
// Removes a lot of unnecessary noise from the output. // Removes a lot of unnecessary noise from the output.
if (!dumpedStackTraceElement) { if (!startDumping) {
dumpStackTraceElement(info, sb, i, ste); dumpStackTraceElement(info, sb, i, ste);
dumpedStackTraceElement = true; startDumping = true;
} }
sb.append("\t- locked " + mi); sb.append("\t- locked " + mi);
sb.append('\n'); sb.append('\n');
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论