【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."
圖一|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.
圖二|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\schedair.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 核心原始碼、排查 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.
留言
張貼留言