# 2.6.2 调度逻辑

通过前文对于公平性的讨论，我们知道对于CFS所管理的任务而言，vruntime相等就是公平的，CFS的工作职责就是维持所有任务的vruntime尽可能相等。总结起来，CFS的工作原理如下：CFS为每个任务维护一个虚拟时间vruntime, 每次调度时都从runqueue中挑选vruntime最小的任务来执行，并将任务执行时所耗费的墙上时间根据任务的权重换算成虚拟时间累计起来；随着时间的消耗，当当前任务的vruntime数值不再是runqueue中最小的时，调度器将其放回runqueue, 重新选择下一个任务。

CFS作为一个单独的调度类（sched\_class），其所有的代码都在文件 `kernel/sched/fair.c` 中。虽然CFS 的工作原理看起来比O(1)简单很多，但该文件的体量却比O(1)大了一个数量级：总共有10k+行代码。除了核心的调度逻辑之外，还涉及到任务组调度、CPU带宽控制、SMP架构下的负载均衡等逻辑。本章我们将详细介绍CFS 的核心调度逻辑，其它主题将在后续章节中探讨。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://s3.shizhz.me/linux-sched/cfs-sched/logic.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
