Search in sources :

Example 61 with MonitorInfo

use of java.lang.management.MonitorInfo in project btrace by btraceio.

the class BTraceRuntime method deadlocks.

static void deadlocks(boolean stackTrace) {
    initThreadMBean();
    if (threadMBean.isSynchronizerUsageSupported()) {
        long[] tids = threadMBean.findDeadlockedThreads();
        if (tids != null && tids.length > 0) {
            ThreadInfo[] infos = threadMBean.getThreadInfo(tids, true, true);
            StringBuilder sb = new StringBuilder();
            for (ThreadInfo ti : infos) {
                sb.append("\"").append(ti.getThreadName()).append("\"" + " Id=").append(ti.getThreadId()).append(" in ").append(ti.getThreadState());
                if (ti.getLockName() != null) {
                    sb.append(" on lock=").append(ti.getLockName());
                }
                if (ti.isSuspended()) {
                    sb.append(" (suspended)");
                }
                if (ti.isInNative()) {
                    sb.append(" (running in native)");
                }
                if (ti.getLockOwnerName() != null) {
                    sb.append(INDENT).append(" owned by ").append(ti.getLockOwnerName()).append(" Id=").append(ti.getLockOwnerId());
                    sb.append(LINE_SEPARATOR);
                }
                if (stackTrace) {
                    // print stack trace with locks
                    StackTraceElement[] stacktrace = ti.getStackTrace();
                    MonitorInfo[] monitors = ti.getLockedMonitors();
                    for (int i = 0; i < stacktrace.length; i++) {
                        StackTraceElement ste = stacktrace[i];
                        sb.append(INDENT).append("at ").append(ste.toString());
                        sb.append(LINE_SEPARATOR);
                        for (MonitorInfo mi : monitors) {
                            if (mi.getLockedStackDepth() == i) {
                                sb.append(INDENT).append("  - locked ").append(mi);
                                sb.append(LINE_SEPARATOR);
                            }
                        }
                    }
                    sb.append(LINE_SEPARATOR);
                }
                LockInfo[] locks = ti.getLockedSynchronizers();
                sb.append(INDENT).append("Locked synchronizers: count = ").append(locks.length);
                sb.append(LINE_SEPARATOR);
                for (LockInfo li : locks) {
                    sb.append(INDENT).append("  - ").append(li);
                    sb.append(LINE_SEPARATOR);
                }
                sb.append(LINE_SEPARATOR);
            }
            getCurrent().send(sb.toString());
        }
    }
}
Also used : ThreadInfo(java.lang.management.ThreadInfo) MonitorInfo(java.lang.management.MonitorInfo) LockInfo(java.lang.management.LockInfo)

Example 62 with MonitorInfo

use of java.lang.management.MonitorInfo in project randomizedtesting by randomizedtesting.

the class Threads method append.

/**
 * Dump {@link ThreadInfo} information.
 */
public static void append(StringBuilder b, ThreadInfo ti) {
    b.append('"').append(ti.getThreadName()).append('"');
    b.append(" ID=").append(ti.getThreadId());
    final State threadState = ti.getThreadState();
    b.append(" ").append(threadState);
    if (ti.getLockName() != null) {
        b.append(" on ").append(ti.getLockName());
    }
    if (ti.getLockOwnerName() != null) {
        b.append(" owned by \"").append(ti.getLockOwnerName()).append("\" ID=").append(ti.getLockOwnerId());
    }
    b.append(ti.isSuspended() ? " (suspended)" : "");
    b.append(ti.isInNative() ? " (in native code)" : "");
    b.append("\n");
    final StackTraceElement[] stack = ti.getStackTrace();
    final LockInfo lockInfo = ti.getLockInfo();
    final MonitorInfo[] monitorInfos = ti.getLockedMonitors();
    for (int i = 0; i < stack.length; i++) {
        b.append("\tat ").append(stack[i]).append("\n");
        if (i == 0 && lockInfo != null) {
            b.append("\t- ").append(lockInfoStrings.get(threadState)).append(lockInfo).append("\n");
        }
        for (MonitorInfo mi : monitorInfos) {
            if (mi.getLockedStackDepth() == i) {
                b.append("\t- locked ").append(mi).append("\n");
            }
        }
    }
    LockInfo[] lockInfos = ti.getLockedSynchronizers();
    if (lockInfos.length > 0) {
        b.append("\tLocked synchronizers:\n");
        for (LockInfo li : ti.getLockedSynchronizers()) {
            b.append("\t- ").append(li).append("\n");
        }
    }
    b.append("\n");
}
Also used : MonitorInfo(java.lang.management.MonitorInfo) State(java.lang.Thread.State) LockInfo(java.lang.management.LockInfo)

