跳到主要內容

Linux io_uring 為什麼仍可能卡住?從固定 Buffer 到 Completion Queue 的 I/O 路徑 / Why Linux io_uring Can Still Stall: Fixed Buffers and Completion Queues



現象:io_uring 非同步,為什麼服務仍會卡住? / Symptom: Why can an io_uring service still stall?

你把大量檔案讀取改成 Linux io_uring,CPU 使用率下降,卻仍看到請求延遲突然拉高。提交請求變得非同步,不代表儲存裝置立刻完成;queue depth、檔案系統與 block I/O 仍會把等待時間留在 completion 之前。

You moved file reads to Linux io_uring, CPU usage fell, yet request latency still spikes. Submission becomes asynchronous; storage, the filesystem, and block I/O can still delay completion.

io_uring 的基本路徑是:應用程式準備 submission queue entry(SQE),核心取出並執行,最後把結果寫成 completion queue entry(CQE)。共享 ring 減少系統呼叫,但 CQE 的 res 才是實際結果;不能只看到 SQE 已提交,就把工作算成完成。

The path is: the application prepares an SQE, the kernel executes it, and a CQE reports the result. Shared rings reduce syscall traffic, but CQE res is the real outcome. Submission is not completion.

io_uring submission queue sends requests to kernel execution and completion queue returns results; observe queue depth, CQE result, device latency, and buffer ownership
圖一|io_uring 的 SQE→核心執行→CQE 路徑;固定 buffer 的生命週期與完成延遲必須分開觀察。 / Figure 1 | The SQE-to-kernel-to-CQE path; buffer lifetime and completion latency are separate signals. Original conceptual diagram.

機制:固定 Buffer 解決什麼,又帶來什麼責任? / Mechanism: what registered buffers solve—and require

對固定大小、重複出現的 I/O,registered buffer 可以先把 buffer 註冊給 io_uring,之後請求只引用已知 buffer,可能減少每次操作的檢查與映射成本。代價是應用程式必須維持 buffer 有效,不能在核心仍可能使用時重用或釋放。這不是零延遲開關,而是把成本與責任搬到初始化與所有權管理。

For repeated I/O with stable buffers, registered buffers can reduce per-operation checking and mapping work. The trade-off is ownership: the application must keep each buffer valid until the kernel is done. This is not a zero-latency switch; it moves cost and responsibility into setup and lifetime management.

可重算示例:每個請求讀 64 KiB,同時維持 32 個未完成請求,理論上的 in-flight 資料量是 64 KiB × 32 = 2 MiB。這只是記憶體配置估算,不是吞吐量保證;若 CQE 回收速度低於提交速度,queue depth 會增加,延遲也可能惡化。

Example: 64 KiB per request with 32 in-flight requests gives 64 KiB × 32 = 2 MiB of theoretical in-flight data. That is a memory estimate, not a throughput guarantee. If CQEs are consumed more slowly than SQEs are submitted, queue depth grows and latency can worsen.

影響與可操作步驟 / Impact and practical steps

先量測,不要先調大 queue:記錄 SQE 提交時間、CQE 回來時間、cqe->res、錯誤碼與未完成數量,再對照 block 裝置延遲。若只記錄 submit 成功,你看不到真正 I/O 等待。對 AI 推論服務,模型載入與資料預取特別容易把高併發誤認成高效能。

Measure before increasing the queue: record SQE submission time, CQE arrival time, cqe->res, errors, and in-flight operations, then compare them with block-device latency. Logging only successful submission hides the wait. Model loading and prefetching make this mistake common in AI inference services.

cat /proc/diskstats; cat /sys/block/nvme0n1/stat; # read-only first checks

安全的第一輪排查是:固定 buffer 大小與數量,為每個 request 建立唯一 ID;只有收到 CQE 才回收 buffer;將提交延遲與完成延遲分開統計;最後才依服務的 p95/p99 延遲基準調整併發。若裝置延遲沒有下降,繼續加大 queue 只會增加排隊,不會創造 IOPS。

A safe first pass is to fix buffer size and count, assign each request a unique ID, recycle a buffer only after its CQE arrives, and report submission and completion latency separately. Tune concurrency from the service p95/p99 baseline. If device latency does not fall, a larger queue adds waiting rather than creating IOPS.

實務上還要分辨核心忙碌、裝置忙碌與應用程式沒有及時收 CQE 三種情況;同一個延遲數字可能來自不同層。把 request ID、buffer ID 與 CQE 結果放在同一筆 trace,才有機會把症狀對回機制。

In practice, distinguish a busy kernel, a busy device, and an application that is late to consume CQEs. The same latency number can come from different layers. Put request ID, buffer ID, and CQE result in one trace to connect symptoms to mechanisms.

資料與限制 / Sources and limits

查核日期:2026-11-12(Asia/Taipei)。本文依 Linux Kernel io_uring 官方文件與 Linux v6.18 原始碼整理;2 MiB 是示範算式,不是實測。本文未宣稱在讀者機器上執行指令,也不把非同步提交當作效能保證。 / Checked on 2026-11-12 (Asia/Taipei). Based on official Linux io_uring documentation and Linux v6.18 source. The 2 MiB value is explanatory, not a benchmark.

  1. Linux Kernel Documentation: io_uring
  2. Linux Kernel Documentation: io_uring setup
  3. Linux v6.18 io_uring source

留言

這個網誌中的熱門文章

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 云平台的集成 最...

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...