跳到主要內容

【Linux 排程機制】從 CFS 到 EEVDF:虛擬截止時間如何給 Linux 排程帶來延遲保證 / [Linux Scheduling] From CFS to EEVDF: How Earliest Eligible Virtual Deadline First Brings Latency Guarantees to Linux

你有沒有遇過這種情況:開著幾個重度應用,滑鼠游標明明沒動,系統卻突然停頓半秒,播放的影片掉幀、遊戲瞬間卡住?這往往不是記憶體不足,而是「排程延遲」——核心決定哪個進程下次使用 CPU 的時間點,沒有給即時工作負載足夠的保證。舊版 Linux 預設排程器 CFS(完全公平排程)追求整體公平,但對「這個任務必須在多久內執行」的延遲上限,缺乏明確承諾。

Have you ever experienced this: several heavy applications running, the mouse cursor sits still, yet the system suddenly freezes for half a second, video stutters, or a game hitches? This is often not memory exhaustion, but "scheduling latency" — the kernel's decision of which process gets the CPU next fails to guarantee timing for latency-sensitive workloads. The default Linux scheduler CFS (Completely Fair Scheduler) pursues overall fairness, but lacks a clear upper-bound commitment on "this task must run within how long."

struct sched_entity (排程實體) Linux 6.18.55 · kernel/sched/fair.c · include/linux/sched.h EEVDF 決策欄位 loadstruct load_weight — 加權負載,決定分攤時間比例 run_nodestruct rb_node — 掛在 CFS 紅黑樹 tasks_timeline 上的節點 vruntimeu64 已用虛擬時間,用來計算 lag/是否合格;挑選核心是 deadline,不是 vruntime 越小就越先跑 deadlineu64 虛擬截止時間,vd_i = ve_i + r_i / w_i(越早越先執行) min_vruntime子樹最小 vruntime,用於 RB-tree 合格性剪枝 min_slice / max_slice執行片段下限與上限,約束每次調度長度 sliceu64 本次請求執行時間,預設 sysctl_sched_base_slice = 700000 ns vlags64 虛擬延遲,約實際與理想執行時間之差 vprot受保護截止時間,保證最小調度量(quantum) cfs_rq / my_q所屬與擁有的調度佇列(CFS group sched) EEVDF 決策欄位 EEVDF 如何從中做決定 1) 任務必須「合格」(entity_eligible):欠下的服務已到,才能進入候選。 2) 在合格任務中,挑選 deadline 最早的那一個(虛擬截止時間最早先執行)。 3) 採用擴增紅黑樹,O(log n) 時間完成挑選,min_vruntime 用於剪枝不合格子樹。 原始碼:D:\Projects\linux-6.18.55\kernel\sched\fair.c / include/linux/sched.h(已查核 2026-11-10)

圖一|Linux 排程實體 struct sched_entity 的核心欄位:vruntime、deadline、min_vruntime、slice、vlag 與 vprot(原創架構示意圖,非產品截圖)

Figure 1 | Core fields of Linux scheduling entity struct sched_entity: vruntime, deadline, min_vruntime, slice, vlag and vprot (Original architectural diagram, not a product screenshot).

現象:CFS 的公平與延遲抖動 / Phenomenon: CFS Fairness and Latency Jitter

CFS 把每個進程當成一個「排程實體」(scheduling entity),記錄它已經使用了多少虛擬時間(vruntime)。 vruntime 越小的進程越優先執行,這樣能讓所有進程分到大致相等的 CPU 時間。但 CFS 的問題在於:它只比較「誰用得比較少」,沒有明確的「誰必須在多久內執行」的截止時間。當系統負載高、進程眾多時,對延遲敏感的任務(例如音訊、影片、即時訊息)可能要等很久才輪到 CPU,造成可感的卡頓。

CFS treats every process as a "scheduling entity," recording how much virtual time (vruntime) it has consumed. The entity with the smallest vruntime runs first, so all processes receive roughly equal CPU time. CFS's problem is that it only compares "who has used less," without a clear "who must run within how long" deadline. Under high load with many processes, latency-sensitive tasks (audio, video, instant messaging) may wait a long time for the CPU, causing noticeable stutter.

機制:EEVDF 與虛擬截止時間 / Mechanism: EEVDF and Virtual Deadlines

Linux 6.6 起,EEVDF(最早合格虛擬截止時間優先,Earliest Eligible Virtual Deadline First)取代 CFS 成為預設公平排程器。EEVDF 的核心想法是:給每個任務設定一個「虛擬截止時間」(deadline),核心總是挑選「已合格且截止時間最早」的任務執行。這樣不只保持公平,還能給每個任務一個可預期的執行時機上限,大幅降低延遲抖動。

