免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1159 | 回复: 0
打印 上一主题 下一主题

Anatomizing Linux scheduler: CFS 1 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-24 15:12 |只看该作者 |倒序浏览
The CFS algorithm uses a fair clock to represent the clock of CFS. All tasks in CFS runqueue  use this clock to achieve fairness. The fairness is calculated as follows:
     cfs_rq.fair_clock - se.wait_runtime
The value is called fair_key and stored in se.fair_key. The RB tree uses this value to sort all sched_entity. The least value is stored in the leftmost node of RB tree;and largest value in the rightmost node of RB tree. When any scheduling occurs, the scheduler selects the leftmost node of RB tree. To fastly obtain the sechdule entity, Linux uses a pointer called "rb_leftmost" to point the leftmost node. The principle of CFS is to achieve, as its name indicated, complete fairness. This means that in a time interval all tasks should runs according to their priorities. In time-sharing system the priorities of all tasks should be the same, but it can use system call nice() to change the task's priorities. So when a task calls nice(), it should boost or deduct the execution time of CPU.
For example, let us assume two tasks running in the system, Task A and Task B. Task A and Task B should occupy 50% cpu capabilities in any time interval when using CFS. In ideal condition, if the time interval is 1 minutes, Task A runs 30 seconds and  Task B 30 seconds. If the time interval is 1 seconds, Task A runs 30 seconds and task B 30 seconds. if the time interval is 1 miliiseconds, Task A runs 500 microseconds and task B 500 microseconds etc. When using nice(), Linux defines load weight to calculate the executing time. Now when a task is boosted one, it obtains 10% cpu more.
   const int prio_to_weight[40] = {
      /* -20 */88818, ...
      /* -10 */9537, ..., 1280,
      /*  0  */NICE_0_LOAD, /* 1024 */
      /*  1  */819, ...
      /*  10 */110,...
   };
When a task is forked, its priority is assigned to 120, i.e. nice(0), which load_weight is 1024(NICE_0_LOAD). When it boosts to 119, i.e. nice(119 - 120) = nice(-1), its load_weight is 1280. Assume that there are two tasks A and B. Initially Prio(A) = 1024 and Prio(B) = 1024. The two tasks occupy 50% CPU. Now task A is boosted to 119, i.e. Prio(A) = 1280. Then its cpu occupiance is :
     prio(A) / (prio(A) + prio(B)) = 1280 / (1280 + 1024) = 55%(almost)
B's cpu occupiance is:
     prio(B) / (prio(A) + prio(B))  = 1024 / (1280 + 1024) = 45%(almost)   
In reality, this results can hardly achieved. The reasons are:
  1. the time granularity of tick
     When does the scheduling occurs? There are the following possible conditions:(not consider real-time condition)
     a. When a new task is forked
     b. when a sleeping task is waked-up
     c. when a tick occurs
     d. when a task yields cpu
     The precise granularity is limited by granularity of tick. This means that when system tick is 5ms, the time interval should be more than 5 milliseconds
  2. the bias of scheduling
     The other part other than scheduler will run some cpu's time.
(to be continued...)
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/103132/showart_2035623.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP