Search in sources :

Example 16 with ThreadInfo

use of java.lang.management.ThreadInfo in project Openfire by igniterealtime.

the class Threading method stacktraces.

private String stacktraces(final ThreadInfo[] threads, final int i) {
    if (i >= threads.length) {
        return "";
    }
    final ThreadInfo thread = threads[i];
    final StringBuilder trace = new StringBuilder();
    for (int stack_i = 0; stack_i < Math.min(thread.getStackTrace().length, MAX_STACK); stack_i++) {
        if (stack_i == (MAX_STACK - 1)) {
            trace.append("    ...");
        } else {
            trace.append("    at ").append(thread.getStackTrace()[stack_i]).append("\n");
        }
    }
    return "\"" + thread.getThreadName() + "\", id " + thread.getThreadId() + " is " + thread.getThreadState() + " on " + thread.getLockName() + ", owned by " + thread.getLockOwnerName() + ", id " + thread.getLockOwnerId() + "\n" + trace + "\n\n" + stacktraces(threads, i + 1);
}
Also used : ThreadInfo(java.lang.management.ThreadInfo)

Example 17 with ThreadInfo

use of java.lang.management.ThreadInfo in project netty by netty.

the class TestUtils method dumpThreads.

private static void dumpThreads(File file) {
    final String filename = file.toString();
    OutputStream out = null;
    try {
        logger.info("Dumping threads: {}", filename);
        final StringBuilder buf = new StringBuilder(8192);
        try {
            for (ThreadInfo info : ManagementFactory.getThreadMXBean().dumpAllThreads(true, true)) {
                buf.append(info);
            }
            buf.append('\n');
        } catch (UnsupportedOperationException ignored) {
            logger.warn("Can't dump threads: ThreadMXBean.dumpAllThreads() unsupported");
            return;
        }
        out = new FileOutputStream(file);
        out.write(buf.toString().getBytes(CharsetUtil.UTF_8));
    } catch (Exception e) {
        logger.warn("Failed to dump threads: {}", filename, e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException ignored) {
            // Ignore.
            }
        }
    }
}
Also used : ThreadInfo(java.lang.management.ThreadInfo) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) XZOutputStream(org.tukaani.xz.XZOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 18 with ThreadInfo

use of java.lang.management.ThreadInfo in project presto by prestodb.

the class ThreadResource method getThreadInfo.

@GET
@Path("/v1/thread")
@Produces(MediaType.APPLICATION_JSON)
public static List<Info> getThreadInfo() {
    ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
    ImmutableList.Builder<Info> builder = ImmutableList.builder();
    for (ThreadInfo info : mbean.getThreadInfo(mbean.getAllThreadIds(), Integer.MAX_VALUE)) {
        builder.add(new Info(info.getThreadId(), info.getThreadName(), info.getThreadState().name(), info.getLockOwnerId() == -1 ? null : info.getLockOwnerId(), toStackTrace(info.getStackTrace())));
    }
    return Ordering.from(byName()).sortedCopy(builder.build());
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) ImmutableList(com.google.common.collect.ImmutableList) ThreadInfo(java.lang.management.ThreadInfo) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 19 with ThreadInfo

use of java.lang.management.ThreadInfo in project randomizedtesting by randomizedtesting.

the class ThreadLeakControl method formatThreadStacksFull.

/** Dump thread state. */
private String formatThreadStacksFull() {
    try {
        StringBuilder b = new StringBuilder();
        b.append("\n==== jstack at approximately timeout time ====\n");
        for (ThreadInfo ti : ManagementFactory.getThreadMXBean().dumpAllThreads(true, true)) {
            Threads.append(b, ti);
        }
        b.append("^^==============================================\n");
        return b.toString();
    } catch (Throwable e) {
    // Ignore, perhaps not available.
    }
    return formatThreadStacks(getThreadsWithTraces());
}
Also used : ThreadInfo(java.lang.management.ThreadInfo)

Example 20 with ThreadInfo

use of java.lang.management.ThreadInfo in project jstorm by alibaba.

the class MetricJstack method dumpThread.

public String dumpThread() throws Exception {
    StringBuilder writer = new StringBuilder();
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    boolean contention = threadMXBean.isThreadContentionMonitoringEnabled();
    long[] threadIds = threadMXBean.getAllThreadIds();
    writer.append(threadIds.length + " active threads:");
    for (long tid : threadIds) {
        writer.append(tid).append(" ");
    }
    writer.append("\n");
    long[] deadLockTids = threadMXBean.findDeadlockedThreads();
    if (deadLockTids != null) {
        writer.append(threadIds.length + " deadlocked threads:");
        for (long tid : deadLockTids) {
            writer.append(tid).append(" ");
        }
        writer.append("\n");
    }
    long[] deadLockMonitorTids = threadMXBean.findMonitorDeadlockedThreads();
    if (deadLockMonitorTids != null) {
        writer.append(threadIds.length + " deadlocked monitor threads:");
        for (long tid : deadLockMonitorTids) {
            writer.append(tid).append(" ");
        }
        writer.append("\n");
    }
    for (long tid : threadIds) {
        ThreadInfo info = threadMXBean.getThreadInfo(tid, Integer.MAX_VALUE);
        if (info == null) {
            writer.append("  Inactive").append("\n");
            continue;
        }
        writer.append("Thread " + getTaskName(info.getThreadId(), info.getThreadName()) + ":").append("\n");
        Thread.State state = info.getThreadState();
        writer.append("  State: " + state).append("\n");
        writer.append("  Blocked count: " + info.getBlockedCount()).append("\n");
        writer.append("  Waited count: " + info.getWaitedCount()).append("\n");
        writer.append(" Cpu time:").append(threadMXBean.getThreadCpuTime(tid) / 1000000).append("ms").append("\n");
        writer.append(" User time:").append(threadMXBean.getThreadUserTime(tid) / 1000000).append("ms").append("\n");
        if (contention) {
            writer.append("  Blocked time: " + info.getBlockedTime()).append("\n");
            writer.append("  Waited time: " + info.getWaitedTime()).append("\n");
        }
        if (state == Thread.State.WAITING) {
            writer.append("  Waiting on " + info.getLockName()).append("\n");
        } else if (state == Thread.State.BLOCKED) {
            writer.append("  Blocked on " + info.getLockName()).append("\n");
            writer.append("  Blocked by " + getTaskName(info.getLockOwnerId(), info.getLockOwnerName())).append("\n");
        }
    }
    for (long tid : threadIds) {
        ThreadInfo info = threadMXBean.getThreadInfo(tid, Integer.MAX_VALUE);
        if (info == null) {
            writer.append("  Inactive").append("\n");
            continue;
        }
        writer.append("Thread " + getTaskName(info.getThreadId(), info.getThreadName()) + ": Stack").append("\n");
        for (StackTraceElement frame : info.getStackTrace()) {
            writer.append("    " + frame.toString()).append("\n");
        }
    }
    return writer.toString();
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo)

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