Chinaunix首页 | 论坛 | 博客
  • 博客访问: 256960
  • 博文数量: 35
  • 博客积分: 883
  • 博客等级: 准尉
  • 技术积分: 656
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-17 09:38
文章分类

全部博文(35)

文章存档

2013年(1)

2012年(34)

分类: LINUX

2012-11-03 11:04:09

linux2.6.34版本的调度器主要是提供了一个调度框架,类似于主板的插槽,内核开发者可以开发自己的调度器适配到这个调度框架中,类似于插入插槽的插卡。现在内核中提供了三种调度算法:实时调度类、公平调度类及idle调度类。内核这种调度器的框架是非常灵活的,给开发自己的调度器降低了难度。今天我们主要讲的是CFS调度算法。
首先,内核有一个基本的调度类,定义如下:

点击(此处)折叠或打开

  1. struct sched_class {
  2.     const struct sched_class *next;

  3.     void (*enqueue_task) (struct rq *rq, struct task_struct *p, int wakeup,
  4.              bool head);
  5.     void (*dequeue_task) (struct rq *rq, struct task_struct *p, int sleep);
  6.     void (*yield_task) (struct rq *rq);

  7.     void (*check_preempt_curr) (struct rq *rq, struct task_struct *p, int flags);

  8.     struct task_struct * (*pick_next_task) (struct rq *rq);
  9.     void (*put_prev_task) (struct rq *rq, struct task_struct *p);

  10. #ifdef CONFIG_SMP
  11.     int (*select_task_rq)(struct task_struct *p, int sd_flag, int flags);

  12.     void (*pre_schedule) (struct rq *this_rq, struct task_struct *task);
  13.     void (*post_schedule) (struct rq *this_rq);
  14.     void (*task_waking) (struct rq *this_rq, struct task_struct *task);
  15.     void (*task_woken) (struct rq *this_rq, struct task_struct *task);

  16.     void (*set_cpus_allowed)(struct task_struct *p,
  17.                  const struct cpumask *newmask);

  18.     void (*rq_online)(struct rq *rq);
  19.     void (*rq_offline)(struct rq *rq);
  20. #endif

  21.     void (*set_curr_task) (struct rq *rq);
  22.     void (*task_tick) (struct rq *rq, struct task_struct *p, int queued);
  23.     void (*task_fork) (struct task_struct *p);

  24.     void (*switched_from) (struct rq *this_rq, struct task_struct *task,
  25.              int running);
  26.     void (*switched_to) (struct rq *this_rq, struct task_struct *task,
  27.              int running);
  28.     void (*prio_changed) (struct rq *this_rq, struct task_struct *task,
  29.              int oldprio, int running);

  30.     unsigned int (*get_rr_interval) (struct rq *rq,
  31.                      struct task_struct *task);

  32. #ifdef CONFIG_FAIR_GROUP_SCHED
  33.     void (*moved_group) (struct task_struct *p, int on_rq);
  34. #endif
  35. }
与之对应的CFS调度器实现如下:

  1. /*
  2.  * All the scheduling class methods:
  3.  */
  4. static const struct sched_class fair_sched_class = {
  5.     .next            = &idle_sched_class,
  6.     .enqueue_task        = enqueue_task_fair,
  7.     .dequeue_task        = dequeue_task_fair,
  8.     .yield_task        = yield_task_fair,

  9.     .check_preempt_curr    = check_preempt_wakeup,

  10.     .pick_next_task        = pick_next_task_fair,
  11.     .put_prev_task        = put_prev_task_fair,

  12. #ifdef CONFIG_SMP
  13.     .select_task_rq        = select_task_rq_fair,

  14.     .rq_online        = rq_online_fair,
  15.     .rq_offline        = rq_offline_fair,

  16.     .task_waking        = task_waking_fair,
  17. #endif

  18.     .set_curr_task = set_curr_task_fair,
  19.     .task_tick        = task_tick_fair,
  20.     .task_fork        = task_fork_fair,

  21.     .prio_changed        = prio_changed_fair,
  22.     .switched_to        = switched_to_fair,

  23.     .get_rr_interval    = get_rr_interval_fair,

  24. #ifdef CONFIG_FAIR_GROUP_SCHED
  25.     .moved_group        = moved_group_fair,
  26. #endif
  27. }
