优化linux内核pick_next_task函数

linux3.3.7的pick_next_task函数的源码如下:

static inline struct task_struct *
pick_next_task(struct rq *rq)
{
	const struct sched_class *class;
	struct task_struct *p;

	/*
	 * Optimization: we know that if all tasks are in
	 * the fair class we can call that function directly:
	 */
	if (likely(rq->nr_running == rq->cfs.h_nr_running)) {
		p = fair_sched_class.pick_next_task(rq);
		if (likely(p))
			return p;
	}

	for_each_class(class) {
		p = class->pick_next_task(rq);
		if (p)
			return p;
	}
	
	
	BUG(); /* the idle class will always have a runnable task */
	
}
仔细分析该代码,发现在rq->nr_running==0的时候,并不是最优的代码,fair_sched_class.pick_next_task会被执行两次,stop_sched_class.pick_next_task以及rt_sched_class.pick_next_task会各被执行一次,最后才能执行到idle_sched_class.pick_next_task。那么如何进行优化呢?

static inline struct task_struct *
pick_next_task(struct rq *rq)
{
        struct task_struct *p;

        if(rq->nr_running!=0){
                p = stop_sched_class.pick_next_task(rq);
                if(p)
                        return p;

                if(rq->rt.rt_nr_running){
                        p = rt_sched_class.pick_next_task(rq);
                        if(p)
                                return p;
                }

                if(rq->cfs.nr_running){
                        p = fair_sched_class.pick_next_task(rq);
                        if(p)
                                return p;
                }
        }
        else{
                p = idle_sched_class.pick_next_task(rq);
                if(p)
                        return p;
        }
        BUG(); /* the idle class will always have a runnable task */
}

改为上面的代码后,当rq->nr_running==0的时候便可以直接选择idle_sched_class.pick_next_task,但是不知道有没有什么副作用?

你可能感兴趣的:(优化,struct,function,Class,optimization,linux内核)