v1.3.17: 省电模式加硬上限频率(scaling_max_freq可选2.4/3.0/3.8GHz,切档自动恢复满血,持久化+上限显示+测速带锁标记)

This commit is contained in:
3970260761 committed 2026-11-08 01:18:01 +08:00
1 parent 755c19849e
commit cdb53ee53a
3 files changed
+81 -22

No files matched your search

+73 -15
View File
@@ -16,7 +16,7 @@ PORT = int(os.environ.get("PORT", "7031"))
# ---------- CardAuth hub (免费形态: 安装统计/在线/公告/版本推送,失败开放) ----------
HUB_URL = os.environ.get("HUB_URL", "https://auth.88531.cn/api").rstrip("/")
HUB_PROJECT_ID = os.environ.get("HUB_PROJECT_ID", "12")
HUB_VERSION = os.environ.get("SITE_VERSION", "1.3.16")
HUB_VERSION = os.environ.get("SITE_VERSION", "1.3.17")
HUB = {"ok": False, "install_id": None, "latest": "", "update_available": False,
"update_required": False, "download_url": "", "changelog": "",
"announcements": [], "popup": [], "ts": 0}
@@ -620,6 +620,9 @@ def api_bench(kind):
p = _read_perf()
r.update({"ok": True, "mode": p.get("mode", ""),
"governor": p.get("governor", ""), "epp": p.get("epp", ""),
"cap_max_mhz": p.get("cap_max_mhz", 0),
"hw_max_mhz": p.get("hw_max_mhz", 0),
"capped": p.get("capped", False),
"mhz_avg": (sum(get_percpu_mhz()) // len(get_percpu_mhz()) if get_percpu_mhz() else 0)})
elif kind == "mem":
r = _bench_mem()
@@ -649,16 +652,39 @@ PERF_MODES = {
"name": "高性能模式"},
}
def _saved_perf_mode():
def _saved_perf():
for p in PERF_FILE_FALLBACKS:
try:
with open(p) as f:
m = (json.load(f) or {}).get("mode", "")
d = json.load(f) or {}
m = d.get("mode", "")
if m in PERF_MODES:
return m
return {"mode": m, "cap": int(d.get("cap") or 0)}
except Exception:
continue
return ""
return {"mode": "", "cap": 0}
def _saved_perf_mode():
return _saved_perf().get("mode", "")
def _read_cpu_freq_limits():
cur_max, hw_max, hw_min = 0, 0, 0
try:
with open("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq") as f:
cur_max = int(f.read().strip()) // 1000
except Exception:
pass
try:
with open("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq") as f:
hw_max = int(f.read().strip()) // 1000
except Exception:
pass
try:
with open("/sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq") as f:
hw_min = int(f.read().strip()) // 1000
except Exception:
pass
return cur_max, hw_max, hw_min
def _read_perf():
gov, epp = "", ""
@@ -677,10 +703,13 @@ def _read_perf():
if v["governor"] == gov and (not epp or v["epp"] == epp):
mode = k
break
cur_max, hw_max, hw_min = _read_cpu_freq_limits()
capped = bool(hw_max and cur_max and cur_max < hw_max)
return {"governor": gov, "epp": epp, "mode": mode,
"saved": _saved_perf_mode()}
"saved": _saved_perf_mode(), "cap_max_mhz": cur_max,
"hw_max_mhz": hw_max, "hw_min_mhz": hw_min, "capped": capped}
def _apply_perf(mode):
def _apply_perf(mode, cap_mhz=0):
if mode not in PERF_MODES:
return {"ok": False, "error": "未知模式"}
want = PERF_MODES[mode]
@@ -699,20 +728,42 @@ def _apply_perf(mode):
ok_e += 1
except Exception:
fail_e += 1
ok_c = fail_c = 0
cap_applied = 0
target = 0
if mode == "powersave" and cap_mhz:
target = cap_mhz * 1000
elif mode != "powersave":
try:
with open("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq") as f:
target = int(f.read().strip())
except Exception:
target = 0
if target:
for p in sorted(glob.glob("/sys/devices/system/cpu/cpu[0-9]*/cpufreq/scaling_max_freq")) + \
sorted(glob.glob("/sys/devices/system/cpu/cpufreq/policy*/scaling_max_freq")):
try:
with open(p, "w") as f:
f.write(str(target))
ok_c += 1
except Exception:
fail_c += 1
cap_applied = target // 1000
if ok_g == 0 and fail_g > 0:
return {"ok": False, "error": "调频接口不可写( governor 写入失败 )"}
try:
with open(PERF_FILE, "w") as f:
json.dump({"mode": mode}, f)
json.dump({"mode": mode, "cap": (cap_mhz if mode == "powersave" else 0)}, f)
except Exception:
try:
with open("/app/perf_mode.json", "w") as f:
json.dump({"mode": mode}, f)
json.dump({"mode": mode, "cap": (cap_mhz if mode == "powersave" else 0)}, f)
except Exception:
pass
st = _read_perf()
st.update({"ok": True, "mode": mode, "gov_ok": ok_g, "gov_fail": fail_g,
"epp_ok": ok_e, "epp_fail": fail_e,
"cap_ok": ok_c, "cap_fail": fail_c, "cap_applied_mhz": cap_applied,
"msg": "已切换为%s,立即生效,不重启电脑" % want["name"]})
return st
@@ -723,16 +774,23 @@ def api_cpu_perf():
@app.route("/api/cpu/perf", methods=["POST"])
def api_cpu_perf_set():
try:
mode = (request.get_json(force=True, silent=True) or {}).get("mode", "")
body = request.get_json(force=True, silent=True) or {}
mode = body.get("mode", "")
try:
cap_mhz = int(body.get("cap_mhz") or 0)
except Exception:
cap_mhz = 0
if cap_mhz < 0:
cap_mhz = 0
except Exception:
mode = ""
r = _apply_perf(mode)
mode, cap_mhz = "", 0
r = _apply_perf(mode, cap_mhz)
return jsonify(r), (200 if r.get("ok") else 400)
try:
_sv = _saved_perf_mode()
if _sv:
_apply_perf(_sv)
_sv = _saved_perf()
if _sv.get("mode"):
_apply_perf(_sv["mode"], _sv.get("cap") or 0)
except Exception:
pass