Chinaunix首页 | 论坛 | 博客
  • 博客访问: 291188
  • 博文数量: 49
  • 博客积分: 3083
  • 博客等级: 中校
  • 技术积分: 710
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-27 08:22
文章分类

全部博文(49)

文章存档

2009年(8)

2008年(41)

分类: LINUX

2008-06-19 13:35:36

     今天突然发现2.6.25.4的kernel代码或许还可以从代码的层次上进行优化处理,样例如下:

/*

*Version:2.6.25.4

*File:kernel/sched.c

*location:2966

*/

        pwr_move = pwr_now = 0;
        imbn = 2;
        if (this_nr_running) {
            this_load_per_task /= this_nr_running;
            if (busiest_load_per_task > this_load_per_task)
                imbn = 1;
        } else
            this_load_per_task = SCHED_LOAD_SCALE;

        if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
                    busiest_load_per_task * imbn) {
            *imbalance = busiest_load_per_task;
            return busiest;
        }

        /*
         * OK, we don't have enough imbalance to justify moving tasks,
         * however we may be able to increase total CPU power used by
         * moving them.
         */

//这里或许可以抢救一下,mark

        pwr_now += busiest->__cpu_power *
                min(busiest_load_per_task, max_load);
        pwr_now += this->__cpu_power *
                min(this_load_per_task, this_load);
        pwr_now /= SCHED_LOAD_SCALE;



   今天看代码看到这里突然发现了两个或许可以简化的操作,以我个人的观点pwr_move = pwr_now = 0;这句在整个代码的过程中起了个初始化的作用.pwr_move以后的if条件语句中要参与运算,所以首先声明下是可以的,但是pwr_now从初始化到第一次运算之间没有进行任何的运算过程,在这里的初始化我认为应该舍去,在第一次参与运算的时候用=替换+=,减少运算的次数,将add和mov操作替换成mov,减少CPU运算次数。在整个运算的过程中,使用了min函数来取得最小值,代码变的清晰了,但是却付出了调用子程序转跳和返回的代价,可以使用一个三元运算符来优化代码。修改后的代码如下




/*

*Version:2.6.25.4(Changed)

*File:kernel/sched.c

*location:2966

*/


     pwr_move = 0;        //删除了 pwr_now = 0;

        imbn = 2;
        if (this_nr_running) {
            this_load_per_task /= this_nr_running;
            if (busiest_load_per_task > this_load_per_task)
                imbn = 1;
        } else
            this_load_per_task = SCHED_LOAD_SCALE;

        if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
                    busiest_load_per_task * imbn) {
            *imbalance = busiest_load_per_task;
            return busiest;
        }

        /*
         * OK, we don't have enough imbalance to justify moving tasks,
         * however we may be able to increase total CPU power used by
         * moving them.
         */

//将函数调用换成了三元运算符

        pwr_now = busiest->__cpu_power *
busiest_load_per_task                 (busiest_load_per_task > max_load ? max_load :);
        pwr_now += this->__cpu_power *
this_load_per_task                 (this_load_per_task > this_load ? this_load :);
        pwr_now /= SCHED_LOAD_SCALE;

阅读(1650) | 评论(0) | 转发(0) |
0

上一篇:SMP感受

下一篇:for_each_cpu_mask定义文件

给主人留下些什么吧!~~