Since Linux 6.6, EEVDF (Earliest Eligible Virtual Deadline First) replaces CFS as the default fair scheduler. EEVDF's core idea is to assign each task a "virtual deadline," and the kernel always picks the eligible task with the earliest deadline to run. This preserves fairness while giving each task a predictable execution upper bound, greatly reducing latency jitter.

tasks_timeline 紅黑樹(按虛擬截止時間排序) EEVDF:挑選合格且 deadline 最早的任務,O(log n) pick_eevdf() D=700 D=400 D=200 D=350 D=1000 D=850 D=1200 根節點 D=700(當前選中候選) 左子樹:全部 deadline < 700 右子樹:全部 deadline > 700 合格實體(entity_eligible)才會被選中;不合格者被 min_vruntime 剪枝 pick_eevdf() 選擇邏輯(kernel/sched/fair.c) 若只有一個實體:直接回傳目前執行中的 curr。 若有 PICK_BUDDY 且 cfs_rq->next 合格:選 next(不影響公平性)。 否則從左至右掃描,挑選第一個合格且 deadline 最早的實體;紅黑樹 O(log n)。 保護片段(protect_slice):若 curr 仍在受保護片段內,繼續執行 curr,避免頻繁搶佔。 虛擬截止時間公式:vd_i = ve_i + r_i / w_i · w_i 由 nice 值決定 · r_i = sysctl_sched_base_slice (700000 ns) 原始碼:D:\Projects\linux-6.18.55\kernel\sched\fair.c(已查核 2026-11-10)

圖二|EEVDF tasks_timeline 紅黑樹:按虛擬截止時間排序,pick_eevdf() 在 O(log n) 時間內挑出合格且 deadline 最早的任務(原創時序架構圖)

Figure 2 | EEVDF tasks_timeline red-black tree ordered by virtual deadline; pick_eevdf() selects the eligible task with the earliest deadline in O(log n) (Original sequence diagram).

EEVDF 的核心資料結構是 struct sched_entity(見圖一)。每個實體記錄 vruntime(已用虛擬時間)、deadline(虛擬截止時間)、min_vruntime(子樹最小 vruntime,用於剪枝)、slice(本次執行片段,預設 700000 ns、約 0.7 ms,來自 sysctl_sched_base_slice)、vlag(虛擬延遲)與 vprot(受保護截止時間)。核心維護一棵按 deadline 排序的紅黑樹 tasks_timeline,pick_eevdf() 函式會從合格實體中挑出 deadline 最早者,時間複雜度 O(log n)。當實體的 vruntime 趕上 deadline 時,update_deadline() 會依公式 vd_i = ve_i + r_i / w_i 重新計算下一個截止時間,w_i 由 nice 值決定,nice 值越低(優先級越高),分配到的虛擬時間斜率越大,執行機會越多。