Example 63 with MonitorInfo

use of java.lang.management.MonitorInfo in project flink by apache.

the class ThreadDumpInfo method stringifyThreadInfo.

/**
 * Custom stringify format of JVM thread info to bypass the MAX_FRAMES = 8 limitation.
 *
 * <p>This method is based on
 * https://github.com/openjdk/jdk/blob/master/src/java.management/share/classes/java/lang/management/ThreadInfo.java#L597
 */
@VisibleForTesting
protected static String stringifyThreadInfo(java.lang.management.ThreadInfo threadInfo, int maxDepth) {
    StringBuilder sb = new StringBuilder("\"" + threadInfo.getThreadName() + "\"" + " Id=" + threadInfo.getThreadId() + " " + threadInfo.getThreadState());
    if (threadInfo.getLockName() != null) {
        sb.append(" on " + threadInfo.getLockName());
    }
    if (threadInfo.getLockOwnerName() != null) {
        sb.append(" owned by \"" + threadInfo.getLockOwnerName() + "\" Id=" + threadInfo.getLockOwnerId());
    }
    if (threadInfo.isSuspended()) {
        sb.append(" (suspended)");
    }
    if (threadInfo.isInNative()) {
        sb.append(" (in native)");
    }
    sb.append('\n');
    int i = 0;
    StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
    for (; i < stackTraceElements.length && i < maxDepth; i++) {
        StackTraceElement ste = stackTraceElements[i];
        sb.append("\tat " + ste.toString());
        sb.append('\n');
        if (i == 0 && threadInfo.getLockInfo() != null) {
            Thread.State ts = threadInfo.getThreadState();
            switch(ts) {
                case BLOCKED:
                    sb.append("\t-  blocked on " + threadInfo.getLockInfo());
                    sb.append('\n');
                    break;
                case WAITING:
                case TIMED_WAITING:
                    sb.append("\t-  waiting on " + threadInfo.getLockInfo());
                    sb.append('\n');
                    break;
                default:
            }
        }
        for (MonitorInfo mi : threadInfo.getLockedMonitors()) {
            if (mi.getLockedStackDepth() == i) {
                sb.append("\t-  locked " + mi);
                sb.append('\n');
            }
        }
    }
    if (i < threadInfo.getStackTrace().length) {
        sb.append("\t...");
        sb.append('\n');
    }
    LockInfo[] locks = threadInfo.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();
}
Also used : MonitorInfo(java.lang.management.MonitorInfo) LockInfo(java.lang.management.LockInfo) VisibleForTesting(org.apache.flink.annotation.VisibleForTesting)

Example 64 with MonitorInfo

use of java.lang.management.MonitorInfo in project neo4j by neo4j.

the class DumpUtils method threadDump.

/**
 * Creates threads dump and try to mimic JVM stack trace as much as possible to allow existing analytics tools to be used
 *
 * @param threadMxBean bean to use for thread dump
 * @param systemProperties dumped vm system properties
 * @return string that contains thread dump
 */
public static String threadDump(ThreadMXBean threadMxBean, Properties systemProperties) {
    ThreadInfo[] threadInfos = threadMxBean.dumpAllThreads(true, true);
    // Reproduce JVM stack trace as far as possible to allow existing analytics tools to be used
    String vmName = systemProperties.getProperty("java.vm.name");
    String vmVersion = systemProperties.getProperty("java.vm.version");
    String vmInfoString = systemProperties.getProperty("java.vm.info");
    StringBuilder sb = new StringBuilder();
    sb.append(String.format("Full thread dump %s (%s %s):\n\n", vmName, vmVersion, vmInfoString));
    for (ThreadInfo threadInfo : threadInfos) {
        sb.append(String.format("\"%s\" #%d\n", threadInfo.getThreadName(), threadInfo.getThreadId()));
        sb.append("   java.lang.Thread.State: ").append(threadInfo.getThreadState()).append("\n");
        StackTraceElement[] stackTrace = threadInfo.getStackTrace();
        for (int i = 0; i < stackTrace.length; i++) {
            StackTraceElement e = stackTrace[i];
            sb.append("\tat ").append(e).append('\n');
            // First stack element info can be found in the thread state
            if (i == 0 && threadInfo.getLockInfo() != null) {
                Thread.State ts = threadInfo.getThreadState();
                switch(ts) {
                    case BLOCKED:
                        sb.append("\t-  blocked on ").append(threadInfo.getLockInfo()).append('\n');
                        break;
                    case WAITING:
                        sb.append("\t-  waiting on ").append(threadInfo.getLockInfo()).append('\n');
                        break;
                    case TIMED_WAITING:
                        sb.append("\t-  waiting on ").append(threadInfo.getLockInfo()).append('\n');
                        break;
                    default:
                }
            }
            for (MonitorInfo mi : threadInfo.getLockedMonitors()) {
                if (mi.getLockedStackDepth() == i) {
                    sb.append("\t-  locked ").append(mi).append('\n');
                }
            }
        }
    }
    return sb.toString();
}
Also used : ThreadInfo(java.lang.management.ThreadInfo) MonitorInfo(java.lang.management.MonitorInfo)

Example 65 with MonitorInfo

use of java.lang.management.MonitorInfo in project neo4j by neo4j.

the class DumpUtils method threadDump.

/**
 * Creates threads dump and try to mimic JVM stack trace as much as possible to allow existing analytics tools to be used
 *
 * @param threadMxBean bean to use for thread dump
 * @param systemProperties dumped vm system properties
 * @return string that contains thread dump
 */
public static String threadDump(ThreadMXBean threadMxBean, Properties systemProperties) {
    ThreadInfo[] threadInfos = threadMxBean.dumpAllThreads(true, true);
    // Reproduce JVM stack trace as far as possible to allow existing analytics tools to be used
    String vmName = systemProperties.getProperty("java.vm.name");
    String vmVersion = systemProperties.getProperty("java.vm.version");
    String vmInfoString = systemProperties.getProperty("java.vm.info");
    StringBuilder sb = new StringBuilder();
    sb.append(String.format("Full thread dump %s (%s %s):\n\n", vmName, vmVersion, vmInfoString));
    for (ThreadInfo threadInfo : threadInfos) {
        sb.append(String.format("\"%s\" #%d\n", threadInfo.getThreadName(), threadInfo.getThreadId()));
        sb.append("   java.lang.Thread.State: ").append(threadInfo.getThreadState()).append("\n");
        StackTraceElement[] stackTrace = threadInfo.getStackTrace();
        for (int i = 0; i < stackTrace.length; i++) {
            StackTraceElement e = stackTrace[i];
            sb.append("\tat ").append(e).append('\n');
            // First stack element info can be found in the thread state
            if (i == 0 && threadInfo.getLockInfo() != null) {
                Thread.State ts = threadInfo.getThreadState();
                switch(ts) {
                    case BLOCKED:
                        sb.append("\t-  blocked on ").append(threadInfo.getLockInfo()).append('\n');
                        break;
                    case WAITING:
                        sb.append("\t-  waiting on ").append(threadInfo.getLockInfo()).append('\n');
                        break;
                    case TIMED_WAITING:
                        sb.append("\t-  waiting on ").append(threadInfo.getLockInfo()).append('\n');
                        break;
                    default:
                }
            }
            for (MonitorInfo mi : threadInfo.getLockedMonitors()) {
                if (mi.getLockedStackDepth() == i) {
                    sb.append("\t-  locked ").append(mi).append('\n');
                }
            }
        }
    }
    return sb.toString();
}
Also used : ThreadInfo(java.lang.management.ThreadInfo) MonitorInfo(java.lang.management.MonitorInfo)

Aggregations

MonitorInfo (java.lang.management.MonitorInfo)78 LockInfo (java.lang.management.LockInfo)41 ThreadInfo (java.lang.management.ThreadInfo)18 ThreadMXBean (java.lang.management.ThreadMXBean)10 Test (org.testng.annotations.Test)8 CompositeData (javax.management.openmbean.CompositeData)7 OutputStreamWriter (java.io.OutputStreamWriter)6 TMonitorInfo (com.navercorp.pinpoint.thrift.dto.command.TMonitorInfo)3 BufferedWriter (java.io.BufferedWriter)3 PrintWriter (java.io.PrintWriter)3 ArrayList (java.util.ArrayList)3 TThreadDump (com.navercorp.pinpoint.thrift.dto.command.TThreadDump)2 State (java.lang.Thread.State)2 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)2 OpenDataException (javax.management.openmbean.OpenDataException)2 MonitorInfoMetricSnapshot (com.navercorp.pinpoint.profiler.monitor.metric.deadlock.MonitorInfoMetricSnapshot)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 CompositeType (javax.management.openmbean.CompositeType)1 VisibleForTesting (org.apache.flink.annotation.VisibleForTesting)1