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

Linux 進(jìn)程管理之任務(wù)綁定

系統(tǒng) Linux
將進(jìn)程與 CPU 進(jìn)行綁定可以提高 CPU 緩存的命中率,從而提高性能。這種綁定關(guān)系就叫做:進(jìn)程的 CPU 親和性。

[[405508]]

本文轉(zhuǎn)載自微信公眾號(hào)「人人都是極客」,作者布道師Peter 。轉(zhuǎn)載本文請(qǐng)聯(lián)系人人都是極客公眾號(hào)。

什么是進(jìn)程的 CPU 親和性?

在多核結(jié)構(gòu)中,每個(gè)核有各自的L1緩存,相同類型的核被劃分在同一個(gè)cluster中,而不同cluster之間又有共用的L2緩存。講負(fù)載均衡的時(shí)候我們講過一個(gè)進(jìn)程在核之間來回切換的時(shí)候,各個(gè)核之間的緩存命中率會(huì)降低,所以,將進(jìn)程與 CPU 進(jìn)行綁定可以提高 CPU 緩存的命中率,從而提高性能。這種綁定關(guān)系就叫做:進(jìn)程的 CPU 親和性。

如何設(shè)置進(jìn)程的 CPU 親和性?

Linux 系統(tǒng)提供了一個(gè)名為 sched_setaffinity 的系統(tǒng)調(diào)用,此系統(tǒng)調(diào)用可以設(shè)置進(jìn)程的 CPU 親和性。

  1. sched_setaffinity(pid_t pid, size_t cpusetsize, const cpu_set_t *mask) 
  • pid:進(jìn)行綁定 CPU 的進(jìn)程ID號(hào)
  • cpusetsize:參數(shù) mask 指向的 CPU 集合的大小
  • mask:與進(jìn)程綁定的 CPU 集合

cpu_set_t 類型是個(gè)位圖,可以理解為 CPU 集,通過宏來進(jìn)行清除、設(shè)置以及判斷:

  1. //初始化,設(shè)為空 
  2. void CPU_ZERO (cpu_set_t *set);  
  3. //將某個(gè)cpu加入cpu集中  
  4. void CPU_SET (int cpu, cpu_set_t *set);  
  5. //將某個(gè)cpu從cpu集中移出  
  6. void CPU_CLR (int cpu, cpu_set_t *set);  
  7. //判斷某個(gè)cpu是否已在cpu集中設(shè)置了  
  8. int CPU_ISSET (int cpu, const cpu_set_t *set); 

CPU 集可以認(rèn)為是一個(gè)掩碼,每個(gè)設(shè)置的位都對(duì)應(yīng)一個(gè)可以合法調(diào)度的 CPU,而未設(shè)置的位則對(duì)應(yīng)一個(gè)不可調(diào)度的 CPU。換言之,線程都被綁定了,只能在那些對(duì)應(yīng)位被設(shè)置了的處理器上運(yùn)行。通常,掩碼中的所有位都被置位了,也就是可以在所有的 CPU 中調(diào)度。

我們來看看 sched_setaffinity 系統(tǒng)調(diào)用的例子,將進(jìn)程綁定到 CPU2 上運(yùn)行:

  1. #define _GNU_SOURCE 
  2. #include <sched.h> 
  3. #include <stdio.h> 
  4. #include <string.h> 
  5. #include <stdlib.h> 
  6. #include <unistd.h> 
  7. #include <errno.h> 
  8.  
  9. int main(int argc, char **argv) 
  10.     int cpus = 0; 
  11.     int  i = 0; 
  12.     cpu_set_t mask; 
  13.     cpu_set_t get; 
  14.  
  15.     cpus = sysconf(_SC_NPROCESSORS_ONLN); 
  16.     printf("cpus: %d\n", cpus); 
  17.  
  18.     CPU_ZERO(&mask);    /* 初始化set集,將set置為空*/ 
  19.     CPU_SET(2, &mask);  /*將本進(jìn)程綁定到CPU2上*/ 
  20.     if (sched_setaffinity(0, sizeof(mask), &mask) == -1) { 
  21.         printf("Set CPU affinity failue, ERROR:%s\n", strerror(errno)); 
  22.         return -1;  
  23.     }    
  24.         
  25.     return 0; 

CPU 親和性的實(shí)現(xiàn)

我們知道每個(gè) CPU 都擁有一個(gè)獨(dú)立的可運(yùn)行進(jìn)程隊(duì)列,系統(tǒng)運(yùn)行的時(shí)候 CPU 只會(huì)從屬于自己的可運(yùn)行進(jìn)程隊(duì)列中按照 CFS 策略,選擇一個(gè)進(jìn)程來運(yùn)行。所以,把進(jìn)程放置在 CPU 對(duì)應(yīng)的可運(yùn)行進(jìn)程隊(duì)列上,也就可將進(jìn)程綁定到指定的 CPU 上。

