You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by le...@apache.org on 2021/01/31 07:48:22 UTC

[incubator-dolphinscheduler] branch 1.3.5-prepare updated: [1.3.5-prepare][cherry-pick]#4524 calculate cpu usage to support application running in container (#4606)

This is an automated email from the ASF dual-hosted git repository.

leonbao pushed a commit to branch 1.3.5-prepare
in repository https://gitbox.apache.org/repos/asf/incubator-dolphinscheduler.git


The following commit(s) were added to refs/heads/1.3.5-prepare by this push:
     new 9a995bf  [1.3.5-prepare][cherry-pick]#4524 calculate cpu usage to support application running in container (#4606)
9a995bf is described below

commit 9a995bfa38a1fa83639f98e3ac502d9a84050ef5
Author: Kirs <ac...@163.com>
AuthorDate: Sun Jan 31 15:48:16 2021 +0800

    [1.3.5-prepare][cherry-pick]#4524 calculate cpu usage to support application running in container (#4606)
    
    issue #4526
    pr #4524
---
 .../dolphinscheduler/common/utils/OSUtils.java     | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java
index 133d993..c5fd7cd 100644
--- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java
+++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/OSUtils.java
@@ -26,6 +26,9 @@ import oshi.hardware.CentralProcessor;
 import oshi.hardware.GlobalMemory;
 import oshi.hardware.HardwareAbstractionLayer;
 
+import org.apache.commons.configuration.Configuration;
+
+import java.lang.management.OperatingSystemMXBean;
 import java.io.BufferedReader;
 import java.io.FileInputStream;
 import java.io.IOException;
@@ -52,6 +55,12 @@ public class OSUtils {
   private static final SystemInfo SI = new SystemInfo();
   public static final String TWO_DECIMAL = "0.00";
 
+    /**
+     * return -1 when the function can not get hardware env info
+     * e.g {@link OSUtils#loadAverage()} {@link OSUtils#cpuUsage()}
+     */
+    public static final double NEGATIVE_ONE = -1;
+
   private static HardwareAbstractionLayer hal = SI.getHardware();
 
   private OSUtils() {}
@@ -110,8 +119,17 @@ public class OSUtils {
    * @return load average
    */
   public static double loadAverage() {
-    double loadAverage =  hal.getProcessor().getSystemLoadAverage();
-
+        double loadAverage;
+        try {
+            OperatingSystemMXBean osBean = ManagementFactory.getPlatformMXBean(OperatingSystemMXBean.class);
+            loadAverage = osBean.getSystemLoadAverage();
+        } catch (Exception e) {
+            logger.error("get operation system load average exception, try another method ", e);
+            loadAverage = hal.getProcessor().getSystemLoadAverage();
+            if (Double.isNaN(loadAverage)) {
+                return NEGATIVE_ONE;
+            }
+        }
     DecimalFormat df = new DecimalFormat(TWO_DECIMAL);
 
     df.setRoundingMode(RoundingMode.HALF_UP);