Pārlūkot izejas kodu

FIX 任务,算法,温度

tjf 2 dienas atpakaļ
vecāks
revīzija
6047a7bb49

+ 26 - 26
ruoyi-common/src/main/java/com/ruoyi/common/utils/Arith.java

@@ -5,28 +5,30 @@ import java.math.RoundingMode;
 
 /**
  * 精确的浮点数运算
- * 
+ *
  * @author ruoyi
  */
-public class Arith
-{
+public class Arith {
 
-    /** 默认除法运算精度 */
+    /**
+     * 默认除法运算精度
+     */
     private static final int DEF_DIV_SCALE = 10;
 
-    /** 这个类不能实例化 */
-    private Arith()
-    {
+    /**
+     * 这个类不能实例化
+     */
+    private Arith() {
     }
 
     /**
      * 提供精确的加法运算。
+     *
      * @param v1 被加数
      * @param v2 加数
      * @return 两个参数的和
      */
-    public static double add(double v1, double v2)
-    {
+    public static double add(double v1, double v2) {
         BigDecimal b1 = new BigDecimal(Double.toString(v1));
         BigDecimal b2 = new BigDecimal(Double.toString(v2));
         return b1.add(b2).doubleValue();
@@ -34,12 +36,12 @@ public class Arith
 
     /**
      * 提供精确的减法运算。
+     *
      * @param v1 被减数
      * @param v2 减数
      * @return 两个参数的差
      */
-    public static double sub(double v1, double v2)
-    {
+    public static double sub(double v1, double v2) {
         BigDecimal b1 = new BigDecimal(Double.toString(v1));
         BigDecimal b2 = new BigDecimal(Double.toString(v2));
         return b1.subtract(b2).doubleValue();
@@ -47,6 +49,7 @@ public class Arith
 
     /**
      * 提供精确的乘法运算。
+     *
      * @param v1 被乘数
      * @param v2 乘数
      * @return 两个参数的积
@@ -61,34 +64,32 @@ public class Arith
     /**
      * 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到
      * 小数点以后10位,以后的数字四舍五入。
+     *
      * @param v1 被除数
      * @param v2 除数
      * @return 两个参数的商
      */
-    public static double div(double v1, double v2)
-    {
+    public static double div(double v1, double v2) {
         return div(v1, v2, DEF_DIV_SCALE);
     }
 
     /**
      * 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指
      * 定精度,以后的数字四舍五入。
-     * @param v1 被除数
-     * @param v2 除数
+     *
+     * @param v1    被除数
+     * @param v2    除数
      * @param scale 表示表示需要精确到小数点以后几位。
      * @return 两个参数的商
      */
-    public static double div(double v1, double v2, int scale)
-    {
-        if (scale < 0)
-        {
+    public static double div(double v1, double v2, int scale) {
+        if (scale < 0) {
             throw new IllegalArgumentException(
                     "The scale must be a positive integer or zero");
         }
         BigDecimal b1 = new BigDecimal(Double.toString(v1));
         BigDecimal b2 = new BigDecimal(Double.toString(v2));
-        if (b1.compareTo(BigDecimal.ZERO) == 0)
-        {
+        if (b1.compareTo(BigDecimal.ZERO) == 0) {
             return BigDecimal.ZERO.doubleValue();
         }
         return b1.divide(b2, scale, RoundingMode.HALF_UP).doubleValue();
@@ -96,14 +97,13 @@ public class Arith
 
     /**
      * 提供精确的小数位四舍五入处理。
-     * @param v 需要四舍五入的数字
+     *
+     * @param v     需要四舍五入的数字
      * @param scale 小数点后保留几位
      * @return 四舍五入后的结果
      */
-    public static double round(double v, int scale)
-    {
-        if (scale < 0)
-        {
+    public static double round(double v, int scale) {
+        if (scale < 0) {
             throw new IllegalArgumentException(
                     "The scale must be a positive integer or zero");
         }

+ 62 - 68
ruoyi-framework/src/main/java/com/ruoyi/framework/web/domain/Server.java

@@ -5,7 +5,6 @@ import com.ruoyi.common.utils.ip.IpUtils;
 import com.ruoyi.framework.web.domain.server.*;
 import oshi.SystemInfo;
 import oshi.hardware.CentralProcessor;
-import oshi.hardware.CentralProcessor.TickType;
 import oshi.hardware.GlobalMemory;
 import oshi.hardware.HardwareAbstractionLayer;
 import oshi.software.os.FileSystem;
@@ -333,65 +332,38 @@ public class Server {
     private void setCpuInfo(CentralProcessor processor) {
         // 1. 获取物理CPU数量
         int physicalPackageCount = processor.getPhysicalPackageCount();
-        System.out.println("物理CPU数量: " + physicalPackageCount);
-        if (physicalPackageCount < 2) {
-            // CPU信息
-            long[] prevTicks = processor.getSystemCpuLoadTicks();
-            Util.sleep(OSHI_WAIT_SECOND);
-            long[] ticks = processor.getSystemCpuLoadTicks();
-            long nice = ticks[TickType.NICE.getIndex()] - prevTicks[TickType.NICE.getIndex()];
-            long irq = ticks[TickType.IRQ.getIndex()] - prevTicks[TickType.IRQ.getIndex()];
-            long softirq = ticks[TickType.SOFTIRQ.getIndex()] - prevTicks[TickType.SOFTIRQ.getIndex()];
-            long steal = ticks[TickType.STEAL.getIndex()] - prevTicks[TickType.STEAL.getIndex()];
-            long cSys = ticks[TickType.SYSTEM.getIndex()] - prevTicks[TickType.SYSTEM.getIndex()];
-            long user = ticks[TickType.USER.getIndex()] - prevTicks[TickType.USER.getIndex()];
-            long iowait = ticks[TickType.IOWAIT.getIndex()] - prevTicks[TickType.IOWAIT.getIndex()];
-            long idle = ticks[TickType.IDLE.getIndex()] - prevTicks[TickType.IDLE.getIndex()];
-            long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
-            cpu.setCpuNum(processor.getLogicalProcessorCount());
-            cpu.setTotal(totalCpu);
-            cpu.setSys(cSys);
-            cpu.setUsed(user);
-            cpu.setWait(iowait);
-            cpu.setFree(idle);
-            cpuList.add(cpu);
-        } else {
-            // 2分组方法:使用物理处理器编号分组
-            Map<Integer, List<CentralProcessor.LogicalProcessor>> cpuGroups = new HashMap<>();
-            for (CentralProcessor.LogicalProcessor lp : processor.getLogicalProcessors()) {
-                int packageId = lp.getPhysicalProcessorNumber() % physicalPackageCount;
-                cpuGroups.computeIfAbsent(packageId, k -> new ArrayList<>()).add(lp);
-            }
+        // 2分组方法:使用物理处理器编号分组
+        Map<Integer, List<CentralProcessor.LogicalProcessor>> cpuGroups = new HashMap<>();
+        for (CentralProcessor.LogicalProcessor lp : processor.getLogicalProcessors()) {
+            int packageId = lp.getPhysicalProcessorNumber() % physicalPackageCount;
+            cpuGroups.computeIfAbsent(packageId, k -> new ArrayList<>()).add(lp);
+        }
 
-            // 3. 获取初始CPU滴答计数
-            long[] initialSystemTicks = processor.getSystemCpuLoadTicks();
-
-            // 4. 等待1秒获取新数据
-            Util.sleep(OSHI_WAIT_SECOND);
-
-            // 5. 获取新的CPU滴答计数
-            long[] newSystemTicks = processor.getSystemCpuLoadTicks();
-
-            // 6. 计算并显示每个物理CPU的数据
-            for (int pkgId = 0; pkgId < physicalPackageCount; pkgId++) {
-                Cpu cpuOne = new Cpu();
-                if (!cpuGroups.containsKey(pkgId)) continue;
-                System.out.println("\n===== 物理CPU " + (pkgId + 1) + " 数据 =====");
-                System.out.println("包含的逻辑处理器数量: " + cpuGroups.get(pkgId).size());
-                cpuOne.setCpuNum(cpuGroups.get(pkgId).size());
-                // 计算该物理CPU的负载
-                double load = calculateCpuLoad(initialSystemTicks, newSystemTicks);
-                System.out.printf("总负载: %.1f%%\n", load * 100);
-                cpuOne.setTotal(load * 100);
-                cpuOne.setSys(newSystemTicks[CentralProcessor.TickType.SYSTEM.getIndex()]);
-                cpuOne.setUsed(newSystemTicks[CentralProcessor.TickType.USER.getIndex()]);
-                cpuOne.setWait(newSystemTicks[CentralProcessor.TickType.IOWAIT.getIndex()]);
-                cpuOne.setFree(newSystemTicks[CentralProcessor.TickType.IDLE.getIndex()]);
-                // 显示详细负载数据
-                printCpuLoadDetails(newSystemTicks);
-                cpuList.add(cpuOne);
-            }
+        // 3. 获取初始CPU滴答计数
+        long[] initialSystemTicks = processor.getSystemCpuLoadTicks();
+
+        // 4. 等待1秒获取新数据
+        Util.sleep(OSHI_WAIT_SECOND);
+
+        // 5. 获取新的CPU滴答计数
+        long[] newSystemTicks = processor.getSystemCpuLoadTicks();
+
+        // 6. 计算并显示每个物理CPU的数据
+        for (int pkgId = 0; pkgId < physicalPackageCount; pkgId++) {
+            Cpu cpuOne = new Cpu();
+            if (!cpuGroups.containsKey(pkgId)) continue;
+            System.out.println("\n===== 物理CPU " + (pkgId + 1) + " 数据 =====");
+            System.out.println("包含的逻辑处理器数量: " + cpuGroups.get(pkgId).size());
+            cpuOne.setCpuNum(cpuGroups.get(pkgId).size());
+            // 计算该物理CPU的负载
+            double load = calculateCpuLoad(initialSystemTicks, newSystemTicks);
+            System.out.printf("总负载: %.1f%%\n", load * 100);
+            cpuOne.setTotal(Arith.round(load * 100, 2));
+            // 显示详细负载数据
+            calculateDetailedCpuLoad(initialSystemTicks, newSystemTicks, cpuOne);
+            cpuList.add(cpuOne);
         }
+
         // 1. 获取所有CPU插槽的温度
         Map<Integer, Double> cpuTemps = getCpuTemperatures();
         if (cpuTemps != null) {
@@ -674,16 +646,38 @@ public class Server {
         return temps;
     }
 
-    private static void printCpuLoadDetails(long[] ticks) {
-        System.out.println("详细负载数据:");
-        System.out.printf("  User:   %d\n", ticks[CentralProcessor.TickType.USER.getIndex()]);
-        System.out.printf("  Nice:   %d\n", ticks[CentralProcessor.TickType.NICE.getIndex()]);
-        System.out.printf("  System: %d\n", ticks[CentralProcessor.TickType.SYSTEM.getIndex()]);
-        System.out.printf("  Idle:   %d\n", ticks[CentralProcessor.TickType.IDLE.getIndex()]);
-        System.out.printf("  IOWait: %d\n", ticks[CentralProcessor.TickType.IOWAIT.getIndex()]);
-        System.out.printf("  IRQ:    %d\n", ticks[CentralProcessor.TickType.IRQ.getIndex()]);
-        System.out.printf("  SoftIRQ:%d\n", ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()]);
-        System.out.printf("  Steal:  %d\n", ticks[CentralProcessor.TickType.STEAL.getIndex()]);
+    private static void calculateDetailedCpuLoad(long[] initialTicks, long[] newTicks, Cpu cpuOne) {
+        // 计算各类型时间增量
+        long user = newTicks[CentralProcessor.TickType.USER.getIndex()] - initialTicks[CentralProcessor.TickType.USER.getIndex()];
+        long nice = newTicks[CentralProcessor.TickType.NICE.getIndex()] - initialTicks[CentralProcessor.TickType.NICE.getIndex()];
+        long system = newTicks[CentralProcessor.TickType.SYSTEM.getIndex()] - initialTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
+        long idle = newTicks[CentralProcessor.TickType.IDLE.getIndex()] - initialTicks[CentralProcessor.TickType.IDLE.getIndex()];
+        long iowait = newTicks[CentralProcessor.TickType.IOWAIT.getIndex()] - initialTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
+        long irq = newTicks[CentralProcessor.TickType.IRQ.getIndex()] - initialTicks[CentralProcessor.TickType.IRQ.getIndex()];
+        long softirq = newTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - initialTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
+        long steal = newTicks[CentralProcessor.TickType.STEAL.getIndex()] - initialTicks[CentralProcessor.TickType.STEAL.getIndex()];
+
+        // 计算总时间(所有类型增量之和)
+        long total = user + nice + system + idle + iowait + irq + softirq + steal;
+
+        System.out.println("详细负载百分比:");
+
+        // 计算并显示每个负载类型的百分比(保留两位小数)
+        if (total > 0) {
+            System.out.printf("  User:   %.2f%%\n", 100.0 * user / total);
+            System.out.printf("  Nice:   %.2f%%\n", 100.0 * nice / total);
+            System.out.printf("  System: %.2f%%\n", 100.0 * system / total);
+            System.out.printf("  Idle:   %.2f%%\n", 100.0 * idle / total);
+            System.out.printf("  IOWait: %.2f%%\n", 100.0 * iowait / total);
+            System.out.printf("  IRQ:    %.2f%%\n", 100.0 * irq / total);
+            System.out.printf("  SoftIRQ:%.2f%%\n", 100.0 * softirq / total);
+            System.out.printf("  Steal:  %.2f%%\n", 100.0 * steal / total);
+            cpuOne.setSys(Arith.round(100.0 * system / total, 2));
+            cpuOne.setFree(Arith.round(100.0 * idle / total, 2));
+            cpuOne.setUsed(Arith.round(100.0 * user / total, 2));
+        } else {
+            System.out.println("  无有效负载数据");
+        }
     }
 
     private static double calculateCpuLoad(long[] initialTicks, long[] newTicks) {

+ 15 - 27
ruoyi-framework/src/main/java/com/ruoyi/framework/web/domain/server/Cpu.java

@@ -1,7 +1,5 @@
 package com.ruoyi.framework.web.domain.server;
 
-import com.ruoyi.common.utils.Arith;
-
 /**
  * CPU相关信息
  * 
@@ -73,53 +71,43 @@ public class Cpu
         this.cpuNum = cpuNum;
     }
 
-    public double getTotal()
-    {
-        return Arith.round(Arith.mul(total, 100), 2);
+    public double getTotal() {
+        return total;
     }
 
-    public void setTotal(double total)
-    {
+    public void setTotal(double total) {
         this.total = total;
     }
 
-    public double getSys()
-    {
-        return Arith.round(Arith.mul(sys / total, 100), 2);
+    public double getSys() {
+        return sys;
     }
 
-    public void setSys(double sys)
-    {
+    public void setSys(double sys) {
         this.sys = sys;
     }
 
-    public double getUsed()
-    {
-        return Arith.round(Arith.mul(used / total, 100), 2);
+    public double getUsed() {
+        return used;
     }
 
-    public void setUsed(double used)
-    {
+    public void setUsed(double used) {
         this.used = used;
     }
 
-    public double getWait()
-    {
-        return Arith.round(Arith.mul(wait / total, 100), 2);
+    public double getWait() {
+        return wait;
     }
 
-    public void setWait(double wait)
-    {
+    public void setWait(double wait) {
         this.wait = wait;
     }
 
-    public double getFree()
-    {
-        return Arith.round(Arith.mul(free / total, 100), 2);
+    public double getFree() {
+        return free;
     }
 
-    public void setFree(double free)
-    {
+    public void setFree(double free) {
         this.free = free;
     }
 }