下面我們追蹤函數(shù) sched_setaffinity 的調(diào)用順序,分析一下進(jìn)程如何與 CPU 進(jìn)行綁定的。

  1. SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len, unsigned long __user *, user_mask_ptr) 
  2. -- sched_setaffinity(pid_t pid, const struct cpumask *in_mask) 
  3. --- __set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask, bool check) 
  4. ---- stop_one_cpu(unsigned int cpu, cpu_stop_fn_t fn, void *arg) 
  5. ----- migration_cpu_stop(void *data) 
  6. ------ __migrate_task(struct rq *rq, struct task_struct *p, int dest_cpu) 
  7. ------- move_queued_task(struct rq *rq, struct task_struct *p, int new_cpu) 
  8. -------- enqueue_task(struct rq *rq, struct task_struct *p, int flags) 
  9. --------- returns the new run queue of destination CPU 

__set_cpus_allowed_ptr 函數(shù)主要分兩種情況來將進(jìn)程綁定到某個(gè) CPU 上:

  1. stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg):把還沒運(yùn)行且在源運(yùn)行隊(duì)列中進(jìn)程,放到指定的 CPU 可運(yùn)行隊(duì)列中
  2. move_queued_task(rq, &rf, p, dest_cpu):把已經(jīng)運(yùn)行的進(jìn)程遷移到指定的 CPU 可運(yùn)行隊(duì)列中

這兩種情況最終都會(huì)調(diào)用 move_queued_task:

  1. static struct rq *move_queued_task(struct rq *rq, struct rq_flags *rf, 
  2.        struct task_struct *p, int new_cpu) 
  3.  lockdep_assert_held(&rq->lock); 
  4.  
  5.  p->on_rq = TASK_ON_RQ_MIGRATING; 
  6.  dequeue_task(rq, p, DEQUEUE_NOCLOCK); 
  7.  set_task_cpu(p, new_cpu); 
  8.  rq_unlock(rq, rf); 
  9.  
  10.  rq = cpu_rq(new_cpu); 
  11.  
  12.  rq_lock(rq, rf); 
  13.  BUG_ON(task_cpu(p) != new_cpu); 
  14.  enqueue_task(rq, p, 0); 
  15.  p->on_rq = TASK_ON_RQ_QUEUED; 
  16.  check_preempt_curr(rq, p, 0); 
  17.  
  18.  return rq; 

這里首先根據(jù)目標(biāo) CPU 找到對(duì)應(yīng)的工作隊(duì)列 rq,然后通過 enqueue_task 把任務(wù)遷移到目標(biāo) CPU 對(duì)應(yīng)的工作隊(duì)列中,CFS 調(diào)度器的話會(huì)調(diào)用到函數(shù) enqueue_task_fair。

enqueue_task_fair 的執(zhí)行流程如下:

  1. 如果通過struct sched_entity 的 on_rq 成員判斷進(jìn)程已經(jīng)在就緒隊(duì)列上, 則無事可。

 

否則, 具體的工作委托給 enqueue_entity,將任務(wù)插入到 CFS 紅黑樹中合適的結(jié)點(diǎn)。

 

責(zé)任編輯:武曉燕 來源: 人人都是極客
相關(guān)推薦

2011-01-11 13:47:27

Linux管理進(jìn)程

2023-03-05 16:12:41

Linux進(jìn)程線程

2023-03-02 23:50:36

Linux進(jìn)程管理

2023-03-03 00:03:07

Linux進(jìn)程管理

2021-04-22 07:47:46

Linux進(jìn)程管理

2021-04-15 05:51:25

Linux

2021-05-12 07:50:02

CFS調(diào)度器Linux

2021-05-17 18:28:36

Linux CFS負(fù)載均衡

2023-03-05 15:28:39

CFSLinux進(jìn)程

2010-02-25 10:28:43

Linux進(jìn)程管理

2011-01-14 14:49:05

2014-08-01 15:38:37

Linux進(jìn)程管理

2009-10-23 17:35:16

linux進(jìn)程管理

2017-03-03 10:10:44

Linux進(jìn)程管理基礎(chǔ)知識(shí)

2011-01-11 13:53:33

Linux管理磁盤

2009-10-27 16:34:02

linux top命令

2021-07-06 21:30:06

Linux進(jìn)程通信

2010-07-21 09:32:03

Linux多核

2022-11-09 08:12:07

2013-10-11 14:51:16

Linux進(jìn)程管理
點(diǎn)贊
收藏

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