Selecting the Next Task

Selecting the next task to run is performed in pick_next_task_fair. The code flow diagram is shown in Figure 2-21.

If no tasks are currently runnable on the queue as indicated by an empty nr_running counter, there is little to do and the function can return immediately. Otherwise, the work is delegated to pick_next_entity.

If a leftmost task is available in the tree, it can immediately be determined using the first_fair helper function, and_pick_next_entity extracts the sched_entity instance from the red-black tree. This is done using the container_of mechanism because the red-black tree manages instances of rb_node that are embedded in sched_entitys.

Now the task has been selected, but some more work is required to mark it as the running task. This is handled by set_next_entity.

kernel/sched_fair.c static void set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se) {

/* 'current' is not kept within the tree. */ if (se->on_rq) {

_dequeue_entity(cfs_rq, se);

Figure 2-21: Code flow diagram for pick_next_task_fair.

The currently executing process is not kept on the run queue, so_dequeue_entity removes it from the red-black tree, setting the leftmost pointer to the next leftmost task if the current task has been the leftmost one. Notice that in our case, the process has been on the run queue for sure, but this need not be the case when set_next_entity is called from different places.

Although the process is not contained in the red-black tree anymore, the connection between process and run queue is not lost, because curr marks it as the running one now:

kernel/sched_fair.c cfs_rq->curr = se;

se->prev_sum_exec_runtime = se->sum_exec_runtime;

Because the process is now the currently active one, the real time spent on the CPU will be charged to sum_exec_runtime, so the kernel preserves the previous setting in prev_sum_exec_runtime. Note that sum_exec_runtime is not reset in the process. The difference sum_exec_runtime - prev_sum_ exec_runtime does therefore denote the real time spent executing on a CPU.

Continue reading here: Handling the Periodic Tick

Was this article helpful?

0 0