CPU Affinity Binding

Source: Internet
Author: User
Tags bitmask set set

The most intuitive benefit of binding a process to the CPU is to reduce cache synchronization and switching between CPUs, increase the hit rate of the CPU cache, and improve the efficiency of the Code.
From the CPU architecture, NUMA has independent local memory, the nodes can be exchanged between the modules to connect and information interaction, so each CPU can access the entire system of memory, but access to remote memory access efficiency is greatly reduced, bound CPU operation on such systems run faster, UMA architecture, Multiple CPUs access the enclosure through the system bus. It is not difficult to see that NUMA uses CPU binding, each core can be more focused on one thing, the resource system is fully used, reducing the loss of synchronization.

Simply put, CPU affinity (affinity) is the tendency for a process to run as long as possible on a given CPU without being migrated to another processor.

Soft affinity (Affinity): The Linux kernel process Scheduler inherently has the feature known as Soft CPU affinity (affinity), which means that the process will run as long as possible on the specified CPU without being migrated to other processors, meaning that processes typically do not migrate frequently between processors. This state is what we want because the low frequency of process migrations means that the resulting load is small.
The 2.6 version of the Linux kernel also includes a mechanism that allows developers to program hard CPU affinity (affinity). This means that an application can explicitly specify which (or what) processor the process is running on.

Hard affinity (Affinity): The simple thing is to use the API provided by the Linux kernel to the user, forcing the process or thread to bind to a specified CPU core operation.
In the Linux kernel, all processes have a related data structure called task_struct. This structure is very important for a number of reasons, and the most relevant to affinity (affinity) is the cpus_allowed bitmask. This bitmask consists of n bits and corresponds to n logical processor one by one in the system. A system with 4 physical CPUs can have 4 bits. If these CPUs are Hyper-Threading enabled, then the system has a 8 bit bitmask.
If a given bit is set for a given process, the process can run on the associated CPU. Therefore, if a process can run on any CPU and be able to migrate between processors as needed, the bitmask is all 1. In fact, this is the default state of processes in Linux.

In general, you only need to use the default scheduler behavior in your application. However, you may want to modify these default behaviors to achieve performance optimizations. Let's take a look at 3 reasons for using hard affinity (affinity).

Cause 1. Have a lot of calculations to do
Scenarios that are based on a large number of calculations are often present in scientific and theoretical calculations, but this can also occur in general domain calculations. A common sign is that you find that your application spends a lot of computing time on multiprocessor machines.
Cause 2. Increase cache Hit rate
On multi-core machines, each CPU has its own cache, caches the information used by the process, and the process may be dispatched by the OS to other CPUs, so that the CPU cache hit rate is low. When the CPU is bound, the program will always run on the specified CPU and not be dispatched by the OS to other CPUs, increasing the CPU cache hit rate.
Cause 3. Running a time-sensitive, deterministic process
The last reason we are interested in CPU affinity (affinity) is a real-time (temporal-sensitive) process. For example, you might want to use hard affinity (affinity) to specify a processor on a 8-way host, while allowing the other 7 processors to handle all normal system scheduling. This approach ensures that long-running, time-sensitive applications can be run, while allowing other applications to monopolize the rest of the compute resources.

Process settings:

1 cpu_set_t Mask;2 /*Initialize set set to null*/3Cpu_zero (&mask);4 /*add 0, 1th CPUs to the collection in turn*/5Cpu_set (0, &mask);6Cpu_set (1, &mask);7 /*binds the current process to the CPU*/8Sched_setaffinity (0,sizeof(mask), &mask);

Thread Settings:

1 cpu_set_t Mask;2 /*Initialize set set to null*/3Cpu_zero (&mask);4 /*add 0, 1th CPUs to the collection in turn*/5Cpu_set (0, &mask);6Cpu_set (1, &mask);7 /*binds the current line regulation regulation to the CPU*/8PTHREAD_SETAFFINITY_NP (Pthread_self (),sizeof(mask), &mask);

Excerpt from:

https://zhuanlan.zhihu.com/p/38541212

CPU Affinity Binding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.