Linux中線程與CPU核的綁定


最近在對項目進行性能優化,由於在多核平台上,所以了解了些進程、線程綁定cpu核的問題,在這里將所學記錄一下。

    不管是線程還是進程,都是通過設置親和性(affinity)來達到目的。對於進程的情況,一般是使用sched_setaffinity這個函數來實現,網上講的也比較多,這里主要講一下線程的情況。
    與進程的情況相似,線程親和性的設置和獲取主要通過下面兩個函數來實現:
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_set_t這個結構體了。這個結構體的理解類似於select中的fd_set,可以理解為cpu集,也是通過約定好的宏來進行清除、設置以及判斷:
//初始化,設為空
      void CPU_ZERO (cpu_set_t *set); 
      //將某個cpu加入cpu集中 
       void CPU_SET (int cpu, cpu_set_t *set); 
       //將某個cpu從cpu集中移出 
       void CPU_CLR (int cpu, cpu_set_t *set); 
       //判斷某個cpu是否已在cpu集中設置了 
       int CPU_ISSET (int cpu, const cpu_set_t *set); 
       cpu集可以認為是一個掩碼,每個設置的位都對應一個可以合法調度的 cpu,而未設置的位則對應一個不可調度的 CPU。換而言之,線程都被綁定了,只能在那些對應位被設置了的處理器上運行。通常,掩碼中的所有位都被置位了,也就是可以在所有的cpu中調度。       
      以下為測試代碼:

點擊(此處)折疊或打開

  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <pthread.h>
  7. #include <sched.h>
  8. void *myfun(void *arg)
  9. {
  10.     cpu_set_t mask;
  11.     cpu_set_t get;
  12.     char buf[256];
  13.     int i;
  14.     int j;
  15.     int num = sysconf(_SC_NPROCESSORS_CONF);
  16.     printf("system has %d processor(s)\n", num);
  17.     for (= 0; i < num; i++) {
  18.         CPU_ZERO(&mask);
  19.         CPU_SET(i, &mask);
  20.         if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) < 0) {
  21.             fprintf(stderr, "set thread affinity failed\n");
  22.         }
  23.         CPU_ZERO(&get);
  24.         if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) < 0) {
  25.             fprintf(stderr, "get thread affinity failed\n");
  26.         }
  27.         for (= 0; j < num; j++) {
  28.             if (CPU_ISSET(j, &get)) {
  29.                 printf("thread %d is running in processor %d\n", (int)pthread_self(), j);
  30.             }
  31.         }
  32.         j = 0;
  33.         while (j++ < 100000000) {
  34.             memset(buf, 0, sizeof(buf));
  35.         }
  36.     }
  37.     pthread_exit(NULL);
  38. }
  39. int main(int argc, char *argv[])
  40. {
  41.     pthread_t tid;
  42.     if (pthread_create(&tid, NULL, (void *)myfun, NULL) != 0) {
  43.         fprintf(stderr, "thread create failed\n");
  44.         return -1;
  45.     }
  46.     pthread_join(tid, NULL);
  47.     return 0;
  48. }
       這段代碼將使myfun線程在所有cpu中依次執行一段時間,在我的四核cpu上,執行結果為   :
       system has 4 processor(s)         
       thread 1095604544 is running in processor 0         
       thread 1095604544 is running in processor 1         
       thread 1095604544 is running in processor 2         
       thread 1095604544 is running in processor 3 
       在一些嵌入式設備中,運行的進程線程比較單一,如果指定進程線程運行於特定的cpu核,減少進程、線程的核間切換,有可能可以獲得更高的性能。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM