1. 程式人生 > 其它 >(轉)Linux下將程序/執行緒繫結到指定CPU核執行

(轉)Linux下將程序/執行緒繫結到指定CPU核執行

轉自:

Linux下將程序/執行緒繫結到指定CPU核執行

如何檢視Linux核數
$ 總核數 = 物理CPU個數 X 每顆物理CPU的核數
$ 總邏輯CPU數 = 物理CPU個數 X 每顆物理CPU的核數 X 超執行緒數

$ 檢視物理CPU個數
cat /proc/cpuinfo| grep "physical id"| sort| uniq| wc -l

$ 檢視每個物理CPU中core的個數(即核數)
cat /proc/cpuinfo| grep "cpu cores"| uniq

$ 檢視邏輯CPU的個數
cat /proc/cpuinfo| grep "processor"| wc -l

processor:指明每個物理CPU中邏輯處理器資訊(序號:0~5表示有6個邏輯CPU)

cpu cores:指明每顆物理CPU中總核數

以I5-8400 6核/6執行緒為例:

WINDOWS檢視方式:

C:\Users\Administrator>wmic

wmic:root\cli>cpu get

NumberOfCores NumberOfEnabledCore NumberOfLogicalProcessors

(CPU核心數)=6 (可用的CPU核心數)=6 (CPU執行緒數)=6

LINUX虛擬機器:

$ cat /proc/cpuinfo | grep "physical id" | uniq
physical id : 0

$ cat /proc/cpuinfo | grep "cpu cores" | uniq
cpu cores : 6

$ cat /proc/cpuinfo | grep "processor" | uniq
processor : 0
processor : 1
processor : 2
processor : 3
processor : 4
processor : 5

在程式中使用系統呼叫sysconf獲取cpu核心數:

#include <unistd.h>

int sysconf(_SC_NPROCESSORS_CONF);/* 返回系統可以使用的核數,但是其值會包括系統中禁用的核的數目,因 此該值並不代表當前系統中可用的核數 */
int sysconf(_SC_NPROCESSORS_ONLN);/* 返回值真正的代表了系統當前可用的核數 */

/* 以下兩個函式與上述類似 */
#include <sys/sysinfo.h>

int get_nprocs_conf (void);/* 可用核數 */
int get_nprocs (void);/* 真正的反映了當前可用核數 */

C語言程式碼如下:

#include <stdio.h>
#include <unistd.h>
#include <sys/sysinfo.h>

int main(int argc, char *argv[])
{
int sc_nprocessor_conf = sysconf(_SC_NPROCESSORS_CONF);
int sc_nprocessor_onln = sysconf(_SC_NPROCESSORS_ONLN);
printf("sc_nprocessor_conf[%d]\n", sc_nprocessor_conf);
printf("sc_nprocessor_onln[%d]\n", sc_nprocessor_onln);

int nprocs_conf = get_nprocs_conf();
int nprocs_onln = get_nprocs();
printf("nprocs_conf[%d]\n", nprocs_conf);
printf("nprocs_onln[%d]\n", nprocs_onln);
return 0;
}

執行結果:

sc_nprocessor_conf[6]
sc_nprocessor_onln[6]
nprocs_conf[6]
nprocs_onln[6]

確定呼叫執行緒正在執行的CPU和NUMA節點
#include <linux/getcpu.h>
int getcpu(unsigned *cpu, unsigned *node, struct getcpu_cache *tcache);
成功時返回0。 出錯時,返回-1,並適當地設定errno
注意:這個系統呼叫沒有glibc包裝。 見注意事項。
測試程式:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <time.h>
#include <sys/types.h>

struct getcpu_cache
{
unsigned long blob[128/sizeof(long)];
};

int getcpu(unsigned *cpu, unsigned *node,struct getcpu_cache *tcache)
{
return syscall(SYS_getcpu, cpu, node, tcache);
}

int main(int argc, char *argv[])
{
unsigned cpu = 0;
unsigned node = 0;
if(getcpu(&cpu,&node,NULL)==-1)
{
printf("getcpu bad \n");
return 1;
}
printf("cpu = %u node = %u\n",cpu,node);
return 0;
}

getcpu()系統呼叫標識呼叫執行緒或程序當前正在執行的處理器和節點,並將它們寫入由cpu和node引數指向的整數。處理器是識別CPU的唯一小整數。該節點是標識NUMA節點的唯一小識別符號。當cpu或節點為NULL時,沒有任何內容寫入相應的指標。
這個系統呼叫的第三個引數現在是未使用的,應該指定為NULL,除非需要可移植到Linux 2.6.23或更早版本(請參閱NOTES)。

