# 2.6.2.4 调度逻辑 - 调度节拍

计算机系统随着时钟节拍需要周期性地做很多事情，例如刷新屏幕、数据落盘等，而进程调度是众多任务中最重要的之一。周期性调度也叫调度节拍，它的入口是函数 `schedule_tick()`, 该函数最终会调用调度类的 `task_tick()` 方法完成操作：

```
/* file: kernel/sched/core.c */
void scheduler_tick(void) {
int cpu = smp_processor_id();
struct rq *rq = cpu_rq(cpu);
struct task_struct *curr = rq->curr;

/* 调用当前任务的调度类的 task_tick 方法 */
curr->sched_class->task_tick(rq, curr, 0);

#ifdef CONFIG_SMP
/* SMP 架构下触发负载均衡 */
rq->idle_balance = idle_cpu(cpu);
trigger_load_balance(rq);
#endif
}
```

CFS 中实现 `task_tick` 方法的是函数 `task_tick_fair`:

```
/* file: kernel/sched/fair.c */
static void task_tick_fair(struct rq *rq, struct task_struct *curr,
                    int queued) {
struct cfs_rq *cfs_rq;
struct sched_entity *se = &curr->se;

/* 在不考虑组调度的情况下，此处的循环只会迭代一次，处理的就是当前任务 */
for_each_sched_entity(se) {
cfs_rq = cfs_rq_of(se);
entity_tick(cfs_rq, se, queued);
}
}
```

实际的逻辑都在函数 `entity_tick` 中，删除无关代码及与组调度相关的逻辑，主要逻辑如下：

```
/* file: kernel/sched/fair.c */
static void entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr,
                int queued)
{
/* 首先更新当前任务及队列的各种时间信息，详见 vruntime 一节 */
update_curr(cfs_rq);

if (cfs_rq->nr_running > 1)
/* 检查是否需要抢占当前任务 */
check_preempt_tick(cfs_rq, curr);
}
```

该函数的主要任务有两个，一个是更新任务的各种时间信息；另一个是检查当前任务是否已经执行地足够久了，如果是的话就需要对其进行抢占，换其它任务来执行。关于抢占的概念将在下一节中介绍。


---

# 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-tick.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.