可以看到就是要把标准调度类的函数都有一个对应的实现。
其次,我们介绍一下CFS调度算法的基本原理:CFS调度器与传统的调度器最大的区别,就是调度的依据不再是时间片轮转,而是依据进程就绪队列中等待的时间长短来进行调度选择,即在就绪队列中等待时间越长的进程得到调度的机会就越大,否则,机会就越小。这一段原理实际上涉及到几个关键的内容需要解释,否则,就有点空中楼阁的意味。
第一个关键内容就是就绪队列的描述和组织形式是什么样的?如何在就绪队列中排序,就绪队列又是如何不断的变动才能保障进程的公平调度的?
1)就绪队列的数据结构cfs_rq定义如下:

点击(此处)折叠或打开

  1. /* CFS-related fields in a runqueue */
  2. struct cfs_rq {
  3.     struct load_weight load;
  4.     unsigned long nr_running;

  5.     u64 exec_clock;
  6.     u64 min_vruntime;

  7.     struct rb_root tasks_timeline;
  8.     struct rb_node *rb_leftmost;

  9.     struct list_head tasks;
  10.     struct list_head *balance_iterator;

  11.     /*
  12.      * 'curr' points to currently running entity on this cfs_rq.
  13.      * It is set to NULL otherwise (i.e when none are currently running).
  14.      */
  15.     struct sched_entity *curr, *next, *last;

  16.     unsigned int nr_spread_over;

  17. #ifdef CONFIG_FAIR_GROUP_SCHED
  18.     struct rq *rq;    /* cpu runqueue to which this cfs_rq is attached */

  19.     /*
  20.      * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
  21.      * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
  22.      * (like users, containers etc.)
  23.      *
  24.      * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
  25.      * list is used during load balance.
  26.      */
  27.     struct list_head leaf_cfs_rq_list;
  28.     struct task_group *tg;    /* group that "owns" this runqueue */

  29. #ifdef CONFIG_SMP
  30.     /*
  31.      * the part of load.weight contributed by tasks
  32.      */
  33.     unsigned long task_weight;

  34.     /*
  35.      * h_load = weight * f(tg)
  36.      *
  37.      * Where f(tg) is the recursive weight fraction assigned to
  38.      * this group.
  39.      */
  40.     unsigned long h_load;

  41.     /*
  42.      * this cpu's part of tg->shares
  43.      */
  44.     unsigned long shares;

  45.     /*
  46.      * load.weight at the time we set shares
  47.      */
  48.     unsigned long rq_weight;
  49. #endif
  50. #endif
  51. }
2)其组织形式采用红黑树进行组织,cfs_rq中下面两个域就体现了红黑的组织:

  1. struct rb_root tasks_timeline;
  2. struct rb_node *rb_leftmost;
其中,tasks_timeline表示红黑树的根,rb_leftmost表示红黑树的最左子节点,即指向在就绪队列汇中最优先被调度的进程。搜索一下代码中tasks_timeline要被使用的地方,发现如下地方被使用:
初始化cfs就绪队列:
  1. static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
  2. {
  3.     cfs_rq->tasks_timeline = RB_ROOT;
  4. 。。。。。。
  5. }
进程入队就绪队列:

点击(此处)折叠或打开

  1. static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  2. {
  3.     struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  4.     struct rb_node *parent = NULL;
  5.     struct sched_entity *entry;
  6.     s64 key = entity_key(cfs_rq, se);
  7.     int leftmost = 1;

  8.     /*
  9.      * Find the right place in the rbtree:
  10.      */
  11.     while (*link) {
  12.         parent = *link;
  13.         entry = rb_entry(parent, struct sched_entity, run_node);
  14.         /*
  15.          * We dont care about collisions. Nodes with
  16.          * the same key stay together.
  17.          */
  18.         if (key < entity_key(cfs_rq, entry)) {
  19.             link = &parent->rb_left;
  20.         } else {
  21.             link = &parent->rb_right;
  22.             leftmost = 0;
  23.         }
  24.     }

  25.     /*
  26.      * Maintain a cache of leftmost tree entries (it is frequently
  27.      * used):
  28.      */
  29.     if (leftmost)
  30.         cfs_rq->rb_leftmost = &se->run_node;

  31.     rb_link_node(&se->run_node, parent, link);
  32.     rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  33. }
