diff --git a/app.py b/app.py index 7176a40..1986dca 100644 --- a/app.py +++ b/app.py @@ -558,6 +558,83 @@ def _df_host(): except Exception: return {} +@app.route("/api/cpu/advise") +def api_cpu_advise(): + """负载诊断:档位结论 + 能否顶住 + 优化建议""" + try: + load = list(os.getloadavg()) + except Exception: + load = [0, 0, 0] + try: + cpu_pct = psutil.cpu_percent(interval=None) + except Exception: + cpu_pct = 0 + try: + vm = psutil.virtual_memory() + except Exception: + vm = None + logical = psutil.cpu_count() or 1 + l1 = load[0] + per_core = l1 / logical + if per_core < 0.5 and (cpu_pct or 0) < 50: + level, verdict = "轻松", "✅ 当前配置完全顶得住,负载很轻,还有大量余量。" + elif per_core < 0.8 and (cpu_pct or 0) < 75: + level, verdict = "正常", "✅ 当前配置顶得住,负载正常,日常使用流畅。" + elif per_core < 1.0 and (cpu_pct or 0) < 90: + level, verdict = "偏高", "⚠️ 当前配置勉强顶住,高峰期可能卡顿,建议按下面方法优化。" + else: + level, verdict = "过载", "🔴 当前配置顶不住了!负载已超过 CPU 承受能力,请立即优化或升级。" + tips = [] + tops = sorted(cache.get("procs", []), key=lambda x: x.get("cpu", 0), reverse=True)[:3] + if tops and (tops[0].get("cpu") or 0) > 50: + tips.append("🎯 头号吃CPU进程是 %s(占用 %s%%,PID %s),如非必需请结束它,效果最明显。" % ( + tops[0].get("name"), tops[0].get("cpu"), tops[0].get("pid"))) + if vm is not None and vm.percent > 85: + tips.append("💾 内存已用 %s%%,内存不足会拖慢 CPU(频繁 swap)。先点内存页「一键回收」,或加内存条。" % vm.percent) + tips.append("🧹 点下方「一键优化系统」:自动数据落盘 + 回收内存缓存 + 内存碎片整理,1 分钟见效。") + tips.append("📦 精简开机自启与常驻 Docker 容器,不用的先停掉(docker stop),负载立降。") + tips.append("🌡️ 检查散热:CPU 过热会自动降频,清理灰尘、保证通风,频率上去了自然流畅。") + return jsonify({"load": [round(x, 2) for x in load], "cpu": round(cpu_pct or 0, 1), + "logical": logical, "per_core": round(per_core, 2), + "level": level, "verdict": verdict, "tips": tips, + "top_cpu": [{"name": t.get("name"), "cpu": t.get("cpu"), "pid": t.get("pid")} for t in tops]}) + +@app.route("/api/sys/optimize", methods=["POST"]) +def api_sys_optimize(): + """一键优化系统: drop caches + sync + compact(若支持),返回前后对比""" + steps = [] + try: + before_mem = psutil.virtual_memory().available + except Exception: + before_mem = 0 + try: + subprocess.run(["sync"], check=False, timeout=15) + steps.append("sync 数据落盘 ✅") + except Exception as e: + steps.append("sync 失败: %s" % e) + try: + p = os.environ.get("PROCFS_PATH", "/proc") + with open(os.path.join(p, "sys", "vm", "drop_caches"), "w") as f: + f.write("3") + steps.append("回收内存缓存 ✅") + except Exception as e: + steps.append("回收缓存失败: %s" % e) + try: + cp = os.path.join(os.environ.get("PROCFS_PATH", "/proc"), "sys", "vm", "compact_memory") + if os.path.exists(cp): + with open(cp, "w") as f: + f.write("1") + steps.append("内存碎片整理 ✅") + except Exception as e: + steps.append("碎片整理跳过: %s" % e) + try: + after_mem = psutil.virtual_memory().available + except Exception: + after_mem = 0 + return jsonify({"ok": True, "steps": steps, + "freed": max(0, after_mem - before_mem), + "available": after_mem}) + @app.route("/api/mem/drop", methods=["POST"]) def api_mem_drop(): import subprocess diff --git a/docs/cpu.png b/docs/cpu.png index dd16f16..6988a45 100644 Binary files a/docs/cpu.png and b/docs/cpu.png differ diff --git a/public/index.html b/public/index.html index eaa553f..c947157 100644 --- a/public/index.html +++ b/public/index.html @@ -56,7 +56,7 @@ button.refresh{background:#20a53a;color:#fff;border:none;border-radius:3px;paddi