Linux 為什麼還沒 OOM 就開始卡頓?用 PSI 讀懂 CPU、記憶體與 I/O 壓力 / Before OOM: Reading Linux Pressure Stall Information
現象:為什麼還沒 OOM 就開始卡? / Symptom: Why does Linux stall before OOM?
你是否遇過這種情況:記憶體還有剩、CPU 也沒有 100%,但 SSH 變慢、服務延遲突然拉高,甚至工作負載尚未被 OOM Killer 終止就已經「像死機」?這不是單看使用率就能回答的問題。Linux Pressure Stall Information(PSI)量的是工作真的因資源不足而等待了多久,讓「還能跑」與「已經在浪費時間」分開。
Have you seen a Linux host become sluggish while memory is still available and CPU is below 100%? PSI addresses the missing signal: not how full a resource looks, but how long runnable work is actually delayed by contention. A machine can be operational and already wasting latency before the OOM killer appears.
PSI 的核心不是額外的效能計數器,而是把資源競爭轉成「停滯時間」。核心分別在 /proc/pressure/cpu、memory、io 匯出資料;some 表示至少一部分工作正在等待,full 則表示所有非閒置工作同時卡住。memory full 或 io full 長時間上升,比單獨看到 free 變少更接近「系統正在失去生產力」的現象。
PSI turns contention into stall time. The kernel exports cpu, memory, and io pressure files under /proc/pressure. “some” means at least some work is stalled; “full” means all non-idle work is stalled at once. A sustained memory-full or io-full signal is often more actionable than a free-memory snapshot.
每行的 avg10、avg60、avg300 是最近 10、60、300 秒的百分比趨勢,total 則是累積微秒。這代表 PSI 適合回答兩種不同問題:短時間尖峰可看 total,持續退化可看 avg。舉例來說,若 10 秒窗口的 memory some 是 12%,可先把它理解為最近這段時間約有 12% 的工作時間受到部分記憶體停滯影響;這不是「12% 記憶體已用掉」,也不是直接的容量比例。
The avg10, avg60, and avg300 fields are rolling percentages for 10, 60, and 300 seconds; total is cumulative microseconds. Use total to notice bursts and averages to notice degradation. If memory some is 12% over ten seconds, read it as roughly twelve percent of recent task time affected by partial memory stalls—not twelve percent of RAM consumed.
這個差別對 AI 推論與資料服務尤其重要。模型載入、頁面回收、儲存裝置抖動可能先造成等待,之後才演變成 OOM 或 timeout。cgroup v2 也能提供每個工作群組的 cpu.pressure、memory.pressure、io.pressure,所以除錯時可把「整台機器壓力」和「某個容器或服務的壓力」分開,不要把鄰居工作負載的問題誤判成自己的記憶體洩漏。
The distinction matters for inference and data services. Model loading, reclaim, and storage jitter can create waiting long before an OOM or timeout. With cgroup v2, each workload group can expose its own pressure files, allowing you to separate host-wide pressure from one container’s pressure instead of blaming every symptom on a memory leak.
可操作的第一步是只讀觀察,不先改 sysctl 或重啟服務:先執行 cat /proc/pressure/memory 與 cat /proc/pressure/io,再同時記錄 free -h、vmstat 1、iostat -xz 1 的短樣本。若 PSI 上升而容量數字沒有同步告警,接著找 reclaim、I/O queue 或 cgroup 限制;若只有某個 cgroup 上升,優先檢查它的限制與工作批次。這是定位線索,不是單一指標的自動判決。
Start with read-only observation: read /proc/pressure/memory and /proc/pressure/io, then capture short samples from free -h, vmstat 1, and iostat -xz 1. If PSI rises without a matching capacity alarm, inspect reclaim, I/O queues, or cgroup limits. If only one cgroup rises, inspect its limits and batch workload first. PSI is a clue, not an automatic verdict.
# 只讀觀察 / read-only observation
cat /proc/pressure/memory
cat /proc/pressure/io
free -h
vmstat 1 5
iostat -xz 1 5需要事件化處理時,官方介面允許對 pressure 檔案寫入門檻,再用 poll 或 epoll 等待通知;例如 some 150000 1000000 表示一秒窗口內累積 150 毫秒部分停滯。門檻應依服務的延遲目標與基準測量設定,不能把別人的數字直接套用。先建立基準,再把 PSI 與請求延遲、錯誤率、回收活動對齊,才知道它是在預警,還是只是正常背景工作。
For event-driven handling, the documented interface lets users register a threshold and wait with poll or epoll; “some 150000 1000000” means 150 ms of partial stall within a one-second window. Tune thresholds from your service latency baseline, not a copied number. Correlate PSI with request latency, errors, and reclaim activity before treating it as an alert.
資料與限制 / Sources and limits
查核日期:2026-11-11(Asia/Taipei)。內容依 Linux Kernel 官方 PSI 與 cgroup v2 文件、Linux 6.18 原始碼樹文件整理;數字 12% 與 150ms 是文件語意示例,不是本機或生產環境測量。本文未宣稱已在你的主機執行命令,也不把 PSI 單獨當成根因證明。 / Checked on 2026-11-11 (Asia/Taipei). Based on official Linux Kernel PSI and cgroup v2 documentation and the Linux 6.18 source tree. The 12% and 150 ms values are explanatory examples, not host or production measurements. Commands were not claimed to be run on your machine, and PSI alone is not proof of root cause.
留言
張貼留言