自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linux內(nèi)核的進程負載均衡機制

系統(tǒng) Linux
在多核系統(tǒng)中,為了更好的利用多CPU并行能力,進程調(diào)度器可以將進程負載盡可能的平均到各個CPU上。再具體實現(xiàn)中,如何選擇將進程遷移到的目標(biāo)CPU,除了考慮各個CPU的負載平衡,還需要將Cache利用納入權(quán)衡因素。同時,對于進程A喚醒進程B這個模型,還做了特殊的處理。

概述

在多核系統(tǒng)中,為了更好的利用多CPU并行能力,進程調(diào)度器可以將進程負載盡可能的平均到各個CPU上。再具體實現(xiàn)中,如何選擇將進程遷移到的目標(biāo)CPU,除了考慮各個CPU的負載平衡,還需要將Cache利用納入權(quán)衡因素。同時,對于進程A喚醒進程B這個模型,還做了特殊的處理。本文分析以Centos kernel 3.10.0-975源碼為藍本。

SMP負載均衡模型

問題

如果只是將CPU負載平均的分布在各個CPU上,那么就無所謂需要調(diào)度域。但是由于Cache以及內(nèi)存Numa的存在,使得進程最好能遷移到與之前運行所在CPU更'近'的CPU上。

以我們常用的Intel X86為例。Cache基本視圖如下圖:

[[261979]]

從Cache和內(nèi)存訪問的視角,如果進程負載均衡需要把進程A遷移到另一個CPU上,

  • 如果目標(biāo)CPU和進程A之前所在CPU正好是同一個物理CPU同一個核心上(超線程),那么Cache利用率最好,畢竟L1,L2和L3中還是'熱'的。
  • 如果目標(biāo)CPU和進程A之前所在CPU正好是同一個物理CPU但不同核心上(多核),那么Cache利用率次之,L3中還有'熱'數(shù)據(jù)。
  • 如果目標(biāo)CPU和進程A之前所在CPU正好是同一個NUMA但是不同物理CPU上(多NUMA結(jié)構(gòu)),雖然Cache已經(jīng)是'冷'了,但至少內(nèi)存訪問還是在本NUMA中。
  • 如果目標(biāo)CPU和進程A之前所在CPU在不同NUMA中,不但Cache是'冷'的,跨NUMA內(nèi)存還有懲罰,此時內(nèi)存訪問速度最差。

SMP組織

為了更好地利用Cache,內(nèi)核將CPU(如果開啟了超線程,那么以邏輯CPU為單位,否則以物理CPU核心為單位)組織成了調(diào)度域。

邏輯視角

假設(shè)某機器為2路4核8核心CPU,它的CPU調(diào)度域邏輯上如下圖:

 

2路NUMA最為簡單,如果是4路NUMA,那么這個視圖在NUMA層級將會復(fù)雜很多,因為跨NUMA訪問根據(jù)訪問距離導(dǎo)致訪問延時還不相同,這部分最后討論。

分層視角

所有CPU一共分為三個層次:SMT,MC,NUMA,每層都包含了所有CPU,但是劃分粒度不同。根據(jù)Cache和內(nèi)存的相關(guān)性劃分調(diào)度域,調(diào)度域內(nèi)的CPU又劃分一次調(diào)度組。越往下層調(diào)度域越小,越往上層調(diào)度域越大。進程負載均衡會盡可能的在底層調(diào)度域內(nèi)部解決,這樣Cache利用率最優(yōu)。

