修订:2026-04-15
闲来无事整了一个脚本判断CPU是否限制,通过这大概判断出宿主机是否超售 。
注:测试结果仅供参考
vi cpu_pool_check.py把下面代码复制进去保存后执行 python3 cpu_pool_check.py
#!/usr/bin/env python3
import os, re, statistics, subprocess
CPUINFO_PATH = "/proc/cpuinfo"
STAT_PATH = "/proc/stat"
def get_cpu_model():
try:
with open(CPUINFO_PATH, "r", encoding="utf-8") as f:
for line in f:
if line.startswith("model name"):
return line.split(":", 1)[1].strip()
return None
except OSError:
return None
def get_steal_time():
try:
with open(STAT_PATH, "r", encoding="utf-8") as f:
line = f.readline().split()
if len(line) < 9:
return 0.0
values = [int(x) for x in line[1:]]
steal = values[7]
total = sum(values)
return (steal / total) * 100 if total > 0 else 0.0
except (OSError, ValueError, IndexError):
return 0.0
def run_openssl(multi=1, cores=1):
cores = max(1, int(cores))
multi = max(1, int(multi))
cmd = ["taskset", "-c", f"0-{cores - 1}", "openssl", "speed"]
if multi > 1:
cmd += ["-multi", str(multi)]
cmd += ["sha256"]
proc = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
check=False,
timeout=120,
)
if proc.returncode != 0:
raise RuntimeError("openssl 执行失败")
matches = re.findall(r"sha256\s+.*\s+([\d.]+)k", proc.stdout)
if not matches:
raise RuntimeError("openssl 输出解析失败")
values_gb = [float(x) / 1024 / 1024 for x in matches]
avg = statistics.mean(values_gb)
jitter = statistics.pstdev(values_gb) / avg * 100 if avg > 0 else 0.0
return avg, jitter
def normalize_score(avg,cores):
"""
多核归一化(避免核数影响)
"""
return avg/cores if cores>0 else 0
def cloud_score(compute,steal,jitter,cores):
score=0
norm=normalize_score(compute,cores)
if norm<0.05:score+=3
elif norm<0.15:score+=2
elif norm<0.3:score+=1
if steal>10:score+=3
elif steal>5:score+=2
elif steal>1:score+=1
if jitter>25:score+=2
elif jitter>10:score+=1
return score,norm
def classify(score,norm,steal):
if norm<0.05 and steal<1:
return "KVM严重超售(算力被压制)"
if score>=6:
return "Oversold(严重超卖云)"
if score>=4:
return "Shared(共享CPU/超分配)"
if score>=2:
return "Burstable(波动型实例)"
return "Dedicated(接近独享/物理机)"
def main():
cores = os.cpu_count() or 1
cpu_model = get_cpu_model()
steal = get_steal_time()
sha, jitter = run_openssl(multi=cores, cores=cores)
avg = sha / cores
score, norm = cloud_score(avg, steal, jitter, cores)
instance_type = classify(score, norm, steal)
print("===== 云算力识别模型 3.0(纯算力版)=====")
if cpu_model:
print(f"CPU型号 : {cpu_model}")
print(f"SHA256总吞吐 : {sha:.2f} GB/s")
print(f"平均单核吞吐 : {avg:.2f} GB/s")
print(f"Steal时间 : {steal:.2f}%")
print(f"波动 : {jitter:.1f}%")
print(f"\n算力归一值 : {norm:.4f}")
print(f"综合评分 : {score}/10")
print(f"实例类型 : {instance_type}")
if norm<0.05:
print("\n🚨 结论:CPU算力严重受限(典型KVM超售/限算力)")
elif score>=6:
print("\n⚠️ 结论:严重超卖云环境,不适合生产")
elif score>=4:
print("\n⚠️ 结论:共享CPU,性能不稳定")
else:
print("\n✅ 结论:CPU算力正常(接近独享)")
print("=====================================")
if __name__ == "__main__":
main()下面是2个云商采用多核测试结果
腾讯云4C8G 测试结果如下
===== 云算力识别模型 3.0(纯算力版)=====
CPU型号 : AMD EPYC 7K83 64-Core Processor
SHA256总吞吐 : 5.68 GB/s
平均单核吞吐 : 1.42 GB/s
Steal时间 : 0.00%
波动 : 0.0%
算力归一值 : 0.3553
综合评分 : 0/10
实例类型 : Dedicated(接近独享/物理机)
✅ 结论:CPU算力正常(接近独享)腾讯云2C2G 测试结果如下
===== 云算力识别模型 3.0(纯算力版)=====
CPU型号 : Intel(R) Xeon(R) Platinum 8255C CPU @ 2.50GHz
SHA256总吞吐 : 0.40 GB/s
平均单核吞吐 : 0.20 GB/s
Steal时间 : 0.00%
波动 : 0.0%
算力归一值 : 0.1007
综合评分 : 2/10
实例类型 : Burstable(波动型实例)
✅ 结论:CPU算力正常(接近独享)小厂16C16G 测试结果如下
===== 云算力识别模型 3.0(纯算力版)=====
CPU型号 : Intel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz
SHA256总吞吐 : 6.31 GB/s
平均单核吞吐 : 0.39 GB/s
Steal时间 : 0.01%
波动 : 0.0%
算力归一值 : 0.0246
综合评分 : 3/10
实例类型 : KVM严重超售(算力被压制)
🚨 结论:CPU算力严重受限(典型KVM超售/限算力)总结:商业/重要项目还是首选大厂