一、概述
现在CPU基本都是多核CPU,一般4核,由操作系统调度,使应用轮番在不同的核上运行。对于特定进程或线程需要绑定到指定的核上运行。
1、使用命令查看CPU信息
通过(
cat /proc/cpuinfo
)命令查看CPU信息
root@analog:~# cat /proc/cpuinfo
processor : 0
model name : ARMv7 Processor rev 0 (v7l)
BogoMIPS : 666.66
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpd32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x3
CPU part : 0xc09
CPU revision : 0
processor : 1
model name : ARMv7 Processor rev 0 (v7l)
BogoMIPS : 666.66
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpd32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x3
CPU part : 0xc09
CPU revision : 0
Hardware : Xilinx Zynq Platform
Revision : 0003
Serial : 0000000000000000
root@analog:~#
processor:指明第几个CPU处理器
cpu cores:指明每个处理器的核心数
2、通过系统调用查看CPU信息
#include <unistd.h>
// 返回系统可以使用的核数,但是其值会包括系统中禁用的核的数目
//因此该值并不代表当前系统中可用的核数
long int sysconf(_SC_NPROCESSORS_CONF);
/* 返回值真正的代表了系统当前可用的核数 */
long int sysconf(_SC_NPROCESSORS_ONLN);
/* 以下两个函数与上述类似 */
#include <sys/sysinfo.h>
/* 可用核数 */
int get_nprocs_conf (void);
/*真正的反映了当前可用核数 */
int get_nprocs (void);
执行实际效果如下:
[10:30@zhouchao ~/test]$cat test_cpu.c
#include <unistd.h>
#include <sys/sysinfo.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
printf("sysconf _SC_NPROCESSORS_CONF = %ld.\n", sysconf(_SC_NPROCESSORS_CONF));
printf("sysconf _SC_NPROCESSORS_ONLN = %ld.\n", sysconf(_SC_NPROCESSORS_ONLN));
printf("================================\n");
printf("get nprocs_conf : %d.\n", get_nprocs_conf());
printf("get nprocs : %d.\n", get_nprocs());
return 0;
}
[10:30@zhouchao ~/test]$./test_cpu
sysconf _SC_NPROCESSORS_CONF = 2.
sysconf _SC_NPROCESSORS_ONLN = 2.
================================
get nprocs_conf : 2.
get nprocs : 2.
二、通过taskset指令配置
1、获取进程IP(ps aux
或pidof [cmd]
)
root@analog:~/zhouchao# ps axu |grep mac205d
root 2326 100 1.4 25220 15048 pts/1 Sl 02:34 0:14 ./mac205d
root 2329 0.0 0.1 3384 1144 pts/1 S+ 02:35 0:00 grep --color=auto mac205d
root@analog:~/zhouchao# pidof ./mac205d
2326
root@analog:~/zhouchao#
2、查看进程当前运行在那个CPU核(taskset -p [pid]
)
root@analog:~/zhouchao# taskset -p 2326
pid 2326's current affinity mask: 1
root@analog:~/zhouchao#
注意:显示的是进程mask,需要转换为二进制,根据bit位为1的指示运行的CPU核,CPU核标号从0开始。
3、指定进程到某个CPU核(taskset -pc [idx] [pid]
)
root@analog:~/zhouchao# taskset -pc 1 2326
pid 2326's current affinity list: 0
pid 2326's new affinity list: 1
root@analog:~/zhouchao# taskset -p 2326
pid 2326's current affinity mask: 2
root@analog:~/zhouchao#
4、启动时绑定CPU核(taskset -c [idx] [cmd]
)
root@analog:~/zhouchao# taskset -c 1 ./mac205d &
[1] 2345
root@analog:~/zhouchao# taskset -p 2345
pid 2345's current affinity mask: 2
root@analog:~/zhouchao#
注意:idx是从0开始的CPU核标号
三、通过系统调用实现用户态进程绑定
sched_setaffinity可以将某个进程绑定到一个特定的CPU.
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <sched.h>
/* 设置进程号为pid的进程运行在mask所设定的CPU上
* 第二个参数cpusetsize是mask所指定的数的长度
* 通常设定为sizeof(cpu_set_t)
* 如果pid的值为0,则表示指定的是当前进程
*/
int sched_setaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);
/* 获得pid所指示的进程的CPU位掩码,并将该掩码返回到mask所指向的结构中 */
int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);
- 示例
#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>
#include<pthread.h>
#define THREAD_MAX_NUM 200 //1个CPU内的最多进程数
int num=0; //cpu中核数
void* threadFun(void* arg) //arg 传递线程标号(自己定义)
{
cpu_set_t mask; //CPU核的集合
cpu_set_t get; //获取在集合中的CPU
int *a = (int *)arg;
int i;
printf("the thread is:%d\n",*a); //显示是第几个线程
CPU_ZERO(&mask); //置空
CPU_SET(*a,&mask); //设置亲和力值
if (sched_setaffinity(0, sizeof(mask), &mask) == -1)//设置线程CPU亲和力
{
printf("warning: could not set CPU affinity, continuing...\n");
}
CPU_ZERO(&get);
if (sched_getaffinity(0, sizeof(get), &get) == -1)//获取线程CPU亲和力
{
printf("warning: cound not get thread affinity, continuing...\n");
}
for (i = 0; i < num; i++)
{
if (CPU_ISSET(i, &get))//判断线程与哪个CPU有亲和力
{
printf("this thread %d is running processor : %d\n", i,i);
}
}
return NULL;
}
int main(int argc, char* argv[])
{
int tid[THREAD_MAX_NUM];
int i;
pthread_t thread[THREAD_MAX_NUM];
num = sysconf(_SC_NPROCESSORS_CONF); //获取核数
if (num > THREAD_MAX_NUM) {
printf("num of cores[%d] is bigger than THREAD_MAX_NUM[%d]!\n", num, THREAD_MAX_NUM);
return -1;
}
printf("system has %i processor(s). \n", num);
for(i=0;i<num;i++)
{
tid[i] = i; //每个线程必须有个tid[i]
pthread_create(&thread[i],NULL,threadFun,(void*)&tid[i]);
}
for(i=0; i< num; i++)
{
pthread_join(thread[i],NULL);//等待所有的线程结束,线程为死循环所以CTRL+C结束
}
return 0;
}
- 运行效果
[10:48@zhouchao ~/test]$gcc -g -o test_bind_process test_bind_process.c -lpthread
[10:48@zhouchao ~/test]$ls
test_bind_process* test_bind_process.c
[10:48@zhouchao ~/test]$./test_bind_process
system has 2 processor(s).
the thread is:0
the thread is:1
this thread 1 is running processor : 1
this thread 0 is running processor : 0
四、通过系统调用实现用户态线程绑定
绑定线程到cpu核上使用pthread_setaffinity_np函数.
#define _GNU_SOURCE /* See feature_test_macros(7) */
#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);
Compile and link with -pthread.
- 示例
#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
int
main(int argc, char *argv[])
{
int s, j;
cpu_set_t cpuset;
pthread_t thread;
thread = pthread_self();
/* Set affinity mask to include CPUs 0 to 7 */
CPU_ZERO(&cpuset);
for (j = 0; j < 8; j++)
CPU_SET(j, &cpuset);
s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
if (s != 0)
handle_error_en(s, "pthread_setaffinity_np");
/* Check the actual affinity mask assigned to the thread */
s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
if (s != 0)
handle_error_en(s, "pthread_getaffinity_np");
printf("Set returned by pthread_getaffinity_np() contained:\n");
for (j = 0; j < CPU_SETSIZE; j++)
if (CPU_ISSET(j, &cpuset))
printf(" CPU %d\n", j);
exit(EXIT_SUCCESS);
}
- 运行效果
[10:54@zhouchao ~/test]$gcc -o test_bind_thread test_bind_thread.c -lpthread
[10:54@zhouchao ~/test]$./test_bind_thread
Set returned by pthread_getaffinity_np() contained:
CPU 0
CPU 1
[10:54@zhouchao ~/test]$
五、通过系统调用实现内核态线程绑定
struct task_struct {
...
int nr_cpus_allowed; //此进程运行的处理器数量
cpumask_t cpus_allowed; //允许的处理器掩码
...
};
/*kernel/kthread.c*/
//绑定时会将进程状态设置为 TASK_UNINTERRUPTIBLE
void kthread_bind(struct task_struct *p, unsigned int cpu)
/*kernel/sched/core.c*/
int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
- 示例
static int rcutorture_booster_init(int cpu) //kernel/rcu/rcutorture.c
kthread_bind(boost_tasks[cpu], cpu);
wake_up_process(boost_tasks[cpu]);
static __init int test_ringbuffer(void) //kernel/trace/ring_buffer.c
kthread_bind(rb_threads[cpu], cpu);
wake_up_process(rb_threads[cpu]);
- 运行效果
暂未验证,省略
六、独占CPU核
上述设置只能保证进程或线程运行在特定的CPU上,仍然不能独自享有,时间片到了也是可能被切换出去的。通过配置Linux的启动参数grub,增加
isolcpus=1,2,3
来隔离指定的CPU核(从0开始)。
- grub配置文件(
/etc/default/grup
)
image
注意:编辑grub文件后再执行该命令
grub2-mkconfig -o /boot/grub2/grub.cfg
更新grub
- cmdline位置(
/proc/cmdline
)
root@analog:~# cat /proc/cmdline
console=ttyPS0,115200 root=/dev/mmcblk0p2 rw earlycon rootfstype=ext4 rootwait cpuidle.off=1 isolcpus=0
root@analog:~#
六、查看CPU运行情况(htop
)
执行
htop
可查看CPU,内存等信息。
image