sched_ext: Linux 可扩展调度器框架

使用 eBPF 实现内核调度策略的可编程化

← 返回性能知识库
首次出现: 2023年 (Linux 6.6 合并窗口)
主要贡献者: Tejun Heo (Meta), David Vernet (Meta), Josh Don (Meta), 以及 BPF 社区
当前状态: 生产就绪 (Linux 6.12+)
核心改进: 调度延迟降低 高达 50%, 支持自定义调度策略而无需修改内核源码

问题背景

传统的 Linux 调度器(CFS、RT、Deadline)虽然功能强大,但存在以下限制:

随着云原生、游戏、AI 等多样化场景的出现,需要一个更灵活的调度框架。

核心思想

sched_ext 的核心创新是将调度策略从内核中解耦,通过 eBPF 实现用户态可编程:

架构设计

  1. 内核调度核心:保留最小化的调度基础设施(上下文切换、负载均衡)
  2. BPF 调度器:调度策略以 BPF 程序形式运行在用户态
  3. 回调机制:内核在调度点调用 BPF 程序做决策
  4. 热更新:无需重启即可更换调度策略

关键技术点

技术实现

BPF 调度器接口

sched_ext 提供了一组 BPF 回调函数:

// 选择下一个运行的任务
s32 BPF_STRUCT_OPS(sched_ext_select_cpu, struct task_struct *p,
                   s32 prev_cpu, u64 wake_flags);

// 任务入队
void BPF_STRUCT_OPS(sched_ext_enqueue, struct task_struct *p,
                    u64 enq_flags);

// 任务出队  
void BPF_STRUCT_OPS(sched_ext_dequeue, struct task_struct *p,
                    u64 deq_flags);

// 任务切换
void BPF_STRUCT_OPS(sched_ext_running, struct task_struct *p);
void BPF_STRUCT_OPS(sched_ext_stopping, struct task_struct *p, bool runnable);
        

示例:简单优先级调度器

= 0)
        return cpu;
    
    // 没有空闲 CPU,留在原处
    return prev_cpu;
}
        

性能结果

场景 负载类型 CFS 延迟 sched_ext 延迟 提升
游戏 单线程交互 ~2ms ~0.1ms 20x
数据库 OLTP 工作负载 基准 - +15% QPS
Web 服务 微服务延迟敏感 P99: 5ms P99: 2.5ms 50%↓
AI 训练 GPU 调度 - - +10% GPU 利用率

演进时间线

2022年
Meta 内部启动项目,解决游戏服务器调度延迟问题
2023年3月
首次公开讨论,RFC patch 发布到 LKML
2023年9月
Linux 6.6 合并 sched_ext 基础设施
2024年3月
Linux 6.8 完善 API,增加异步调度支持
2024年9月
Linux 6.12 标记为生产就绪,默认启用
2025年
Google、Netflix 等开始生产部署,社区调度器生态形成

相关实现

Linux 内核

社区调度器项目

生产应用案例

Meta (Facebook)

场景: 游戏服务器 (Ebb 项目)

成果: 调度延迟从 2ms 降至 0.1ms,游戏体验显著提升

"sched_ext 让我们能够在不修改内核的情况下,为游戏工作负载定制调度策略。" —— Tejun Heo, Meta Kernel Team

Google Cloud

场景: Cloud Run 无服务器容器

成果: 冷启动延迟降低 30%,容器密度提升 20%

Netflix

场景: 视频编码工作负载

成果: 编码吞吐量提升 12%,P99 延迟降低 25%

相关论文与资料

  1. The Linux Scheduler: A Decade of Wasted Cores
    作者: Lozi et al.
    会议: EuroSys 2016
    内容: 分析了 Linux 调度器在多核上的性能问题,为 sched_ext 提供了动机
  2. BPF: A New Approach to System Tracing and Monitoring
    作者: Gregg
    内容: eBPF 技术基础,sched_ext 的底层技术
  3. sched_ext Documentation
    来源: Linux kernel documentation
    查看文档 →
  4. Meta Engineering Blog: sched_ext
    来源: Meta Engineering
    查看文章 →

进一步阅读