CPU thread-bound

1. Bind purpose

In the Linux system, the kernel thread scheduling is done autonomously. When the system is running on a multi-core CPU, it is possible to switch execution threads on different CPU core back and forth. This is not conducive to the CPU cache, a high page fault rates.

With Intel i5 CPU, for example. In the multi-core CPU configuration, each CPU has its own L1, L2 cache, L3 cache is shared. If a thread switch back and forth between the CPU, each CPU cache hit rate will be reduced. And if the thread regardless of the schedule is always can be executed on a CPU, so its data L1, L2 cache hit rate will be significantly improved.

2. Use the bind command

Taskset can use the command to specify the binding thread on the CPU.

taskset command follows:

taskset -p <cpu_set> <pid>

Pid thread can be used pstree -p | grep <pthread_name>to get. But the premise of this method is the name of the thread must be set, otherwise it will not pstree to view threads.

cpu_set thread set corresponding CPU, as an integer type, a value of a bit position which corresponds to the number of CPU (starting from 0). E.g. cpu_set corresponding No. 1 CPU 1 (0001), the corresponding number when 2 CPU 4 (0100).

When a plurality cpu_set bit to 1 indicates that the system will randomly scheduled thread running on one of the CPU. For example, when a thread corresponding to 3 cpu_set i.e. binary 0011, the system indicates that the thread is scheduled to run on a random number or the number 0 CPU 1 CPU.

When not in use taskset <cpu_set>use only -p <pid>when the situation indicates that the query CPU sets the corresponding thread.

The following example will use the command taskset the thread number is 1958 thread binding for the No. 1 CPU:

~ # taskset -p 1958
pid 1958's current affinity mask: 3
~ # taskset -p 2 1958
pid 1958's current affinity mask: 3
pid 1958's new affinity mask: 2

3. Binding in the program

In Linux, may be used sched_setaffinity()to change the set CPU corresponding to the thread, the thread is bound to a CPU.

Sched_setaffinity function belongs to the first document <sched.h>, the following function prototype:

int sched_setaffinity(pid_t pid, size_t cpusetsize, const cpu_set_t* mask);

Visible, which is actually the thread pidto a specified set of CPU. When the transmission parameter pidis 0 for the current thread to a CPU set mask.

cpu_set_t Of the type essentially an array of integers, Linux offers the following five macro function set operation of the CPU (read only the data type):

Macro Functions Features
CPU_SET(int cpu, cpu_set_t* cpusetp) To cpuadd to cpusetpthe
CPU_CLR(int cpu, cpu_set_t* cpusetp) To cputhe cpusetpdeletion
int CPU_ISSET(int cpu, cpu_set_t* cpusetp) Determining cpuwhether cpusetpin the presence of 1 is returned, otherwise 0
CPU_ZERO(cpu_set_t* cpusetp) Empty cpusetp, that is cpusetpevery bit bit is cleared
CPU_COUNT(cpu_set_t* cpusetp) Query cpusetpthe number of CPU bit position is the number 1, that thread scheduling

Wherein, cpureferring to the CPU number which, starting from the number 0.

When using the above macro, you must first open the macro __USE_CHUto _GNU_SOURCEtell the compiler starts above macro function, and these must be declared before the macro in the header file all declarations:

#ifndef __USE_CHU
#define __USE_CHU
#endif

#define _GNU_SOURCE

#include <sched.h>

The following example is the current thread-bound No. 3 CPU:

cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(3, &mask);
sched_setaffinity(0, sizeof(cpu_set_t), &mask);
Published 60 original articles · won praise 36 · views 5867

Guess you like

Origin blog.csdn.net/qq_35692077/article/details/104045657