Search in sources :

Example 76 with ThreadInfo

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

the class OneLineFormatter method getThreadName.

/**
     * LogRecord has threadID but no thread name.
     * LogRecord uses an int for thread ID but thread IDs are longs.
     * If the real thread ID > (Integer.MAXVALUE / 2) LogRecord uses it's own
     * ID in an effort to avoid clashes due to overflow.
     * <p>
     * Words fail me to describe what I think of the design decision to use an
     * int in LogRecord for a long value and the resulting mess that follows.
     */
private static String getThreadName(int logRecordThreadId) {
    Map<Integer, String> cache = threadNameCache.get();
    String result = null;
    if (logRecordThreadId > (Integer.MAX_VALUE / 2)) {
        result = cache.get(Integer.valueOf(logRecordThreadId));
    }
    if (result != null) {
        return result;
    }
    if (logRecordThreadId > Integer.MAX_VALUE / 2) {
        result = UNKNOWN_THREAD_NAME + logRecordThreadId;
    } else {
        // Double checked locking OK as threadMxBean is volatile
        if (threadMxBean == null) {
            synchronized (threadMxBeanLock) {
                if (threadMxBean == null) {
                    threadMxBean = ManagementFactory.getThreadMXBean();
                }
            }
        }
        ThreadInfo threadInfo = threadMxBean.getThreadInfo(logRecordThreadId);
        if (threadInfo == null) {
            return Long.toString(logRecordThreadId);
        }
        result = threadInfo.getThreadName();
    }
    cache.put(Integer.valueOf(logRecordThreadId), result);
    return result;
}
Also used : ThreadInfo(java.lang.management.ThreadInfo)

Example 77 with ThreadInfo

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

the class AbstractHazelcastClassRunner method generateThreadDump.

private String generateThreadDump() {
    StringBuilder dump = new StringBuilder();
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true);
    long currentThreadId = Thread.currentThread().getId();
    for (ThreadInfo threadInfo : threadInfos) {
        long threadId = threadInfo.getThreadId();
        if (threadId == currentThreadId) {
            continue;
        }
        dump.append('"');
        dump.append(threadInfo.getThreadName());
        dump.append("\" ");
        Thread.State state = threadInfo.getThreadState();
        dump.append("\n\tjava.lang.Thread.State: ");
        dump.append(state);
        if (threadInfo.getLockName() != null) {
            dump.append(", on lock=").append(threadInfo.getLockName());
        }
        if (threadInfo.getLockOwnerName() != null) {
            dump.append(", owned by ").append(threadInfo.getLockOwnerName());
            dump.append(", id=").append(threadInfo.getLockOwnerId());
        }
        if (THREAD_CPU_TIME_INFO_AVAILABLE) {
            dump.append(", cpu=").append(threadMXBean.getThreadCpuTime(threadId)).append(" nsecs");
            dump.append(", usr=").append(threadMXBean.getThreadUserTime(threadId)).append(" nsecs");
        }
        if (THREAD_CONTENTION_INFO_AVAILABLE) {
            dump.append(", blocked=").append(threadInfo.getBlockedTime()).append(" msecs");
            dump.append(", waited=").append(threadInfo.getWaitedTime()).append(" msecs");
        }
        StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
        for (StackTraceElement stackTraceElement : stackTraceElements) {
            dump.append("\n\t\tat ");
            dump.append(stackTraceElement);
        }
        dump.append("\n\n");
    }
    return dump.toString();
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo)

Example 78 with ThreadInfo

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

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
     */
public static void printThreadInfo(PrintWriter 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 79 with ThreadInfo

use of java.lang.management.ThreadInfo in project heron by twitter.

the class JVMMetrics method getThreadsMetrics.

private void getThreadsMetrics() {
    // Set the cpu usage for every single thread
    if (threadMXBean.isThreadCpuTimeSupported()) {
        threadMXBean.setThreadCpuTimeEnabled(true);
        long tmpOtherThreadsCpuTime = 0;
        long tmpOtherThreadsUserCpuTime = 0;
        for (long id : threadMXBean.getAllThreadIds()) {
            long cpuTime = threadMXBean.getThreadCpuTime(id);
            long cpuUserTime = threadMXBean.getThreadUserTime(id);
            ThreadInfo threadInfo = threadMXBean.getThreadInfo(id);
            if (threadInfo != null) {
                String threadName = threadInfo.getThreadName();
                if (threadName.equals(ThreadNames.THREAD_GATEWAY_NAME) || threadName.equals(ThreadNames.THREAD_SLAVE_NAME)) {
                    threadsCPUTimeNs.scope(threadName).setValue(cpuTime);
                    threadsUserCPUTimeNs.scope(threadName).setValue(cpuUserTime);
                } else {
                    tmpOtherThreadsCpuTime += cpuTime;
                    tmpOtherThreadsUserCpuTime += cpuUserTime;
                }
            }
        }
        otherThreadsCPUTimeNs.setValue(tmpOtherThreadsCpuTime);
        otherThreadsUserCPUTimeNs.setValue(tmpOtherThreadsUserCpuTime);
        jvmThreadCount.setValue(threadMXBean.getThreadCount());
        jvmDaemonThreadCount.setValue(threadMXBean.getDaemonThreadCount());
    }
}
Also used : ThreadInfo(java.lang.management.ThreadInfo)

Example 80 with ThreadInfo

use of java.lang.management.ThreadInfo in project commons by twitter.

the class ContentionPrinter method getLines.

@Override
public Iterable<String> getLines(HttpServletRequest request) {
    List<String> lines = Lists.newLinkedList();
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    Map<Long, StackTraceElement[]> threadStacks = Maps.newHashMap();
    for (Map.Entry<Thread, StackTraceElement[]> entry : Thread.getAllStackTraces().entrySet()) {
        threadStacks.put(entry.getKey().getId(), entry.getValue());
    }
    Set<Long> lockOwners = Sets.newHashSet();
    lines.add("Locked threads:");
    for (ThreadInfo t : bean.getThreadInfo(bean.getAllThreadIds())) {
        switch(t.getThreadState()) {
            case BLOCKED:
            case WAITING:
            case TIMED_WAITING:
                lines.addAll(getThreadInfo(t, threadStacks.get(t.getThreadId())));
                if (t.getLockOwnerId() != -1)
                    lockOwners.add(t.getLockOwnerId());
                break;
        }
    }
    if (lockOwners.size() > 0) {
        lines.add("\nLock Owners");
        for (ThreadInfo t : bean.getThreadInfo(Longs.toArray(lockOwners))) {
            lines.addAll(getThreadInfo(t, threadStacks.get(t.getThreadId())));
        }
    }
    return lines;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) Map(java.util.Map)

Aggregations

ThreadInfo (java.lang.management.ThreadInfo)124 ThreadMXBean (java.lang.management.ThreadMXBean)56 HashMap (java.util.HashMap)10 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 HashSet (java.util.HashSet)7 Map (java.util.Map)7 LockInfo (java.lang.management.LockInfo)6 MonitorInfo (java.lang.management.MonitorInfo)6 Test (org.junit.Test)5 Date (java.util.Date)4 CountDownLatch (java.util.concurrent.CountDownLatch)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 HealthCheckResultEntry (fish.payara.notification.healthcheck.HealthCheckResultEntry)2