CFS进程调度

[cpp]  view plain copy
  1. 一、概述  
  2. linux 2.6.23中采用了一个全新的调度策略CFS(Completely Fair Scheduler)来处理非实时进程。  
  3.   
  4. 二、主要数据结构  
  5. 1.为了和原先的实时策略更好的融合,linux在实现CFS之余,还将内核的调度策略模块化,添加了新的结构体sched_class用于管理不同的调度器。  
  6.   
  7. 2.CFS没有用传统的调度器中时间片的概念,而是使用了新的结构体sched_entity来跟踪一个进程的运行时间。  
  8.     se也可表示组调度,在此不做分析,所以这里se代表一个进程。  
  9. struct sched_entity {  
  10.     struct load_weight  load;       /* for load-balancing */    //se的权重  
  11.     struct rb_node      run_node;               //在红黑树上的节点  
  12.     unsigned int        on_rq;                      //该se是否在rq上  
  13.   
  14.     u64         exec_start;                 //当前cfs_rq的时间,用于计算时间差  
  15.     u64         sum_exec_runtime;           //进程总共运行的时间,real-run time  
  16.     u64         vruntime;                       //进程的virtual-run time  
  17.     u64         prev_sum_exec_runtime;      //进程在醒来的时间  
  18.   
  19.     u64         nr_migrations;  
  20. };  
  21.   
  22. 3.rq  
  23.   
  24. 三、tick中断处理  
  25. 每一次进入tick中断后,会进入scheduler_tick函数来进行scheduler相关的处理:  
  26. void scheduler_tick(void)  
  27. {  
  28.     int cpu = smp_processor_id();  
  29.     struct rq *rq = cpu_rq(cpu);  
  30.     struct task_struct *curr = rq->curr;  
  31.   
  32.     raw_spin_lock(&rq->lock);  
  33.     /* 
  34.         更新运行队列的时间,rq->clock和rq->clock_task,可以看出在真正计算 
  35.         时间的时候用的是clock_task   
  36.     */  
  37.     update_rq_clock(rq);      
  38.   
  39.     curr->sched_class->task_tick(rq, curr, 0);  
  40.     raw_spin_unlock(&rq->lock);  
  41.   
  42.     perf_event_task_tick();  
  43. }  
  44.   
  45. 由于添加了模块化的架构,如果采用的是CFS,将会跳转到函数task_tick_fair进而到函数entity_tick。  
  46. 这个函数的作用是更新进程的时间数据,在判断是否被其他进程抢占。  
  47. static void  
  48. entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)  
  49. {  
  50.     /* 
  51.      * Update run-time statistics of the 'current'. 
  52.      */  
  53.     update_curr(cfs_rq);  
  54.   
  55.     if (cfs_rq->nr_running > 1)  
  56.         check_preempt_tick(cfs_rq, curr);  
  57. }  
  58.   
  59. 正如函数注释所说,update_curr用来更新run-time的统计数据,系统会根据这些数据最终决定调度哪个进程。  
  60. static void update_curr(struct cfs_rq *cfs_rq)  
  61. {  
  62.     struct sched_entity *curr = cfs_rq->curr;  
  63.     u64 now = rq_of(cfs_rq)->clock_task;  
  64.     unsigned long delta_exec;  
  65.   
  66.     if (unlikely(!curr))  
  67.         return;  
  68.   
  69.     /* 
  70.      * Get the amount of time the current task was running 
  71.      * since the last time we changed load (this cannot 
  72.      * overflow on 32 bits): 
  73.      */  
  74.     /* 
  75.         delta_exec为自从上次变动以来的时间。 
  76.     */  
  77.     delta_exec = (unsigned long)(now - curr->exec_start);  
  78.     if (!delta_exec)  
  79.         return;  
  80.   
  81.     __update_curr(cfs_rq, curr, delta_exec);  
  82.     curr->exec_start = now;  
  83. }  
  84.   
  85. /* 
  86.  * Update the current task's runtime statistics. Skip current tasks that 
  87.  * are not in our scheduling class. 
  88.  */  
  89. static inline void  
  90. __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,  
  91.           unsigned long delta_exec)  
  92. {  
  93.     unsigned long delta_exec_weighted;  
  94.   
  95.     //更新进程的真实运行时间  
  96.     curr->sum_exec_runtime += delta_exec;  
  97.   
  98.     /*  calc_delta_fair用来将真实时间转化为虚拟时间。 
  99.         进程的优先级不同,它在系统中的地位(也就是权重)也不同 
  100.         进程的优先级越高,它的虚拟时间走的越慢。 
  101.     */  
  102.     delta_exec_weighted = calc_delta_fair(delta_exec, curr);  
  103.   
  104.     //更新进程的虚拟运行时间  
  105.     curr->vruntime += delta_exec_weighted;  
  106.     update_min_vruntime(cfs_rq);  
  107. }  
  108.   
  109. 每个进程在其产生(fork)的时候,都会根据其父亲的优先级产生它的优先级和权重(sched_fork函数)。  
  110. /* 
  111.  * delta /= w 
  112.  */  
  113. static inline unsigned long  
  114. calc_delta_fair(unsigned long delta, struct sched_entity *se)  
  115. {  
  116.     //如果该进程拥有nice为0的权重,这是他的虚拟时钟和真实时钟是一样速度的。  
  117.     if (unlikely(se->load.weight != NICE_0_LOAD))  
  118.         delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);  
  119.   
  120.     return delta;  
  121. }  
  122.   
  123. 从注释来看calc_delta_mine的计算公式为delta *= weight / lw,也就是说进程的权重越大,时钟走的越慢,而且是线性的。  
  124.   
  125. min_vruntime是cfs的rq中的一个成员,是cfs时间的基准,在cfs中起这至关重要的作用。  
  126. 自cfs产生以来,这部分的代码改动也是很频繁的。  
  127. static void update_min_vruntime(struct cfs_rq *cfs_rq)  
  128. {  
  129.     u64 vruntime = cfs_rq->min_vruntime;  
  130.   
  131.     /* 
  132.         由于当前运行的进程是不在红黑树上的,所以关于cfs_rq->min_vruntime的更新 
  133.         必须要考虑当前的进程,以免产生不公平,这是以前的调度器所疏忽的。 
  134.         如果有当前进程,就以当前进程作为基准计算 
  135.     */  
  136.     if (cfs_rq->curr)  
  137.         vruntime = cfs_rq->curr->vruntime;  
  138.   
  139.     if (cfs_rq->rb_leftmost) {  
  140.         struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,  
  141.                            struct sched_entity,  
  142.                            run_node);  
  143.   
  144.         if (!cfs_rq->curr)  
  145.             /* 
  146.                 如果没有当前进程,这个在什么时候出现? 
  147.                 其他策略的进程在运行时? 
  148.                 就不用考虑它了,就是最小的运行时间 
  149.             */  
  150.             vruntime = se->vruntime;  
  151.         else  
  152.             /* 
  153.                 如果有当前进程,还要考虑这个最小的运行时间 
  154.             */  
  155.             vruntime = min_vruntime(vruntime, se->vruntime);  
  156.     }  
  157.   
  158.     //最后,更新cfs_rq->min_vruntime,这个值是单调增加的。  
  159.     cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);  
  160. }  
  161.   
  162. 分析完update_curr函数,我们回到entity_tick函数,接着往下看。下面两行的作用是判断当前的进程是否需要被抢占。能够发生抢占首要条件是nr_running大于1。  
  163. /* 
  164.  * Preempt the current task with a newly woken task if needed: 
  165.  */  
  166. static void  
  167. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)  
  168. {  
  169.     unsigned long ideal_runtime, delta_exec;  
  170.     struct sched_entity *se;  
  171.     s64 delta;  
  172.   
  173.     //计算curr进程的理想运行时间  
  174.     ideal_runtime = sched_slice(cfs_rq, curr);  
  175.     //计算该进程实际运行时间  
  176.     delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;  
  177.     //如果实际运行的时间超出了它应该运行的时间,则set该进程的TIF_NEED_RESCHED标志位  
  178.     if (delta_exec > ideal_runtime) {  
  179.         resched_task(rq_of(cfs_rq)->curr);  
  180.         //如注释,如果当前进程运行了足够时间,就取消它在buddy中的优先权  
  181.         /* 
  182.          * The current task ran long enough, ensure it doesn't get 
  183.          * re-elected due to buddy favours. 
  184.          */  
  185.         clear_buddies(cfs_rq, curr);  
  186.         return;  
  187.     }  
  188.   
  189.     /* 
  190.         这里是第二个抢占条件:最小虚拟运行时间的进程和当前进程的虚拟运行时间进行比较, 
  191.         如果后者比前者大了 ideal_runtime,就需要进行调度。 
  192.         为什么要用虚拟时间个sched_slice产生的真实时间进行比较呢? 
  193.          大了ideal_runtime又能代表什么呢? 
  194.     */  
  195.     /* 
  196.      * Ensure that a task that missed wakeup preemption by a 
  197.      * narrow margin doesn't have to wait for a full slice. 
  198.      * This also mitigates buddy induced latencies under load. 
  199.      */  
  200.     if (delta_exec < sysctl_sched_min_granularity)  
  201.         return;  
  202.   
  203.     se = __pick_first_entity(cfs_rq);  
  204.     delta = curr->vruntime - se->vruntime;  
  205.   
  206.     if (delta < 0)  
  207.         return;  
  208.   
  209.     if (delta > ideal_runtime)  
  210.         resched_task(rq_of(cfs_rq)->curr);  
  211. }  
  212.   
  213. sched_slice函数用来计算一个进程时间基准(wall time),一个进程的理论运行时间是和整个cfs_rq中的进程数量和权重有关系的。  
  214. 不考虑调度组的话,函数等价于:  
  215. /* 
  216.  * We calculate the wall-time slice from the period by taking a part 
  217.  * proportional to the weight. 
  218.  * 
  219.  * s = p*P[w/rw] 
  220.  */  
  221. static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)  
  222. {  
  223.     /* 
  224.         __sched_period这个函数得到的是每一个进程运行一次的时间总和,公式为: 
  225.         p = (nr <= nl) ? l : mg * nr 
  226.         l   :系统常数,为调度延时,就是系统所有进程运行一次的时间总和 
  227.         nl  :系统常数,系统活动进程的上限 
  228.         nr  :当前的进程数 
  229.         mg  :系统常数,最小的调度时间间隔 
  230.     */  
  231.     u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);  
  232.     struct load_weight *load;  
  233.     struct load_weight lw;  
  234.   
  235.     load = &cfs_rq->load;  
  236.   
  237.     //这里和上面都考虑了情况,当进程刚刚创建时,当前进程不在运行队列中,为了计算合理,就临时加上去。  
  238.     if (unlikely(!se->on_rq)) {  
  239.         lw = cfs_rq->load;  
  240.   
  241.         update_load_add(&lw, se->load.weight);  
  242.         load = &lw;  
  243.     }  
  244.     //根据权重得到属于自己的那份时间  
  245.     slice = calc_delta_mine(slice, se->load.weight, load);  
  246.   
  247.     return slice;  
  248. }  
  249.   
  250. 四、新进程中的调度  
  251. 1.sched_fork出现在copy_process中。  
  252. /* 
  253.  * fork()/clone()-time setup: 
  254.  */  
  255. void sched_fork(struct task_struct *p)  
  256. {  
  257.     unsigned long flags;  
  258.     int cpu = get_cpu();  
  259.   
  260.     //初始化task_struct中调度器相关的成员。  
  261.     __sched_fork(p);  
  262.     /* 
  263.      * We mark the process as running here. This guarantees that 
  264.      * nobody will actually run it, and a signal or other external 
  265.      * event cannot wake it up and insert it on the runqueue either. 
  266.      */  
  267.     p->state = TASK_RUNNING;  
  268.   
  269.     //确保临时的优先级的提升不会继承到新的进程中  
  270.     /* 
  271.      * Make sure we do not leak PI boosting priority to the child. 
  272.      */  
  273.     p->prio = current->normal_prio;  
  274.   
  275.     //如果设置了sched_reset_on_fork,会恢复默认调度策略  
  276.     /* 
  277.      * Revert to default priority/policy on fork if requested. 
  278.      */  
  279.     if (unlikely(p->sched_reset_on_fork)) {  
  280.         if (task_has_rt_policy(p)) {  
  281.             p->policy = SCHED_NORMAL;  
  282.             p->static_prio = NICE_TO_PRIO(0);  
  283.             p->rt_priority = 0;  
  284.         } else if (PRIO_TO_NICE(p->static_prio) < 0)  
  285.             p->static_prio = NICE_TO_PRIO(0);  
  286.   
  287.         p->prio = p->normal_prio = __normal_prio(p);  
  288.         //根据优先级和调度策略设置权重  
  289.         set_load_weight(p);  
  290.   
  291.         /* 
  292.          * We don't need the reset flag anymore after the fork. It has 
  293.          * fulfilled its duty: 
  294.          */  
  295.         p->sched_reset_on_fork = 0;  
  296.     }  
  297.   
  298.     //如果不是实时进程,就用CFS调度  
  299.     if (!rt_prio(p->prio))  
  300.         p->sched_class = &fair_sched_class;  
  301.   
  302.     if (p->sched_class->task_fork)  
  303.         p->sched_class->task_fork(p);  
  304.   
  305.     .......  
  306. }  
  307.   
  308. 进入CFS中的task_fork_fair函数。  
  309. /* 
  310.  * called on fork with the child task as argument from the parent's context 
  311.  *  - child not yet on the tasklist 
  312.  *  - preemption disabled 
  313.  */  
  314. static void task_fork_fair(struct task_struct *p)  
  315. {  
  316.     struct cfs_rq *cfs_rq = task_cfs_rq(current);  
  317.     struct sched_entity *se = &p->se, *curr = cfs_rq->curr;  
  318.     int this_cpu = smp_processor_id();  
  319.     struct rq *rq = this_rq();  
  320.     unsigned long flags;  
  321.   
  322.     raw_spin_lock_irqsave(&rq->lock, flags);  
  323.   
  324.     //更新rq的时钟  
  325.     update_rq_clock(rq);  
  326.   
  327.     //更新cfs_rq的统计数据  
  328.     update_curr(cfs_rq);  
  329.   
  330.     //子进程虚拟时间以 父进程的虚拟时间为基准  
  331.     if (curr)  
  332.         se->vruntime = curr->vruntime;  
  333.     //调整子进程虚拟时间  
  334.     place_entity(cfs_rq, se, 1);  
  335.   
  336.     /* 
  337.         参数sysctl_sched_child_runs_first强制子进程在父进程之前运行。 
  338.         entity_before(curr, se)判断是否需要进行虚拟运行时间的对调, 
  339.         只有父进程的虚拟运行时间小于子进程的虚拟运行时间时才需要此操作。 
  340.     */  
  341.     if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {  
  342.         /* 
  343.          * Upon rescheduling, sched_class::put_prev_task() will place 
  344.          * 'current' within the tree based on its new key value. 
  345.          */  
  346.         swap(curr->vruntime, se->vruntime);  
  347.         resched_task(rq->curr);  
  348.     }  
  349.   
  350.     /* 
  351.         将vruntime减去一个cfs_rq->min_vruntime,使得vruntime变成一个相对的时间, 
  352.         这样做好像是为了在SMP的情况下有一个标准的接口,这个我还不太清楚, 
  353.         有机会再研究 
  354.     */  
  355.     se->vruntime -= cfs_rq->min_vruntime;  
  356.   
  357.     raw_spin_unlock_irqrestore(&rq->lock, flags);  
  358. }  
  359.   
  360. static void  
  361. place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)  
  362. {  
  363.     //基准为min_vruntime  
  364.     u64 vruntime = cfs_rq->min_vruntime;  
  365.   
  366.     /* 
  367.      * The 'current' period is already promised to the current tasks, 
  368.      * however the extra weight of the new task will slow them down a 
  369.      * little, place the new task so that it fits in the slot that 
  370.      * stays open at the end. 
  371.      */  
  372.     /* 
  373.         新进程开始的时候,基准值要比min_vruntime稍微慢一些。 
  374.     */  
  375.     if (initial)  
  376.         vruntime += sched_vslice(cfs_rq, se);  
  377.   
  378.     /* sleeps up to a single latency don't count. */  
  379.     if (!initial) {  
  380.         /* 
  381.             这里是睡眠进程唤醒时的时间处理。 
  382.             因为进程休眠了,se->vruntime肯定很小,但是如果太小 
  383.             的话,又会抢其他进程运行的机会。所以这里采用了 
  384.             cfs_rq->min_vruntime - thresh。即保证了它调度的优先权, 
  385.             又不至于太小,影响其他进程。 
  386.         */  
  387.         unsigned long thresh = sysctl_sched_latency;  
  388.   
  389.         /* 
  390.          * Halve their sleep time's effect, to allow 
  391.          * for a gentler effect of sleepers: 
  392.          */  
  393.         if (sched_feat(GENTLE_FAIR_SLEEPERS))  
  394.             thresh >>= 1;  
  395.   
  396.         vruntime -= thresh;  
  397.     }  
  398.   
  399.     //这里保证vruntime只能往大了调整  
  400.     /* ensure we never gain time by being placed backwards. */  
  401.     vruntime = max_vruntime(se->vruntime, vruntime);  
  402.   
  403.     se->vruntime = vruntime;  
  404. }  
  405.   
  406. 2.创建新的进程的另一个调度相关的函数是wake_up_new_task,作用见下面注释。  
  407. /* 
  408.  * wake_up_new_task - wake up a newly created task for the first time. 
  409.  * 
  410.  * This function will do some initial scheduler statistics housekeeping 
  411.  * that must be done for every newly created context, then puts the task 
  412.  * on the runqueue and wakes it. 
  413.  */  
  414. void wake_up_new_task(struct task_struct *p)  
  415. {  
  416.     unsigned long flags;  
  417.     struct rq *rq;  
  418.   
  419.     raw_spin_lock_irqsave(&p->pi_lock, flags);  
  420.   
  421.     rq = __task_rq_lock(p);  
  422.     //添加进程到运行队列中  
  423.     activate_task(rq, p, 0);  
  424.     p->on_rq = 1;  
  425.   
  426.     //判断新的进程是否能抢占当前进程  
  427.     check_preempt_curr(rq, p, WF_FORK);  
  428.   
  429.     task_rq_unlock(rq, p, &flags);  
  430. }  
  431.   
  432. activate_task最终会调到CFS中的函数enqueue_task_fair  
  433. /* 
  434.  * The enqueue_task method is called before nr_running is 
  435.  * increased. Here we update the fair scheduling stats and 
  436.  * then put the task into the rbtree: 
  437.  */  
  438. static void  
  439. enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)  
  440. {  
  441.     struct cfs_rq *cfs_rq;  
  442.     struct sched_entity *se = &p->se;  
  443.   
  444.     //如果已经在运行队列中,什么也不做就返回  
  445.     if (se->on_rq)  
  446.         return;  
  447.   
  448.     enqueue_entity(cfs_rq, se, flags);  
  449.   
  450.     cfs_rq->h_nr_running++;  
  451. }  
  452.   
  453. static void  
  454. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)  
  455. {  
  456.     /* 
  457.      * Update the normalized vruntime before updating min_vruntime 
  458.      * through callig update_curr(). 
  459.      */  
  460.     /* 
  461.         如果是新创建的进程,可以看到前面减去了一个cfs_rq->min_vruntime, 
  462.         这里再加回来。 
  463.     */  
  464.     if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))  
  465.         se->vruntime += cfs_rq->min_vruntime;  
  466.   
  467.     /* 
  468.      * Update run-time statistics of the 'current'. 
  469.      */  
  470.     update_curr(cfs_rq);  
  471.   
  472.     if (flags & ENQUEUE_WAKEUP) {  
  473.         //如果是进程唤醒时的操作(try_to_wake_up),就会进这里  
  474.         place_entity(cfs_rq, se, 0);  
  475.     }  
  476.   
  477.     account_entity_enqueue(cfs_rq, se);  
  478.   
  479.     //添加进程到红黑树  
  480.     if (se != cfs_rq->curr)  
  481.         __enqueue_entity(cfs_rq, se);  
  482.     se->on_rq = 1;  
  483. }  
  484.   
  485. static void  
  486. account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)  
  487. {  
  488.     //添加进程的权重到cfs的权重中  
  489.     update_load_add(&cfs_rq->load, se->load.weight);  
  490.     //添加进程的权重到rq的权重中  
  491.     inc_cpu_load(rq_of(cfs_rq), se->load.weight);  
  492.   
  493.     //cfs_rq的进程数量加一  
  494.     cfs_rq->nr_running++;  
  495. }  
  496.   
  497. static void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)  
  498. {  
  499.     const struct sched_class *class;  
  500.   
  501.     if (p->sched_class == rq->curr->sched_class) {  
  502.         rq->curr->sched_class->check_preempt_curr(rq, p, flags);  
  503.     } else {  
  504.         for_each_class(class) {  
  505.             if (class == rq->curr->sched_class)  
  506.                 break;  
  507.             if (class == p->sched_class) {  
  508.                 resched_task(rq->curr);  
  509.                 break;  
  510.             }  
  511.         }  
  512.     }  
  513.   
  514.     /* 
  515.      * A queue event has occurred, and we're going to schedule.  In 
  516.      * this case, we can save a useless back to back clock update. 
  517.      */  
  518.     if (rq->curr->on_rq && test_tsk_need_resched(rq->curr))  
  519.         rq->skip_clock_update = 1;  
  520. }  
  521.   
  522. 判断当前进程是否能被进程p抢占  
  523. /* 
  524.  * Preempt the current task with a newly woken task if needed: 
  525.  */  
  526. static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)  
  527. {  
  528.     struct task_struct *curr = rq->curr;  
  529.     struct sched_entity *se = &curr->se, *pse = &p->se;  
  530.     struct cfs_rq *cfs_rq = task_cfs_rq(curr);  
  531.     int scale = cfs_rq->nr_running >= sched_nr_latency;  
  532.     int next_buddy_marked = 0;  
  533.   
  534.     / /要唤醒的进程和当前运行的进程是同一个,就返回  
  535.     if (unlikely(se == pse))  
  536.         return;  
  537.   
  538.     /* 
  539.      * We can come here with TIF_NEED_RESCHED already set from new task 
  540.      * wake up path. 
  541.      * 
  542.      * Note: this also catches the edge-case of curr being in a throttled 
  543.      * group (e.g. via set_curr_task), since update_curr() (in the 
  544.      * enqueue of curr) will have resulted in resched being set.  This 
  545.      * prevents us from potentially nominating it as a false LAST_BUDDY 
  546.      * below. 
  547.      */  
  548.     //如果调度位已经设置,就不再往下在走了  
  549.     if (test_tsk_need_resched(curr))  
  550.         return;  
  551.   
  552.     /* Idle tasks are by definition preempted by non-idle tasks. */  
  553.     //下面这种情况是一定需要抢占的。  
  554.     if (unlikely(curr->policy == SCHED_IDLE) &&  
  555.         likely(p->policy != SCHED_IDLE))  
  556.         goto preempt;  
  557.   
  558.     /* 
  559.      * Batch and idle tasks do not preempt non-idle tasks (their preemption 
  560.      * is driven by the tick): 
  561.      */  
  562.     //如果不是使用CFS,就返回  
  563.     if (unlikely(p->policy != SCHED_NORMAL))  
  564.         return;  
  565.   
  566.     update_curr(cfs_rq_of(se));  
  567.   
  568.     //判断是否需要抢占的核心函数  
  569.     if (wakeup_preempt_entity(se, pse) == 1) {  
  570.         /* 
  571.          * Bias pick_next to pick the sched entity that is 
  572.          * triggering this preemption. 
  573.          */  
  574.         //last指向最后一个别调度出去的进程。  
  575.         if (!next_buddy_marked)  
  576.             set_next_buddy(pse);  
  577.         goto preempt;  
  578.     }  
  579.   
  580.     return;  
  581.   
  582. preempt:  
  583.     resched_task(curr);  
  584.     /* 
  585.      * Only set the backward buddy when the current task is still 
  586.      * on the rq. This can happen when a wakeup gets interleaved 
  587.      * with schedule on the ->pre_schedule() or idle_balance() 
  588.      * point, either of which can * drop the rq lock. 
  589.      * 
  590.      * Also, during early boot the idle thread is in the fair class, 
  591.      * for obvious reasons its a bad idea to schedule back to it. 
  592.      */  
  593.     if (unlikely(!se->on_rq || curr == rq->idle))  
  594.         return;  
  595.   
  596.     if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))  
  597.         set_last_buddy(se);  
  598. }  
  599.   
  600. /* 
  601.  * Should 'se' preempt 'curr'. 
  602.  * 
  603.  *             |s1 
  604.  *        |s2 
  605.  *   |s3 
  606.  *         g 
  607.  *      |<--->|c 
  608.  * 
  609.  *  w(c, s1) = -1 
  610.  *  w(c, s2) =  0 
  611.  *  w(c, s3) =  1 
  612.  * 
  613.  */  
  614. static int  
  615. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)  
  616. {  
  617.     //计算两个虚拟时间之差  
  618.     s64 gran, vdiff = curr->vruntime - se->vruntime;  
  619.   
  620.     //如果se的虚拟时间比curr还大,说明本该curr执行,无需抢占  
  621.     if (vdiff <= 0)  
  622.         return -1;  
  623.   
  624.     /* 
  625.         gran为需要抢占的时间差,只有两个时间差大于需要抢占的时间差, 
  626.         才需要抢占,这里避免了太频繁的抢占。 
  627.     */  
  628.     gran = wakeup_gran(curr, se);  
  629.     if (vdiff > gran)  
  630.         return 1;  
  631.   
  632.     return 0;  
  633. }  
  634.   
  635. 五、schedule函数  
  636. 1.如果需要调度的话就有将当前进程从运行队列中清出去,这里调用函数deactivate_task,flags为DEQUEUE_SLEEP。  
  637. /* 
  638.  * deactivate_task - remove a task from the runqueue. 
  639.  */  
  640. static void deactivate_task(struct rq *rq, struct task_struct *p, int flags)  
  641. {  
  642.     if (task_contributes_to_load(p))  
  643.         rq->nr_uninterruptible++;  
  644.   
  645.     dequeue_task(rq, p, flags);  
  646. }  
  647.   
  648. static void dequeue_task(struct rq *rq, struct task_struct *p, int flags)  
  649. {  
  650.     //更新rq时间  
  651.     update_rq_clock(rq);  
  652.   
  653.     p->sched_class->dequeue_task(rq, p, flags);  
  654. }  
  655.   
  656. CFS中,调用函数dequeue_task_fair  
  657. /* 
  658.  * The dequeue_task method is called before nr_running is 
  659.  * decreased. We remove the task from the rbtree and 
  660.  * update the fair scheduling stats: 
  661.  */  
  662. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)  
  663. {  
  664.     struct cfs_rq *cfs_rq;  
  665.     struct sched_entity *se = &p->se;  
  666.     int task_sleep = flags & DEQUEUE_SLEEP;  
  667.   
  668.     cfs_rq = cfs_rq_of(se);  
  669.     dequeue_entity(cfs_rq, se, flags);  
  670.   
  671.     cfs_rq->h_nr_running--;  
  672. }  
  673.   
  674. static void  
  675. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)  
  676. {  
  677.     /* 
  678.      * Update run-time statistics of the 'current'. 
  679.      */  
  680.     update_curr(cfs_rq);  
  681.   
  682.     update_stats_dequeue(cfs_rq, se);  
  683.     if (flags & DEQUEUE_SLEEP) {  
  684.         //更新一些统计数据  
  685.     }  
  686.   
  687.     //如果进程退出,清掉buddy里的指针  
  688.     clear_buddies(cfs_rq, se);  
  689.   
  690.     if (se != cfs_rq->curr)  
  691.         __dequeue_entity(cfs_rq, se);  
  692.     se->on_rq = 0;  
  693.     //account_entity_enqueue的相反操作  
  694.     account_entity_dequeue(cfs_rq, se);  
  695.   
  696.     /* 
  697.         还是为了SMP 
  698.     */  
  699.     /* 
  700.      * Normalize the entity after updating the min_vruntime because the 
  701.      * update can refer to the ->curr item and we need to reflect this 
  702.      * movement in our normalized position. 
  703.      */  
  704.     if (!(flags & DEQUEUE_SLEEP))  
  705.         se->vruntime -= cfs_rq->min_vruntime;  
  706.   
  707.     update_min_vruntime(cfs_rq);  
  708. }  
  709.   
  710. 2.忽略SMP,第二个操作就是put_prev_task  
  711. static void put_prev_task(struct rq *rq, struct task_struct *prev)  
  712. {  
  713.     if (prev->on_rq || rq->skip_clock_update < 0)  
  714.         update_rq_clock(rq);  
  715.     prev->sched_class->put_prev_task(rq, prev);  
  716. }  
  717.   
  718. /* 
  719.  * Account for a descheduled task: 
  720.  */  
  721. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)  
  722. {  
  723.     struct sched_entity *se = &prev->se;  
  724.     struct cfs_rq *cfs_rq;  
  725.   
  726.     cfs_rq = cfs_rq_of(se);  
  727.     put_prev_entity(cfs_rq, se);  
  728. }  
  729.   
  730. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)  
  731. {  
  732.     if (prev->on_rq) {  
  733.         /* 
  734.             如下面解释,被调度的进程有可能还是在运行队列上的,例如在 
  735.             调度的时候收到了一个信号。 
  736.         */  
  737.         /* 
  738.          * If still on the runqueue then deactivate_task() 
  739.          * was not called and update_curr() has to be done: 
  740.          */  
  741.         update_curr(cfs_rq);  
  742.   
  743.         /* Put 'current' back into the tree. */  
  744.         /* 
  745.             正在运行进程,是不在红黑树中的,set_next_entity有这个处理, 
  746.             运行的进程并不在红黑树中移动。 
  747.             所以既然不运行了,还是要添加到运行队列中去。 
  748.         */  
  749.         __enqueue_entity(cfs_rq, prev);  
  750.     }  
  751.     cfs_rq->curr = NULL;  
  752. }  
  753.   
  754. 3.pick_next_task选择下一个要运行的进程  
  755. /* 
  756.  * Pick up the highest-prio task: 
  757.  */  
  758. static inline struct task_struct *  
  759. pick_next_task(struct rq *rq)  
  760. {  
  761.     const struct sched_class *class;  
  762.     struct task_struct *p;  
  763.   
  764.     /* 
  765.      * Optimization: we know that if all tasks are in 
  766.      * the fair class we can call that function directly: 
  767.      */  
  768.     //如果所有的进程都是CFS的进程,就用这个CFS的方法处理  
  769.     if (likely(rq->nr_running == rq->cfs.h_nr_running)) {  
  770.         p = fair_sched_class.pick_next_task(rq);  
  771.         if (likely(p))  
  772.             return p;  
  773.     }  
  774.   
  775.     for_each_class(class) {  
  776.         p = class->pick_next_task(rq);  
  777.         if (p)  
  778.             return p;  
  779.     }  
  780. }  
  781.   
  782. CFS中的选择下一个进程的函数:  
  783. static struct task_struct *pick_next_task_fair(struct rq *rq)  
  784. {  
  785.     struct task_struct *p;  
  786.     struct cfs_rq *cfs_rq = &rq->cfs;  
  787.     struct sched_entity *se;  
  788.   
  789.     //如果没有进程在运行队列中,就返回NULL  
  790.     if (!cfs_rq->nr_running)  
  791.         return NULL;  
  792.   
  793.     se = pick_next_entity(cfs_rq);  
  794.     set_next_entity(cfs_rq, se);  
  795.   
  796.     p = task_of(se);  
  797.   
  798.     return p;  
  799. }  
  800.   
  801. /* 
  802.  * Pick the next process, keeping these things in mind, in this order: 
  803.  * 1) keep things fair between processes/task groups 
  804.  * 2) pick the "next" process, since someone really wants that to run 
  805.  * 3) pick the "last" process, for cache locality 
  806.  * 4) do not run the "skip" process, if something else is available 
  807.  */  
  808. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)  
  809. {  
  810.     //取下红黑树中最坐边的节点  
  811.     struct sched_entity *se = __pick_first_entity(cfs_rq);  
  812.     struct sched_entity *left = se;  
  813.   
  814.     /* 
  815.         如果这个进程是被skip的(例如利用sched_yield函数), 
  816.         且如果下一个进程和第一个进程虚拟时间没有差得太远, 
  817.         就选择下一个进程运行 
  818.     */  
  819.     /* 
  820.      * Avoid running the skip buddy, if running something else can 
  821.      * be done without getting too unfair. 
  822.      */  
  823.     if (cfs_rq->skip == se) {  
  824.         struct sched_entity *second = __pick_next_entity(se);  
  825.         if (second && wakeup_preempt_entity(second, left) < 1)  
  826.             se = second;  
  827.     }  
  828.   
  829.     /* 
  830.      * Prefer last buddy, try to return the CPU to a preempted task. 
  831.      */  
  832.     //最有被调度出去的进程有优先执行权  
  833.     if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)  
  834.         se = cfs_rq->last;  
  835.   
  836.     clear_buddies(cfs_rq, se);  
  837.   
  838.     return se;  
  839. }  
  840.   
  841. static void  
  842. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)  
  843. {  
  844.     /* 'current' is not kept within the tree. */  
  845.     if (se->on_rq) {  
  846.         //从这里看出cfs_rq->curr并不在红黑树中  
  847.         __dequeue_entity(cfs_rq, se);  
  848.     }  
  849.   
  850.     //设置exec_start为cfs_rq的clock_task,重新开始计时  
  851.     update_stats_curr_start(cfs_rq, se);  
  852.     cfs_rq->curr = se;  
  853.   
  854.     //记录下这个时间到prev_sum_exec_runtime  
  855.     se->prev_sum_exec_runtime = se->sum_exec_runtime;  
  856. }  
  857.   
  858. 六、唤醒进程  
  859. /** 
  860.  * try_to_wake_up - wake up a thread 
  861.  * @p: the thread to be awakened 
  862.  * @state: the mask of task states that can be woken 
  863.  * @wake_flags: wake modifier flags (WF_*) 
  864.  * 
  865.  * Put it on the run-queue if it's not already there. The "current" 
  866.  * thread is always on the run-queue (except when the actual 
  867.  * re-schedule is in progress), and as such you're allowed to do 
  868.  * the simpler "current->state = TASK_RUNNING" to mark yourself 
  869.  * runnable without the overhead of this. 
  870.  * 
  871.  * Returns %true if @p was woken up, %false if it was already running 
  872.  * or @state didn't match @p's state. 
  873.  */  
  874. static int  
  875. try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)  
  876. {  
  877.     unsigned long flags;  
  878.     int success = 0;  
  879.   
  880.     raw_spin_lock_irqsave(&p->pi_lock, flags);  
  881.     //如果需要唤醒的进程的状态不是唤醒程序的状态,就什么都不做  
  882.     if (!(p->state & state))  
  883.         goto out;  
  884.   
  885.     success = 1; /* we're going to change ->state */  
  886.   
  887.     //如果进程一直都在rq上,就做一次远程唤醒  
  888.     if (p->on_rq && ttwu_remote(p, wake_flags))  
  889.         goto out;  
  890.   
  891.     ttwu_queue(p, 0);  
  892. out:  
  893.     raw_spin_unlock_irqrestore(&p->pi_lock, flags);  
  894.   
  895.     return success;  
  896. }  
  897.   
  898. 如下面注释所说,这是一次轻量级的唤醒,比如在进入schedule的时候,检测到了一个信号,这个进程就不会从rq上拿出。  
  899. 如果是这种情况,处理起来就相对简单了,仅仅是检查一下进程是否能够抢占当前进程,再将进程的状态设为TASK_RUNNING。  
  900. /* 
  901.  * Called in case the task @p isn't fully descheduled from its runqueue, 
  902.  * in this case we must do a remote wakeup. Its a 'light' wakeup though, 
  903.  * since all we need to do is flip p->state to TASK_RUNNING, since 
  904.  * the task is still ->on_rq. 
  905.  */  
  906. static int ttwu_remote(struct task_struct *p, int wake_flags)  
  907. {  
  908.     struct rq *rq;  
  909.     int ret = 0;  
  910.   
  911.     rq = __task_rq_lock(p);  
  912.     if (p->on_rq) {  
  913.         ttwu_do_wakeup(rq, p, wake_flags);  
  914.         ret = 1;  
  915.     }  
  916.     __task_rq_unlock(rq);  
  917.   
  918.     return ret;  
  919. }  
  920.   
  921. /* 
  922.  * Mark the task runnable and perform wakeup-preemption. 
  923.  */  
  924. static void  
  925. ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)  
  926. {  
  927.     check_preempt_curr(rq, p, wake_flags);  
  928.   
  929.     p->state = TASK_RUNNING;  
  930. }  
  931.   
  932. static void ttwu_queue(struct task_struct *p, int cpu)  
  933. {  
  934.     struct rq *rq = cpu_rq(cpu);  
  935.   
  936.     raw_spin_lock(&rq->lock);  
  937.     ttwu_do_activate(rq, p, 0);  
  938.     raw_spin_unlock(&rq->lock);  
  939. }  
  940.   
  941. static void  
  942. ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags)  
  943. {  
  944.     ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_WAKING);  
  945.     //检查一下进程是否能够抢占当前进程,再将进程的状态设为TASK_RUNNING。  
  946.     ttwu_do_wakeup(rq, p, wake_flags);  
  947. }  
  948.   
  949. static void ttwu_activate(struct rq *rq, struct task_struct *p, int en_flags)  
  950. {  
  951.     activate_task(rq, p, en_flags);  
  952.     p->on_rq = 1;  
  953.   
  954.     /* if a worker is waking up, notify workqueue */  
  955.     if (p->flags & PF_WQ_WORKER)  
  956.         wq_worker_waking_up(p, cpu_of(rq));  
  957. }  
  958.   
  959. 附:  
  960.   
  961. [PATCH 11/12] sched: Remove the cfs_rq dependency from set_task_cpu()  
  962.   
  963. From: Peter Zijlstra   
  964. Date: Wed Dec 16 2009 - 12:08:23 EST  
  965. Next message: Uwe Kleine-König: "Re: [PATCH 4/7] can/at91: don't check platform_get_irq's returnvalue against zero"  
  966. Previous message: Peter Zijlstra: "[PATCH 06/12] sched: Ensure set_task_cpu() is never called on blocked tasks"  
  967. In reply to: tip-bot for Ingo Molnar: "[tip:sched/urgent] sched: Make warning less noisy"  
  968. Next in thread: tip-bot for Peter Zijlstra: "[tip:sched/urgent] sched: Remove the cfs_rq dependency from set_task_cpu()"  
  969. Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]  
  970. In order to remove the cfs_rq dependency from set_task_cpu() we need  
  971. to ensure the task is cfs_rq invariant for all callsites.  
  972.   
  973. The simple approach is to substract cfs_rq->min_vruntime from  
  974. se->vruntime on dequeue, and add cfs_rq->min_vruntime on enqueue.  
  975.   
  976. However, this has the downside of breaking FAIR_SLEEPERS since we  
  977. loose the old vruntime as we only maintain the relative position.  
  978.   
  979. To solve this, we observe that we only migrate runnable tasks, we do  
  980. this using deactivate_task(.sleep=0) and activate_task(.wakeup=0),  
  981. therefore we can restrain the min_vruntime invariance to that state.  
  982.   
  983. The only other case is wakeup balancing, since we want to maintain the  
  984. old vruntime we cannot make it relative on dequeue, but since we don't  
  985. migrate inactive tasks, we can do so right before we activate it  
  986. again.  
  987.   
  988. This is where we need the new pre-wakeup hook, we need to call this  
  989. while still holding the old rq->lock. We could fold it into  
  990. ->select_task_rq(), but since that has multiple callsites and would  
  991. obfuscate the locking requirements, that seems like a fudge.  
  992.   
  993. This leaves the fork() case, simply make sure that ->task_fork()  
  994. leaves the ->vruntime in a relative state.  
  995.   
  996. This covers all cases where set_task_cpu() gets called, and ensures it  
  997. sees a relative vruntime.  
  998.   
  999. Signed-off-by: Peter Zijlstra <a.p.zijlstra@xxxxxxxxx>  
  1000. ---  
  1001. include/linux/sched.h | 2 +-  
  1002. kernel/sched.c | 6 +-----  
  1003. kernel/sched_fair.c | 50 ++++++++++++++++++++++++++++++++++++++++++++------  
  1004. 3 files changed, 46 insertions(+), 12 deletions(-)  
  1005.   
  1006. Index: linux-2.6/kernel/sched.c  
  1007. ===================================================================  
  1008. --- linux-2.6.orig/kernel/sched.c  
  1009. +++ linux-2.6/kernel/sched.c  
  1010. @@ -2038,8 +2038,6 @@ task_hot(struct task_struct *p, u64 now,  
  1011. void set_task_cpu(struct task_struct *p, unsigned int new_cpu)  
  1012. {  
  1013. int old_cpu = task_cpu(p);  
  1014. -   struct cfs_rq *old_cfsrq = task_cfs_rq(p),  
  1015. -    *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);  
  1016.   
  1017. #ifdef CONFIG_SCHED_DEBUG  
  1018. /* 
  1019. @@ -2056,8 +2054,6 @@ void set_task_cpu(struct task_struct *p, 
  1020. perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 
  1021. 1, 1, NULL, 0); 
  1022. } 
  1023. -   p->se.vruntime -= old_cfsrq->min_vruntime - 
  1024. -    new_cfsrq->min_vruntime; 
  1025.  
  1026. __set_task_cpu(p, new_cpu); 
  1027. } 
  1028. @@ -10109,7 +10105,7 @@ void sched_move_task(struct task_struct  
  1029.  
  1030. #ifdef CONFIG_FAIR_GROUP_SCHED 
  1031. if (tsk->sched_class->moved_group) 
  1032. -    tsk->sched_class->moved_group(tsk); 
  1033. +    tsk->sched_class->moved_group(tsk, on_rq); 
  1034. #endif 
  1035.  
  1036. if (unlikely(running)) 
  1037. Index: linux-2.6/kernel/sched_fair.c 
  1038. =================================================================== 
  1039. --- linux-2.6.orig/kernel/sched_fair.c 
  1040. +++ linux-2.6/kernel/sched_fair.c 
  1041. @@ -510,6 +510,7 @@ __update_curr(struct cfs_rq *cfs_rq, str 
  1042. curr->sum_exec_runtime += delta_exec; 
  1043. schedstat_add(cfs_rq, exec_clock, delta_exec); 
  1044. delta_exec_weighted = calc_delta_fair(delta_exec, curr); 
  1045. + 
  1046. curr->vruntime += delta_exec_weighted; 
  1047. update_min_vruntime(cfs_rq); 
  1048. } 
  1049. @@ -765,16 +766,26 @@ place_entity(struct cfs_rq *cfs_rq, stru 
  1050. se->vruntime = vruntime; 
  1051. } 
  1052.  
  1053. +#define ENQUEUE_WAKEUP 1 
  1054. +#define ENQUEUE_MIGRATE 2 
  1055. + 
  1056. static void 
  1057. -enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int wakeup) 
  1058. +enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags) 
  1059. { 
  1060. /* 
  1061. +    * Update the normalized vruntime before updating min_vruntime 
  1062. +    * through callig update_curr(). 
  1063. +    */  
  1064. +   if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_MIGRATE))  
  1065. +    se->vruntime += cfs_rq->min_vruntime;  
  1066. +  
  1067. +   /* 
  1068. * Update run-time statistics of the 'current'. 
  1069. */  
  1070. update_curr(cfs_rq);  
  1071. account_entity_enqueue(cfs_rq, se);  
  1072.   
  1073. -   if (wakeup) {  
  1074. +   if (flags & ENQUEUE_WAKEUP) {  
  1075. place_entity(cfs_rq, se, 0);  
  1076. enqueue_sleeper(cfs_rq, se);  
  1077. }  
  1078. @@ -828,6 +839,14 @@ dequeue_entity(struct cfs_rq *cfs_rq, st  
  1079. __dequeue_entity(cfs_rq, se);  
  1080. account_entity_dequeue(cfs_rq, se);  
  1081. update_min_vruntime(cfs_rq);  
  1082. +  
  1083. +   /* 
  1084. +    * Normalize the entity after updating the min_vruntime because the 
  1085. +    * update can refer to the ->curr item and we need to reflect this 
  1086. +    * movement in our normalized position. 
  1087. +    */  
  1088. +   if (!sleep)  
  1089. +    se->vruntime -= cfs_rq->min_vruntime;  
  1090. }  
  1091.   
  1092. /*  
  1093. @@ -1038,13 +1057,19 @@ static void enqueue_task_fair(struct rq   
  1094. {  
  1095. struct cfs_rq *cfs_rq;  
  1096. struct sched_entity *se = &p->se;  
  1097. +   int flags = 0;  
  1098. +  
  1099. +   if (wakeup)  
  1100. +    flags |= ENQUEUE_WAKEUP;  
  1101. +   if (p->state == TASK_WAKING)  
  1102. +    flags |= ENQUEUE_MIGRATE;  
  1103.   
  1104. for_each_sched_entity(se) {  
  1105. if (se->on_rq)  
  1106. break;  
  1107. cfs_rq = cfs_rq_of(se);  
  1108. -    enqueue_entity(cfs_rq, se, wakeup);  
  1109. -    wakeup = 1;  
  1110. +    enqueue_entity(cfs_rq, se, flags);  
  1111. +    flags = ENQUEUE_WAKEUP;  
  1112. }  
  1113.   
  1114. hrtick_update(rq);  
  1115. @@ -1120,6 +1145,14 @@ static void yield_task_fair(struct rq *r  
  1116.   
  1117. #ifdef CONFIG_SMP  
  1118.   
  1119. +static void task_waking_fair(struct rq *rq, struct task_struct *p)  
  1120. +{  
  1121. +   struct sched_entity *se = &p->se;  
  1122. +   struct cfs_rq *cfs_rq = cfs_rq_of(se);  
  1123. +  
  1124. +   se->vruntime -= cfs_rq->min_vruntime;  
  1125. +}  
  1126. +  
  1127. #ifdef CONFIG_FAIR_GROUP_SCHED  
  1128. /*  
  1129. * effective_load() calculates the load change as seen from the root_task_group  
  1130. @@ -1978,6 +2011,8 @@ static void task_fork_fair(struct task_s  
  1131. resched_task(rq->curr);  
  1132. }  
  1133.   
  1134. +   se->vruntime -= cfs_rq->min_vruntime;  
  1135. +  
  1136. raw_spin_unlock_irqrestore(&rq->lock, flags);  
  1137. }  
  1138.   
  1139. @@ -2031,12 +2066,13 @@ static void set_curr_task_fair(struct rq  
  1140. }  
  1141.   
  1142. #ifdef CONFIG_FAIR_GROUP_SCHED  
  1143. -static void moved_group_fair(struct task_struct *p)  
  1144. +static void moved_group_fair(struct task_struct *p, int on_rq)  
  1145. {  
  1146. struct cfs_rq *cfs_rq = task_cfs_rq(p);  
  1147.   
  1148. update_curr(cfs_rq);  
  1149. -   place_entity(cfs_rq, &p->se, 1);  
  1150. +   if (!on_rq)  
  1151. +    place_entity(cfs_rq, &p->se, 1);  
  1152. }  
  1153. #endif  
  1154.   
  1155. @@ -2076,6 +2112,8 @@ static const struct sched_class fair_sch  
  1156. .move_one_task   = move_one_task_fair,  
  1157. .rq_online   = rq_online_fair,  
  1158. .rq_offline  = rq_offline_fair,  
  1159. +  
  1160. +   .task_waking     = task_waking_fair,  
  1161. #endif  
  1162.   
  1163. .set_curr_task = set_curr_task_fair,  
  1164. Index: linux-2.6/include/linux/sched.h  
  1165. ===================================================================  
  1166. --- linux-2.6.orig/include/linux/sched.h  
  1167. +++ linux-2.6/include/linux/sched.h  
  1168. @@ -1116,7 +1116,7 @@ struct sched_class {  
  1169. struct task_struct *task);  
  1170.   
  1171. #ifdef CONFIG_FAIR_GROUP_SCHED  
  1172. -   void (*moved_group) (struct task_struct *p);  
  1173. +   void (*moved_group) (struct task_struct *p, int on_rq);  
  1174. #endif  
  1175. };  

  1176. 转自 http://blog.csdn.net/arriod/article/details/7033895

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值