儲存在cpu中的資訊僅在呼叫時保持為當前狀態:除非使用sched_setaffinity(2)修復了CPU關聯性,否則核心可能隨時更改CPU。
(通常情況下不會發生這種情況,因為排程器會盡量減少CPU之間的移動來保持快取熱度,但這是可能的。)呼叫者必須允許在呼叫返回時,在cpu和node中返回的資訊不再是當前的

什麼是綁核
所謂綁核,其實就是設定某個程序/執行緒與某個CPU核的親和力(affinity)。設定以後,Linux排程器就會讓這個程序/執行緒只在所繫結的核上面去執行。但並不是說該程序/執行緒就獨佔這個CPU的核,其他的程序/執行緒還是可以在這個核上面執行的。

掩碼形式綁核
將掩碼轉換為二進位制形式,從最低位到最高位代表物理CPU的#0、#1、……、#n號核。某位的值為0表示不綁該核,1表示綁。比如:0x00000001的二進位制為0000...0001,只有第0號核的位置是1,所以表示只綁0號核;0x00000003的二進位制為0000...0011,第0和1號核的位置是1,所以表示綁CPU的0號和1號核;再比如0xFFFFFFFF的二進位制為1111...1111,所有32個核的位置都為1,所以表示綁CPU的0~31核。

掩碼形式的綁核命令為:

taskset -p mask pid

列表形式
列表形式指直接指定要綁的CPU核的列表,列表中可以有一個或多個核。具體語法如下:

taskset -cp cpu-list pid

其中cpu-list是數字化的cpu列表,從0開始。多個不連續的cpu可用逗號連線,連續的可用短現連線,比如0,1,3-6等。

比如taskset -cp 0,1,3-6 2677 命令表示將程序2677 繫結到#0、#1、#3~#6號核上面。

多程序和多執行緒在cpu核上執行分析:

每個 CPU 核執行一個程序的時候,由於每個程序的資源都獨立,所以 CPU 核心之間切換的時候無需考慮上下文。
每個 CPU 核執行一個執行緒的時候,有時執行緒之間需要共享資源,所以這些資源必須從 CPU 的一個核心被複制到另外一個核心,這會造成額外的開銷。

舉例如下:

1, 使用taskset指令繫結程序到特定CPU步驟

測試程式:cpu_processor_test.c

#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
pid_t pid = getpid();
pid_t ppid = getppid();
printf("ppid %d pid %d\n", ppid, pid);
while(1)
{
sleep(10);
}
return 0;
}

gcc cpu_processor_test.c -o cpu_processor_test

(1)獲取程序pid

$ ps -aux | grep "cpu_processor_test"
myroot 1582 0.0 0.0 4352 628 pts/8 S+ 22:04 0:00 ./cpu_processor_test

(2) 檢視程序當前執行在哪些cpu上

$ taskset -p 1582
pid 1582's current affinity mask: 3f

顯示的十進位制數字3轉換為2進製為最低6個是1,每個1對應一個cpu,所以程序執行在6個cpu上。

(3)指定程序執行在cpu2上

$ taskset -pc 2 1582
pid 1582's current affinity list: 0-5
pid 1582's new affinity list: 2

注意,cpu的標號是從0開始的,所以cpu1表示第二個cpu(第一個cpu的標號是0)。

至此,就把應用程式繫結到了cpu1上執行

(4)確認繫結命令

$ taskset -p 1582
pid 1582's current affinity mask: 4

2,啟動程式時繫結cpu

(1)啟動時繫結到第3個cpu

$ taskset -c 2 ./cpu_processor_test&
[1] 1615
ppid 1439 pid 1615

(2)檢視確認繫結情況

$ taskset -p 1615
pid 1615's current affinity mask: 4

使用sched_setaffinity系統呼叫

sched_setaffinity可以將某個程序繫結到一個特定的CPU。

#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <sched.h>

/* 設定程序號為pid的程序執行在mask所設定的CPU上
* 第二個引數cpusetsize是mask所指定的數的長度
* 通常設定為sizeof(cpu_set_t)

* 如果pid的值為0,則表示指定的是當前程序
*/
int sched_setaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);

int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);/* 獲得pid所指示的程序的CPU位掩碼,並將該掩碼返回到mask所指向的結構中 */

例項

#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/sysinfo.h>
#include<unistd.h>

#define __USE_GNU
#include<sched.h>
#include<ctype.h>
#include<string.h>
#include<pthread.h>

#define MAX_THREAD_NUM 200 //1個CPU內的最多程序數

