Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 2: Process Management and Scheduling


The newly woken task need not necessarily be handled by the completely fair scheduler. If the new task
is a real-time task, rescheduling is immediately requested because real-time tasks always preempt CFS
tasks:

kernel/sched_fair.c
static void check_preempt_wakeup(struct rq *rq, struct task_struct *p)
{
struct task_struct *curr = rq->curr;
struct cfs_rq *cfs_rq = task_cfs_rq(curr);
struct sched_entity *se = &curr->se, *pse = &p->se;
unsigned long gran;

if (unlikely(rt_prio(p->prio))) {
update_rq_clock(rq);
update_curr(cfs_rq);
resched_task(curr);
return;
}
...

The most convenient cases areSCHED_BATCHtasks — they do not preempt other tasks by definition.

kernel/sched.c
if (unlikely(p->policy == SCHED_BATCH))
return;
...

When a running task is preempted by a new task, the kernel ensures that the old one has at least
been running for a certain minimum amount of time. The minimum is kept insysctl_sched_
wakeup_granularity, which crossed our path before. Recall that it is per default set to 4 ms. This refers
to real time, so the kernel first needs to convert it into virtual time if required:

kernel/sched_fair.c
gran = sysctl_sched_wakeup_granularity;
if (unlikely(se->load.weight != NICE_0_LOAD))
gran = calc_delta_fair(gran, &se->load);
...

If the virtual run time of the currently executing task (represented by its scheduling entityse)islarger
than the virtual run time of the new task plus the granularity safety, a rescheduling is requested:

kernel/sched_fair.c
if (pse->vruntime + gran < se->vruntime)
resched_task(curr);
}

The added ‘‘buffer’’ time ensures that tasks are not switched too frequently so that not too much time is
spent in context switching instead of doing real work.

2.6.7 Handling New Tasks


The last operation of the completely fair scheduler that we need to consider is the hook function that
is called when new tasks are created:task_new_fair. The behavior of the function is controllable with
Free download pdf