linux下如何把進(jìn)程/線程綁定到特定cpu核上運(yùn)行
概述
現(xiàn)在大家使用的基本上都是多核cpu,一般是4核的。平時(shí)應(yīng)用程序在運(yùn)行時(shí)都是由操作系統(tǒng)管理的。操作系統(tǒng)對(duì)應(yīng)用進(jìn)程進(jìn)行調(diào)度,使其在不同的核上輪番運(yùn)行。
對(duì)于普通的應(yīng)用,操作系統(tǒng)的默認(rèn)調(diào)度機(jī)制是沒有問題的。但是,當(dāng)某個(gè)進(jìn)程需要較高的運(yùn)行效率時(shí),就有必要考慮將其綁定到單獨(dú)的核上運(yùn)行,以減小由于在不同的核上調(diào)度造成的開銷。
把某個(gè)進(jìn)程/線程綁定到特定的cpu核上后,該進(jìn)程就會(huì)一直在此核上運(yùn)行,不會(huì)再被操作系統(tǒng)調(diào)度到其他核上。但綁定的這個(gè)核上還是可能會(huì)被調(diào)度運(yùn)行其他應(yīng)用程序的。
操作系統(tǒng)對(duì)多核cpu的調(diào)度
目前windows和linux都支持對(duì)多核cpu進(jìn)行調(diào)度管理。
- 軟件開發(fā)在多核環(huán)境下的核心是多線程開發(fā)。
- 這個(gè)多線程不僅代表了軟件實(shí)現(xiàn)上多線程,要求在硬件上也采用多線程技術(shù)。
多核操作系統(tǒng)的關(guān)注點(diǎn)在于進(jìn)程的分配和調(diào)度。進(jìn)程的分配將進(jìn)程分配到合理的物理核上,因?yàn)椴煌暮嗽诠蚕硇院蜌v史運(yùn)行情況都是不同的。
有的物理核能夠共享二級(jí)cache,而有的卻是獨(dú)立的。
如果將有數(shù)據(jù)共享的進(jìn)程分配給有共享二級(jí)cache的核上,將大大提升性能;反之,就有可能影響性能。
進(jìn)程調(diào)度會(huì)涉及實(shí)時(shí)性、負(fù)載均衡等問題,目前研究的熱點(diǎn)問題主要集中在以下方面:
- 程序的并行開發(fā)設(shè)計(jì)
- 多進(jìn)程的時(shí)間相關(guān)性
- 任務(wù)的分配和調(diào)度
- 緩存的錯(cuò)誤共享
- 一致性訪問問題
- 進(jìn)程間通信
- 多處理器核內(nèi)部資源競爭
多進(jìn)程和多線程在cpu核上運(yùn)行時(shí)情況如下:
- 每個(gè) CPU 核運(yùn)行一個(gè)進(jìn)程的時(shí)候,由于每個(gè)進(jìn)程的資源都獨(dú)立,所以 CPU 核心之間切換的時(shí)候無需考慮上下文
- 每個(gè) CPU 核運(yùn)行一個(gè)線程的時(shí)候,有時(shí)線程之間需要共享資源,所以這些資源必須從 CPU 的一個(gè)核心被復(fù)制到另外一個(gè)核心,這會(huì)造成額外的開銷
綁定進(jìn)程到cpu核上運(yùn)行
查看cpu有幾個(gè)核
使用cat /proc/cpuinfo
查看cpu信息,如下兩個(gè)信息:
processor
,指明第幾個(gè)cpu處理器cpu cores
,指明每個(gè)處理器的核心數(shù)
也可以使用系統(tǒng)調(diào)用sysconf獲取cpu核心數(shù):
#include <unistd.h> int sysconf(_SC_NPROCESSORS_CONF);/* 返回系統(tǒng)可以使用的核數(shù),但是其值會(huì)包括系統(tǒng)中禁用的核的數(shù)目,因 此該值并不代表當(dāng)前系統(tǒng)中可用的核數(shù) */ int sysconf(_SC_NPROCESSORS_ONLN);/* 返回值真正的代表了系統(tǒng)當(dāng)前可用的核數(shù) */ /* 以下兩個(gè)函數(shù)與上述類似 */ #include <sys/sysinfo.h> int get_nprocs_conf (void);/* 可用核數(shù) */ int get_nprocs (void);/* 真正的反映了當(dāng)前可用核數(shù) */
我使用的是虛擬機(jī),有2個(gè)處理器,每個(gè)處理器只有一個(gè)核,等同于一個(gè)處理器兩個(gè)核心。
使用taskset指令
- 獲取進(jìn)程pid
-> % ps PID TTY TIME CMD 2683 pts/1 00:00:00 zsh 2726 pts/1 00:00:00 dgram_servr 2930 pts/1 00:00:00 ps
- 查看進(jìn)程當(dāng)前運(yùn)行在哪個(gè)cpu上
-> % taskset -p 2726 pid 2726's current affinity mask: 3
顯示的十進(jìn)制數(shù)字3轉(zhuǎn)換為2進(jìn)制為最低兩個(gè)是1,每個(gè)1對(duì)應(yīng)一個(gè)cpu,所以進(jìn)程運(yùn)行在2個(gè)cpu上。
- 指定進(jìn)程運(yùn)行在cpu1上
-> % taskset -pc 1 2726 pid 2726's current affinity list: 0,1 pid 2726's new affinity list: 1
注意,cpu的標(biāo)號(hào)是從0開始的,所以cpu1表示第二個(gè)cpu(第一個(gè)cpu的標(biāo)號(hào)是0)。
至此,就把應(yīng)用程序綁定到了cpu1上運(yùn)行,查看如下:
-> % taskset -p 2726 pid 2726's current affinity mask: 2
- 啟動(dòng)程序時(shí)綁定cpu
#啟動(dòng)時(shí)綁定到第二個(gè)cpu -> % taskset -c 1 ./dgram_servr& [1] 3011 #查看確認(rèn)綁定情況 -> % taskset -p 3011 pid 3011's current affinity mask: 2
使用sched_setaffinity系統(tǒng)調(diào)用
sched_setaffinity可以將某個(gè)進(jìn)程綁定到一個(gè)特定的CPU。
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include <sched.h> /* 設(shè)置進(jìn)程號(hào)為pid的進(jìn)程運(yùn)行在mask所設(shè)定的CPU上 * 第二個(gè)參數(shù)cpusetsize是mask所指定的數(shù)的長度 * 通常設(shè)定為sizeof(cpu_set_t) * 如果pid的值為0,則表示指定的是當(dāng)前進(jìn)程 */ 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所指示的進(jìn)程的CPU位掩碼,并將該掩碼返回到mask所指向的結(jié)構(gòu)中 */
- 實(shí)例
#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 THREAD_MAX_NUM 200 //1個(gè)CPU內(nèi)的最多進(jìn)程數(shù) int num=0; //cpu中核數(shù) void* threadFun(void* arg) //arg 傳遞線程標(biāo)號(hào)(自己定義) { cpu_set_t mask; //CPU核的集合 cpu_set_t get; //獲取在集合中的CPU int *a = (int *)arg; int i; printf("the thread is:%d\n",*a); //顯示是第幾個(gè)線程 CPU_ZERO(&mask); //置空 CPU_SET(*a,&mask); //設(shè)置親和力值 if (sched_setaffinity(0, sizeof(mask), &mask) == -1)//設(shè)置線程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 (i = 0; i < num; i++) { if (CPU_ISSET(i, &get))//判斷線程與哪個(gè)CPU有親和力 { printf("this thread %d is running processor : %d\n", i,i); } } return NULL; } int main(int argc, char* argv[]) { int tid[THREAD_MAX_NUM]; int i; pthread_t thread[THREAD_MAX_NUM]; num = sysconf(_SC_NPROCESSORS_CONF); //獲取核數(shù) if (num > THREAD_MAX_NUM) { printf("num of cores[%d] is bigger than THREAD_MAX_NUM[%d]!\n", num, THREAD_MAX_NUM); return -1; } printf("system has %i processor(s). \n", num); for(i=0;i<num;i++) { tid[i] = i; //每個(gè)線程必須有個(gè)tid[i] pthread_create(&thread[i],NULL,threadFun,(void*)&tid[i]); } for(i=0; i< num; i++) { pthread_join(thread[i],NULL);//等待所有的線程結(jié)束,線程為死循環(huán)所以CTRL+C結(jié)束 } return 0; }
- 運(yùn)行結(jié)果
-> % ./a.out
system has 2 processor(s).
the thread is:0
the thread is:1
this thread 0 is running processor : 0
this thread 1 is running processor : 1
綁定線程到cpu核上運(yùn)行
綁定線程到cpu核上使用pthread_setaffinity_np函數(shù),其原型定義如下:
#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.
各參數(shù)的意義與sched_setaffinity相似。
- 實(shí)例
#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; 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); }
- 運(yùn)行結(jié)果
-> % ./a.out
Set returned by pthread_getaffinity_np() contained:
CPU 0
CPU 1
總結(jié)
可以使用多種方法把進(jìn)程/線程指定到特定的cpu核上運(yùn)行。
在具體使用中,要根據(jù)使用場景和需求決定使用何種方式。個(gè)人認(rèn)為,重要的一步還是要先確定是否要使用把線程綁定到核心的方式。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
centos7系統(tǒng)nginx服務(wù)器下phalcon環(huán)境搭建方法詳解
這篇文章主要介紹了centos7系統(tǒng)nginx服務(wù)器下phalcon環(huán)境搭建方法,結(jié)合具體實(shí)例形式詳細(xì)分析了centos7的nginx服務(wù)器搭建phalcon的具體操作步驟與相關(guān)設(shè)置技巧,需要的朋友可以參考下2019-09-09Linux下制作給ARM開發(fā)板使用的文件系統(tǒng)
今天小編就為大家分享一篇關(guān)于Linux下制作給ARM開發(fā)板使用的文件系統(tǒng),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12