如題
你要先確認該程式有用到支援CPU Affinity (CPU親合力)的程式碼。
https://hackgrass.blogspot.com/2016/10/cpu-affinity-cpu.html
CPU Affinity (CPU親合力)
一. 定義:
CPU Affinity(親合力),係指在linux 中,process 想儘量長時間運行在某個指定的CPU上 ,且不被轉移至其他CPU 的傾向性。
二. 函式 :
CPU親合力用一個 cpu_set_t 型態來產生一CPU集合,下面討論函式:
cpu_set_t cpuset;
CPU_ZERO(&cpuset) :清空給定變數cpuset 中的CPU set。
CPU_SET(n, &cpuset):將CPU n(0~n)加進給定變數cpuset集合中。
CPU_CLR(n, &cpuset):將CPU n 從給定變數cpuset集合中移除。
CPU_ISSET(n, &cpuset):檢查CPU n是否存在於這個cpuset集合中.
以下為最重要的兩個函式:
sched_setaffinity(pid_t pid, unsigned int cpusetsize, cpu_set_t *cpuset) ;
該函式將process為pid,丟進名為cpuset 的CPU set中執行。
cpusetsize 即為cpuset的長度,通常設定為sizeof(cpu_set_t)。
若pid給予0,則表示目前的process。
sched_getaffinity(pid_t pid, unsigned int cpusetsize, cpu_set_t *cpuset) ;
取得指定pid的process可以運行在哪些cpu中。
三.pthread
若我們討論的範疇為thread對於CPU的affinity時,我們會改用以下兩個函式:
pthread_setaffinity_np(pthread_t thread,size_t cpusetsize,const cpu_set_t *cpuset);
參數差別只在上一個是pid(process),這個是tid(thread)。通常我們會使用pthread_self()來表示目前的thread。
回傳值為一int型態;若綁定成功時,會回傳0。
int pthread_getaffinity_np(pthread_t thread,size_t cpusetsize, cpu_set_t *cpuset);
1. 先用 lscpu -e 列出所有的 core, MAXMHZ較小的即為 e-core, 以此例來說, 0-15為p-core, 16-23為e-core
2. 使用 taskset 啟動你的程式, 例如 "taskset -c 0-15 你的執行擋", 如此就只會用p-core執行你的程式
我用"7z b" (7zip benchmark)在12900K上驗證過, 執行 "7z b" 會在System Monitor上看到24 core全滿, 但用
"taskset -c 0-15 7z b" 就只會看到前16 core會滿. 後面8 core idle
內文搜尋

X