进程的goodness值通过goodness函数计算。goodness返回下面两类中的一个值:1,000以下或者1,000以上。1,000和1,000以上的值只能赋给“实时”进程,从0到999的值只能赋给“普通”进程。实际上普通进程的goodness值只使用了这个范围底部的一部分,从0到41(或者对于SMP来说是0到56,因为SMP模式会优先照顾等待同一个处理器的进程)。无论是在SMP还是在UP上,实时进程的goodness值的范围都是从1,001到1,099。
有关这两类goodness结果的重要的一点是该值在实时系统中的范围肯定会比非实时系统的范围要高(因此偏移量(offset)是100而不是1000)。POSIX..1b规定内核要确保在实时进程和非实时进程同时竞争CPU时,实时进程要优先于非实时进程。由于调度程序总是选择具有最大goodness值的进程,又由于任何尚未释放CPU的实时进程的goodness值总是比非实时进程的goodness大,Linux对这一点的遵守是很容易得到证明的。
尽管在goodness上面的标题注释中有所说明,该函数还是从不会返回-1,000的,也不会返回其它的负值。由于idle进程的counter值为负,所以如果使用idle进程作为参数调用goodness,就会返回负值,但这是不会发生的。
goodness只是一个简单的函数,但是它是Linux调度程序必不可少的部分。运行对立中的每个进程每次执行schedule时都可能调用它,因此其执行速度必须很快。但是如果一旦它调度失误,那么整个系统都要遭殃了。考虑到这些冲突压力,我想改进现有的系统是相当困难的。
Theheart of the scheduling algorithm includes identifying the best
candidateamong all processes in the runqueue list. This is what the goodness(
)
function does. It receives as input parameters prev
(the descriptor
pointer of the previously running process) and p
(the descriptor
pointer of the process to evaluate). The integervalue c
returned by goodness(
)
measures the "goodness" of p
and hasthe following
meanings:
p
must never be selected; this value is returned when
the runqueuelist contains only init_task
.p
has exhausted its quantum. Unless p
is the first process in the runqueue list and all runnable processes havealso exhausted
their quantum, it will not be selected for execution.p
is a conventional process that has not exhausted its
quantum; ahigher value of c
denotes a higher level ofgoodness.p
is a real-time process; a higher value of c
denotes a higher level of goodness.The goodness( )
function is equivalent to:
if (p->policy != SCHED_OTHER)
return 1000 + p->rt_priority;
if (p->counter == 0)
return 0;
if (p->mm == prev->mm)
return p->counter + p->priority + 1;
return p->counter + p->priority;
If the process is real-time, its goodness is set to at least 1000. If itis
a conventional process that has exhausted its quantum, its goodness isset to 0; otherwise,
it is set to p->counter + p->priority
.
A small bonus is given to p
if it shares theaddress space
with prev
(i.e., if their processdescriptors' mm
fields point to
the same memorydescriptor). The rationale for this bonus is that if p
runs
right after prev
, it will usethe same page tables, hence the same memory;
some of the valuable data maystill be in the hardware cache.