Search in sources :

Example 46 with ThreadMXBean

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

the class ThreadUsageHealthCheck method execute.

@Override
public Result execute() {
    FormattingResultLog log = new FormattingResultLog();
    log.debug("Checking threads for exceeding {}% CPU time within time period of {}ms", cpuTimeThresholdWarn, samplePeriodInMs);
    try {
        ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean();
        List<ThreadTimeInfo> threadTimeInfos = collectThreadTimeInfos(log, threadMxBean);
        Collections.sort(threadTimeInfos);
        float totalCpuTimePercentage = 0;
        for (int i = 0; i < threadTimeInfos.size(); i++) {
            ThreadTimeInfo threadInfo = threadTimeInfos.get(i);
            float cpuTimePercentage = ((float) threadInfo.getCpuTime() / ((float) samplePeriodInMs * 1000000)) * 100f;
            totalCpuTimePercentage += cpuTimePercentage;
            String msg = String.format("%4.1f", cpuTimePercentage) + "% used by thread \"" + threadInfo.name + "\"";
            if (i < 3 || (i < 10 && cpuTimePercentage > 15)) {
                log.info(msg);
            } else {
                log.debug(msg);
            }
        }
        log.info(threadTimeInfos.size() + " threads using " + String.format("%4.1f", totalCpuTimePercentage) + "% CPU Time");
        boolean isHighCpuTime = totalCpuTimePercentage > cpuTimeThresholdWarn;
        if (isHighCpuTime) {
            log.warn("Threads are consuming significant CPU time " + (cpuTimeThresholdWarnIsConfigured ? "(more than configured " + cpuTimeThresholdWarn + "% within " + samplePeriodInMs + "ms)" : "(more than " + availableProcessors + " cores * 90% = " + cpuTimeThresholdWarn + "%)"));
        }
        checkForDeadlock(log, threadMxBean);
    } catch (Exception e) {
        LOG.error("Could not analyse thread usage " + e, e);
        log.healthCheckError("Could not analyse thread usage", e);
    }
    return new Result(log);
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) FormattingResultLog(org.apache.sling.hc.util.FormattingResultLog) Result(org.apache.sling.hc.api.Result)

Example 47 with ThreadMXBean

use of java.lang.management.ThreadMXBean in project gerrit by GerritCodeReview.

the class ProcMetricModule method procJvmThread.

private void procJvmThread(MetricMaker metrics) {
    ThreadMXBean thread = ManagementFactory.getThreadMXBean();
    metrics.newCallbackMetric("proc/jvm/thread/num_live", Integer.class, new Description("Current live thread count").setGauge().setUnit("threads"), () -> thread.getThreadCount());
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) Description(com.google.gerrit.metrics.Description)

Example 48 with ThreadMXBean

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

the class JvmThreadingImpl method checkJvmThreadContentionMonitoring.

/**
     * Checker for the "JvmThreadContentionMonitoring" variable.
     */
public void checkJvmThreadContentionMonitoring(EnumJvmThreadContentionMonitoring x) throws SnmpStatusException {
    //Can't be set externaly to unsupported state.
    if (JvmThreadContentionMonitoringUnsupported.intValue() == x.intValue()) {
        log.debug("checkJvmThreadContentionMonitoring", "Try to set to illegal unsupported value");
        throw new SnmpStatusException(SnmpDefinitions.snmpRspWrongValue);
    }
    if ((JvmThreadContentionMonitoringEnabled.intValue() == x.intValue()) || (JvmThreadContentionMonitoringDisabled.intValue() == x.intValue())) {
        // The value is valid, but is the feature supported ?
        ThreadMXBean mbean = getThreadMXBean();
        if (mbean.isThreadContentionMonitoringSupported())
            return;
        log.debug("checkJvmThreadContentionMonitoring", "Unsupported operation, can't set state");
        throw new SnmpStatusException(SnmpDefinitions.snmpRspInconsistentValue);
    }
    log.debug("checkJvmThreadContentionMonitoring", "Try to set to unknown value");
    throw new SnmpStatusException(SnmpDefinitions.snmpRspWrongValue);
}
Also used : SnmpStatusException(com.sun.jmx.snmp.SnmpStatusException) ThreadMXBean(java.lang.management.ThreadMXBean)

Example 49 with ThreadMXBean

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

the class JvmThreadInstanceEntryImpl method getJvmThreadInstBlockTimeMs.

/**
     * Getter for the "JvmThreadInstBlockTimeMs" variable.
     */
public Long getJvmThreadInstBlockTimeMs() throws SnmpStatusException {
    long l = 0;
    final ThreadMXBean tmb = JvmThreadingImpl.getThreadMXBean();
    if (tmb.isThreadContentionMonitoringSupported()) {
        l = info.getBlockedTime();
        //Monitoring is disabled
        if (l == -1)
            l = 0;
    }
    return new Long(l);
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean)

Example 50 with ThreadMXBean

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

the class JvmThreadInstanceEntryImpl method getJvmThreadInstCpuTimeNs.

/**
     * Getter for the "JvmThreadInstCpuTimeNs" variable.
     */
public Long getJvmThreadInstCpuTimeNs() throws SnmpStatusException {
    long l = 0;
    final ThreadMXBean tmb = JvmThreadingImpl.getThreadMXBean();
    try {
        if (tmb.isThreadCpuTimeSupported()) {
            l = tmb.getThreadCpuTime(info.getThreadId());
            log.debug("getJvmThreadInstCpuTimeNs", "Cpu time ns : " + l);
            //Cpu time measurement is disabled or the id is not valid.
            if (l == -1)
                l = 0;
        }
    } catch (UnsatisfiedLinkError e) {
        // XXX Revisit: catch TO BE EVENTUALLY REMOVED
        log.debug("getJvmThreadInstCpuTimeNs", "Operation not supported: " + e);
    }
    return new Long(l);
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean)

Aggregations

ThreadMXBean (java.lang.management.ThreadMXBean)87 ThreadInfo (java.lang.management.ThreadInfo)43 HashMap (java.util.HashMap)8 MemoryMXBean (java.lang.management.MemoryMXBean)7 RuntimeMXBean (java.lang.management.RuntimeMXBean)7 IOException (java.io.IOException)6 OperatingSystemMXBean (java.lang.management.OperatingSystemMXBean)6 ArrayList (java.util.ArrayList)6 File (java.io.File)5 HashSet (java.util.HashSet)5 Map (java.util.Map)5 ClassLoadingMXBean (java.lang.management.ClassLoadingMXBean)4 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)4 MemoryUsage (java.lang.management.MemoryUsage)4 Date (java.util.Date)4 MonitorInfo (java.lang.management.MonitorInfo)3 Properties (java.util.Properties)3 Test (org.junit.Test)3 SnmpStatusException (com.sun.jmx.snmp.SnmpStatusException)2 FileOutputStream (java.io.FileOutputStream)2