加入收藏 | 设为首页 | 会员中心 | 我要投稿 衡阳站长网 (https://www.0734zz.cn/)- 数据集成、设备管理、备份、数据加密、智能搜索!
当前位置: 首页 > 服务器 > 搭建环境 > Windows > 正文

Linux内核的进程负载均衡机制

发布时间:2019-04-12 12:55:26 所属栏目:Windows 来源:金庆辉
导读:概述 在多核系统中,为了更好的利用多CPU并行能力,进程调度器可以将进程负载尽可能的平均到各个CPU上。再具体实现中,如何选择将进程迁移到的目标CPU,除了考虑各个CPU的负载平衡,还需要将Cache利用纳入权衡因素。同时,对于进程A唤醒进程B这个模型,还

当A进程唤醒B进程时,假设都是普通进程,那么将会调用try_to_wake_up()->select_task_rq()->select_task_rq_fair()

  1. /*  * sched_balance_self: balance the current task (running on cpu) in domains  * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and  * SD_BALANCE_EXEC.  *  * Balance, ie. select the least loaded group.  *  * Returns the target CPU number, or the same CPU if no balancing is needed.  *  * preempt must be disabled.  */ 
  2. /* A进程给自己或者B进程选择一个CPU运行,  * 1: A唤醒B  * 2: A fork()出B后让B运行  * 3: A execute()后重新选择自己将要运行的CPU  */  
  3. static int 
  4. select_task_rq_fair(struct task_struct *p, int prev_cpu, int sd_flag, int wake_flags) 
  5.     struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL; 
  6.     int cpu = smp_processor_id(); 
  7.     int new_cpu = cpu; 
  8.     int want_affine = 0; 
  9.     int sync = wake_flags & WF_SYNC; 
  10.  
  11.     /* 当A进程唤醒B进程时,从try_to_wake_up()进入本函数,这里会置位SD_BALANCE_WAKE。 */ 
  12.     if (sd_flag & SD_BALANCE_WAKE) { 
  13.         /* B进程被唤醒时希望运行的CPU尽可能离A进程所在CPU近一点 */ 
  14.         if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p))) 
  15.             want_affine = 1; 
  16.         new_cpu = prev_cpu; 
  17.         record_wakee(p); 
  18.     } 
  19.  
  20.     rcu_read_lock(); 
  21.     /*       * 如果是A唤醒B模式,则查找同时包含A所在cpu和B睡眠前所在prev_cpu的最低级别的调度域。因为A进程      * 和B进程大概率会有某种数据交换关系,唤醒B时让它们所在的CPU离的近一点会性能最优。      * 否则,查找包含了sd_flag的最高调度域。      */ 
  22.     for_each_domain(cpu, tmp) { 
  23.         if (!(tmp->flags & SD_LOAD_BALANCE)) 
  24.             continue; 
  25.  
  26.         /*       * If both cpu and prev_cpu are part of this domain,         * cpu is a valid SD_WAKE_AFFINE target.         */         
  27.         if (want_affine && (tmp->flags & SD_WAKE_AFFINE) && 
  28.             cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) { 
  29.             affine_sd = tmp; 
  30.             break; 
  31.         } 
  32.  
  33.         if (tmp->flags & sd_flag) 
  34.             sd = tmp; 
  35.     } 
  36.  
  37.     /* 如果是A唤醒B模式,则在同时包含A所在cpu和B睡眠前所在prev_cpu的最低级别的调度域中寻找合适的CPU */ 
  38.     if (affine_sd) { 
  39.        /*          * wake_affine()计算A所在CPU和B睡眠前所在CPU的负载值,判断出B进程唤醒时是否         * 需要离A近一点。         */ 
  40.         if (cpu != prev_cpu && wake_affine(affine_sd, p, sync)) 
  41.             prev_cpu = cpu; 
  42.  
  43.        /* 在与prev_cpu共享LLC的CPU中寻找空闲CPU,如果没有找到,则返回prev_cpu。这里将确定         * B进程唤醒后在哪个CPU运行。         */ 
  44.         new_cpu = select_idle_sibling(p, prev_cpu); 
  45.         goto unlock; 
  46.     } 
  47.  
  48.     /* 到这里,A进程和B进程基本是没有啥亲缘关系的。不用考虑两个进程的Cache亲缘性 */ 
  49.     while (sd) { 
  50.         int load_idx = sd->forkexec_idx; 
  51.         struct sched_group *group; 
  52.         int weight; 
  53.  
  54.         if (!(sd->flags & sd_flag)) { 
  55.             sd = sd->child; 
  56.             continue; 
  57.         } 
  58.  
  59.         if (sd_flag & SD_BALANCE_WAKE) 
  60.             load_idx = sd->wake_idx; 
  61.  
  62.         group = find_idlest_group(sd, p, cpu, load_idx); 
  63.         if (!group) { 
  64.             sd = sd->child; 
  65.             continue; 
  66.         } 
  67.  
  68.         new_cpu = find_idlest_cpu(group, p, cpu); 
  69.         if (new_cpu == -1 || new_cpu == cpu) { 
  70.             /* Now try balancing at a lower domain level of cpu */ 
  71.             sd = sd->child; 
  72.             continue; 
  73.         } 
  74.  
  75.         /* Now try balancing at a lower domain level of new_cpu */ 
  76.         cpu = new_cpu; 
  77.         weight = sd->span_weight; 
  78.         sd = NULL; 
  79.         for_each_domain(cpu, tmp) { 
  80.             if (weight <= tmp->span_weight) 
  81.                 break; 
  82.             if (tmp->flags & sd_flag) 
  83.                 sd = tmp; 
  84.         } 
  85.         /* while loop will break here if sd == NULL */ 
  86.     } 
  87. unlock: 
  88.     rcu_read_unlock(); 
  89.  
  90.     return new_cpu; 
  91. }  
  1. /*  * Try and locate an idle CPU in the sched_domain.  */ 
  2.  /* 寻找离target CPU最近的空闲CPU(Cache或者内存距离最近)*/ 
  3. static int select_idle_sibling(struct task_struct *p, int target) 
  4.     struct sched_domain *sd; 
  5.     struct sched_group *sg; 
  6.     int i = task_cpu(p); 
  7.      
  8.     /* target CPU正好空闲,自己跟自己当然最近*/ 
  9.     if (idle_cpu(target)) 
  10.         return target; 
  11.  
  12.     /*   * If the prevous cpu is cache affine and idle, don't be stupid.     */ 
  13.     /*       * p进程所在的CPU跟target CPU有Cache共享关系(SMT,或者MC层才有这个关系),并且是空闲的,那就用它了。      * Cache共享说明距离很近了       */ 
  14.     if (i != target && cpus_share_cache(i, target) && idle_cpu(i)) 
  15.         return i; 
  16.  
  17.     /*   * Otherwise, iterate the domains and find an elegible idle cpu.     */ 
  18.     /*      * 在与target CPU有LLC Cache共享关系的调度域中寻找空闲CPU。注意,在X86体系中只有SMT和MC层的调度域才有Cache共享。      */ 
  19.     sd = rcu_dereference(per_cpu(sd_llc, target));     
  20.     /* 在我的机器上是按MC,SMT调度域顺序遍历 */ 
  21.     for_each_lower_domain(sd) { 
  22.         sg = sd->groups; 
  23.         do { 
  24.             if (!cpumask_intersects(sched_group_cpus(sg), 
  25.                         tsk_cpus_allowed(p))) 
  26.                 goto next; 
  27.  
  28.            /* 调度组内所有CPU都是空闲状态,才能选定 */ 
  29.             for_each_cpu(i, sched_group_cpus(sg)) { 
  30.                 if (i == target || !idle_cpu(i)) 
  31.                     goto next; 
  32.             } 
  33.  
  34.            /* 选择全部CPU都空闲的调度组中第一个CPU*/ 
  35.             target = cpumask_first_and(sched_group_cpus(sg), 
  36.                     tsk_cpus_allowed(p)); 
  37.             goto done; 
  38. next: 
  39.             sg = sg->next; 
  40.         } while (sg != sd->groups); 
  41.     } 
  42. done: 
  43.     return target; 

