使用 eBPF 实现内核调度策略的可编程化
← 返回性能知识库传统的 Linux 调度器(CFS、RT、Deadline)虽然功能强大,但存在以下限制:
随着云原生、游戏、AI 等多样化场景的出现,需要一个更灵活的调度框架。
sched_ext 的核心创新是将调度策略从内核中解耦,通过 eBPF 实现用户态可编程:
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 利用率 |
场景: 游戏服务器 (Ebb 项目)
成果: 调度延迟从 2ms 降至 0.1ms,游戏体验显著提升
"sched_ext 让我们能够在不修改内核的情况下,为游戏工作负载定制调度策略。" —— Tejun Heo, Meta Kernel Team
场景: Cloud Run 无服务器容器
成果: 冷启动延迟降低 30%,容器密度提升 20%
场景: 视频编码工作负载
成果: 编码吞吐量提升 12%,P99 延迟降低 25%