Linux進程或線程綁定到CPU

Linux進程或線程綁定到CPU

爲了讓程序擁有更好的性能,有時候須要將進程或線程綁定到特定的CPU,這樣能夠減小調度的開銷和保護關鍵進程或線程。函數

進程綁定到CPU

Linux提供一個接口,能夠將進程綁定到特定的CPU性能

#include <sched.h>測試

int sched_setaffinity(pid_t pid, size_t cpusetsize, const cpu_set_t *mask);spa

int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);線程

參數code

pid:進程的id號,若是pid0,則表示本進程blog

cpusetsizemask的大小接口

mask:運行進程的CPU,能夠經過如下函數操做mask進程

#define CPU_SET(cpu, cpusetp) //設置cpuget

#define CPU_CLR(cpu, cpusetp) //刪除cpu

#define CPU_ISSET(cpu, cpusetp) //判斷cpu

#define CPU_ZERO(cpusetp) //初始化爲0

示例代碼

#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <sched.h>
 
void WasteTime()
{
    int abc = 10000000;
    while(abc--)
    {
        int tmp = 10000*10000;
    }
    sleep(1);

}

int main(int argc, char **argv)
{
    cpu_set_t mask;
    while(1)
    {
 
        CPU_ZERO(&mask);
        CPU_SET(0, &mask); 
        if (sched_setaffinity(0, sizeof(mask), &mask) < 0) {
            perror("sched_setaffinity");
        }
        WasteTime();
 
        CPU_ZERO(&mask);
        CPU_SET(1, &mask); 
        if (sched_setaffinity(0, sizeof(mask), &mask) < 0) {
            perror("sched_setaffinity");
        }
        WasteTime();
     
        CPU_ZERO(&mask);
        CPU_SET(2, &mask); 
        if (sched_setaffinity(0, sizeof(mask), &mask) < 0) {
            perror("sched_setaffinity");
        }
        WasteTime();
     
        CPU_ZERO(&mask);
        CPU_SET(3, &mask); 
        if (sched_setaffinity(0, sizeof(mask), &mask) < 0) {
            perror("sched_setaffinity");
        }
        WasteTime();
    }
}

 

測試

編譯以後運行程序,輸入命令top -p 進程id,輸入f,輸入j,輸入回車,能夠看到進程在cpu0123之間不停切換。

 

線程綁定到CPU

不單單進程能夠綁定到CPU,線程也能夠。Linux提供一個接口,能夠將線程綁定到特定的CPU

#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);

該接口與進程綁定到CPU的接口的使用方法基本一致。

當進程綁定到特定的CPU以後,線程仍是能夠綁定到其餘的CPU的,沒有衝突。

示例代碼

#include <stdio.h>
#include <math.h>
#include <pthread.h>
#include <unistd.h>
#include <sched.h>

void WasteTime()
{
    int abc = 10000000;
    while(abc--)
    {
        int tmp = 10000*10000;
    }
    sleep(1);

}

void *thread_func(void *param)
{
    cpu_set_t mask;
    while(1)
    {
         CPU_ZERO(&mask);
        CPU_SET(1, &mask); 

        if (pthread_setaffinity_np(pthread_self(), sizeof(mask),
            &mask) < 0) {
            perror("pthread_setaffinity_np");
        }
 
        WasteTime();

         CPU_ZERO(&mask);
        CPU_SET(2, &mask); 
        if (pthread_setaffinity_np(pthread_self(), sizeof(mask),
            &mask) < 0) {
            perror("pthread_setaffinity_np");
        }

        WasteTime();
    }
}
 
void *thread_func1(void *param)
{
    cpu_set_t mask;
    while(1)
    {
         CPU_ZERO(&mask);
        CPU_SET(3, &mask); 

        if (pthread_setaffinity_np(pthread_self(), sizeof(mask),
            &mask) < 0) {
            perror("pthread_setaffinity_np");
        }
 
        WasteTime();

         CPU_ZERO(&mask);
        CPU_SET(4, &mask); 
        if (pthread_setaffinity_np(pthread_self(), sizeof(mask),
            &mask) < 0) {
            perror("pthread_setaffinity_np");
        }

        WasteTime();
    }
}
 
int main(int argc, char *argv[])
{
    cpu_set_t mask;
     CPU_ZERO(&mask);
    CPU_SET(0, &mask); 
    if (sched_setaffinity(0, sizeof(mask), &mask) < 0) {
        perror("sched_setaffinity");
    }

    pthread_t my_thread;
 
    if (pthread_create(&my_thread, NULL, thread_func,
        NULL) != 0) {
        perror("pthread_create");
    }
    if (pthread_create(&my_thread, NULL, thread_func1,
        NULL) != 0) {
        perror("pthread_create");
    }
    while(1) { WasteTime(); }
    pthread_exit(NULL);

}

 

測試

編譯運行以後,輸入命令top -p 進程id,輸入f,輸入j,輸入回車,輸入H,能夠看到主線程一直保持在cpu0,一個線程在cpu12以前切換,另外一個線程在cpu34之間切換。

 

相關文章
相關標籤/搜索