從分層的視角分析,下圖是調(diào)度域?qū)嶋H組織方式,每層都有per-cpu數(shù)組保存每個CPU對應(yīng)的調(diào)度域和調(diào)度組,它們是在初始化時已經(jīng)提前分配的內(nèi)存。值得注意的是

  • 每個CPU對應(yīng)的調(diào)度域數(shù)據(jù)結(jié)構(gòu)都包含了有效的內(nèi)容,比如說SMT層中,CPU0和CPU1對應(yīng)的不同調(diào)度域數(shù)據(jù)結(jié)構(gòu),內(nèi)容是一模一樣的。
  • 每個CPU對應(yīng)的調(diào)度組數(shù)據(jù)結(jié)構(gòu)不一定包含了有效內(nèi)容,比如說MC層中,CPU0和CPU1指向不同的struct sched_domain,但是sched_domain->groups指向的調(diào)度組確是同樣的數(shù)據(jù)結(jié)構(gòu),這些調(diào)度組組成了環(huán)。
 

單CPU視角

從單個CPU的視角分析,下圖是調(diào)度域?qū)嶋H組織方式。

 

[[261980]]

 

每個CPU的進程運行隊列有一個成員指向其所在調(diào)度域。從最低層到最高層。

我們可以在/proc/sys/kernel/sched_domain/cpuX/ 中看到CPU實際使用的調(diào)度域個數(shù)以及每個調(diào)度域的名字和配置參數(shù)。

負載均衡時機

  • 周期性調(diào)用進程調(diào)度程序scheduler_tick()->trigger_load_balance()中,通過軟中斷觸發(fā)負載均衡。
  • 某個CPU上無可運行進程,__schedule()準(zhǔn)備調(diào)度idle進程前,會嘗試從其它CPU上pull一批進程過來。

周期性負載均衡

CPU對應(yīng)的運行隊列數(shù)據(jù)結(jié)構(gòu)中記錄了下一次周期性負載均衡的時間,當(dāng)超過這個時間點后,將觸發(fā)SCHED_SOFTIRQ軟中斷來進行負載均衡。

  1. void trigger_load_balance(struct rq *rq, int cpu) 
  2.         /* Don't need to rebalance while attached to NULL domain */ 
  3.         if (time_after_eq(jiffies, rq->next_balance) && 
  4.             likely(!on_null_domain(cpu))) 
  5.                 raise_softirq(SCHED_SOFTIRQ); 
  6. #ifdef CONFIG_NO_HZ_COMMON 
  7.         if (nohz_kick_needed(rq) && likely(!on_null_domain(cpu))) 
  8.                 nohz_balancer_kick(cpu); 
  9. #endif 

以下是rebalance_domains()函數(shù)核心流程,值得注意的是,每個層級的調(diào)度間隔不是固定的,而是臨時計算出來,他在一個可通過proc接口配置的最小值和最大值之間。

 

[[261981]]

 

以下是對CPU的每個層級調(diào)度域調(diào)用load_balance()函數(shù)核心流程,目的是把一些進程遷移到指定的CPU(該場景就是當(dāng)前CPU)。

 

[[261982]]

 

以我的服務(wù)器為例,觀察不同層級調(diào)度域的調(diào)度間隔范圍,時間單位為jiffies。

Level

min_interval

max_interval

SMT

2

4

MC

40

80

NUMA

80

160

可見,SMT負載均衡頻率最高,越往上層越低。這也符合體系結(jié)構(gòu)特點,在越低層次遷移進程代價越小(Cache利用率高),所以可以更加頻繁一點。

CPU進入idle前負載均衡

當(dāng)進程調(diào)度函數(shù)__schedule()把即將切換到idle進程前,會發(fā)生一次負載均衡來避免當(dāng)前CPU空閑。

  1. static void __sched __schedule(void) 
  2.         ... 
  3.         if (unlikely(!rq->nr_running)) 
  4.                 idle_balance(cpu, rq); 
  5.  
  6.         ... 