进程出队就绪队列:

点击(此处)折叠或打开

  1. static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  2. {
  3.     if (cfs_rq->rb_leftmost == &se->run_node) {
  4.         struct rb_node *next_node;

  5.         next_node = rb_next(&se->run_node);
  6.         cfs_rq->rb_leftmost = next_node;
  7.     }

  8.     rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  9. }
挑选最后一个进程:

点击(此处)折叠或打开

  1. static struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
  2. {
  3.     struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);

  4.     if (!last)
  5.         return NULL;

  6.     return rb_entry(last, struct sched_entity, run_node);
  7. }
从上面的代码片段,我们还会发现令整个红黑树发生变动的动作主要就是进程入队和进程出队。
3)在整个linux内核中,都有哪些时机可能会导致进程的入队和出队,从而导致红黑树发生变化呢?

  1. activate_task、try_to_wake_up、wake_up_new_task、__sched_setscheduler、__migrate_task pull_task schedule、rt_mutex_setprio、set_user_nice、sched_move_task
上面是主要调用入队和出队的地方,总结起来应当是几个时机点:唤醒、调度、迁移(负载均衡)、睡眠及其他的一些静态设置优先级和调度的地方。
第二个关键内容就是时间更新的相关内容,详细的细节已经在前一篇时间相关内容中阐述了。主要就是在时钟中断中计算每个进程理想运行时间和总的运行时间,而在update_curr中增加虚拟时间。更新虚拟时间的主要地点如下:

点击(此处)折叠或打开

  1. |->enqueue_entity
  2. |
  3. |->dequeue_entity
  4. |
  5. |->put_prev_entity
  6. |
  7. |->entity_tick
  8. |
  9. |->yield_task_fair
  10. |
  11. |->check_preempt_wakeup
  12. |
  13. |->task_fork_fair
  14. |
  15. |->moved_group_fair
第三个关键内容是进程的优先级和权重。
进程的优先级:在内核中是从0~139,其中0~99为实时优先级,剩下的为普通优先级。用户可以使用nice改变进程的静态优先级(范围从-20~19)。而进程的优先级又分为三种:静态优先级、普通优先级和动态优先级。三者关系为:1、对于非实时进程来说,三者的值相同,均为静态优先级。2、对于优先级提高的非实时进程来说,静态优先级和普通优先级相同,而动态优先级不同,为被临时提高的优先级。3、对于实时进程来说,静态优先级不变,普通优先级为MAX_RT_PRIO-1-rt_priority,动态优先级不变。
进程的权重:权重和优先级及时间有着密切关系。
权重与优先级的关系为:

点击(此处)折叠或打开

  1. static const int prio_to_weight[40] = {
  2.  /* -20 */ 88761, 71755, 56483, 46273, 36291,
  3.  /* -15 */ 29154, 23254, 18705, 14949, 11916,
  4.  /* -10 */ 9548, 7620, 6100, 4904, 3906,
  5.  /* -5 */ 3121, 2501, 1991, 1586, 1277,
  6.  /* 0 */ 1024, 820, 655, 526, 423,
  7.  /* 5 */ 335, 272, 215, 172, 137,
  8.  /* 10 */ 110, 87, 70, 56, 45,
  9.  /* 15 */ 36, 29, 23, 18, 15,
  10. };
可以看到,优先级越高,权重越大。
权重和时间的关系:

  1. ideal_time = sum_runtime *se.weight/cfs_rq.weight
  2. 1) vruntime = delta* NICE_0_LOAD/se.weight;(if curr.nice!=NICE_0_LOAD)
  3. 2)vruntime = delta; (ifcurr.nice=NICE_0_LOAD)
所以,从上面来看,优先级越高,权重越大,所得到的虚拟时间越少,最后越靠近就绪队列的红黑中的左边,否则,就越靠右边。而就绪队列红黑树的排序时靠如下key来决定的:

点击(此处)折叠或打开

  1. static inline s64 entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se)
  2. {
  3.     return se->vruntime - cfs_rq->min_vruntime;
  4. }
