use of java.lang.management.LockInfo in project egit by eclipse.
the class TestUtils method dumpThreads.
public static String dumpThreads() {
final StringBuilder dump = new StringBuilder();
final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
final ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(threadMXBean.isObjectMonitorUsageSupported(), threadMXBean.isSynchronizerUsageSupported());
for (ThreadInfo threadInfo : threadInfos) {
dump.append("Thread ").append(threadInfo.getThreadId()).append(' ').append(threadInfo.getThreadName()).append(' ').append(threadInfo.getThreadState()).append('\n');
LockInfo blocked = threadInfo.getLockInfo();
if (blocked != null) {
dump.append(" Waiting for ").append(blocked);
String lockOwner = threadInfo.getLockOwnerName();
if (lockOwner != null && !lockOwner.isEmpty()) {
dump.append(" held by ").append(lockOwner).append("(id=").append(threadInfo.getLockOwnerId()).append(')');
}
dump.append('\n');
}
for (LockInfo lock : threadInfo.getLockedSynchronizers()) {
dump.append(" Holding ").append(lock).append('\n');
}
for (StackTraceElement s : threadInfo.getStackTrace()) {
dump.append(" at ").append(s).append('\n');
}
}
return dump.toString();
}
use of java.lang.management.LockInfo in project nifi-registry by apache.
the class BootstrapListener method writeDump.
private static void writeDump(final OutputStream out) throws IOException {
final ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
final ThreadInfo[] infos = mbean.dumpAllThreads(true, true);
final long[] deadlockedThreadIds = mbean.findDeadlockedThreads();
final long[] monitorDeadlockThreadIds = mbean.findMonitorDeadlockedThreads();
final List<ThreadInfo> sortedInfos = new ArrayList<>(infos.length);
for (final ThreadInfo info : infos) {
sortedInfos.add(info);
}
Collections.sort(sortedInfos, new Comparator<ThreadInfo>() {
@Override
public int compare(ThreadInfo o1, ThreadInfo o2) {
return o1.getThreadName().toLowerCase().compareTo(o2.getThreadName().toLowerCase());
}
});
final StringBuilder sb = new StringBuilder();
for (final ThreadInfo info : sortedInfos) {
sb.append("\n");
sb.append("\"").append(info.getThreadName()).append("\" Id=");
sb.append(info.getThreadId()).append(" ");
sb.append(info.getThreadState().toString()).append(" ");
switch(info.getThreadState()) {
case BLOCKED:
case TIMED_WAITING:
case WAITING:
sb.append(" on ");
sb.append(info.getLockInfo());
break;
default:
break;
}
if (info.isSuspended()) {
sb.append(" (suspended)");
}
if (info.isInNative()) {
sb.append(" (in native code)");
}
if (deadlockedThreadIds != null && deadlockedThreadIds.length > 0) {
for (final long id : deadlockedThreadIds) {
if (id == info.getThreadId()) {
sb.append(" ** DEADLOCKED THREAD **");
}
}
}
if (monitorDeadlockThreadIds != null && monitorDeadlockThreadIds.length > 0) {
for (final long id : monitorDeadlockThreadIds) {
if (id == info.getThreadId()) {
sb.append(" ** MONITOR-DEADLOCKED THREAD **");
}
}
}
final StackTraceElement[] stackTraces = info.getStackTrace();
for (final StackTraceElement element : stackTraces) {
sb.append("\n\tat ").append(element);
final MonitorInfo[] monitors = info.getLockedMonitors();
for (final MonitorInfo monitor : monitors) {
if (monitor.getLockedStackFrame().equals(element)) {
sb.append("\n\t- waiting on ").append(monitor);
}
}
}
final LockInfo[] lockInfos = info.getLockedSynchronizers();
if (lockInfos.length > 0) {
sb.append("\n\t");
sb.append("Number of Locked Synchronizers: ").append(lockInfos.length);
for (final LockInfo lockInfo : lockInfos) {
sb.append("\n\t- ").append(lockInfo.toString());
}
}
sb.append("\n");
}
if (deadlockedThreadIds != null && deadlockedThreadIds.length > 0) {
sb.append("\n\nDEADLOCK DETECTED!");
sb.append("\nThe following thread IDs are deadlocked:");
for (final long id : deadlockedThreadIds) {
sb.append("\n").append(id);
}
}
if (monitorDeadlockThreadIds != null && monitorDeadlockThreadIds.length > 0) {
sb.append("\n\nMONITOR DEADLOCK DETECTED!");
sb.append("\nThe following thread IDs are deadlocked:");
for (final long id : monitorDeadlockThreadIds) {
sb.append("\n").append(id);
}
}
writer.write(sb.toString());
writer.flush();
}
use of java.lang.management.LockInfo in project logging-log4j2 by apache.
the class ExtendedThreadInformation method printStack.
@Override
public void printStack(final StringBuilder sb, final StackTraceElement[] stack) {
int i = 0;
for (final StackTraceElement element : stack) {
sb.append("\tat ").append(element.toString());
sb.append('\n');
if (i == 0 && threadInfo.getLockInfo() != null) {
final Thread.State ts = threadInfo.getThreadState();
switch(ts) {
case BLOCKED:
sb.append("\t- blocked on ");
formatLock(sb, threadInfo.getLockInfo());
sb.append('\n');
break;
case WAITING:
sb.append("\t- waiting on ");
formatLock(sb, threadInfo.getLockInfo());
sb.append('\n');
break;
case TIMED_WAITING:
sb.append("\t- waiting on ");
formatLock(sb, threadInfo.getLockInfo());
sb.append('\n');
break;
default:
}
}
for (final MonitorInfo mi : threadInfo.getLockedMonitors()) {
if (mi.getLockedStackDepth() == i) {
sb.append("\t- locked ");
formatLock(sb, mi);
sb.append('\n');
}
}
++i;
}
final LockInfo[] locks = threadInfo.getLockedSynchronizers();
if (locks.length > 0) {
sb.append("\n\tNumber of locked synchronizers = ").append(locks.length).append('\n');
for (final LockInfo li : locks) {
sb.append("\t- ");
formatLock(sb, li);
sb.append('\n');
}
}
}
use of java.lang.management.LockInfo in project samza by apache.
the class ThreadUtil method toString.
/**
* Copy of ThreadInfo#toString() without the hardcoded MAX_FRAMES = 8 restriction on thread stack depth.
*
* Returns a string representation of this thread info.
* The format of this string depends on the implementation.
* The returned string will typically include
* the thread name, the getThreadId thread ID, its state,
* and a stack trace if any.
*
* @return a string representation of this thread info.
*/
private static String toString(ThreadInfo info) {
StringBuilder sb = new StringBuilder("\"" + info.getThreadName() + "\"" + " Id=" + info.getThreadId() + " " + info.getThreadState());
if (info.getLockName() != null) {
sb.append(" on " + info.getLockName());
}
if (info.getLockOwnerName() != null) {
sb.append(" owned by \"" + info.getLockOwnerName() + "\" Id=" + info.getLockOwnerId());
}
if (info.isSuspended()) {
sb.append(" (suspended)");
}
if (info.isInNative()) {
sb.append(" (in native)");
}
sb.append('\n');
int i = 0;
for (; i < info.getStackTrace().length; i++) {
StackTraceElement ste = info.getStackTrace()[i];
sb.append("\tat " + ste.toString());
sb.append('\n');
if (i == 0 && info.getLockInfo() != null) {
Thread.State ts = info.getThreadState();
switch(ts) {
case BLOCKED:
sb.append("\t- blocked on " + info.getLockInfo());
sb.append('\n');
break;
case WAITING:
sb.append("\t- waiting on " + info.getLockInfo());
sb.append('\n');
break;
case TIMED_WAITING:
sb.append("\t- waiting on " + info.getLockInfo());
sb.append('\n');
break;
default:
}
}
for (MonitorInfo mi : info.getLockedMonitors()) {
if (mi.getLockedStackDepth() == i) {
sb.append("\t- locked " + mi);
sb.append('\n');
}
}
}
if (i < info.getStackTrace().length) {
sb.append("\t...");
sb.append('\n');
}
LockInfo[] locks = info.getLockedSynchronizers();
if (locks.length > 0) {
sb.append("\n\tNumber of locked synchronizers = " + locks.length);
sb.append('\n');
for (LockInfo li : locks) {
sb.append("\t- " + li);
sb.append('\n');
}
}
sb.append('\n');
return sb.toString();
}
use of java.lang.management.LockInfo in project pinpoint by naver.
the class ThreadDumpUtils method setLockInfo.
private static void setLockInfo(ThreadDumpMetricSnapshot threadDump, ThreadInfo threadInfo) {
threadDump.setLockName(threadInfo.getLockName());
threadDump.setLockOwnerId(threadInfo.getLockOwnerId());
threadDump.setLockOwnerName(threadInfo.getLockOwnerName());
LockInfo[] lockInfos = threadInfo.getLockedSynchronizers();
if (lockInfos != null) {
for (LockInfo lockInfo : lockInfos) {
if (lockInfo == null) {
continue;
}
threadDump.addLockedSynchronizer(lockInfo.toString());
}
} else {
threadDump.setLockedSynchronizers(Collections.<String>emptyList());
}
}
Aggregations