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;
}
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;
}
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;
}
Aggregations