Linux編程之《進程/線程綁定CPU》,多進程多線程編程
Intro
-----
通常我們在編寫伺服器代碼時,可以通過將當前進程綁定到固定的CPU核心或者線程綁定到固定的CPU核心來提高系統發送器的效率來提高程式執行的效率,下面將完整代碼貼上。
```
/************************************************
* 該常式講解了進程、線程綁定到固定的cpu核心上運行
* 來提高程式運行效率
************************************************/
#include <unistd.h>
#ifndef __USE_GNU
#define __USE_GNU // 為了使用SET_SET()等宏定義,但unistd.h裡面好像已經定義了
#endif
#include <sched.h>
#include <pthread.h>
#include <stdio.h>
#include <vector>
unsigned int systemCPUNum()
{
// _SC_NPROCESSORS_CONF的值為CPU個數,基於0開始編號
return sysconf(_SC_NPROCESSORS_CONF);
}
bool currentProcessAffinity(std::vector<unsigned int>& runningCPUVector)
{
cpu_set_t cpuSet;
// 清空一個CPU集合
CPU_ZERO(&cpuSet);
// 得到指定進程ID綁定到哪個CPU
int ret = sched_getaffinity(0, // 0代表當前進程
sizeof(cpuSet),
&cpuSet);
if (ret < 0)
{
return false;
}
unsigned int cpuNum = systemCPUNum();
runningCPUVector.clear();
for (unsigned int i = 0; i < cpuNum; ++i)
{
// 檢查一個CPU號是否在一個集合中
if (CPU_ISSET(i, &cpuSet))
{
runningCPUVector.push_back(i);
}
}
return true;
}
bool setCurrentProcessAffinity(const std::vector<unsigned int>& needBindCPUVector)
{
cpu_set_t cpuSet;
// 清空一個CPU集合
CPU_ZERO(&cpuSet);
for (auto& iter : needBindCPUVector)
{
CPU_SET(iter, &cpuSet);
}
// 將指定進程ID綁定到CPU
int ret = sched_setaffinity(0, // 0代表當前進程
sizeof(cpuSet),
&cpuSet);
if (ret < 0)
{
return false;
}
return true;
}
bool currentThreadAffinity(std::vector<unsigned int>& runningCPUVector)
{
cpu_set_t cpuSet;
// 清空一個CPU集合
CPU_ZERO(&cpuSet);
// 得到指定線程ID綁定到哪個CPU
int ret = pthread_getaffinity_np(pthread_self(),
sizeof(cpuSet),
&cpuSet);
if (ret < 0)
{
return false;
}
unsigned int cpuNum = systemCPUNum();
runningCPUVector.clear();
for (unsigned int i = 0; i < cpuNum; ++i)
{
// 檢查一個CPU號是否在一個集合中
if (CPU_ISSET(i, &cpuSet))
{
runningCPUVector.push_back(i);
}
}
return true;
}
bool setCurrentThreadAffinity(const std::vector<unsigned int>& needBindCPUVector)
{
cpu_set_t cpuSet;
// 清空一個CPU集合
CPU_ZERO(&cpuSet);
for (auto& iter : needBindCPUVector)
{
CPU_SET(iter, &cpuSet);
}
// 將指定線程ID綁定到CPU
int ret = pthread_setaffinity_np(pthread_self(),
sizeof(cpuSet),
&cpuSet);
if (ret < 0)
{
return false;
}
return true;
}
int main()
{
printf("*****Process bind CPU sample*****\n");
unsigned int cpuNum = systemCPUNum();
printf("Current system has %u CPU(s)\n", cpuNum);
std::vector<unsigned int> runningCPUVector;
if (!currentProcessAffinity(runningCPUVector))
{
printf("Get current process was bound witch CPU failed\n");
return 1;
}
for (auto& iter : runningCPUVector)
{
printf("Current process is running at %u CPU\n", iter);
}
std::vector<unsigned int> needBindCPUVector {0, 2};
if (!setCurrentProcessAffinity(needBindCPUVector))
{
printf("Current process bind CPU failed\n");
return 1;
}
printf("Current process bind CPU success\n");
runningCPUVector.clear();
if (!currentProcessAffinity(runningCPUVector))
{
printf("Get current process was bound witch CPU failed\n");
return 1;
}
for (auto& iter : runningCPUVector)
{
printf("Current process is running at %u CPU\n", iter);
}
printf("\n*****Thread bind CPU sample*****\n");
runningCPUVector.clear();
if (!currentThreadAffinity(runningCPUVector))
{
printf("Get current thread was bound witch CPU failed\n");
return 1;
}
for (auto& iter : runningCPUVector)
{
printf("Thread %lu is running at %u CPU\n", pthread_self(), iter);
}
needBindCPUVector.clear();
needBindCPUVector.push_back(1);
if (!setCurrentThreadAffinity(needBindCPUVector))
{
printf("Current thread bind CPU failed\n");
return 1;
}
printf("Thread %lu bind CPU success\n", pthread_self());
runningCPUVector.clear();
if (!currentThreadAffinity(runningCPUVector))
{
printf("Get current thread was bound witch CPU failed\n");
return 1;
}
for (auto& iter : runningCPUVector)
{
printf("Thread %lu is running at %u CPU\n", pthread_self(), iter);
}
return 0;
}
```
程式執行的輸出結果:
```
*****Process bind CPU sample*****
Current system has 4 CPU(s)
Current process is running at 0 CPU
Current process is running at 1 CPU
Current process is running at 2 CPU
Current process is running at 3 CPU
Current process bind CPU success
Current process is running at 0 CPU
Current process is running at 2 CPU
*****Thread bind CPU sample*****
Thread 139871129114432 is running at 0 CPU
Thread 139871129114432 is running at 2 CPU
Thread 139871129114432 bind CPU success
Thread 139871129114432 is running at 1 CPU
```
該例子的github地址:https://github.com/chxuan/samples/blob/master/BindCPU/BindCPU.cpp