|
@@ -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) {
|