The core data structure of EEVDF is struct sched_entity (see Figure 1). Each entity records vruntime (virtual time consumed), deadline (virtual deadline), min_vruntime (subtree minimum vruntime for pruning), slice (this run's time slice, default 700000 ns ≈ 0.7 ms from sysctl_sched_base_slice), vlag (virtual lag), and vprot (protected deadline). The kernel maintains a red-black tree tasks_timeline sorted by deadline, and pick_eevdf() selects the eligible entity with the earliest deadline in O(log n). When an entity's vruntime catches up to its deadline, update_deadline() recomputes the next deadline via vd_i = ve_i + r_i / w_i, where w_i is determined by nice value; lower nice (higher priority) yields a larger virtual-time slope and more CPU share.

核心原始碼驗證基準(Linux 6.18.55):

經查核 D:\Projects\linux-6.18.55\kernel\sched air.c 與 include/linux/sched.h:

  • struct sched_entity 包含 vruntime、deadline、min_vruntime、slice、vlag、vprot 與 run_node(rb_node)。
  • pick_eevdf() 從 tasks_timeline 紅黑樹中挑出合格且 deadline 最早的實體,採 O(logn) 掃描;min_vruntime 用於剪枝不合格子樹。
  • update_deadline() 依 vd_i = ve_i + r_i / w_i 計算,sysctl_sched_base_slice 預設 700000 ns。
  • entity_lag() 給定穩態延遲界限:-r_max < lag < max(r_max, q)。

Kernel Code Verification (Linux 6.18.55): Inspected kernel/sched/fair.c and include/linux/sched.h:

  • struct sched_entity includes vruntime, deadline, min_vruntime, slice, vlag, vprot, and run_node (rb_node).
  • pick_eevdf() picks the eligible entity with the earliest deadline from tasks_timeline red-black tree, O(log n) scan; min_vruntime prunes ineligible subtrees.
  • update_deadline() computes vd_i = ve_i + r_i / w_i; sysctl_sched_base_slice defaults to 700000 ns.
  • entity_lag() gives steady-state latency bound: -r_max < lag < max(r_max, q).

影響:延遲保證與即時工作負載 / Impact: Latency Guarantees and Real-Time Workloads

EEVDF 最大的好處是「延遲可預期」。對桌面使用者,滑鼠與鍵盤回應更即時;對多媒體與遊戲,畫面掉幀與音訊爆音減少;對伺服器,尾延遲(p99 latency)更穩定。與傳統即時排程(SCHED_FIFO/RR)不同,EEVDF 不需要把任務提到最高優先級,就在公平排程框架內自然獲得延遲上限,避免低優先級任務長期被餓死(starvation)。

EEVDF's biggest benefit is "predictable latency." Desktop users get more immediate mouse and keyboard response; multimedia and gaming see fewer dropped frames and audio glitches; servers enjoy more stable tail latency (p99). Unlike traditional real-time scheduling (SCHED_FIFO/RR), EEVDF does not need to boost tasks to the highest priority — it obtains a latency upper bound within the fair scheduling framework, avoiding starvation of low-priority tasks.

可操作步驟:查看與確認 EEVDF 運行狀態 / Actionable Steps: Inspecting EEVDF

想確認你的系統正在使用 EEVDF,並觀察排程行為,可嘗試以下唯讀觀察步驟(無需重新編譯核心):

To confirm your system uses EEVDF and observe scheduling behavior, try these read-only observation steps (no kernel rebuild needed):

# 1. 查看目前採用的排程類別(應為 EEVDF)
cat /sys/kernel/debug/sched_features | grep EEVDF

# 2. 查看基礎執行片段(ns),預設 700000
cat /proc/sys/kernel/sched_base_slice_ns

# 3. 觀察特定進程的排程統計與 vruntime
cat /proc/[PID]/sched

# 4. 用 perf 捕捉排程延遲(需 root)
perf sched record -a sleep 10
perf sched latency

這些指令都是唯讀觀察,不會改動系統狀態。若你需要更細緻的調優(例如調整 nice 值或 sched_latency),建議先在測試環境驗證,避免影響生產 workload。

These commands are read-only observations and do not alter system state. If you need finer tuning (e.g., adjusting nice values or sched_latency), validate in a test environment first to avoid impacting production workloads.

📚 布萊嗯研究所・系統核心與底層架構專題導流:

👉 【下一篇預告】:【Linux 虛擬記憶體篇】OOM Killer 如何決定犧牲哪一個進程?核心 oom_score 與 oom_adj 機制深度拆解
工程師桌面選品 工作站與開發環境好物:散熱支架、多螢幕擴充座與人體工學周邊

深入研讀 Linux 核心原始碼、排查 EEVDF 排程延遲時,多螢幕工作與長時間追蹤效能數據是常態。搭配高品質筆電散熱支架、高效多孔快充擴充座與人體工學外接鍵盤,能顯著提升排查效率與工作專注度。點擊下方連結可前往蝦皮探索工程師精選配備。

※ 利益揭露:本篇文章參與蝦皮分潤計畫,透過上方推廣連結成立有效訂單,本站可能獲得微薄分潤收益支持持續營運創作。商品即時售價、活動優惠與庫存以蝦皮賣場當下頁面為準。

官方查核與文獻依據 / Sources and Verification:

查核日期:2026-11-10。本文核心機制依據 Linux 核心原始碼:

  • Linux Kernel Source Tree: kernel/sched/fair.c & include/linux/sched.h (Linux 6.18.55,本機 D:\Projects\linux-6.18.55)
  • Linux Kernel Documentation: Documentation/scheduler/sched-eevdf.rst
  • Wikipedia: Earliest Eligible Virtual Deadline First (EEVDF)

References: Linux 6.18.55 kernel sources kernel/sched/fair.c and include/linux/sched.h (local D:\Projects\linux-6.18.55), Documentation/scheduler/sched-eevdf.rst, Wikipedia EEVDF (Verified 2026-11-10). Original structure diagrams produced specifically for this publication; not vendor screenshots.

留言

這個網誌中的熱門文章

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