之所以用个减法来实现,主要考虑到vruntime是进程获得执行的虚拟时间的长度,也就是说在进程睡眠的时候,这个虚拟时间的长度是不会增长的,而min_vruntime是整个就绪队列的最小虚拟时间基准,是一直会增长的。当一个进程睡眠一段时间后,通过上面的算法计算出来的key就是变小,从而更靠近红黑树的左边,更容易得到调度。因为这个进程受到了不公平的待遇,需要平衡一下。而利用键值排序红黑树的地方在进程入队的时候,具体如下:

点击(此处)折叠或打开

  1. static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  2. {
  3.     struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  4.     struct rb_node *parent = NULL;
  5.     struct sched_entity *entry;
  6.     s64 key = entity_key(cfs_rq, se);
  7.     int leftmost = 1;

  8.     /*
  9.      * Find the right place in the rbtree:
  10.      */
  11.     while (*link) {
  12.         parent = *link;
  13.         entry = rb_entry(parent, struct sched_entity, run_node);
  14.         /*
  15.          * We dont care about collisions. Nodes with
  16.          * the same key stay together.
  17.          */
  18.         if (key < entity_key(cfs_rq, entry)) {
  19.             link = &parent->rb_left;
  20.         } else {
  21.             link = &parent->rb_right;
  22.             leftmost = 0;
  23.         }
  24.     }

  25.     /*
  26.      * Maintain a cache of leftmost tree entries (it is frequently
  27.      * used):
  28.      */
  29.     if (leftmost)
  30.         cfs_rq->rb_leftmost = &se->run_node;

  31.     rb_link_node(&se->run_node, parent, link);
  32.     rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  33. }
我们再简单解析一下CFS调度器的几个关键的函数:
进程入队操作:

点击(此处)折叠或打开

  1. static void
  2. enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup, bool head)
  3. {
  4.     struct cfs_rq *cfs_rq;
  5.     struct sched_entity *se = &p->se;
  6.     int flags = 0;
  7. //若进程此前已经是可运行状态,则wakeup为0,否则,想入队的进程最近被唤醒,并转为运行状态则为1
  8.     if (wakeup)
  9.         flags |= ENQUEUE_WAKEUP;
  10.     if (p->state == TASK_WAKING)
  11.         flags |= ENQUEUE_MIGRATE;
  12. //遍历组内的各个实体
  13.     for_each_sched_entity(se) {
  14.         if (se->on_rq)
  15.             break;
  16.         cfs_rq = cfs_rq_of(se);
  17. //入队操作
  18.         enqueue_entity(cfs_rq, se, flags);
  19.         flags = ENQUEUE_WAKEUP;
  20.     }

  21.     hrtick_update(rq);
  22. }

点击(此处)折叠或打开

  1. static void
  2. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
  3. {
  4.     /*
  5.      * Update the normalized vruntime before updating min_vruntime
  6.      * through callig update_curr().
  7.      */
  8.     if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_MIGRATE))
  9.         se->vruntime += cfs_rq->min_vruntime;

  10.     /*
  11.      * Update run-time statistics of the 'current'.
  12.      */
  13.     update_curr(cfs_rq);
  14.     account_entity_enqueue(cfs_rq, se);
  15. //更新进程的虚拟时间
  16.     if (flags & ENQUEUE_WAKEUP) {
  17.         place_entity(cfs_rq, se, 0);
  18.         enqueue_sleeper(cfs_rq, se);
  19.     }

  20.     update_stats_enqueue(cfs_rq, se);
  21.     check_spread(cfs_rq, se);
  22.     if (se != cfs_rq->curr)
  23. //进程入队,重排红黑树
  24.         __enqueue_entity(cfs_rq, se);
  25. }
进程出队操作:

点击(此处)折叠或打开

  1. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
  2. {
  3.     struct cfs_rq *cfs_rq;
  4.     struct sched_entity *se = &p->se;
  5. //遍历调度组
  6.     for_each_sched_entity(se) {
  7.         cfs_rq = cfs_rq_of(se);
  8. //执行具体的出队操作
  9.         dequeue_entity(cfs_rq, se, sleep);
  10.         /* Don't dequeue parent if it has other entities besides us */
  11.         if (cfs_rq->load.weight)
  12.             break;
  13.         sleep = 1;
  14.     }

  15.     hrtick_update(rq);
  16. }

