|
@@ -35,6 +35,7 @@ import java.io.FileInputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStreamReader;
|
|
|
import java.lang.management.ManagementFactory;
|
|
|
+import java.lang.management.OperatingSystemMXBean;
|
|
|
import java.lang.management.RuntimeMXBean;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.text.DecimalFormat;
|
|
@@ -96,16 +97,22 @@ public class OSUtils {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * get available physical memory size
|
|
|
+ * get available physical or pod memory size
|
|
|
* <p>
|
|
|
* Keep 2 decimal
|
|
|
*
|
|
|
- * @return available Physical Memory Size, unit: G
|
|
|
+ * @return Available physical or pod memory size, unit: G
|
|
|
*/
|
|
|
public static double availablePhysicalMemorySize() {
|
|
|
- GlobalMemory memory = hal.getMemory();
|
|
|
- double availablePhysicalMemorySize = memory.getAvailable() / 1024.0 / 1024 / 1024;
|
|
|
+ double availablePhysicalMemorySize;
|
|
|
|
|
|
+ if (KubernetesUtils.isKubernetesMode()) {
|
|
|
+ long freeMemory = Runtime.getRuntime().freeMemory();
|
|
|
+ availablePhysicalMemorySize = freeMemory / 1024.0 / 1024 / 1024;
|
|
|
+ } else {
|
|
|
+ GlobalMemory memory = hal.getMemory();
|
|
|
+ availablePhysicalMemorySize = memory.getAvailable() / 1024.0 / 1024 / 1024;
|
|
|
+ }
|
|
|
DecimalFormat df = new DecimalFormat(TWO_DECIMAL);
|
|
|
df.setRoundingMode(RoundingMode.HALF_UP);
|
|
|
return Double.parseDouble(df.format(availablePhysicalMemorySize));
|
|
@@ -123,7 +130,13 @@ public class OSUtils {
|
|
|
long now = System.currentTimeMillis();
|
|
|
if (now - prevTickTime > 950) {
|
|
|
// Enough time has elapsed.
|
|
|
- cpuUsage = processor.getSystemCpuLoadBetweenTicks(prevTicks);
|
|
|
+ if (KubernetesUtils.isKubernetesMode()) {
|
|
|
+ OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
|
|
|
+ cpuUsage = operatingSystemMXBean.getSystemLoadAverage();
|
|
|
+ } else {
|
|
|
+ cpuUsage = processor.getSystemCpuLoadBetweenTicks(prevTicks);
|
|
|
+ }
|
|
|
+
|
|
|
prevTickTime = System.currentTimeMillis();
|
|
|
prevTicks = processor.getSystemCpuLoadTicks();
|
|
|
}
|