When using Linux systems, we often encounter some CPU-intensive tasks, such as data processing, compiling programs, etc., which require a large amount of computing resources to complete. However, if our system hardware configuration is low, it will cause slow task execution and seriously affect our work efficiency. To address this problem, this article will introduce a technology called "CPU binding", which can bind a process to a specific CPU core, thereby improving the execution efficiency of the process.
For ordinary applications, the default scheduling mechanism of the operating system is no problem. However, when a process requires higher operating efficiency, it is necessary to consider binding it to a separate core to reduce the overhead caused by scheduling on different cores.
After binding a process/thread to a specific CPU core, the process will always run on this core and will no longer be scheduled by the operating system to other cores. But the bound core may still be scheduled to run other applications.
Scheduling of multi-core CPUs by the operating system
Currently both windows and linux support scheduling and management of multi-core CPUs.
The core of software development in a multi-core environment is multi-threaded development. This multi-threading not only represents multi-threading in software implementation, but also requires the use of multi-threading technology in hardware.
The focus of multi-core operating systems is on process allocation and scheduling. Process allocation allocates processes to reasonable physical cores, because different cores have different sharing and historical operating conditions. Some physical cores can share the second level cache, while others are independent. If a process with data sharing is allocated to a core with a shared secondary cache, performance will be greatly improved; otherwise, performance may be affected.
Process scheduling will involve real-time, load balancing and other issues. Current research hot issues mainly focus on the following aspects:
The situation when multiple processes and multi-threads are running on the CPU core is as follows:
When each CPU core runs a process, since the resources of each process are independent, there is no need to consider the context when switching between CPU cores
When each CPU core runs a thread, sometimes the threads need to share resources, so these resources must be copied from one core of the CPU to another, which will cause additional overhead
Bind the process to run on the cpu core
Check how many cores the cpu has
Use cat /proc/cpuinfo to view cpu information, the following two information:
processor, specify which cpu processor
cpu cores, specify the number of cores per processor
You can also use the system call sysconf to obtain the number of cpu cores:
#include int sysconf(_SC_NPROCESSORS_CONF);/* 返回系统可以使用的核数,但是其值会包括系统中禁用的核的数目,因 此该值并不代表当前系统中可用的核数 */ int sysconf(_SC_NPROCESSORS_ONLN);/* 返回值真正的代表了系统当前可用的核数 */ /* 以下两个函数与上述类似 */ #include int get_nprocs_conf (void);/* 可用核数 */ int get_nprocs (void);/* 真正的反映了当前可用核数 */
I am using a virtual machine with 2 processors. Each processor has only one core, which is equivalent to two cores on one processor.
Use taskset directive
Get process pid
-> % ps PID TTY TIME CMD 2683 pts/1 00:00:00 zsh 2726 pts/1 00:00:00 dgram_servr 2930 pts/1 00:00:00 ps
View which CPU the process is currently running on
-> % taskset -p 2726 pid 2726's current affinity mask: 3
The displayed decimal number 3 is converted to binary and the lowest two are 1. Each 1 corresponds to a CPU, so the process runs on 2 CPUs.
Specify the process to run on cpu1
-> % taskset -pc 1 2726 pid 2726's current affinity list: 0,1 pid 2726's new affinity list: 1
Note that the cpu label starts from 0, so cpu1 represents the second cpu (the first cpu label is 0).
At this point, the application is bound to cpu1 to run. See the following:
-> % taskset -p 2726 pid 2726's current affinity mask: 2
Bind cpu when starting the program
#启动时绑定到第二个cpu -> % taskset -c 1 ./dgram_servr& [1] 3011 #查看确认绑定情况 -> % taskset -p 3011 pid 3011's current affinity mask: 2
Use sched_setaffinity system call
sched_setaffinity can bind a process to a specific CPU.
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include /* 设置进程号为pid的进程运行在mask所设定的CPU上 * 第二个参数cpusetsize是mask所指定的数的长度 * 通常设定为sizeof(cpu_set_t) * 如果pid的值为0,则表示指定的是当前进程 */ int sched_setaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask); int sched_getaffinity(pid_t pid, size_t cpusetsize, cpu_set_t *mask);/* 获得pid所指示的进程的CPU位掩码,并将该掩码返回到mask所指向的结构中 */
Example
#include #include #include #include #include #define __USE_GNU #include #include #include #include #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 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;ifor(i=0; ireturn 0; }
operation result
-> % ./a.out system has 2 processor(s). the thread is:0 the thread is:1 this thread 0 is running processor : 0 this thread 1 is running processor : 1
Bind the thread to run on the cpu core
Bind threads to the cpu core using the pthread_setaffinity_np function. Its prototype is defined as follows:
#define _GNU_SOURCE /* See feature_test_macros(7) */ #include 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.
The meaning of each parameter is similar to sched_setaffinity.
Example
#define _GNU_SOURCE #include #include #include #include #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 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 if (CPU_ISSET(j, &cpuset)) printf(" CPU %d\n", j); exit(EXIT_SUCCESS); }
operation result
-> % ./a.out Set returned by pthread_getaffinity_np() contained: CPU 0 CPU 1
Through the introduction of this article, we have learned how to use CPU binding technology to bind a process to a specific CPU core, thereby significantly improving the execution efficiency of the process. In actual applications, we can choose appropriate CPU binding solutions according to different scenarios and needs to achieve the best performance improvement effect. I hope this article can help readers better understand and apply CPU binding technology, and achieve higher work efficiency in the use of Linux systems.
The above is the detailed content of Dual-core CPU killer makes your Linux process run faster!. For more information, please follow other related articles on the PHP Chinese website!