linux应用如何进行cpu绑定

linux应用如何进行cpu绑定,第1张

1 安装taskset

$ yum install util-linux1

如果系统没有taskset命令,使用yum安装util-linux即可,这是一个工具集,其中包含了taskset命令。

2 查看应用的cpu亲和力(affinity)

$ taskset -p 14795pid 14795's current affinity mask: 3

$ taskset -cp 14795

pid 14795's current affinity list: 0,11234

示例中,查看进程14795的cpu亲和力,使用-p选项指定需要查询的进程号,默认打印的是一个十六进制数,如果使用-cp选项打印的是一个cpu列表,表示相应的cpu核。3的二进制形式是0011,相应的第0位和第1位都是1,对应了-cp打印的0和1,表示14795进程只能运行在cpu的第0个核和第1个核。

3 将应用绑定到指定的cpu运行

$ taskset -p 0x1 14795pid 14795's current affinity mask: 3pid 14795's new affinity mask: 1123

$ taskset -cp 0 14795pid 14795's current affinity list: 0,1

pid 14795's new affinity list: 0123

示例中,通过taskset命令重新设置了进程14795的cpu亲和力,前后2种方式设置效果一样,都表示进程14795只能运行在cpu的第0个核。因为-p指定的0x01二进制形式为0001,第0位是1,表示第0个cpu核。-cp直接指定了0,也表示第0个cpu核。

除了通过taskset命令绑定应用到指定的cpu上,也可以通过taskset命令启动应用,并指定应用运行的cpu,例如:

$ taskset 0x1 sleep 10000 &

[2] 14925$ taskset -p 14925pid 14925's current affinity mask: 1

$ taskset -cp 14925

pid 14925's current affinity list: 0123456

示例中,通过taskset启动应用(使用sleep命令模拟应用),并设置相应的cpu亲和力,即进程14925只能运行在cpu的第0个核。启动程序后查看进程的cpu亲和力,和启动时设置的相同。

另外,除了通过taskset命令实现cpu绑定,很多语言都提供了相应的api实现cpu绑定功能,例如c的sched_setaffinity和sched_getaffinity,python 3的os.sched_setaffinity和os.sched_getaffinity。

大概的介绍一下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 copy

#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 = 0i <numi++)

{

if (CPU_ISSET(i, &get))

{

printf("this process %d is running processor : %d\n",getpid(), i)

}

}

}

return 0

}

进程设置CPU运行,其实只能是单线程。多线程设定CPU如下:

[cpp] view plain copy

#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 = 0i <numi++) {

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 = 0j <numj++)

{

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

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/6170059.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-17
下一篇 2023-03-17

发表评论

登录后才能评论

评论列表(0条)

保存