调用execve()系统调用时

  1. /*  * sched_exec - execve() is a valuable balancing opportunity, because at  * this point the task has the smallest effective memory and cache footprint.  */ 
  2. void sched_exec(void) 
  3.     struct task_struct *p = current; 
  4.     unsigned long flags; 
  5.     int dest_cpu; 
  6.  
  7.     raw_spin_lock_irqsave(&p->pi_lock, flags); 
  8.     /* 选择最合适的CPU,这里由于进程execve()后,之前的Cache就无意义了,因此选择目标CPU不用考虑Cache距离 */ 
  9.     dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), SD_BALANCE_EXEC, 0); 
  10.     if (dest_cpu == smp_processor_id()) 
  11.         goto unlock; 
  12.  
  13.     if (likely(cpu_active(dest_cpu))) { 
  14.         struct migration_arg arg = { p, dest_cpu }; 
  15.  
  16.         raw_spin_unlock_irqrestore(&p->pi_lock, flags); 
  17.         stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg); 
  18.         return; 
  19.     } 
  20. unlock: 
  21.     raw_spin_unlock_irqrestore(&p->pi_lock, flags); 

fork的子进程第一次被调度运行时

  1. do_fork()->wake_up_new_task() 
  2.  
  3. /*  * wake_up_new_task - wake up a newly created task for the first time.  *  * This function will do some initial scheduler statistics housekeeping  * that must be done for every newly created context, then puts the task  * on the runqueue and wakes it.  */ 
  4. void wake_up_new_task(struct task_struct *p) 
  5.     unsigned long flags; 
  6.     struct rq *rq; 
  7.  
  8.     raw_spin_lock_irqsave(&p->pi_lock, flags); 
  9. #ifdef CONFIG_SMP 
  10.     /*   * Fork balancing, do it here and not earlier because:   *  - cpus_allowed can change in the fork path   *  - any previously selected cpu might disappear through hotplug    */ 
  11.     /* 选择最合适的CPU,这里由于进程execve()后,之前的Cache就无意义了,因此选择目标CPU不用考虑Cache距离 */ 
  12.     set_task_cpu(p, select_task_rq(p, task_cpu(p), SD_BALANCE_FORK, 0)); 
  13. #endif 
  14.  
  15.     /* Initialize new task's runnable average */ 
  16.     init_task_runnable_average(p); 
  17.     rq = __task_rq_lock(p); 
  18.     activate_task(rq, p, 0); 
  19.     p->on_rq = TASK_ON_RQ_QUEUED; 
  20.     trace_sched_wakeup_new(p, true); 
  21.     check_preempt_curr(rq, p, WF_FORK); 
  22. #ifdef CONFIG_SMP 
  23.     if (p->sched_class->task_woken) 
  24.         p->sched_class->task_woken(rq, p); 
  25. #endif 
  26.     task_rq_unlock(rq, p, &flags); 
  27. }  
SMP负载均衡模型的配置

可以在/proc/sys/kernel/sched_domain/cpuX/中可以对指定CPU所在不同层的调度域进行设置

(编辑:衡阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读