Thread affinity why requires CPU affinity of threads
The application is executed in multi-threaded way, most of the time the thread can be reasonably dispatched. In some cases, however, an important thread is paused, and the time slice is assigned to a thread that is not important. When a thread pauses to hibernate and then wakes up, it needs to reload "cache line" (CPU L1/L2 cache). When a thread's working time is short and needs to be woken up frequently, it means that the entire process is slow and can be 2-5 times slower than a single-threaded scenario.
Some threads of the application may need to be executed all the time, not hibernate due to CPU scheduling, which requires the thread to be executed on one CPU at a time.
Https://github.com/OpenHFT/Java-Thread-Affinity implements binding a thread to the CPU, for example: Thread x must use a physical CPU, Threads y and Z use different logical CPUs (CPU Hyper-Threading technology) of the same physical CPU. This code identifies the independent CPU and binds the thread to the appropriate CPU according to the rules provided by the developer. This tool can identify a separate CPU and, based on the principle of best effort, assign threads to the CPU according to the rules provided. Even without an independent core, you can do the best.
Principle
This tool reads/proc/cpuinfo/to determine the layout of the CPU. If you do not have this file then think of one slot per CPU.
By default, independent CPUs are queried only on unused CPUs. For example, a total of 16 CPUs, 8 of which are usually not used (as determined by the thread affinity set at boot time), then java-thread-affinity will use these CPUs.
Attention
If more than one process uses this library, you need to specify the CPU for a process, otherwise these processes will share the CPU. Add-daffinity.reserved={cpu-mark-in-hex} at the command line to specify which CPU the process uses to resolve the problem.
Ps
This article mentions the time loss of context switching: http://ifeve.com/java-context-switch/
Java Threading CPU Affinity tool