Search in sources :

Example 6 with ThreadInfo

use of java.lang.management.ThreadInfo in project hbase by apache.

the class TimedOutTestsListener method buildDeadlockInfo.

static String buildDeadlockInfo() {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    long[] threadIds = threadBean.findMonitorDeadlockedThreads();
    if (threadIds != null && threadIds.length > 0) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter out = new PrintWriter(stringWriter);
        ThreadInfo[] infos = threadBean.getThreadInfo(threadIds, true, true);
        for (ThreadInfo ti : infos) {
            printThreadInfo(ti, out);
            printLockInfo(ti.getLockedSynchronizers(), out);
            out.println();
        }
        out.close();
        return stringWriter.toString();
    } else {
        return null;
    }
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter)

Example 7 with ThreadInfo

use of java.lang.management.ThreadInfo in project hbase by apache.

the class ReflectionUtils method printThreadInfo.

/**
   * Print all of the thread's information and stack traces.
   *
   * @param stream the stream to
   * @param title a string title for the stack trace
   */
private static void printThreadInfo(PrintStream stream, String title) {
    final int STACK_DEPTH = 20;
    boolean contention = threadBean.isThreadContentionMonitoringEnabled();
    long[] threadIds = threadBean.getAllThreadIds();
    stream.println("Process Thread Dump: " + title);
    stream.println(threadIds.length + " active threads");
    for (long tid : threadIds) {
        ThreadInfo info = threadBean.getThreadInfo(tid, STACK_DEPTH);
        if (info == null) {
            stream.println("  Inactive");
            continue;
        }
        stream.println("Thread " + getTaskName(info.getThreadId(), info.getThreadName()) + ":");
        Thread.State state = info.getThreadState();
        stream.println("  State: " + state);
        stream.println("  Blocked count: " + info.getBlockedCount());
        stream.println("  Waited count: " + info.getWaitedCount());
        if (contention) {
            stream.println("  Blocked time: " + info.getBlockedTime());
            stream.println("  Waited time: " + info.getWaitedTime());
        }
        if (state == Thread.State.WAITING) {
            stream.println("  Waiting on " + info.getLockName());
        } else if (state == Thread.State.BLOCKED) {
            stream.println("  Blocked on " + info.getLockName());
            stream.println("  Blocked by " + getTaskName(info.getLockOwnerId(), info.getLockOwnerName()));
        }
        stream.println("  Stack:");
        for (StackTraceElement frame : info.getStackTrace()) {
            stream.println("    " + frame.toString());
        }
    }
    stream.flush();
}
Also used : ThreadInfo(java.lang.management.ThreadInfo)

Example 8 with ThreadInfo

use of java.lang.management.ThreadInfo in project hive by apache.

the class StackServlet method printThreadInfo.

/**
   * Print all of the thread's information and stack traces.
   *
   * @param stream the stream to
   * @param title a string title for the stack trace
   */
private synchronized void printThreadInfo(PrintStream stream, String title) {
    final int STACK_DEPTH = 20;
    boolean contention = threadBean.isThreadContentionMonitoringEnabled();
    long[] threadIds = threadBean.getAllThreadIds();
    stream.println("Process Thread Dump: " + title);
    stream.println(threadIds.length + " active threads");
    for (long tid : threadIds) {
        ThreadInfo info = threadBean.getThreadInfo(tid, STACK_DEPTH);
        if (info == null) {
            stream.println("  Inactive");
            continue;
        }
        stream.println("Thread " + getTaskName(info.getThreadId(), info.getThreadName()) + ":");
        Thread.State state = info.getThreadState();
        stream.println("  State: " + state);
        stream.println("  Blocked count: " + info.getBlockedCount());
        stream.println("  Wtaited count: " + info.getWaitedCount());
        if (contention) {
            stream.println("  Blocked time: " + info.getBlockedTime());
            stream.println("  Waited time: " + info.getWaitedTime());
        }
        if (state == Thread.State.WAITING) {
            stream.println("  Waiting on " + info.getLockName());
        } else if (state == Thread.State.BLOCKED) {
            stream.println("  Blocked on " + info.getLockName());
            stream.println("  Blocked by " + getTaskName(info.getLockOwnerId(), info.getLockOwnerName()));
        }
        stream.println("  Stack:");
        for (StackTraceElement frame : info.getStackTrace()) {
            stream.println("    " + frame.toString());
        }
    }
    stream.flush();
}
Also used : ThreadInfo(java.lang.management.ThreadInfo)

