導航:首頁 > 操作系統 > linux進程綁定cpu

linux進程綁定cpu

發布時間:2022-09-27 03:37:27

linux下怎麼解除中斷和cpu綁定

在多 CPU 的環境中,還有一個中斷平衡的問題,比如,網卡中斷會教給哪個 CPU 處理,這個參數控制哪些 CPU 可以綁定 IRQ 中斷。其中的 {number} 是對應設備的中斷編號,可以用下面的命令找出: cat /proc/interrupt 比如,一般 eth0 的 IRQ

㈡ linux 單進程 跨多cpu

每個進程可以創建N個線程

㈢ 如何在Ubuntu中綁定CPU進程

現在科技在不斷發展現在多CPU的趨勢越來越大了. 有時候為了更好地操作機器, 需要將某個進程綁定到具體的CPU上去。大家可能不能理解將進程綁定到CPU中運行是什麼意思,簡單的說就是進程/線程與cpu綁定,最直觀的好處就是提高了cpu cache的命中率,從而減少內存訪問損耗,提高程序的速度,將普通進程變成核心進程。下面小編就像大家介紹在Ubuntu中怎麼綁定CPU進程,Ubuntu(烏班圖)是一個以桌面應用為主的Linux操作系統,和小編一起學習吧。

在Ubuntu中綁定CPU進程的方法

taskset -cp 《CPU ID | CPU IDs》 《Process ID》

下面用一個簡單的例子來說明怎樣做到。

1. CPU利用率達100%的樣例代碼:

class Test {

public static void main(String args[]) {

int i = 0;

while (true) {

i++;

}

}

}

2. 編譯並運行上面的樣例代碼

# javac Test.java

# java Test &

[1] 26531

3. 使用htop命令查看CPU的利用率

如果未安裝htop工具,執行下面的命令:

# apt-get install htop

Reading package lists... Done

Building dependency tree

Reading state information... Done

The following NEW packages will be installed:

htop

0 upgraded, 1 newly installed, 0 to remove and 41 not upgraded.

Need to get 66.9 kB of archives.

After this operation, 183 kB of additional disk space will be used.

Get:1 http://mirrors.163.com/ubuntu/ precise/universe htop amd64 1.0.1-1 [66.9 kB]

Fetched 66.9 kB in 0s (163 kB/s)

Selecting previously unselected package htop.

(Reading database ... 57100 files and directories currently installed.)

Unpacking htop (from .../htop_1.0.1-1_amd64.deb)...

Processing triggers for man-db ...

Setting up htop (1.0.1-1)...

安裝完成後,執行命令:

# htop

上面的視圖可以看到,CPU2的利用率達到100%,且這個進程有可能被分配到其它CPU核上運行,這個分配是不定的。

4. 進程綁定CPU核

運行以下命令,把此Java進程(進程ID號為26502)永久的分配給5號CPU核(CPU核號從0開始計算,因此序號4指的是5號CPU核)

# taskset -cp 5 26531

pid 26531『s current affinity list: 0-7

pid 26531』s new affinity list: 5

從上面的視圖中可以看到6號CPU核的利用率為100%。

隨著CPU核的多個化,這樣的綁定方法也是一樣的,無論綁定哪個CPU核都能啟動同樣的效果,相信大家都追求運行的高速度,趕快來學習綁定CPU進程的方法吧!

㈣ linux下把進程/線程綁定到特定cpu核上運行

你那個是系統下把CPU的核說釘在五河以下是比較好的,因為吧和內心壓力非常大,發熱量非常大。

㈤ Linux 如何綁定指定線程在某個固定CPU上

大概的介紹一下Linux 的指定CPU運行,包括進程和線程。linux下的top命令是可以查看當前的cpu的運行狀態,按1可以查看系統有多少個CPU,以及每個CPU的運行狀態。
可是如何查看線程的CPU呢?top
-Hp pid,pid就是你當前程序的進程號,如果是多線程的話,是可以查看進程內所有線程的CPU和內存使用情況。

pstree可以查看主次線程,同樣的pstree -p pid。可以查看進程的線程情況。

taskset這個其實才是重點,可以查看以及設置當前進程或線程運行的CPU(設置親和力)。

taskset -pc pid,查看當前進程的cpu,當然有的時候不只是一個,taskset -pc cpu_num pid ,cpu_num就是設置的cpu。

這樣的話基本的命令和操作其實大家都知道了,接下來就是在代碼中完成這些操作,並通過命令去驗證代碼的成功率。

進程制定CPU運行:

[cpp] view plain
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/sysinfo.h>
#include<unistd.h>
#define __USE_GNU
#include<sched.h>
#include<ctype.h>
#include<string.h>

int main(int argc, char* argv[])
{
//sysconf獲取有幾個CPU
int num = sysconf(_SC_NPROCESSORS_CONF);
int created_thread = 0;
int myid;
int i;
int j = 0;

//原理其實很簡單,就是通過cpu_set_t進行位與操作
cpu_set_t mask;
cpu_set_t get;

if (argc != 2)
{
printf("usage : ./cpu num\n");
exit(1);
}

myid = atoi(argv[1]);

printf("system has %i processor(s). \n", num);

//先進行清空,然後設置掩碼
CPU_ZERO(&mask);
CPU_SET(myid, &mask);

//設置進程的親和力
if (sched_setaffinity(0, sizeof(mask), &mask) == -1)
{
printf("warning: could not set CPU affinity, continuing...\n");
}
while (1)
{

CPU_ZERO(&get);
//獲取當前進程的親和力
if (sched_getaffinity(0, sizeof(get), &get) == -1)
{
printf("warning: cound not get cpu affinity, continuing...\n");
}
for (i = 0; i < num; i++)
{
if (CPU_ISSET(i, &get))
{
printf("this process %d is running processor : %d\n",getpid(), i);
}
}
}
return 0;
}

進程設置CPU運行,其實只能是單線程。多線程設定CPU如下:

[cpp] view plain
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sched.h>

void *myfun(void *arg)
{
cpu_set_t mask;
cpu_set_t get;
char buf[256];
int i;
int j;
//同樣的先去獲取CPU的個數
int num = sysconf(_SC_NPROCESSORS_CONF);
printf("system has %d processor(s)\n", num);

for (i = 0; i < num; i++) {
CPU_ZERO(&mask);
CPU_SET(i, &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");
}
for (j = 0; j < num; j++)
{
if (CPU_ISSET(j, &get))
{
printf("thread %d is running in processor %d\n", (int)pthread_self(), j);
}
}
j = 0;
while (j++ < 100000000) {
memset(buf, 0, sizeof(buf));
}
}
pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
pthread_t tid;
if (pthread_create(&tid, NULL, (void *)myfun, NULL) != 0)
{
fprintf(stderr, "thread create failed\n");
return -1;
}
pthread_join(tid, NULL);
return 0;
}

㈥ linux下cpu的核綁定和隔離有什麼不同

額 剛剛編輯完,結果沒保存,然後·····只能重新總結一份。
我這個是在啟動虛擬機之後,在虛擬機中跑DPDK,測試結果很不理想,然後我的領導說可以做CPU的隔離核綁定,然後做了之後發現確實效果有所提升。所以寫一下小結。僅供大家參考。

1、首先創建隔離核,在系統啟動的時候在INTEL_IOMMU=OFF那一行最後添加上isolcpus=2,3,4,5,6 隔離出5個核
2、系統啟動,檢查host上是否隔離成功,命令如下:
# ps –eLo ruser,pid,ppid,lwp,psr,args | awk 『{if($5==1) print $0}』
# ps –eLo ruser,pid,ppid,lwp,psr,args | awk 『{if($5==2) print $0}』
# ps –eLo ruser,pid,ppid,lwp,psr,args | awk 『{if($5==3) print $0}』
# ps –eLo ruser,pid,ppid,lwp,psr,args | awk 『{if($5==4) print $0}』通過查看線程確定是否隔離,如果隔離成功,則只有幾個線程。
3、啟動虛擬機之後,查看qemu的線程
# ps –eLo ruser,pid,ppid,lwp,psr,args | grep qemu | grep –v grep
4、綁定qemu的進程,綁定核
# taskset –p 0x4 28423
# taskset –p 0x8 28424
5、查看QEMU綁定是否生效
# ps –eLo ruser,pid,ppid,lwp,psr,args | grep qemu | grep –v grep
6、查看cpu2/3/4/5上運行的線程
# ps –eLo ruser,pid,ppid,lwp,psr,args | awk 『{if($5==2) print $0}』

沒什麼技術含量,僅供大家參考。

㈦ 如何指定CPU只運行特定任務,linux中斷interrupt

cpuset 允許把所有進程echo到一個cpuset目錄中,與指定的cpu綁定。

The following script which is a starting point for investigating willmove all the processes to a specific cpuset with one cpu.


