Steps to Pin CPU Core With Java Thread
While executing a program using multi-core processor, depending on its requirements it might use single core to execute or it might switch cores. Most of the times it is better option to let Operating System handle such things, but what if we want to execute particular process on particular cores to achieve better performance from system, then we can use something Linux has provided us called as 'Affinity'.
Assigning particular CPU core to a particular process is known as 'affinity' in Linux.
Every process while getting executed on a core creates a cache of its own. When this process gets shifted to another core this cache needs to be updated. So this updating and left over from previous process's cache cause reduction in performance.
Instead of this if a particular core is assigned to a process there won't be such problems, no need to remove or update cache, ultimately resulting in better performance.Every thread and interrupt source that belongs to system has this property. This is basically used by OS scheduler while deciding which threads to run on which cores. If processor affinity is used in a better manner, it helps to get maximum performance.
Process Affinity:To assign process to a particular core we will use 'taskset'taskset:Syntax:taskset [options] [mask | list ] [pid | command [arg]...]Taskset is used to set or retrieve the CPU affinity of a running process considering its PID(Process ID) or launching a new command with a given affinity. The Linux scheduler obeys this given CPU affinity and the process will run on that core only. While working with threads there is an alternative for taskset i.e. Java-Thread-AffinityHow to use tasksetThe taskset tool is part of "util-linux" package in Linux, and most of Linux distros has it as pre-installed by default. If taskset is not available on Linux system, install it as follows.
How to install taskset on CentOS, Fedora or RHEL