跳到主要內容

從 EEVDF 看 Linux 核心對端側 AI 延遲敏感度的排程配額優化 / Optimizing Edge AI Latency via Linux EEVDF: Scheduling Quotas and Virtual Deadlines

daily-tech:2026-11-26

從 EEVDF 看 Linux 核心對端側 AI 延遲敏感度的排程配額優化 / Optimizing Edge AI Latency via Linux EEVDF: Scheduling Quotas and Virtual Deadlines

現象:在 Linux 與 Android 邊緣裝置上部屬端側 AI(例如即時語音辨識、手勢追蹤或端側小型語言模型 SLM Token 生成)時,許多開發團隊在處理高負載背景任務時,常發現 AI 推論會出現不可預期的延遲抖動(Latency Jitter)。即便系統已經透過前篇討論的 DMA-BUF 零拷貝技術消除了大部分的記憶體搬移開銷,但當 AI 執行緒被硬體中斷(如相機新圖框就緒、麥克風音訊緩衝區填滿)喚醒時,往往無法第一時間搶佔 CPU 核心,而是在就緒佇列(Runqueue)中痛苦排隊 5 到 15 毫秒。這類延遲使得即時互動系統的影音同步失效,嚴重傷害端側 AI 的流暢體驗。

Symptom: When deploying on-device AI workloads (such as real-time keyword spotting, gesture tracking, or edge small language model SLM token streaming) on Linux and Android devices under concurrent system loads, developers frequently encounter severe latency jitter. Even after eliminating redundant memory copies using the DMA-BUF zero-copy pipeline analyzed in our previous article, AI threads awakened by hardware interrupts (e.g., camera frame completions or audio buffer fills) often fail to seize CPU cores immediately. Instead, they languish in runqueues for 5 to 15 milliseconds, causing audio-visual desynchronization and degrading the responsive feel of edge AI interactions.

Linux EEVDF 核心排程器:端側 AI 虛擬截止期 (Virtual Deadline) 機制資格判定 (Eligibility)entity_eligible() / Lag 判定vruntime <= cfs_rq->min_vruntime過往未超額消耗 CPU 時間 (Lag >= 0)進入可執行侯選集合 (Candidate Pool)虛擬截止期 (Virtual Deadline)d_i = v_i + (q_i / w_i)配額粒度 q_i (sched_slice)AI 執行緒設定較小切片 (Slice)截止期 d_i 大幅提前、優先被選中紅黑樹檢索 (pick_eevdf)Earliest Eligible Virtual Deadline兼具頻寬公平與低搶佔延遲AI 喚醒即刻排程搶佔 CPU 核心端到端延遲抖動 (Jitter) 大幅抑制Linux 核心原始碼比對:D:\Projects\linux-6.18.55\kernel\sched\fair.c透過 pick_eevdf() 函式與 sched_setattr() 延遲敏感配額 (sched_runtime / sched_deadline),解除 AI 任務等待延遲
圖 1|Linux 核心 EEVDF 虛擬截止期與資格判定機制架構圖 / Figure 1 | Linux kernel EEVDF virtual deadline and eligibility scheduling architecture.

機制:從 CFS 的單純公平到 EEVDF 的虛擬截止期 / Mechanism: From CFS Pure Fairness to EEVDF Virtual Deadlines

機制:造成喚醒延遲的核心根源在於過去 Linux 長年採用的 CFS(Completely Fair Scheduler)排程機制。CFS 僅根據虛擬執行時間(vruntime)的單一維度來挑選候選任務,在演算法設計上將「吞吐量頻寬公平(Throughput Fairness)」與「延遲敏感度(Latency Sensitivity)」緊密捆綁在一起;想要讓 AI 執行緒快速搶佔 CPU,過去唯一的手段是大幅調高 nice 權重,但這會讓該執行緒無節制地吞噬過量的 CPU 總算力。

