日期:2014-05-16  浏览次数:20662 次

Professional Linux Kernel Architecture 2.6.3 section 疑问

在Professional Linux Kernel Architecture 2.6.3 section 中作者对CFS 的place_entity 中sched_entity 的vruntime 更新提到:

However, if the sleeper has accumulated a large unfairness as indicated by a large se_vruntime value, the kernel must honor this. If se->vruntime is larger than the previously computed difference, it is kept as the vruntime of the process, which leads to a leftward placement on the red-black tree — recall that large vruntime values are good to schedule early!

这个就奇怪了,明明vruntime 越大,对应在rb tree 中的位置就越右,怎么还越左,然后越早scheduler.

再反过来看place_entity 这段代码: version linux kernel 3.4.10 in kernel/sched/fair.c

static void
place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
{
	u64 vruntime = cfs_rq->min_vruntime;

	/*
	 * The 'current' period is already promised to the current tasks,
	 * however the extra weight of the new task will slow them down a
	 * little, place the new task so that it fits in the slot that
	 * stays open at the end.
	 */
	if (initial && sched_feat(START_DEBIT))
		vruntime += sched_vslice(cfs_rq, se);

	/* sleeps up to a single latency don't count. */
	if (!initial) {
		unsigned long thresh = sysctl_sched_latency;

		/*
		 * Halve their sleep time's effect, to allow
		 * for a gentler effect of sleepers:
		 */
		if (sched_feat(GENTLE_FAIR_SLEEPERS))
			thresh >>= 1;

		vruntime -= thresh;
	}

	/* ensure we never gain time by being placed backwards. */
	vruntime = max_vruntime(se->vruntime, vruntime);

	se->vruntime = vruntime;
}
?


--------------------------------------------------
/* ensure we never gain time by being placed backwards. */
vruntime = max_vruntime(se->vruntime, vruntime);

作者当时的用意应当是防止vruntime 出现倒流的情况; 反过来,我们考察两种case.
1. process 短暂sleep ,这时se->vruntime 一般比min_vruntime 要大,选择se->vruntime 无可厚非。
2. process 长时间sleep, 这时se->vruntime 已经要比min_vruntime 要小很多,通过在min_vruntime 削掉sysctl_sched_latency 可保证插入rb tree 时,位置越往左,从而这个进程即越早被sched 到。 这可让长时间sleep 的process 尽早执行,也算一种奖励把。

至于书上写到的,真的怪怪的。

?