if[!-d/sys/fs/cgroup];then
echo"cgroupnotsupportedbythekernel"
else
mkdir/sys/fs/cgroup/cpuset
mount-tcgroup-ocpusetcpuset/sys/fs/cgroup/cpuset
echo1>/sys/fs/cgroup/cpuset/cgroup.clone_children
mkdir/sys/fs/cgroup/cpuset/cpu0
mkdir/sys/fs/cgroup/cpuset/cpu1

#assignacpuforcgroupcpu0
echo0>/sys/fs/cgroup/cpuset/cpu0/cpuset.cpus

#moveoutselftothisnewcgroup
echo$$>/sys/fs/cgroup/cpuset/cpu0/tasks
foriin$(cat/sys/fs/cgroup/cpuset/tasks);do
echo$i>/sys/fs/cgroup/cpuset/cpu0/tasks||echo"failedtoaddpid$i/$(cat/proc/$i/comm)"
done

#assignacpuforcgroupcpu1
echo1>/sys/fs/cgroup/cpuset/cpu1/cpuset.cpus

#
#totheothercgroupwon'tuseit
echo1>/sys/fs/cgroup/cpuset/cpu1/cpuset.cpu_exclusive
fi

把系統中的進程與CPU0綁定,然後把CPU1設置 exclusive屬性,這樣其它cgroup不會使用這個CPU。

開啟stress壓力測試


在此基礎上再開啟一個CPUhot:

可見新啟動的任務不會佔用CPU1。

將CPUhot進程與 CPU1綁定:

此時觀察CPU使用率:

另外還有一個問題就是怎樣禁止一個中斷打斷cpu?

內核提供了中斷的affinity,但要使用這個還需要關閉 irq balancer 進程。

如下腳本可以設置中斷的cpu affinity。


for i in $(find /proc/irq -name "smp_affinity"); do echo 1 > $i; done

以上腳本把所有中斷的CPU Affinity都設置為CPU0。

這樣在/proc/irq目錄下的每一個中斷其CPU Affinity都被設置為CPU0。

此時仍有一系列的中斷會打斷CPU1:

Single function call interrupts

Local timer interrupts

另外要讓中斷的CPU Affinity起作用,irq balance 服務必須被關閉。但這樣中斷負載平衡就被打斷,能不能修改irq balance 代碼,讓其在規定的若干個CPU核心上負責中斷平衡

㈧ linux查看中斷綁定在哪個cpu

cpuset 允許把所有進程echo到一個cpuset目錄中,與指定的cpu綁定。 The following script which is a starting point for investigating willmove all the processes to a specific cpuset with one cpu. if [ ! -d /sys/fs/cgroup ]; then echo...

㈨ 如何指定CPU只運行特定任務,linux中斷interrupt

cpuset 允許把所有進程echo到一個cpuset目錄中,與指定的cpu綁定。
The following script which is a starting point for investigating willmove all the processes to a specific cpuset with one cpu.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

if [ ! -d /sys/fs/cgroup ]; then
echo "cgroup not supported by the kernel"
else
mkdir /sys/fs/cgroup/cpuset
mount -t cgroup -ocpuset cpuset /sys/fs/cgroup/cpuset
echo 1 > /sys/fs/cgroup/cpuset/cgroup.clone_children
mkdir /sys/fs/cgroup/cpuset/cpu0
mkdir /sys/fs/cgroup/cpuset/cpu1

# assign a cpu for cgroup cpu0
echo 0 > /sys/fs/cgroup/cpuset/cpu0/cpuset.cpus

# move outself to this new cgroup
echo $$ > /sys/fs/cgroup/cpuset/cpu0/tasks
for i in $(cat /sys/fs/cgroup/cpuset/tasks); do
echo $i > /sys/fs/cgroup/cpuset/cpu0/tasks || echo "failed to add pid $i/$(cat /proc/$i/comm)"
done

# assign a cpu for cgroup cpu1
echo 1 > /sys/fs/cgroup/cpuset/cpu1/cpuset.cpus

# make cpu1 exclusive so processes belonging
# to the other cgroup won't use it
echo 1 > /sys/fs/cgroup/cpuset/cpu1/cpuset.cpu_exclusive
fi

把系統中的進程與CPU0綁定,然後把CPU1設置 exclusive屬性,這樣其它cgroup不會使用這個CPU。
開啟stress壓力測試