点击(此处)折叠或打开

  1. static void
  2. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
  3. {
  4.     /*
  5.      * Update run-time statistics of the 'current'.
  6.      */
  7.     update_curr(cfs_rq);

  8.     update_stats_dequeue(cfs_rq, se);
  9.     if (sleep) {
  10. #ifdef CONFIG_SCHEDSTATS
  11.         if (entity_is_task(se)) {
  12.             struct task_struct *tsk = task_of(se);

  13.             if (tsk->state & TASK_INTERRUPTIBLE)
  14.                 se->sleep_start = rq_of(cfs_rq)->clock;
  15.             if (tsk->state & TASK_UNINTERRUPTIBLE)
  16.                 se->block_start = rq_of(cfs_rq)->clock;
  17.         }
  18. #endif
  19.     }

  20.     clear_buddies(cfs_rq, se);

  21.     if (se != cfs_rq->curr)
  22. //进程出队,重排红黑树。
  23.         __dequeue_entity(cfs_rq, se);
  24.     account_entity_dequeue(cfs_rq, se);
  25.     update_min_vruntime(cfs_rq);

  26.     /*
  27.      * Normalize the entity after updating the min_vruntime because the
  28.      * update can refer to the ->curr item and we need to reflect this
  29.      * movement in our normalized position.
  30.      */
  31.     if (!sleep)
  32.         se->vruntime -= cfs_rq->min_vruntime;
  33. }
最后,我们说一下,每个进程的时间控制是在什么时机做的?实际上是在周期调度entity_tick中实现的,具体如下:

点击(此处)折叠或打开

  1. static void
  2. entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
  3. {
  4.     /*
  5.      * Update run-time statistics of the 'current'.
  6.      */
  7.     update_curr(cfs_rq);

  8. #ifdef CONFIG_SCHED_HRTICK
  9.     /*
  10.      * queued ticks are scheduled to match the slice, so don't bother
  11.      * validating it and just reschedule.
  12.      */
  13.     if (queued) {
  14.         resched_task(rq_of(cfs_rq)->curr);
  15.         return;
  16.     }
  17.     /*
  18.      * don't let the period tick interfere with the hrtick preemption
  19.      */
  20.     if (!sched_feat(DOUBLE_TICK) &&
  21.             hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
  22.         return;
  23. #endif

  24.     if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT))
  25. //在此处检测进程的运行时间是否超期
  26.         check_preempt_tick(cfs_rq, curr);
  27. }

点击(此处)折叠或打开

  1. static void
  2. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  3. {
  4.     unsigned long ideal_runtime, delta_exec;
  5. //进程应当运行的理想时间
  6.     ideal_runtime = sched_slice(cfs_rq, curr);
  7. //进程时机运行的时间
  8.     delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  9. //检查进程运行的时间是否超出了理想运行的时间,若是,则设置调度标志,调度进程
  10.     if (delta_exec > ideal_runtime) {
  11.         resched_task(rq_of(cfs_rq)->curr);
  12.         /*
  13.          * The current task ran long enough, ensure it doesn't get
  14.          * re-elected due to buddy favours.
  15.          */
  16.         clear_buddies(cfs_rq, curr);
  17.         return;
  18.     }

  19.     /*
  20.      * Ensure that a task that missed wakeup preemption by a
  21.      * narrow margin doesn't have to wait for a full slice.
  22.      * This also mitigates buddy induced latencies under load.
  23.      */
  24.     if (!sched_feat(WAKEUP_PREEMPT))
  25.         return;
  26. //实际运行的时间小于最小的可以保证的时间
  27.     if (delta_exec < sysctl_sched_min_granularity)
  28.         return;

  29.     if (cfs_rq->nr_running > 1) {
  30.         struct sched_entity *se = __pick_next_entity(cfs_rq);
  31.         s64 delta = curr->vruntime - se->vruntime;
  32. //当前进程运行的虚拟时间大于(红黑树上最左子节点的虚拟时间+进程应当运行的时间)时,当前进程也要被调度。
  33.         if (delta > ideal_runtime)
  34.             resched_task(rq_of(cfs_rq)->curr);
  35.     }
  36. }









 
 




 
阅读(7711) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

Bean_lee2012-11-07 23:34:26

本来应该写篇文章总结CFS的,一直也提不起精神来。