Search in sources :

Example 41 with MonitorInfo

use of java.lang.management.MonitorInfo in project indy by Commonjava.

the class ThreadDumper method dumpThreads.

public static void dumpThreads() {
    StringBuilder sb = new StringBuilder();
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);
    Stream.of(threadInfos).forEachOrdered((ti) -> {
        if (sb.length() > 0) {
            sb.append("\n\n");
        }
        sb.append(ti.getThreadName()).append("\n  State: ").append(ti.getThreadState()).append("\n  Lock Info: ").append(ti.getLockInfo()).append("\n  Monitors:");
        MonitorInfo[] monitors = ti.getLockedMonitors();
        if (monitors == null || monitors.length < 1) {
            sb.append("  -NONE-");
        } else {
            sb.append("\n  - ").append(join(monitors, "\n  - "));
        }
        sb.append("\n  Trace:\n    ").append(join(ti.getStackTrace(), "\n    "));
    });
    System.out.println(sb);
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) MonitorInfo(java.lang.management.MonitorInfo)

Example 42 with MonitorInfo

use of java.lang.management.MonitorInfo in project indy by Commonjava.

the class DiagnosticsManager method getThreadDumpString.

public String getThreadDumpString() {
    StringBuilder sb = new StringBuilder();
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);
    Stream.of(threadInfos).forEachOrdered((ti) -> {
        if (sb.length() > 0) {
            sb.append("\n\n");
        }
        sb.append(ti.getThreadName()).append("\n  State: ").append(ti.getThreadState()).append("\n  Lock Info: ").append(ti.getLockInfo()).append("\n  Monitors:");
        MonitorInfo[] monitors = ti.getLockedMonitors();
        if (monitors == null || monitors.length < 1) {
            sb.append("  -NONE-");
        } else {
            sb.append("\n  - ").append(join(monitors, "\n  - "));
        }
        sb.append("\n  Trace:\n    ").append(join(ti.getStackTrace(), "\n    "));
    });
    return sb.toString();
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) MonitorInfo(java.lang.management.MonitorInfo)

Example 43 with MonitorInfo

use of java.lang.management.MonitorInfo in project jdk8u_jdk by JetBrains.

the class ThreadInfoCompositeData method lockedMonitors.

public MonitorInfo[] lockedMonitors() {
    CompositeData[] lockedMonitorsData = (CompositeData[]) cdata.get(LOCKED_MONITORS);
    // The LockedMonitors item cannot be null, but if it is we will get
    // a NullPointerException when we ask for its length.
    MonitorInfo[] monitors = new MonitorInfo[lockedMonitorsData.length];
    for (int i = 0; i < lockedMonitorsData.length; i++) {
        CompositeData cdi = lockedMonitorsData[i];
        monitors[i] = MonitorInfo.from(cdi);
    }
    return monitors;
}
Also used : MonitorInfo(java.lang.management.MonitorInfo) CompositeData(javax.management.openmbean.CompositeData)

Example 44 with MonitorInfo

use of java.lang.management.MonitorInfo in project jdk8u_jdk by JetBrains.

the class ThreadInfoCompositeData method getCompositeData.

protected CompositeData getCompositeData() {
    // Convert StackTraceElement[] to CompositeData[]
    StackTraceElement[] stackTrace = threadInfo.getStackTrace();
    CompositeData[] stackTraceData = new CompositeData[stackTrace.length];
    for (int i = 0; i < stackTrace.length; i++) {
        StackTraceElement ste = stackTrace[i];
        stackTraceData[i] = StackTraceElementCompositeData.toCompositeData(ste);
    }
    // Convert MonitorInfo[] and LockInfo[] to CompositeData[]
    CompositeData lockInfoData = LockInfoCompositeData.toCompositeData(threadInfo.getLockInfo());
    // Convert LockInfo[] and MonitorInfo[] to CompositeData[]
    LockInfo[] lockedSyncs = threadInfo.getLockedSynchronizers();
    CompositeData[] lockedSyncsData = new CompositeData[lockedSyncs.length];
    for (int i = 0; i < lockedSyncs.length; i++) {
        LockInfo li = lockedSyncs[i];
        lockedSyncsData[i] = LockInfoCompositeData.toCompositeData(li);
    }
    MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();
    CompositeData[] lockedMonitorsData = new CompositeData[lockedMonitors.length];
    for (int i = 0; i < lockedMonitors.length; i++) {
        MonitorInfo mi = lockedMonitors[i];
        lockedMonitorsData[i] = MonitorInfoCompositeData.toCompositeData(mi);
    }
    // CONTENTS OF THIS ARRAY MUST BE SYNCHRONIZED WITH
    // threadInfoItemNames!
    final Object[] threadInfoItemValues = { new Long(threadInfo.getThreadId()), threadInfo.getThreadName(), threadInfo.getThreadState().name(), new Long(threadInfo.getBlockedTime()), new Long(threadInfo.getBlockedCount()), new Long(threadInfo.getWaitedTime()), new Long(threadInfo.getWaitedCount()), lockInfoData, threadInfo.getLockName(), new Long(threadInfo.getLockOwnerId()), threadInfo.getLockOwnerName(), stackTraceData, new Boolean(threadInfo.isSuspended()), new Boolean(threadInfo.isInNative()), lockedMonitorsData, lockedSyncsData };
    try {
        return new CompositeDataSupport(threadInfoCompositeType, threadInfoItemNames, threadInfoItemValues);
    } catch (OpenDataException e) {
        // Should never reach here
        throw new AssertionError(e);
    }
}
Also used : MonitorInfo(java.lang.management.MonitorInfo) CompositeData(javax.management.openmbean.CompositeData) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) OpenDataException(javax.management.openmbean.OpenDataException) LockInfo(java.lang.management.LockInfo)

Example 45 with MonitorInfo

use of java.lang.management.MonitorInfo in project dubbo by alibaba.

the class JVMUtil method getThreadDumpString.

private static String getThreadDumpString(ThreadInfo threadInfo) {
    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[] stackTrace = threadInfo.getStackTrace();
    MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();
    for (; i < stackTrace.length && i < 32; i++) {
        StackTraceElement ste = stackTrace[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:
                    sb.append("\t-  waiting on " + threadInfo.getLockInfo());
                    sb.append('\n');
                    break;
                case TIMED_WAITING:
                    sb.append("\t-  waiting on " + threadInfo.getLockInfo());
                    sb.append('\n');
                    break;
                default:
            }
        }
        for (MonitorInfo mi : lockedMonitors) {
            if (mi.getLockedStackDepth() == i) {
                sb.append("\t-  locked " + mi);
                sb.append('\n');
            }
        }
    }
    if (i < stackTrace.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)

Aggregations

MonitorInfo (java.lang.management.MonitorInfo)77 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