Search in sources :

Example 91 with ThreadMXBean

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

the class ConnectorStopDeadlockTest method waitForBlock.

//    static MonitorInfo[] threadLocks(Thread t) {
//        ThreadMXBean tm = ManagementFactory.getThreadMXBean();
//        ThreadInfo[] tis = tm.getThreadInfo(new long[] {t.getId()}, true, true);
//        if (tis[0] == null)
//            return null;
//        else
//            return tis[0].getLockedMonitors();
//    }
//
//    static void showLocks(Thread t) {
//        System.out.println("Locks for " + t.getName() + ":");
//        MonitorInfo[] mis = threadLocks(t);
//        if (mis == null)
//            System.out.println("  (no longer exists)");
//        else if (mis.length == 0)
//            System.out.println("  (none)");
//        else {
//            for (MonitorInfo mi : mis)
//                System.out.println("  " + mi);
//        }
//    }
// Wait until thread t blocks waiting for a lock held by the calling thread,
// or until it exits.
static void waitForBlock(Thread t) {
    Thread currentThread = Thread.currentThread();
    System.out.println("waiting for thread " + t.getName() + " to block " + "on a lock held by thread " + currentThread.getName());
    ThreadMXBean tm = ManagementFactory.getThreadMXBean();
    while (true) {
        ThreadInfo ti = tm.getThreadInfo(t.getId());
        if (ti == null) {
            System.out.println("  thread has exited");
            return;
        }
        if (ti.getLockOwnerId() == currentThread.getId()) {
            System.out.println("  thread now blocked");
            return;
        }
        Thread.yield();
    }
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo)

Example 92 with ThreadMXBean

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

the class MetricUtils method instantiateThreadMetrics.

private static void instantiateThreadMetrics(MetricGroup metrics) {
    final ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
    metrics.gauge("Count", new Gauge<Integer>() {

        @Override
        public Integer getValue() {
            return mxBean.getThreadCount();
        }
    });
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean)

Example 93 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)

Example 94 with ThreadMXBean

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

the class ProgressWatchdog method generateThreadDump.

/**
 * Generates limited thread dump with only threads involved into persistence.
 *
 * @return string to log.
 */
private String generateThreadDump() {
    final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    int depth = 100;
    final ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), depth);
    final StringBuilder dump = new StringBuilder();
    for (ThreadInfo threadInfo : threadInfos) {
        String name = threadInfo.getThreadName();
        if (name.contains("checkpoint-runner") || name.contains("db-checkpoint-thread") || name.contains("wal-file-archiver") || name.contains("data-streamer") || (clientThreadsName != null && name.contains(clientThreadsName))) {
            String str = threadInfo.toString();
            if (name.contains("db-checkpoint-thread")) {
                dump.append(str);
                dump.append("(Full stacktrace)");
                StackTraceElement[] stackTrace = threadInfo.getStackTrace();
                int i = 0;
                for (; i < stackTrace.length && i < depth; i++) {
                    StackTraceElement ste = stackTrace[i];
                    dump.append("\tat ").append(ste.toString());
                    dump.append('\n');
                }
                if (i < stackTrace.length) {
                    dump.append("\t...");
                    dump.append('\n');
                }
                dump.append('\n');
            } else
                dump.append(str);
        } else {
            String s = threadInfo.toString();
            if (s.contains(FileWriteAheadLogManager.class.getSimpleName()) || s.contains(FilePageStoreManager.class.getSimpleName()))
                dump.append(s);
        }
    }
    return dump.toString();
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo)

Example 95 with ThreadMXBean

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

the class IgniteUtils method printStackTrace.

/**
 * @param threadId Thread ID.
 * @param sb Builder.
 */
public static void printStackTrace(long threadId, GridStringBuilder sb) {
    ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
    ThreadInfo threadInfo = mxBean.getThreadInfo(threadId, Integer.MAX_VALUE);
    printThreadInfo(threadInfo, sb, Collections.<Long>emptySet());
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo)

Aggregations

ThreadMXBean (java.lang.management.ThreadMXBean)105 ThreadInfo (java.lang.management.ThreadInfo)56 HashMap (java.util.HashMap)9 RuntimeMXBean (java.lang.management.RuntimeMXBean)8 MemoryMXBean (java.lang.management.MemoryMXBean)7 IOException (java.io.IOException)6 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)6 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 File (java.io.File)5 HashSet (java.util.HashSet)5 Test (org.junit.Test)5 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)4 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)4 MemoryUsage (java.lang.management.MemoryUsage)4 HealthCheckResultEntry (fish.payara.notification.healthcheck.HealthCheckResultEntry)3 HealthCheckResult (fish.payara.nucleus.healthcheck.HealthCheckResult)3 LockInfo (java.lang.management.LockInfo)3 ManagementFactory (java.lang.management.ManagementFactory)3 MonitorInfo (java.lang.management.MonitorInfo)3