线程锁定CPU linux,linux 线程与CPU绑定
看到很多程序都是根據CPU個數來創建線程個數,當時很不理解他們之間的關系,請教了項目組的同事后才有了大致了解。
1. 相關系統函數
下面的函數可以通過man命令查詢到。
SYNOPSIS
#define _GNU_SOURCE
#include
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.
DESCRIPTION
The pthread_setaffinity_np() sets the CPU affinity mask of the thread thread to the CPU set pointed to by cpuset. If the call is successful, and the thread is
not currently running on one of the CPUs in cpuset, then it is migrated to one of those CPUs.
The pthread_getaffinity_np() function returns the CPU affinity mask of the thread thread in the buffer pointed to by cpuset.
For more details on CPU affinity masks, see sched_setaffinity(2). For a description of a set of macros that can be used to manipulate and inspect CPU sets, see
CPU_SET(3).
The argument cpusetsize is the length (in bytes) of the buffer pointed to by cpuset. Typically, this argument would be specified as sizeof(cpu_set_t). (It may
be some other value, if using the macros described in CPU_SET(3) for dynamically allocating a CPU set.)
RETURN VALUE
On success, these functions return 0; on error, they return a non-zero error number.
簡而言之,這兩個函數一個設置在哪個CPU核上運行,另一個獲取設置的參數(mask),既可以查詢出當前進程在運行在哪個核上
CPU_ZERO() Clears set, so that it contains no CPUs.
設置為空,沒有任何CPU
CPU_SET() Add CPU cpu to set.
將某個核加入CPU集合
CPU_CLR() Remove CPU cpu from set.
將某個核清理出CPU集合
CPU_ISSET() Test to see if CPU cpu is a member of set.
判斷某個核是否在CPU集合中設置。cpu集合可以認為是一個掩碼,每個設置的位都對應一個可以合法調度的 cpu,而未設置的位則對應一個不可調度的 CPU。換而言之,線程都被綁定了,只能在那些對應位被設置了的處理器上運行。通常,掩碼中的所有位都被置位了,也就是可以在所有的cpu中調度。
CPU_COUNT() Return the number of CPUs in set.
2.下面是我的測試代碼。
測試環境:
系統: SUSE 10
linux 內核版本:2.6.32.12
CPU: 8核
2.1 ?根據CPU核個數創建線程,并綁定
void *myfunWithMultiThread(void *arg)
{
cpu_set_t mask;
cpu_set_t get;
int i = 0;
int num = 0;
int cpuID = *(int *)arg;
CPU_ZERO(&mask);
CPU_SET(cpuID, &mask);
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
fprintf(stderr, "set thread affinity failed\n");
}
CPU_ZERO(&get);
if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) {
fprintf(stderr, "get thread affinity failed\n");
}
num = sysconf(_SC_NPROCESSORS_CONF);
for (i = 0; i < num; i++) {
if (CPU_ISSET(i, &get)) {
printf("thread %d is running in processor %d\n", (int)pthread_self(), i);
printf("Original setting is in processor %d\n\n", cpuID);
}
}
sleep(10);
}
int main(int argc, char *argv[])
{
pthread_t tid;
int num = 0;
int i =0;
num = sysconf(_SC_NPROCESSORS_CONF);
int id[16];
printf("System has %d processor(s),so create %d threads\n", num,num);
for(i = 0; i < num; i++)
{
id[i] = i;
if (pthread_create(&tid, NULL, (void *)myfunWithMultiThread, (void *)&id[i]) != 0)
{
fprintf(stderr, "thread create failed\n");
return -1;
}
}
pthread_join(tid, NULL);
return 0;
}
運行結果:
System has 8 processor(s),so create 8 threads
thread 1188759312 is running in processor 5
Original setting is in processor 5
thread 1205544720 is running in processor 3
Original setting is in processor 3
thread 1197152016 is running in processor 4
Original setting is in processor 4
thread 1230722832 is running in processor 0
Original setting is in processor 0
thread 1213937424 is running in processor 2
Original setting is in processor 2
thread 1222330128 is running in processor 1
Original setting is in processor 1
thread 1180366608 is running in processor 6
Original setting is in processor 6
thread 1171973904 is running in processor 7
Original setting is in processor 7
2.2 不綁定測試代碼
System has 8 processor(s),so create 8 threads
thread -196520176 is running in processor 0
thread -196520176 is running in processor 1
thread -196520176 is running in processor 2
thread -196520176 is running in processor 3
thread -196520176 is running in processor 4
thread -196520176 is running in processor 5
thread -196520176 is running in processor 6
thread -196520176 is running in processor 7
thread -221698288 is running in processor 0
thread -221698288 is running in processor 1
thread -221698288 is running in processor 2
thread -221698288 is running in processor 3
thread -221698288 is running in processor 4
thread -221698288 is running in processor 5
thread -221698288 is running in processor 6
thread -221698288 is running in processor 7
thread -213305584 is running in processor 0
thread -213305584 is running in processor 1
thread -213305584 is running in processor 2
thread -213305584 is running in processor 3
thread -213305584 is running in processor 4
thread -213305584 is running in processor 5
thread -213305584 is running in processor 6
thread -213305584 is running in processor 7
thread -230090992 is running in processor 0
thread -230090992 is running in processor 1
thread -230090992 is running in processor 2
thread -204912880 is running in processor 0
thread -204912880 is running in processor 1
thread -204912880 is running in processor 2
thread -238483696 is running in processor 0
thread -230090992 is running in processor 3
由以上結果可以看出,linux是不會默認對線程進行和CPU進行綁定的,如果有需要必須自己顯式的調用函數綁定。
3. 結論
線程與CPU進行綁定可以減少線程在不同核之間切換的開銷,但是要遵循一定的規則,并不是每個線程都與特定CPU綁定效率高。一般來說計算密集型的程序與CPU綁定與不綁定相比效果要好得多,但對于IO密集型的程序來說,影響不是太大。
總結
以上是生活随笔為你收集整理的线程锁定CPU linux,linux 线程与CPU绑定的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Python】浮点数计算时的不准确性以
- 下一篇: 计算机原理含汇编语言,计算机组成原理(含