跳到主要內容

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.

Linux PSI 將資源狀態轉成 some 與 full 停滯訊號,再連到 cgroup、回收與 I/O queue 排查 / Linux PSI turns resource contention into some and full stall signals, then guides cgroup, reclaim and I/O queue diagnosis
圖一|PSI 連接資源狀態、實際停滯與排查行動;原創概念架構圖,非產品畫面。 / Figure 1 | PSI connects resource state, actual stalls, and diagnostic actions; original conceptual diagram.

每行的 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.

  1. Linux Kernel Documentation: PSI - Pressure Stall Information
  2. Linux Kernel Documentation: Control Group v2
  3. Linux 6.18 source: Documentation/accounting/psi.rst

留言

這個網誌中的熱門文章

kimiflare:將 Moonshot Kimi-K2.6 直接部署於 Cloudflare 的終端 AI 編碼助手 | kimiflare: A Terminal Coding Agent Powered by Moonshot Kimi-K2.6 on Cloudflare

無需中繼,在你的 Cloudflare 帳戶中直接運行強大的 Kimi 終端編碼智能體。 | No middleman, run the powerful Kimi terminal coding agent directly on your own Cloudflare account. 🔎 工具速覽 / AT A GLANCE Category AI Coding Agent / Developer Tool Pricing Free (Open-weight model / Cloudflare Workers AI pricing) BestFor Developers seeking a private, high-context terminal AI assistant with direct infrastructure control. GitHub Stars ⭐ 36 🚀 引言 / Introduction kimiflare 是一款為開發者量身打造的終端編碼智能體,它將 Moonshot 的 1 兆參數開源模型 Kimi-K2.6 與 Cloudflare Workers AI 完美結合。不同於傳統 AI 工具,kimiflare 讓流量直接在你的 Cloudflare 帳戶中流動,徹底擺脫第三方代理或 AI 閘道的束縛,實現最高等級的控制力與隱私。 | kimiflare is a terminal coding agent tailored for developers, seamlessly combining Moonshot's 1T-parameter open-source model Kimi-K2.6 with Cloudflare Workers AI. Unlike traditional AI tools, kimiflare routes traffic directly through your Cloudflare account, eliminating the n...

text-to-cad: 讓 AI 驅動的參數化 CAD 建模成為現實 / text-to-cad: Bringing AI-Driven Parametric CAD Modeling to Reality

一個讓 AI 代理能生成、預覽並迭代 3D CAD 模型的開源框架 / An open-source harness for AI agents to generate, preview, and iterate 3D CAD models. 🔎 工具速覽 / AT A GLANCE Category AI-Powered CAD Tooling / AI 驅動 CAD 工具 Pricing Free (Open Source) / 免費(開源) BestFor Robotics engineers and product designers using AI coding agents / 使用 AI 程式碼代理的機器人工程師與產品設計師 GitHub Stars ⭐ 93 🚀 引言 / Introduction text-to-cad 是一個強大的開源框架,旨在填補大型語言模型(LLM)與專業 3D 建模之間的鴻溝。它允許開發者利用 Claude Code 或 Codex 等 AI 代理,透過程式碼生成精確的參數化幾何模型,並提供即時預覽與迭代能力。 text-to-cad is a powerful open-source harness designed to bridge the gap between Large Language Models (LLMs) and professional 3D modeling. It enables developers to leverage AI agents like Claude Code or Codex to generate precise parametric geometry via code, providing real-time preview and iteration capabilities. 🛠️ 核心功能 / Key Features 與 Codex 或 Claude Code 等代理...

Orca Skills: 開源賦能,加速雲安全工作流

加速雲安全工作流 - Orca Skills 整合多種工具與插件,打造高效安全環境 | Accelerate Cloud Security Workflows with Orca Skills, a Suite of Tools and Plugins 🔎 工具速覽 / AT A GLANCE Category 开源安全工具 Pricing 开源免费 BestFor 雲安全团队、DevSecOps工程师 GitHub Stars ⭐ 33 🚀 引言 / Introduction 在雲端環境中,確保安全不僅是一種技術挑戰,更是對工作效率的一場考驗。Orca Skills 是一款專為加速 Orca 云平台安全工作流而設計的开源插件工具包。透過整合多種工具和插件,幫助我們在複雜的安全環境下保持高效率。 🛠️ 核心功能 / Key Features Orca Skills 提供了一系列功能強大的安全工作流工具,包括警報處理、影響分析、配置源頭追溯等。這些工具不僅能夠幫助我們快速定位問題,還能確保在解決問題時避免造成不必要的後續問題。 💡 技術亮點 / Tech Highlights Orca Skills 真正的亮點在于其開放性與兼容性。它不僅支持Anthropic Claude Code、Cursor 和 Codex,還能夠與 MCP(Orca Security)集成,為安全團隊提供全面的支持。 📦 快速上手 / Quick Start 要快速開始使用 Orca Skills,首先需要安裝所需環境和工具。接下來,配置 MCP 以實現與 Orca Cloud Platform 的集成。最後,測試各個技能插件以確保其正常運行。 中英文對照: 要快速開始使用 Orca Skills 首先需要安裝所需環境和工具 接著配置 MCP 以實現與 Orca 云平台的集成 最...