Search in sources :

Example 56 with ThreadMXBean

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

the class IgniteUtils method dumpThreads.

/**
 * Performs thread dump and prints all available info to the given log.
 *
 * @param log Logger.
 */
public static void dumpThreads(@Nullable IgniteLogger log) {
    ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
    final Set<Long> deadlockedThreadsIds = getDeadlockedThreadIds(mxBean);
    if (deadlockedThreadsIds.size() == 0)
        warn(log, "No deadlocked threads detected.");
    else
        warn(log, "Deadlocked threads detected (see thread dump below) " + "[deadlockedThreadsCnt=" + deadlockedThreadsIds.size() + ']');
    ThreadInfo[] threadInfos = mxBean.dumpAllThreads(mxBean.isObjectMonitorUsageSupported(), mxBean.isSynchronizerUsageSupported());
    GridStringBuilder sb = new GridStringBuilder("Thread dump at ").a(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss z").format(new Date(U.currentTimeMillis()))).a(NL);
    for (ThreadInfo info : threadInfos) {
        printThreadInfo(info, sb, deadlockedThreadsIds);
        sb.a(NL);
        if (info.getLockedSynchronizers() != null && info.getLockedSynchronizers().length > 0) {
            printSynchronizersInfo(info.getLockedSynchronizers(), sb);
            sb.a(NL);
        }
    }
    sb.a(NL);
    warn(log, sb.toString());
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) AtomicLong(java.util.concurrent.atomic.AtomicLong) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 57 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project drools by kiegroup.

the class TestStatusListener method writeThreadDump.

private synchronized void writeThreadDump() throws IOException {
    final ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
    for (final ThreadInfo ti : threadMxBean.dumpAllThreads(true, true)) {
        writer.write(ti.toString());
        if (ti.getStackTrace().length > 8) {
            writer.write("full stacktrace:\n");
            writer.write(fullStackTrace(ti));
            writer.write("\n");
        }
    }
    writer.newLine();
    writer.flush();
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo)

Example 58 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project scheduling by ow2-proactive.

the class TimedOutTestsListener method buildDeadlockInfo.

private static String buildDeadlockInfo() {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    long[] threadIds = threadBean.findMonitorDeadlockedThreads();
    if (threadIds != null && threadIds.length > 0) {
        StringBuilder sb = new StringBuilder();
        ThreadInfo[] infos = threadBean.getThreadInfo(threadIds, true, true);
        for (ThreadInfo ti : infos) {
            printThreadInfo(ti, sb);
            printLockInfo(ti.getLockedSynchronizers(), sb);
            sb.append(System.getProperty("line.separator"));
        }
        return sb.toString();
    } else {
        return null;
    }
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo)

Example 59 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project Payara by payara.

the class HoggingThreadsHealthCheck method doCheck.

@Override
public HealthCheckResult doCheck() {
    if (!getOptions().isEnabled()) {
        return null;
    }
    HealthCheckResult result = new HealthCheckResult();
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    if (!threadBean.isCurrentThreadCpuTimeSupported()) {
        result.add(new HealthCheckResultEntry(HealthCheckResultStatus.CHECK_ERROR, "JVM implementation or OS does" + " not support getting CPU times"));
        return result;
    }
    final long[] ids = threadBean.getAllThreadIds();
    for (long id : ids) {
        if (id == Thread.currentThread().getId())
            continue;
        final long c = threadBean.getThreadCpuTime(id);
        final long u = threadBean.getThreadUserTime(id);
        ThreadInfo threadInfo = threadBean.getThreadInfo(id);
        if (c == -1 || u == -1)
            continue;
        ThreadTimes times = threadTimes.get(id);
        if (times == null) {
            times = new ThreadTimes();
            times.setId(id);
            times.setName(threadInfo.getThreadName());
            times.setStartCpuTime(c);
            times.setEndCpuTime(c);
            times.setStartUserTime(u);
            times.setEndUserTime(u);
            threadTimes.put(id, times);
        } else {
            times.setStartCpuTime(times.getEndCpuTime());
            times.setStartUserTime(times.getEndUserTime());
            times.setEndCpuTime(c);
            times.setEndUserTime(u);
            long checkTime = getOptions().getUnit().toMillis(getOptions().getTime());
            long duration = times.getEndCpuTime() - times.getStartCpuTime();
            double percentage = ((double) (TimeUnit.NANOSECONDS.toMillis(duration)) / (double) (checkTime)) * 100;
            if (percentage > options.getThresholdPercentage()) {
                if (times.getRetryCount() == 0) {
                    times.setInitialStartCpuTime(System.nanoTime());
                    times.setInitialStartUserTime(System.nanoTime());
                }
                if (times.getRetryCount() >= options.getRetryCount()) {
                    result.add(new HealthCheckResultEntry(HealthCheckResultStatus.CRITICAL, "Thread with <id-name>: " + id + "-" + times.getName() + " is a hogging thread for the last " + prettyPrintDuration(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - times.getInitialStartCpuTime())) + "\n" + prettyPrintStackTrace(threadInfo.getStackTrace())));
                }
                times.setRetryCount(times.getRetryCount() + 1);
            }
        }
    }
    return result;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) HealthCheckResult(fish.payara.nucleus.healthcheck.HealthCheckResult) HealthCheckResultEntry(fish.payara.notification.healthcheck.HealthCheckResultEntry) ThreadTimes(fish.payara.nucleus.healthcheck.entity.ThreadTimes)

Example 60 with ThreadMXBean

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

the class JvmThreadingImpl method checkJvmThreadCpuTimeMonitoring.

/**
     * Checker for the "JvmThreadCpuTimeMonitoring" variable.
     */
public void checkJvmThreadCpuTimeMonitoring(EnumJvmThreadCpuTimeMonitoring x) throws SnmpStatusException {
    //Can't be set externaly to unsupported state.
    if (JvmThreadCpuTimeMonitoringUnsupported.intValue() == x.intValue()) {
        log.debug("checkJvmThreadCpuTimeMonitoring", "Try to set to illegal unsupported value");
        throw new SnmpStatusException(SnmpDefinitions.snmpRspWrongValue);
    }
    if ((JvmThreadCpuTimeMonitoringEnabled.intValue() == x.intValue()) || (JvmThreadCpuTimeMonitoringDisabled.intValue() == x.intValue())) {
        // The value is a valid value. But is the feature supported?
        ThreadMXBean mbean = getThreadMXBean();
        if (mbean.isThreadCpuTimeSupported())
            return;
        // Not supported.
        log.debug("checkJvmThreadCpuTimeMonitoring", "Unsupported operation, can't set state");
        throw new SnmpStatusException(SnmpDefinitions.snmpRspInconsistentValue);
    }
    // Unknown value.
    log.debug("checkJvmThreadCpuTimeMonitoring", "unknown enum value ");
    throw new SnmpStatusException(SnmpDefinitions.snmpRspWrongValue);
}
Also used : SnmpStatusException(com.sun.jmx.snmp.SnmpStatusException) ThreadMXBean(java.lang.management.ThreadMXBean)

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