Search in sources :

Example 1 with ThreadTimes

use of fish.payara.nucleus.healthcheck.entity.ThreadTimes in project Payara by payara.

the class CpuUsageHealthCheck method getTotalCpuTime.

public long getTotalCpuTime() {
    final Collection<ThreadTimes> threadTimesValues = threadTimes.values();
    long time = 0L;
    for (ThreadTimes times : threadTimesValues) time += times.getEndCpuTime() - times.getStartCpuTime();
    return time;
}
Also used : ThreadTimes(fish.payara.nucleus.healthcheck.entity.ThreadTimes)

Example 2 with ThreadTimes

use of fish.payara.nucleus.healthcheck.entity.ThreadTimes 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 3 with ThreadTimes

use of fish.payara.nucleus.healthcheck.entity.ThreadTimes in project Payara by payara.

the class CpuUsageHealthCheck 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 == java.lang.Thread.currentThread().getId())
            continue;
        final long c = threadBean.getThreadCpuTime(id);
        final long u = threadBean.getThreadUserTime(id);
        if (c == -1 || u == -1)
            continue;
        ThreadTimes times = threadTimes.get(id);
        if (times == null) {
            times = new ThreadTimes();
            times.setId(id);
            times.setStartCpuTime(c);
            times.setEndCpuTime(c);
            times.setStartUserTime(u);
            times.setEndUserTime(u);
            threadTimes.put(id, times);
        } else {
            times.setEndCpuTime(c);
            times.setEndUserTime(u);
        }
    }
    long totalCpuTime = getTotalCpuTime();
    long time = System.nanoTime();
    double percentage = ((double) (totalCpuTime - totalTimeBefore) / (double) (time - timeBefore)) * 100;
    result.add(new HealthCheckResultEntry(decideOnStatusWithRatio(percentage), "CPU%: " + new DecimalFormat("#.00").format(percentage) + ", Time CPU used: " + prettyPrintDuration(TimeUnit.NANOSECONDS.toMillis(getTotalCpuTime() - totalTimeBefore))));
    totalTimeBefore = totalCpuTime;
    timeBefore = time;
    return result;
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) DecimalFormat(java.text.DecimalFormat) HealthCheckResult(fish.payara.nucleus.healthcheck.HealthCheckResult) HealthCheckResultEntry(fish.payara.notification.healthcheck.HealthCheckResultEntry) ThreadTimes(fish.payara.nucleus.healthcheck.entity.ThreadTimes)

Aggregations

ThreadTimes (fish.payara.nucleus.healthcheck.entity.ThreadTimes)3 HealthCheckResultEntry (fish.payara.notification.healthcheck.HealthCheckResultEntry)2 HealthCheckResult (fish.payara.nucleus.healthcheck.HealthCheckResult)2 ThreadMXBean (java.lang.management.ThreadMXBean)2 ThreadInfo (java.lang.management.ThreadInfo)1 DecimalFormat (java.text.DecimalFormat)1