Search in sources :

Example 96 with ThreadInfo

use of java.lang.management.ThreadInfo 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 97 with ThreadInfo

use of java.lang.management.ThreadInfo in project symmetric-ds by JumpMind.

the class MonitorTypeCpu method getNotificationMessage.

protected String getNotificationMessage(long value, long threshold, long period) {
    ThreadInfo[] infos = new ThreadInfo[TOP_THREADS];
    long[] cpuUsages = new long[TOP_THREADS];
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    for (long threadId : threadBean.getAllThreadIds()) {
        ThreadInfo info = threadBean.getThreadInfo(threadId);
        if (info.getThreadState() != Thread.State.TERMINATED) {
            rankTopUsage(infos, cpuUsages, info, threadBean.getThreadCpuTime(threadId));
        }
    }
    String text = "CPU usage is at " + value;
    for (int i = 0; i < infos.length; i++) {
        text += "Top #" + (i + 1) + " CPU thread " + infos[i].getThreadId() + " is using " + (cpuUsages[i] / 1000000000f) + "s";
        text += logStackTrace(threadBean.getThreadInfo(infos[i].getThreadId(), MAX_STACK_DEPTH));
    }
    return text;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) IBuiltInExtensionPoint(org.jumpmind.extension.IBuiltInExtensionPoint)

Example 98 with ThreadInfo

use of java.lang.management.ThreadInfo in project symmetric-ds by JumpMind.

the class MonitorTypeMemory method getMessage.

public String getMessage(long value, long threshold, long period) {
    long maxMemory = tenuredPool.getUsage().getMax();
    long usedMemory = tenuredPool.getUsage().getUsed();
    String text = "Memory threshold exceeded, " + usedMemory + " of " + maxMemory;
    ThreadInfo[] infos = new ThreadInfo[TOP_THREADS];
    long[] byteUsages = new long[TOP_THREADS];
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    for (long threadId : threadBean.getAllThreadIds()) {
        ThreadInfo info = threadBean.getThreadInfo(threadId);
        if (info.getThreadState() != Thread.State.TERMINATED) {
            rankTopUsage(infos, byteUsages, info, getThreadAllocatedBytes(threadBean, threadId));
        }
    }
    for (int i = 0; i < infos.length; i++) {
        text += "Top #" + (i + 1) + " memory thread " + infos[i].getThreadId() + " is using " + String.format("%.1f", ((double) byteUsages[i] / 1048576f)) + "MB";
        text += logStackTrace(threadBean.getThreadInfo(infos[i].getThreadId(), MAX_STACK_DEPTH));
    }
    return text;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) IBuiltInExtensionPoint(org.jumpmind.extension.IBuiltInExtensionPoint)

Example 99 with ThreadInfo

use of java.lang.management.ThreadInfo in project symmetric-ds by JumpMind.

the class ProcessInfo method getThreadData.

public static ThreadData getThreadData(long threadId) {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    ThreadInfo info = threadBean.getThreadInfo(threadId, 100);
    if (info != null) {
        String threadName = info.getThreadName();
        return new ThreadData(threadName, AppUtils.formatStackTrace(info.getStackTrace()));
    } else {
        return null;
    }
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo)

Example 100 with ThreadInfo

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

Aggregations

ThreadInfo (java.lang.management.ThreadInfo)107 ThreadMXBean (java.lang.management.ThreadMXBean)43 HashMap (java.util.HashMap)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 HashSet (java.util.HashSet)6 Map (java.util.Map)6 LockInfo (java.lang.management.LockInfo)5 MonitorInfo (java.lang.management.MonitorInfo)5 Date (java.util.Date)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Test (org.junit.Test)4 FileOutputStream (java.io.FileOutputStream)3 PrintStream (java.io.PrintStream)3 PrintWriter (java.io.PrintWriter)3 Method (java.lang.reflect.Method)3 LinkedHashSet (java.util.LinkedHashSet)3 TreeMap (java.util.TreeMap)3 ExecutorService (java.util.concurrent.ExecutorService)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2