Search in sources :

Example 86 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project karaf by apache.

the class EnvironmentDumpProvider method dumpThreadsInformation.

private void dumpThreadsInformation(final PrintWriter outPW) {
    final ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
    if (null == mxBean) {
        return;
    }
    outPW.println("Threads:");
    outPW.printf(INDENT_KEY_VALUE_FORMAT, "live", formatLong(mxBean.getThreadCount())).println();
    outPW.printf(INDENT_KEY_VALUE_FORMAT, "daemon", formatLong(mxBean.getDaemonThreadCount())).println();
    outPW.printf(INDENT_KEY_VALUE_FORMAT, "peak", formatLong(mxBean.getPeakThreadCount())).println();
    outPW.printf(INDENT_KEY_VALUE_FORMAT, "total", formatLong(mxBean.getTotalStartedThreadCount())).println();
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean)

Example 87 with ThreadMXBean

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

Aggregations

ThreadMXBean (java.lang.management.ThreadMXBean)87 ThreadInfo (java.lang.management.ThreadInfo)43 HashMap (java.util.HashMap)8 MemoryMXBean (java.lang.management.MemoryMXBean)7 RuntimeMXBean (java.lang.management.RuntimeMXBean)7 IOException (java.io.IOException)6 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)6 ArrayList (java.util.ArrayList)6 File (java.io.File)5 HashSet (java.util.HashSet)5 Map (java.util.Map)5 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)4 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)4 MemoryUsage (java.lang.management.MemoryUsage)4 Date (java.util.Date)4 MonitorInfo (java.lang.management.MonitorInfo)3 Properties (java.util.Properties)3 Test (org.junit.Test)3 SnmpStatusException (com.sun.jmx.snmp.SnmpStatusException)2 FileOutputStream (java.io.FileOutputStream)2