int cpu_cores_num = 0; //cpu中核數
void *thread_test(void *arg) //arg 傳遞執行緒標號(自己定義)
{
cpu_set_t mask; //CPU核的集合
cpu_set_t get; //獲取在集合中的CPU
int *cpu_index = (int *)arg;
int i;

printf("the thread is:%d\n",*cpu_index); //顯示是第幾個執行緒
CPU_ZERO(&mask); //置空
CPU_SET(*cpu_index,&mask); //設定親和力值
if (sched_setaffinity(0, sizeof(mask), &mask) == -1)//設定執行緒CPU親和力
{
printf("warning: could not set CPU affinity, continuing...\n");
}

CPU_ZERO(&get);
if (sched_getaffinity(0, sizeof(get), &get) == -1)//獲取執行緒CPU親和力
{
printf("warning: cound not get thread affinity, continuing...\n");
}
for (int cpu_core_index = 0; cpu_core_index < cpu_cores_num; cpu_core_index++)
{
if (CPU_ISSET(cpu_core_index, &get))//判斷執行緒與哪個CPU有親和力
{
printf("this thread %d is running processor : %d\n", cpu_core_index, cpu_core_index);
}
}

return NULL;
}

int main(int argc, char *argv[])
{
int tid[MAX_THREAD_NUM] = {0};
int cpu_core_index = 0;
pthread_t thread[MAX_THREAD_NUM];

cpu_cores_num = sysconf(_SC_NPROCESSORS_CONF); //獲取核數
if (cpu_cores_num > MAX_THREAD_NUM)
{
printf("cpu_cores_num of cores[%d] is bigger than MAX_THREAD_NUM[%d]!\n", cpu_cores_num, MAX_THREAD_NUM);
return -1;
}
printf("system has %i processor(s). \n", cpu_cores_num);

for(cpu_core_index = 0; cpu_core_index < cpu_cores_num;cpu_core_index++)
{
tid[cpu_core_index] = cpu_core_index; //每個執行緒必須有個tid[i]
pthread_create(&thread[cpu_core_index],NULL,thread_test,(void*)&tid[cpu_core_index]);
}
for(cpu_core_index = 0; cpu_core_index < cpu_cores_num; cpu_core_index++)
{
pthread_join(thread[cpu_core_index],NULL);//等待所有的執行緒結束,執行緒為死迴圈所以CTRL+C結束
}
return 0;
}

執行結果

$ gcc cpu_processor_test.c -o cpu_processor_test -pthread

$./cpu_processor_test
system has 6 processor(s).
the thread is:0
the thread is:2
the thread is:5
the thread is:3
this thread 5 is running processor : 5
this thread 2 is running processor : 2
this thread 3 is running processor : 3
the thread is:1
the thread is:4
this thread 4 is running processor : 4
this thread 0 is running processor : 0
this thread 1 is running processor : 1

繫結執行緒到cpu核上執行

繫結執行緒到cpu核上使用pthread_setaffinity_np函式,其原型定義如下:

#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <pthread.h>

int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize, const cpu_set_t *cpuset);
int pthread_getaffinity_np(pthread_t thread, size_t cpusetsize, cpu_set_t *cpuset);

Compile and link with -pthread.

各引數的意義與sched_setaffinity相似。

例項

#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define handle_error_en(en, msg) \
do { \
errno = en; \
perror(msg); \
exit(EXIT_FAILURE); \
} while (0)

int main(int argc, char *argv[])
{
int s, j;
cpu_set_t cpuset;

pthread_t thread = pthread_self();
/* Set affinity mask to include CPUs 0 to 7 */

CPU_ZERO(&cpuset);
for (j = 0; j < 8; j++)
{
CPU_SET(j, &cpuset);
}

s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
if (s != 0) handle_error_en(s, "pthread_setaffinity_np");

/* Check the actual affinity mask assigned to the thread */
s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
if (s != 0) handle_error_en(s, "pthread_getaffinity_np");
printf("Set returned by pthread_getaffinity_np() contained:\n");
for (j = 0; j < CPU_SETSIZE; j++)
{
if (CPU_ISSET(j, &cpuset)) printf(" CPU %d\n", j);
}

exit(EXIT_SUCCESS);

return 0;
}

執行結果

$ gcc cpu_processor_test.c -o cpu_processor_test -pthread
$ ./cpu_processor_test
Set returned by pthread_getaffinity_np() contained:
CPU 0
CPU 1
CPU 2
CPU 3
CPU 4
CPU 5

總結

可以使用多種方法把程序/執行緒指定到特定的cpu核上執行。

在具體使用中,要根據使用場景和需求決定使用何種方式。
————————————————
版權宣告:本文為CSDN博主「IT筆記」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處連結及本宣告。
原文連結:https://blog.csdn.net/u013416923/article/details/120317841