核心函數(shù)idle_balance()。基本上也是盡可能在低層調(diào)度域中負載均衡。

  1. /*  * idle_balance is called by schedule() if this_cpu is about to become  * idle. Attempts to pull tasks from other CPUs.  */ 
  2. void idle_balance(int this_cpu, struct rq *this_rq) 
  3.     unsigned long next_balance = jiffies + HZ; 
  4.     struct sched_domain *sd; 
  5.     int pulled_task = 0; 
  6.     u64 curr_cost = 0; 
  7.  
  8.     this_rq->idle_stamp = rq_clock(this_rq); 
  9.  
  10.     /* 如果該CPU平均空閑時間小于/proc中的配置值或者該cpu調(diào)度域中所有cpu都是idle狀態(tài),那么不需要負載均衡了*/ 
  11.     if (this_rq->avg_idle < sysctl_sched_migration_cost || 
  12.         !this_rq->rd->overload) { 
  13.         rcu_read_lock(); 
  14.         sd = rcu_dereference_check_sched_domain(this_rq->sd); 
  15.         if (sd) 
  16.             update_next_balance(sd, 0, &next_balance); 
  17.         rcu_read_unlock(); 
  18.  
  19.         goto out
  20.     } 
  21.  
  22.     /*   * Drop the rq->lock, but keep IRQ/preempt disabled.     */ 
  23.     raw_spin_unlock(&this_rq->lock); 
  24.  
  25.     update_blocked_averages(this_cpu); 
  26.     rcu_read_lock(); 
  27.     /* 從底向上遍歷調(diào)度域,只要遷移成功一個進程就跳出循環(huán)*/ 
  28.     for_each_domain(this_cpu, sd) { 
  29.         int should_balance; 
  30.         u64 t0, domain_cost; 
  31.  
  32.         if (!(sd->flags & SD_LOAD_BALANCE)) 
  33.             continue
  34.  
  35.         /*           * 如果(當(dāng)前累積的負載均衡開銷時間 + 歷史上該層級負載均衡開銷最大值)已經(jīng)大于CPU平均空閑時間了,          * 那么就沒有必要負載均衡了。注意,sd->max_newidle_lb_cost會在load_balance()函數(shù)中緩慢減少。          */ 
  36.         if (this_rq->avg_idle < curr_cost + sd->max_newidle_lb_cost) { 
  37.             update_next_balance(sd, 0, &next_balance); 
  38.             break; 
  39.         } 
  40.  
  41.         /* 我的機器上該標(biāo)記總是設(shè)置了SD_BALANCE_NEWIDLE */ 
  42.         if (sd->flags & SD_BALANCE_NEWIDLE) { 
  43.             t0 = sched_clock_cpu(this_cpu); 
  44.  
  45.             pulled_task = load_balance(this_cpu, this_rq, 
  46.                            sd, CPU_NEWLY_IDLE, 
  47.                            &should_balance); 
  48.             
  49.             domain_cost = sched_clock_cpu(this_cpu) - t0; 
  50.             if (domain_cost > sd->max_newidle_lb_cost) 
  51.                 sd->max_newidle_lb_cost = domain_cost; 
  52.  
  53.            /* 記錄了當(dāng)前負載均衡開銷累計值 */ 
  54.             curr_cost += domain_cost; 
  55.         } 
  56.  
  57.         update_next_balance(sd, 0, &next_balance); 
  58.  
  59.         /*       * Stop searching for tasks to pull if there are         * now runnable tasks on this rq.        */         
  60.         if (pulled_task || this_rq->nr_running > 0) { 
  61.             this_rq->idle_stamp = 0; 
  62.             break; 
  63.         } 
  64.     } 
  65.     rcu_read_unlock(); 
  66.  
  67.     raw_spin_lock(&this_rq->lock); 
  68.  
  69. out
  70.     /* Move the next balance forward */ 
  71.     if (time_after(this_rq->next_balance, next_balance)) 
  72.         this_rq->next_balance = next_balance; 
  73.  
  74.     if (curr_cost > this_rq->max_idle_balance_cost) 
  75.         this_rq->max_idle_balance_cost = curr_cost; 

其它需要用到SMP負載均衡模型的時機

內(nèi)核運行中,還有部分情況中需要用掉SMP負載均衡模型來確定最佳運行CPU:

  • 進程A喚醒進程B時,try_to_wake_up()中會考慮進程B將在哪個CPU上運行。
  • 進程調(diào)用execve()系統(tǒng)調(diào)用時。
  • fork出子進程,子進程第一次被調(diào)度運