在此基礎上再開啟一個CPUhot:
可見新啟動的任務不會佔用CPU1。
將CPUhot進程與 CPU1綁定:
此時觀察CPU使用率:
另外還有一個問題就是怎樣禁止一個中斷打斷cpu?
內核提供了中斷的affinity,但要使用這個還需要關閉 irq balancer 進程。
如下腳本可以設置中斷的cpu affinity。

for i in $(find /proc/irq -name "smp_affinity"); do echo 1 > $i; done
以上腳本把所有中斷的CPU Affinity都設置為CPU0。
這樣在/proc/irq目錄下的每一個中斷其CPU Affinity都被設置為CPU0。
此時仍有一系列的中斷會打斷CPU1:
Single function call interrupts
Local timer interrupts

另外要讓中斷的CPU Affinity起作用,irq balance 服務必須被關閉。但這樣中斷負載平衡就被打斷,能不能修改irq balance 代碼,讓其在規定的若干個CPU核心上負責中斷平衡

㈩ 如何將一個進程(線程)綁定到一個固定的CPU

第一種:linux的shell命令行方式,命令名字為taskset。第二種就是代碼實現級別的了,pthread_setaffinity_np和sched_setaffinity函數介面。
第一種方式我已經驗證過了,確實可行。同時驗證了我心中的疑問:如果將某個線程綁定到某個物理核上之後,在此線程運行結束前,會不會有別的線程被調度到此物理核上執行? 寫了一個死循環驗證了下,發現綁定之後是不會調度別的線程在此核上運行的!(肉眼觀察的,時不時觀察下,沒發現別的線程在此核上執行;對比了下沒有綁定的情況,會發現過段時間此線程就會被調度到別的核心上執行)
此種方式有個問題,就是只有線程運行起來後才會被綁定到某個核上,不夠及時。
具體的方式為:
1.首先根據系統的差別運行如下安裝命令:
sudo apt-get install util-linux (Debian,Ubuntu or Linux Mint)
sudo yum install util-linux (Fedora,CentOS or RHEL)
2.相關命令的使用:
2.1 使用命令 taskset -p <PID> 來獲得此Process的 CPU affinity。
eg: taskset -p 2915 ------> pid 2915's current affinity mask:ff; ff=="1111 1111",沒一個1代表一個核,共8個核,能用的核數也為8個核。
2.2 使用命令 taskset -cp <PID> 可獲得數字形式的CPU affinity。
eg: taskset -cp 2915 ------> pid 2915's current affinity list: 0--7。
接下來為將進程pin到某個核上的命令;
2.3 taskset -p <COREMASK> <PID>
eg:taskset -p 0x11 9030 ------>pid 9030's current affinity mask: ff , pid 9030's new affinity mask: 11 。意思就是將此進程綁定到了CPU core 0 and 4。
2.4 taskset -cp <CORE-LIST> <PID>
eg:taskset -cp 0,4 9030 ------>the same as below.
With "-c" option, you can specify a list of numeric CPU core IDs separated by commas, or even include ranges (e.g., 0,2,5,6-10).

2.5 taskset <COREMASK> <EXECUTABLE>
eg: taskset 0x1 xxxx ----->"xxxx" represented the name of one program.
另外:參考文章最後的位置說到,綁定到此物理核之後,別的進程(線程)還可以調度到此核上執行,但是沒說綁定的這個線程沒執行完之前是否會被別的線程擠掉。根據我的觀察是不會被擠掉,這我在文章的開頭也有提到。

閱讀全文

與linux進程綁定cpu相關的資料

熱點內容
我的世界紅藍隊比賽是什麼伺服器 瀏覽:736
法國兩個男人火車上吃母乳 瀏覽:812
水生電影完整版 瀏覽:23
演化博弈pdf 瀏覽:489
二手伺服器主機怎麼用 瀏覽:158
小女生電影1980年法國 瀏覽:357
楚喬傳txt下載 瀏覽:847
秀妍和楊偉發生車禍韓國電影 瀏覽:559
jipx 中文字幕 瀏覽:170
港版武則天電影2000版 瀏覽:312
美娜妍希韓國電影 瀏覽:783
四級大片名字 瀏覽:716
javapanel大小 瀏覽:929
程序員徵友平台 瀏覽:464
程序里添加編譯時間 瀏覽:47
BL視頻APP 瀏覽:492
蕭九作品集txt 瀏覽:361
反應剛剛解放時期諜戰片 瀏覽:970
和美女被困隧道的七天 瀏覽:802