Search in sources :

Example 41 with LockInfo

use of java.lang.management.LockInfo 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 42 with LockInfo

use of java.lang.management.LockInfo in project sling by apache.

the class ControlListener method dumpThreads.

// ---------- socket support
private void dumpThreads(final Socket socket) throws IOException {
    final ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    final ThreadInfo[] threadInfos = threadBean.dumpAllThreads(true, true);
    for (ThreadInfo thread : threadInfos) {
        printThread(socket, thread);
        // add locked synchronizers
        final LockInfo[] locks = thread.getLockedSynchronizers();
        writeLine(socket, "-");
        writeLine(socket, "-   Locked ownable synchronizers:");
        if (locks.length > 0) {
            for (LockInfo li : locks) {
                writeLine(socket, String.format("-        - locked %s", formatLockInfo(li.getClassName(), li.getIdentityHashCode())));
            }
        } else {
            writeLine(socket, "-        - None");
        }
        // empty separator line
        writeLine(socket, "-");
    }
    final long[] deadLocked;
    if (threadBean.isSynchronizerUsageSupported()) {
        deadLocked = threadBean.findDeadlockedThreads();
    } else {
        deadLocked = threadBean.findMonitorDeadlockedThreads();
    }
    if (deadLocked != null) {
        final ThreadInfo[] dl = threadBean.getThreadInfo(deadLocked, true, true);
        final Set<ThreadInfo> dlSet = new HashSet<ThreadInfo>(Arrays.asList(dl));
        int deadlockCount = 0;
        for (ThreadInfo current : dl) {
            if (dlSet.remove(current)) {
                // find and record a single deadlock
                ArrayList<ThreadInfo> loop = new ArrayList<ThreadInfo>();
                do {
                    loop.add(current);
                    for (ThreadInfo cand : dl) {
                        if (cand.getThreadId() == current.getLockOwnerId()) {
                            current = (dlSet.remove(cand)) ? cand : null;
                            break;
                        }
                    }
                } while (current != null);
                deadlockCount++;
                // print the deadlock
                writeLine(socket, "-Found one Java-level deadlock:");
                writeLine(socket, "-=============================");
                for (ThreadInfo thread : loop) {
                    writeLine(socket, String.format("-\"%s\" #%d", thread.getThreadName(), thread.getThreadId()));
                    writeLine(socket, String.format("-  waiting on %s", formatLockInfo(thread.getLockInfo().getClassName(), thread.getLockInfo().getIdentityHashCode())));
                    writeLine(socket, String.format("-  which is held by \"%s\" #%d", thread.getLockOwnerName(), thread.getLockOwnerId()));
                }
                writeLine(socket, "-");
                writeLine(socket, "-Java stack information for the threads listed above:");
                writeLine(socket, "-===================================================");
                for (ThreadInfo thread : loop) {
                    printThread(socket, thread);
                }
                writeLine(socket, "-");
            }
        }
        //            "Thread-8":
        //                waiting to lock monitor 7f89fb80da08 (object 7f37a0968, a java.lang.Object),
        //                which is held by "Thread-7"
        //              "Thread-7":
        //                waiting to lock monitor 7f89fb80b0b0 (object 7f37a0958, a java.lang.Object),
        //                which is held by "Thread-8"
        writeLine(socket, String.format("-Found %d deadlocks.", deadlockCount));
    }
    writeLine(socket, RESPONSE_OK);
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) ArrayList(java.util.ArrayList) LockInfo(java.lang.management.LockInfo) HashSet(java.util.HashSet)

Example 43 with LockInfo

use of java.lang.management.LockInfo 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)

Example 44 with LockInfo

use of java.lang.management.LockInfo in project com.revolsys.open by revolsys.

the class DeadlockLogger method toString.