Example 9 with ThreadInfo

use of java.lang.management.ThreadInfo in project hive by apache.

the class LlapDaemonExecutorMetrics method updateThreadMetrics.

private void updateThreadMetrics(MetricsRecordBuilder rb) {
    if (threadMXBean.isThreadCpuTimeSupported() && threadMXBean.isThreadCpuTimeEnabled()) {
        final long[] ids = threadMXBean.getAllThreadIds();
        final ThreadInfo[] infos = threadMXBean.getThreadInfo(ids);
        for (int i = 0; i < ids.length; i++) {
            ThreadInfo threadInfo = infos[i];
            if (threadInfo == null) {
                continue;
            }
            String threadName = threadInfo.getThreadName();
            long threadId = ids[i];
            Integer id = executorNames.get(threadName);
            if (id != null) {
                executorThreadCpuTime[id].set(threadMXBean.getThreadCpuTime(threadId));
                executorThreadUserTime[id].set(threadMXBean.getThreadUserTime(threadId));
            }
        }
        for (int i = 0; i < numExecutors; i++) {
            rb.addGauge(cpuMetricsInfoMap.get(i), executorThreadCpuTime[i].value());
            rb.addGauge(userMetricsInfoMap.get(i), executorThreadUserTime[i].value());
        }
    }
}
Also used : ThreadInfo(java.lang.management.ThreadInfo)

Example 10 with ThreadInfo

use of java.lang.management.ThreadInfo in project metrics by dropwizard.

the class ThreadDump method dump.

/**
     * Dumps all of the threads' current information to an output stream.
     *
     * @param out an output stream
     */
public void dump(OutputStream out) {
    final ThreadInfo[] threads = this.threadMXBean.dumpAllThreads(true, true);
    final PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, UTF_8));
    for (int ti = threads.length - 1; ti >= 0; ti--) {
        final ThreadInfo t = threads[ti];
        writer.printf("\"%s\" id=%d state=%s", t.getThreadName(), t.getThreadId(), t.getThreadState());
        final LockInfo lock = t.getLockInfo();
        if (lock != null && t.getThreadState() != Thread.State.BLOCKED) {
            writer.printf("%n    - waiting on <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName());
            writer.printf("%n    - locked <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName());
        } else if (lock != null && t.getThreadState() == Thread.State.BLOCKED) {
            writer.printf("%n    - waiting to lock <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName());
        }
        if (t.isSuspended()) {
            writer.print(" (suspended)");
        }
        if (t.isInNative()) {
            writer.print(" (running in native)");
        }
        writer.println();
        if (t.getLockOwnerName() != null) {
            writer.printf("     owned by %s id=%d%n", t.getLockOwnerName(), t.getLockOwnerId());
        }
        final StackTraceElement[] elements = t.getStackTrace();
        final MonitorInfo[] monitors = t.getLockedMonitors();
        for (int i = 0; i < elements.length; i++) {
            final StackTraceElement element = elements[i];
            writer.printf("    at %s%n", element);
            for (int j = 1; j < monitors.length; j++) {
                final MonitorInfo monitor = monitors[j];
                if (monitor.getLockedStackDepth() == i) {
                    writer.printf("      - locked %s%n", monitor);
                }
            }
        }
        writer.println();
        final LockInfo[] locks = t.getLockedSynchronizers();
        if (locks.length > 0) {
            writer.printf("    Locked synchronizers: count = %d%n", locks.length);
            for (LockInfo l : locks) {
                writer.printf("      - %s%n", l);
            }
            writer.println();
        }
    }
    writer.println();
    writer.flush();
}
Also used : ThreadInfo(java.lang.management.ThreadInfo) MonitorInfo(java.lang.management.MonitorInfo) OutputStreamWriter(java.io.OutputStreamWriter) LockInfo(java.lang.management.LockInfo) PrintWriter(java.io.PrintWriter)

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