喚醒進程時

當(dāng)A進程喚醒B進程時,假設(shè)都是普通進程,那么將會調(diào)用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 setIn 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.     /* 當(dāng)A進程喚醒B進程時,從try_to_wake_up()進入本函數(shù),這里會置位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的最低級別的調(diào)度域。因為A進程      * 和B進程大概率會有某種數(shù)據(jù)交換關(guān)系,喚醒B時讓它們所在的CPU離的近一點會性能最優(yōu)。      * 否則,查找包含了sd_flag的最高調(diào)度域。      */ 
  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的最低級別的調(diào)度域中尋找合適的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進程基本是沒有啥親緣關(guān)系的。不用考慮兩個進程的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或者內(nèi)存距離最近)*/ 
  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正好空閑,自己跟自己當(dāng)然最近*/ 
  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共享關(guān)系(SMT,或者MC層才有這個關(guān)系),并且是空閑的,那就用它了。      * 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共享關(guān)系的調(diào)度域中尋找空閑CPU。注意,在X86體系中只有SMT和MC層的調(diào)度域才有Cache共享。      */ 
  19.     sd = rcu_dereference(per_cpu(sd_llc, target));     
  20.     /* 在我的機器上是按MC,SMT調(diào)度域順序遍歷 */ 
  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.            /* 調(diào)度組內(nèi)所有CPU都是空閑狀態(tài),才能選定 */ 
  29.             for_each_cpu(i, sched_group_cpus(sg)) { 
  30.                 if (i == target || !idle_cpu(i)) 
  31.                     goto next
  32.             } 
  33.  
  34.            /* 選擇全部CPU都空閑的調(diào)度組中第一個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; 

調(diào)用execve()系統(tǒng)調(diào)用時

  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就無意義了,因此選擇目標(biāo)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的子進程第一次被調(diào)度運行時

  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就無意義了,因此選擇目標(biāo)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所在不同層的調(diào)度域進行設(shè)置

主要分兩類:

  • 調(diào)度層名字:name
  • 調(diào)度域支持的特性:設(shè)置flags文件值,比如SD_LOAD_BALANCE,SD_BALANCE_NEWIDLE,SD_BALANCE_EXEC等,它將決定上文函數(shù)遍歷調(diào)度域時是否忽略本域。
  • 調(diào)度域計算參數(shù):其它所有文件。 

 

責(zé)任編輯:龐桂玉 來源: 騰訊云-云+社區(qū)
相關(guān)推薦

2021-04-22 07:47:46

Linux進程管理

2021-08-30 07:49:31

Linux內(nèi)核負載均衡

2021-05-17 18:28:36

Linux CFS負載均衡

2024-11-14 09:10:13

消費者RocketMQ負載均衡

2009-10-29 09:41:01

Linux內(nèi)核DeviceMappe

2010-05-05 21:39:29

linux負載均衡

2017-07-03 08:08:25

負載均衡分類

2010-04-27 12:56:35

lvs負載均衡

2009-10-23 19:11:32

linux集群

2010-04-27 12:29:08

Linux負載均衡

2010-05-06 12:18:34

IP負載均衡

2010-04-27 12:42:45

LVS負載均衡

2017-08-16 16:20:01

Linux內(nèi)核態(tài)搶占用戶態(tài)搶占

2021-04-15 05:51:25

Linux

2012-05-14 14:09:53

Linux內(nèi)核調(diào)度系統(tǒng)

2021-04-21 14:56:28

負載均衡高并發(fā)優(yōu)化技術(shù)架構(gòu)

2011-12-02 22:51:46

Nginx負載均衡

2018-11-07 10:12:37

2020-11-20 07:55:55

Linux內(nèi)核映射

2010-05-10 15:58:14

porxy負載均衡
點贊
收藏

51CTO技術(shù)棧公眾號