private Object toString(final ThreadInfo threadInfo) {
    final String threadName = threadInfo.getThreadName();
    final long threadId = threadInfo.getThreadId();
    final State threadState = threadInfo.getThreadState();
    final StringBuilder sb = new StringBuilder("\"" + threadName + "\"" + " Id=" + threadId + " " + threadState);
    final String lockName = threadInfo.getLockName();
    if (lockName != null) {
        sb.append(" on " + lockName);
    }
    final String lockOwnerName = threadInfo.getLockOwnerName();
    if (lockOwnerName != null) {
        final long lockOwnerId = threadInfo.getLockOwnerId();
        sb.append(" owned by \"" + lockOwnerName + "\" Id=" + lockOwnerId);
    }
    if (threadInfo.isSuspended()) {
        sb.append(" (suspended)");
    }
    if (threadInfo.isInNative()) {
        sb.append(" (in native)");
    }
    sb.append('\n');
    final StackTraceElement[] stackTrace = threadInfo.getStackTrace();
    int i = 0;
    for (; i < stackTrace.length && i < 500; i++) {
        final StackTraceElement ste = stackTrace[i];
        sb.append("\tat " + ste.toString());
        sb.append('\n');
        final LockInfo lockInfo = threadInfo.getLockInfo();
        if (i == 0 && lockInfo != null) {
            final Thread.State ts = threadState;
            switch(ts) {
                case BLOCKED:
                    sb.append("\t-  blocked on " + lockInfo);
                    sb.append('\n');
                    break;
                case WAITING:
                    sb.append("\t-  waiting on " + lockInfo);
                    sb.append('\n');
                    break;
                case TIMED_WAITING:
                    sb.append("\t-  waiting on " + lockInfo);
                    sb.append('\n');
                    break;
                default:
            }
        }
        final MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();
        for (final 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');
    }
    final LockInfo[] locks = threadInfo.getLockedSynchronizers();
    if (locks.length > 0) {
        sb.append("\n\tNumber of locked synchronizers = " + locks.length);
        sb.append('\n');
        for (final LockInfo li : locks) {
            sb.append("\t- " + li);
            sb.append('\n');
        }
    }
    sb.append('\n');
    return sb.toString();
}
Also used : State(java.lang.Thread.State) MonitorInfo(java.lang.management.MonitorInfo) State(java.lang.Thread.State) LockInfo(java.lang.management.LockInfo)

Example 45 with LockInfo

use of java.lang.management.LockInfo in project h2database by h2database.

the class ThreadDeadlockDetector method printLockInfo.

private static void printLockInfo(PrintWriter print, LockInfo[] locks, String tableWaitingForLock, ArrayList<String> tableExclusiveLocks, ArrayList<String> tableSharedLocksMap) {
    print.println(INDENT + "Locked synchronizers: count = " + locks.length);
    for (LockInfo li : locks) {
        print.println(INDENT + "  - " + li);
    }
    if (tableWaitingForLock != null) {
        print.println(INDENT + "Waiting for table: " + tableWaitingForLock);
    }
    if (tableExclusiveLocks != null) {
        print.println(INDENT + "Exclusive table locks: count = " + tableExclusiveLocks.size());
        for (String name : tableExclusiveLocks) {
            print.println(INDENT + "  - " + name);
        }
    }
    if (tableSharedLocksMap != null) {
        print.println(INDENT + "Shared table locks: count = " + tableSharedLocksMap.size());
        for (String name : tableSharedLocksMap) {
            print.println(INDENT + "  - " + name);
        }
    }
    print.println();
}
Also used : LockInfo(java.lang.management.LockInfo)

Aggregations

LockInfo (java.lang.management.LockInfo)69 MonitorInfo (java.lang.management.MonitorInfo)41 ThreadInfo (java.lang.management.ThreadInfo)17 ThreadMXBean (java.lang.management.ThreadMXBean)9 OutputStreamWriter (java.io.OutputStreamWriter)6 Test (org.testng.annotations.Test)6 ArrayList (java.util.ArrayList)4 BufferedWriter (java.io.BufferedWriter)3 PrintWriter (java.io.PrintWriter)3 CompositeData (javax.management.openmbean.CompositeData)3 TMonitorInfo (com.navercorp.pinpoint.thrift.dto.command.TMonitorInfo)2 TThreadDump (com.navercorp.pinpoint.thrift.dto.command.TThreadDump)2 State (java.lang.Thread.State)2 HashSet (java.util.HashSet)2 CompositeDataSupport (javax.management.openmbean.CompositeDataSupport)2 OpenDataException (javax.management.openmbean.OpenDataException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1