|
@@ -401,6 +401,157 @@ public class Server {
|
|
System.out.printf("CPU插槽 %d 温度: %.1f°C\n", socket, temp);
|
|
System.out.printf("CPU插槽 %d 温度: %.1f°C\n", socket, temp);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ // 2. 获取双CPU的整体速度
|
|
|
|
+ Map<Integer, Double> cpuSpeeds = getCpuSpeeds();
|
|
|
|
+ if (!cpuSpeeds.isEmpty()) {
|
|
|
|
+ for (Integer socket : cpuSpeeds.keySet()) {
|
|
|
|
+ cpuList.get(socket).setSpeed(cpuSpeeds.get(socket));
|
|
|
|
+ System.out.printf("CPU插槽 %d 整体速度: %.2f GHz\n", socket, cpuSpeeds.get(socket));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 获取双CPU整体速度
|
|
|
|
+ private static Map<Integer, Double> getCpuSpeeds() {
|
|
|
|
+ Map<Integer, Double> speeds = new HashMap<>();
|
|
|
|
+
|
|
|
|
+ // 方法1: 从/proc/cpuinfo获取每个物理CPU的平均速度
|
|
|
|
+ try (BufferedReader br = new BufferedReader(new FileReader("/proc/cpuinfo"))) {
|
|
|
|
+ String line;
|
|
|
|
+ int currentSocket = -1;
|
|
|
|
+ Map<Integer, Double> socketTotalSpeed = new HashMap<>();
|
|
|
|
+ Map<Integer, Integer> socketCoreCount = new HashMap<>();
|
|
|
|
+
|
|
|
|
+ while ((line = br.readLine()) != null) {
|
|
|
|
+ if (line.startsWith("physical id")) {
|
|
|
|
+ // 提取物理CPU ID
|
|
|
|
+ String[] parts = line.split(":");
|
|
|
|
+ currentSocket = Integer.parseInt(parts[1].trim());
|
|
|
|
+
|
|
|
|
+ // 初始化计数器
|
|
|
|
+ if (!socketTotalSpeed.containsKey(currentSocket)) {
|
|
|
|
+ socketTotalSpeed.put(currentSocket, 0.0);
|
|
|
|
+ socketCoreCount.put(currentSocket, 0);
|
|
|
|
+ }
|
|
|
|
+ } else if (line.startsWith("cpu MHz") && currentSocket >= 0) {
|
|
|
|
+ // 提取核心速度
|
|
|
|
+ String[] parts = line.split(":");
|
|
|
|
+ double coreSpeed = Double.parseDouble(parts[1].trim()) / 1000.0; // MHz转GHz
|
|
|
|
+
|
|
|
|
+ // 累加到所属物理CPU
|
|
|
|
+ socketTotalSpeed.put(currentSocket, socketTotalSpeed.get(currentSocket) + coreSpeed);
|
|
|
|
+ socketCoreCount.put(currentSocket, socketCoreCount.get(currentSocket) + 1);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 计算每个物理CPU的平均速度
|
|
|
|
+ for (Integer socket : socketTotalSpeed.keySet()) {
|
|
|
|
+ double totalSpeed = socketTotalSpeed.get(socket);
|
|
|
|
+ int coreCount = socketCoreCount.get(socket);
|
|
|
|
+ speeds.put(socket, totalSpeed / coreCount);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!speeds.isEmpty()) return speeds;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ System.err.println("/proc/cpuinfo读取错误: " + e.getMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 方法2: 使用lscpu命令获取物理CPU速度
|
|
|
|
+ try {
|
|
|
|
+ Process process = Runtime.getRuntime().exec("lscpu -p=socket,core,CPU,MHz");
|
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
|
|
+ String line;
|
|
|
|
+ Map<Integer, Double> socketTotalSpeed = new HashMap<>();
|
|
|
|
+ Map<Integer, Integer> socketCoreCount = new HashMap<>();
|
|
|
|
+
|
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
|
+ // 跳过注释行
|
|
|
|
+ if (line.startsWith("#")) continue;
|
|
|
|
+
|
|
|
|
+ String[] parts = line.split(",");
|
|
|
|
+ if (parts.length >= 4) {
|
|
|
|
+ try {
|
|
|
|
+ int socket = Integer.parseInt(parts[0]);
|
|
|
|
+ double speed = Double.parseDouble(parts[3]) / 1000.0; // MHz转GHz
|
|
|
|
+
|
|
|
|
+ socketTotalSpeed.putIfAbsent(socket, 0.0);
|
|
|
|
+ socketCoreCount.putIfAbsent(socket, 0);
|
|
|
|
+
|
|
|
|
+ socketTotalSpeed.put(socket, socketTotalSpeed.get(socket) + speed);
|
|
|
|
+ socketCoreCount.put(socket, socketCoreCount.get(socket) + 1);
|
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
|
+ // 忽略格式错误行
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 计算每个物理CPU的平均速度
|
|
|
|
+ for (Integer socket : socketTotalSpeed.keySet()) {
|
|
|
|
+ double totalSpeed = socketTotalSpeed.get(socket);
|
|
|
|
+ int coreCount = socketCoreCount.get(socket);
|
|
|
|
+ speeds.put(socket, totalSpeed / coreCount);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!speeds.isEmpty()) return speeds;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ System.err.println("lscpu命令执行错误: " + e.getMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 方法3: 使用sysfs接口(回退方案)
|
|
|
|
+ try {
|
|
|
|
+ File cpuDir = new File("/sys/devices/system/cpu/");
|
|
|
|
+ Map<Integer, Double> socketTotalSpeed = new HashMap<>();
|
|
|
|
+ Map<Integer, Integer> socketCoreCount = new HashMap<>();
|
|
|
|
+
|
|
|
|
+ for (File file : cpuDir.listFiles()) {
|
|
|
|
+ if (file.getName().matches("cpu\\d+")) {
|
|
|
|
+ String coreId = file.getName().replace("cpu", "");
|
|
|
|
+
|
|
|
|
+ // 获取物理CPU ID
|
|
|
|
+ File topologyDir = new File(file, "topology");
|
|
|
|
+ File physicalPackageIdFile = new File(topologyDir, "physical_package_id");
|
|
|
|
+ int socket = 0;
|
|
|
|
+
|
|
|
|
+ if (physicalPackageIdFile.exists()) {
|
|
|
|
+ try (BufferedReader br = new BufferedReader(new FileReader(physicalPackageIdFile))) {
|
|
|
|
+ socket = Integer.parseInt(br.readLine().trim());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 读取核心速度
|
|
|
|
+ String freqPath = "/sys/devices/system/cpu/cpu" + coreId + "/cpufreq/scaling_cur_freq";
|
|
|
|
+ File freqFile = new File(freqPath);
|
|
|
|
+
|
|
|
|
+ if (freqFile.exists()) {
|
|
|
|
+ try (BufferedReader br = new BufferedReader(new FileReader(freqFile))) {
|
|
|
|
+ String line = br.readLine();
|
|
|
|
+ if (line != null) {
|
|
|
|
+ double freq = Double.parseDouble(line.trim()) / 1_000_000.0; // 转换为GHz
|
|
|
|
+
|
|
|
|
+ socketTotalSpeed.putIfAbsent(socket, 0.0);
|
|
|
|
+ socketCoreCount.putIfAbsent(socket, 0);
|
|
|
|
+
|
|
|
|
+ socketTotalSpeed.put(socket, socketTotalSpeed.get(socket) + freq);
|
|
|
|
+ socketCoreCount.put(socket, socketCoreCount.get(socket) + 1);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 计算每个物理CPU的平均速度
|
|
|
|
+ for (Integer socket : socketTotalSpeed.keySet()) {
|
|
|
|
+ double totalSpeed = socketTotalSpeed.get(socket);
|
|
|
|
+ int coreCount = socketCoreCount.get(socket);
|
|
|
|
+ speeds.put(socket, totalSpeed / coreCount);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!speeds.isEmpty()) return speeds;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ System.err.println("sysfs接口访问错误: " + e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ System.out.println("无法检测到CPU速度");
|
|
|
|
+ return speeds;
|
|
}
|
|
}
|
|
|
|
|
|
// 获取双CPU温度
|
|
// 获取双CPU温度
|