你好,游客 登录 注册 搜索
背景:
阅读新闻

返璞归真的Linux BFS调度器

[日期:2012-04-14] 来源:Linux社区  作者:dog250 [字体:大 中 小]

c.进程调度的pick-next

在BFS中,首先要调度的是抢占的进程,如果有进程抢占其它正在运行的进程,那么将它投入运行:
  1. static inline struct  
  2. task_struct *earliest_deadline_task(struct rq *rq, struct task_struct *idle)  
  3. {  
  4.     unsigned long long_deadline, shortest_deadline;  
  5.     struct task_struct *edt, *p;  
  6.     unsigned int cpu = rq->cpu;  
  7.     struct list_head *queue;  
  8.     int idx = 0;  
  9.   
  10.     if (rq->preempt_next) {  
  11.         //rq->preempt_next在try_preempt中设置  
  12.         if (likely(task_queued(rq->preempt_next) &&  
  13.             cpu_isset(cpu, rq->preempt_next->cpus_allowed))) {  
  14.                 edt = rq->preempt_next;  
  15.                 goto out_take;  
  16.         }  
  17.     }  
  18. retry:  
  19.     idx = find_next_bit(grq.prio_bitmap, PRIO_LIMIT, idx);  
  20.     queue = &grq.queue[idx];  
  21.     if (idx < MAX_RT_PRIO) {  
  22.         /* We found rt tasks */  
  23.         list_for_each_entry(p, queue, run_list) {  
  24.             if (cpu_isset(cpu, p->cpus_allowed)) {  
  25.                 edt = p;  
  26.                 goto out_take;  
  27.             }  
  28.         }  
  29.         /* More rt tasks, we couldn't take the lower prio ones */  
  30.         ++idx;  
  31.         goto retry;  
  32.     }  
  33.   
  34.     /* No rt tasks, find earliest deadline task */  
  35.     edt = idle;  
  36.     if (unlikely(idx >= PRIO_LIMIT)) {  
  37.         /* All rt tasks but none suitable for this cpu */  
  38.         goto out;  
  39.     }  
  40.   
  41.     long_deadline = shortest_deadline = longest_deadline() * 2 + 1;  
  42.     list_for_each_entry(p, queue, run_list) {  
  43.         unsigned long deadline_diff;  
  44.         /* Make sure cpu affinity is ok */  
  45.         if (!cpu_isset(cpu, p->cpus_allowed))  
  46.             continue;  
  47.   
  48.         deadline_diff = p->deadline - jiffies;  
  49.   
  50.         /* Normalise all old deadlines and cope with jiffy wrap. */  
  51.         if (deadline_diff > long_deadline)  
  52.             deadline_diff = 0;  
  53.   
  54.         /* Select the earliest deadline task now */  
  55.         //简单的冒泡比较  
  56.         if (edt == idle || deadline_diff < shortest_deadline) {  
  57.             shortest_deadline = deadline_diff;  
  58.             edt = p;  
  59.         }  
  60.     }  
  61.     if (edt == idle) {  
  62.         if (idx < IDLE_PRIO) {  
  63.             /* Haven't checked for SCHED_IDLEPRIO tasks yet */  
  64.             idx++;  
  65.             goto retry;  
  66.         }  
  67.         goto out;  
  68.     }  
  69. out_take:  
  70.     take_task(rq, edt);  
  71. out:  
  72.     return edt;  
  73. }  

d.时间片用尽

略......

自己的体验

费了九牛二虎之力,编译好了带有BFS的内核(实际上只是在2.6.32内核上打上了一个patch,然后重新编译内核而已,然而这个工作量不小,你可以试试...),基于Debian的桌面版本,硬件是在垃圾场淘的巨老无比的P4,效果十分可观,起码和Mac OS一样了,由此我再也不把RedHat的桌面看作可有可无的部分了。以前我总是编辑/etc/inittab文件将其启动到level 3,然后用ssh登录操作之,现在我也可以直接进level 5了,这就是BFS的效果。顺带说一句,如果你使用很高档或者比较高档的硬件,那么这个效果就不明显了,学子们注意了,别较真儿
        听说Android使用了该调度器,我还没有尝试,因为我没有预算买设备。

3.更多的寓意

写这篇文章的目的在于引出一些寓意,而不是介绍BFS调度器本身。该寓意在于,历史得看问题或许会更好。埋没在历史深处的事务并不一定是过时的,它可能是最能反映问题本质的,不要将其置之不顾,时不时地回头看一眼,有的时候,它会给你更多的思路。
        所谓的操作系统进程调度就是为了让所有的CPU都动起来,这是一门艺术而不仅仅是一门技术。你既不能让CPU空闲,又不能让某些进程排队等待时间过长,这是一个CPU利用率和对待进程公平性的游戏。这才是问题的本质,而这个问题的本质在不同的硬件配置以及使用场景下又有不同的表现,因此调度器为了迎合这种差异巨大的不同的表现不可能做成一个统一的,只有具体问题具体分析才能得到良好的表现linux
相关资讯       Linux资料 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款