Mechanism: The structural root of wakeup delays stems from the legacy CFS (Completely Fair Scheduler) used in Linux for decades. CFS relies strictly on virtual runtime (vruntime) as its sole scheduling metric, intrinsically coupling throughput bandwidth fairness with latency sensitivity. Historically, the only way to grant an AI thread immediate preemption was drastically raising its nice priority, which inadvertently allowed it to monopolize total CPU bandwidth at the expense of other services.

為了解開這層死結,現代 Linux 核心全面引進了由 Peter Zijlstra 主導的 EEVDF(Earliest Eligible Virtual Deadline First)排程器。在 kernel/sched/fair.c 中,核心透過兩大步驟挑選下一個執行實體(pick_eevdf):
1. 資格判定(Eligibility):檢查實體的 vruntime 是否小於等於佇列的 min_vruntime(即 lag >= 0),確認該任務在過去的時間窗口內沒有超額預支 CPU 份額。
2. 虛擬截止期(Virtual Deadline):針對所有符合資格的候選實體,根據公式計算其虛擬截止期:d_i = v_i + (q_i / w_i)。其中 q_i 為分配給該任務的排程時間配額切片(Slice),w_i 為其權重。當一個任務被配置了較小的配額切片(例如 1ms),其虛擬截止期會大幅提前;在紅黑樹尋找最左側符合資格節點時,即可在完全不破壞長期算力頻寬公平的前提下,實現毫秒級別的極速搶佔!

To decouple this trade-off, modern Linux kernels adopted the EEVDF (Earliest Eligible Virtual Deadline First) scheduler, spearheaded by Peter Zijlstra. In kernel/sched/fair.c, the scheduler picks the next entity via pick_eevdf using a two-stage evaluation:
1. Eligibility Check: Verifies whether an entity's vruntime is less than or equal to the runqueue's min_vruntime (meaning lag >= 0), ensuring the task has not overdrawn its fair CPU share in preceding windows.
2. Virtual Deadline Assignment: Computes the deadline for all eligible candidates via d_i = v_i + (q_i / w_i), where q_i represents the allocated scheduling time slice and w_i represents weight. By assigning a compact slice (e.g., 1ms) to an AI thread, its virtual deadline shifts forward dramatically. When searching the augmented red-black tree, the task achieves millisecond-grade preemption without monopolizing long-term bandwidth!

影響:端側 AI 推論延遲抖動收斂與長尾改善 / Impact: Latency Jitter Suppression and Tail Optimization

影響:在端側多工環境下,相機捕捉、神經網路推論與 UI 算繪通常分別執行於不同的執行緒。在傳統排程器下,一旦有耗時編譯或重度檔案解壓縮行程在背景執行,AI 喚醒的長尾延遲(P99 Latency)會急遽攀升至數十毫秒;而改用 EEVDF 並賦予適當的切片配額後,推論執行緒能在喚醒瞬間被認定為具備極佳的虛擬截止期,搶先於背景運算獲得 CPU 調度。這不僅大幅抑制了推論長尾延遲的劇烈波動,更能讓端側模型維持精準穩定的幀率(Frame Rate)。

Impact: In multi-tenant edge environments, camera ingestion, neural inference, and UI rendering execute across distinct concurrent threads. Under traditional scheduling, background compilation or heavy file decompression spikes AI wakeup P99 tail latencies to tens of milliseconds. Under EEVDF with tuned slice quotas, inference threads are evaluated with imminent virtual deadlines upon waking, preempting background tasks instantly. This suppresses tail latency spikes and stabilizes inference framerates with deterministic consistency.

可操作步驟:配置 EEVDF 延遲敏感配額與排程觀察 / Actionable Steps: Configuring EEVDF Slice Quotas and Inspection

可操作步驟:在支援 EEVDF 的 Linux 核心(如 Linux 6.6+ 或本次查核的 6.18 核心)中,可透過以下步驟觀察與優化 AI 執行緒的延遲敏感度:

Actionable Steps: On Linux kernels supporting EEVDF (such as Linux 6.6+ or the verified 6.18 branch), developers can inspect and optimize AI thread latency sensitivity through the following workflow:

# 1. 檢視系統預設的 CFS/EEVDF 基礎時間切片(單位:奈秒)
sysctl kernel.sched_base_slice_ns
# 預設通常為 3000000 (3ms) 或 5000000 (5ms)

# 2. 透過 sched_setattr 系統呼叫為端側 AI 執行緒設定專屬切片配額
# 於 C/C++ 程式中設定 struct sched_attr:
struct sched_attr attr;
memset(&attr, 0, sizeof(attr));
attr.size = sizeof(attr);
attr.sched_policy = SCHED_OTHER; // 保持 SCHED_OTHER 公平排程
attr.sched_flags = SCHED_FLAG_KEEP_ALL;
attr.sched_runtime = 1000000;   // 設定 1ms 小切片以提前虛擬截止期
syscall(SYS_sched_setattr, gettid(), &attr, 0);

# 3. 追蹤執行緒在佇列中的延遲 (Wakeup Latency) 與排程行為
# 使用 Linux 內建 perf 追蹤排程搶佔延遲事件
perf sched record -- sleep 2
perf sched latency

在進行端側 AI 部署時,請注意切片大小並非越小越好。若切片設定過於激進(如小於 500 微秒),將會導致過於頻繁的上下文交換(Context Switch),反而加劇 CPU 快取(L1/L2 Cache)污染。建議將音訊與視覺前處理執行緒切片設定於 1ms 到 2ms 之間,兼顧極速響應與運算快取命中率。

When optimizing on-device AI pipelines, slice size must be balanced prudently. Configuring excessively aggressive slices (e.g., below 500 microseconds) triggers frequent context switching, which pollutes CPU L1/L2 caches and degrades throughput. Slices between 1ms and 2ms provide optimal balance between instantaneous preemption and high cache locality.

查核紀錄與核心原始碼對照:
1. 穩定版核心原始碼查核:D:\Projects\linux-6.18.55\kernel\sched\fair.c(查核 pick_eevdf() 實作、entity_eligible() 資格判斷及 sched_base_slice 配額計算)。
2. 最新核心演進比對:D:\Projects\linux-next\kernel\sched\fair.c(比對 delayed dequeue 與 lag 維護機制的演進優化)。
3. 官方文件:Linux Kernel EEVDF Scheduler Design Documentation、Linux Programmer's Manual: sched_setattr(2) 及 LWN: An EEVDF CPU scheduler for Linux。
查核基準日:2026-11-26(Asia/Taipei)。

Verification Record and Source Alignment:
1. Stable kernel verification: D:\Projects\linux-6.18.55\kernel\sched\fair.c (verified pick_eevdf() implementation, entity_eligible() qualification logic, and sched_base_slice quota calculus).
2. Latest kernel tracking: D:\Projects\linux-next\kernel\sched\fair.c (compared delayed dequeue and lag maintenance evolution).
3. Official references: Linux Kernel EEVDF Scheduler Design Documentation, Linux Programmer's Manual: sched_setattr(2), and LWN: An EEVDF CPU scheduler for Linux.
Audit reference date: 2026-11-26 (Asia/Taipei).

邊緣運算與核心開發設備推薦:在進行 Linux 核心除錯與端側 AI 模型推論驗證時,具備穩定散熱風扇與高速 NVMe 擴充插槽的邊緣開發板,能避免熱衰竭對延遲量測造成的失真。推薦選用支援 Linux 6.x 與硬體硬即時擴充的嵌入式套件。
查看蝦皮高效能邊緣運算開發板與散熱配件

Edge AI Development Recommendation: When profiling Linux kernels and edge inference models, high-efficiency SBCs equipped with active cooling and fast NVMe storage prevent thermal throttling from skewing latency benchmarks.
View High-Performance Edge Computing Boards on Shopee

利益揭露:點選上述分潤連結購買商品,本站將獲得微薄推廣佣金,不會增加您的購買費用,感謝對深度技術解析的支持。 / Affiliate Disclosure: Purchasing via the link above earns this site a modest referral commission at no additional cost to you. Thank you for supporting deep-dive tech research.

留言

這個網誌中的熱門文章

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 等代理...

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

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