Catch UnsatisfiedLinkError when getting CPU usage

Affects issues:
- Fixed #1807
This commit is contained in:
Risto Lahtela 2021-03-21 11:12:24 +02:00
parent 624ed50054
commit 19b64455b4

View File

@ -54,15 +54,19 @@ public class SystemUsage {
double averageUsage;
OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
if (osBean instanceof com.sun.management.OperatingSystemMXBean) {
com.sun.management.OperatingSystemMXBean nativeOsBean = (com.sun.management.OperatingSystemMXBean) osBean;
averageUsage = nativeOsBean.getSystemCpuLoad();
} else {
int availableProcessors = osBean.getAvailableProcessors();
averageUsage = osBean.getSystemLoadAverage() / availableProcessors;
}
if (averageUsage < 0) {
averageUsage = -1; // If unavailable, getSystemLoadAverage() returns -1
try {
if (osBean instanceof com.sun.management.OperatingSystemMXBean) {
com.sun.management.OperatingSystemMXBean nativeOsBean = (com.sun.management.OperatingSystemMXBean) osBean;
averageUsage = nativeOsBean.getSystemCpuLoad();
} else {
int availableProcessors = osBean.getAvailableProcessors();
averageUsage = osBean.getSystemLoadAverage() / availableProcessors;
}
if (averageUsage < 0) {
averageUsage = -1; // If unavailable, getSystemLoadAverage() returns -1
}
} catch (UnsatisfiedLinkError e) {
averageUsage = -1; // Using some docker or something
}
return averageUsage * 100.0;
}