Skip to content

Commit f941210

Browse files
authored
fix(check): 修复自检插件在ARM设备下的CPU频率获取逻辑 (#2057)
- 将插件版本从0.1更新至0.2 - 新增安全获取ARM设备CPU频率的函数get_arm_cpu_freq_safe - 优化CPU信息采集逻辑,提高在ARM架构下的兼容性
1 parent 761c8da commit f941210

File tree

2 files changed

+46
-36
lines changed

2 files changed

+46
-36
lines changed

zhenxun/builtin_plugins/check/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
""".strip(),
2727
extra=PluginExtraData(
2828
author="HibiKier",
29-
version="0.1",
29+
version="0.2",
3030
plugin_type=PluginType.SUPERUSER,
3131
configs=[
3232
RegisterConfig(

zhenxun/builtin_plugins/check/data_source.py

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import contextlib
12
from dataclasses import dataclass
23
import os
34
from pathlib import Path
@@ -18,7 +19,47 @@
1819
GOOGLE_URL = "https://www.google.com/"
1920

2021
VERSION_FILE = Path() / "__version__"
21-
ARM_KEY = "aarch64"
22+
23+
24+
def get_arm_cpu_freq_safe():
25+
"""获取ARM设备CPU频率"""
26+
# 方法1: 优先从系统频率文件读取
27+
freq_files = [
28+
"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq",
29+
"/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq",
30+
"/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq",
31+
"/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq",
32+
]
33+
34+
for freq_file in freq_files:
35+
try:
36+
with open(freq_file) as f:
37+
frequency = int(f.read().strip())
38+
return round(frequency / 1000000, 2) # 转换为GHz
39+
except (OSError, ValueError):
40+
continue
41+
42+
# 方法2: 解析/proc/cpuinfo
43+
with contextlib.suppress(OSError, FileNotFoundError, ValueError, PermissionError):
44+
with open("/proc/cpuinfo") as f:
45+
for line in f:
46+
if "CPU MHz" in line:
47+
freq = float(line.split(":")[1].strip())
48+
return round(freq / 1000, 2) # 转换为GHz
49+
# 方法3: 使用lscpu命令
50+
with contextlib.suppress(OSError, subprocess.SubprocessError, ValueError):
51+
env = os.environ.copy()
52+
env["LC_ALL"] = "C"
53+
result = subprocess.run(
54+
["lscpu"], capture_output=True, text=True, env=env, timeout=10
55+
)
56+
57+
if result.returncode == 0:
58+
for line in result.stdout.split("\n"):
59+
if "CPU max MHz" in line or "CPU MHz" in line:
60+
freq = float(line.split(":")[1].strip())
61+
return round(freq / 1000, 2) # 转换为GHz
62+
return 0 # 如果所有方法都失败,返回0
2263

2364

2465
@dataclass
@@ -37,7 +78,7 @@ def get_cpu_info(cls):
3778
if _cpu_freq := psutil.cpu_freq():
3879
cpu_freq = round(_cpu_freq.current / 1000, 2)
3980
else:
40-
cpu_freq = 0
81+
cpu_freq = get_arm_cpu_freq_safe()
4182
return CPUInfo(core=cpu_core, usage=cpu_usage, freq=cpu_freq)
4283

4384

@@ -160,44 +201,13 @@ def __get_version() -> str | None:
160201
return None
161202

162203

163-
def __get_arm_cpu():
164-
env = os.environ.copy()
165-
env["LC_ALL"] = "en_US.UTF-8"
166-
cpu_info = subprocess.check_output(["lscpu"], env=env).decode()
167-
model_name = ""
168-
cpu_freq = 0
169-
for line in cpu_info.splitlines():
170-
if "Model name" in line:
171-
model_name = line.split(":")[1].strip()
172-
if "CPU MHz" in line:
173-
cpu_freq = float(line.split(":")[1].strip())
174-
return model_name, cpu_freq
175-
176-
177-
def __get_arm_oracle_cpu_freq():
178-
cpu_freq = subprocess.check_output(
179-
["dmidecode", "-s", "processor-frequency"]
180-
).decode()
181-
return round(float(cpu_freq.split()[0]) / 1000, 2)
182-
183-
184204
async def get_status_info() -> dict:
185205
"""获取信息"""
186206
data = await __build_status()
187207

188208
system = platform.uname()
189-
if system.machine == ARM_KEY and not (
190-
cpuinfo.get_cpu_info().get("brand_raw") and data.cpu.freq
191-
):
192-
model_name, cpu_freq = __get_arm_cpu()
193-
if not data.cpu.freq:
194-
data.cpu.freq = cpu_freq or __get_arm_oracle_cpu_freq()
195-
data = data.get_system_info()
196-
data["brand_raw"] = model_name
197-
else:
198-
data = data.get_system_info()
199-
data["brand_raw"] = cpuinfo.get_cpu_info().get("brand_raw", "Unknown")
200-
209+
data = data.get_system_info()
210+
data["brand_raw"] = cpuinfo.get_cpu_info().get("brand_raw", "Unknown")
201211
baidu, google = await __get_network_info()
202212
data["baidu"] = "#8CC265" if baidu else "red"
203213
data["google"] = "#8CC265" if google else "red"

0 commit comments

Comments
 (0)