Normally it is left up to the operating system to decide which CPUs or cores of the machine will be used to run an application. Depending on the number of threads required by the application at any given time and the needs of other applications, the OS will try to move threads around as needed to keep the overall load on each CPU balanced. When there are more threads than CPUs (which is the norm), the OS will share CPUs amongst several threads, meaning that threads will be paused until it is their turn. For some applications, this method may not work well, and it can be useful to dedicate certain CPUs or groups of CPUs to specific applications. Most OS platforms provide the ability to specify that a process should only execute on specific CPUs. This is commonly called CPU affinity or CPU pinning.
Dedicating specific CPUs to your application can be a way to ensure that it will always have enough CPU resources. On the contrary, you could explicitly keep some processors unused in order to guarantee sufficient resources for other processes running on the same machine. Setting processor affinity can also significantly optimize CPU cache. In other words, whenever a process changes the CPU, its data is transferred from one CPU to another. This transfer is costly but you can minimize it by pinning the process to a limited number of CPUs. You should, however, be careful to allow enough processors for your application, as it may have the ability to execute parallel tasks on different CPUs when available.
Unfortunately, Java does not provide a solution to launch the JVM with core or processor affinity. Should you want to pin your Java application to specific CPUs, you would have to either use commands like procaff, taskset or invoke native code. These solutions are platform specific.
Solution |
The Java Service Wrapper Professional Edition addresses this issue by providing a unified and easy way to configure CPU affinity without having to write code. As of version 3.5.27, only Windows and Linux are supported, but additional platform support will be coming in near future releases. The configuration can target the Wrapper process as well as its controlled JVM process. Each have their own properties to configure processor affinity but if you only specify those of the Wrapper, by default the controlled JVM process will inherit the same configuration and automatically execute using the same CPU(s) as the Wrapper process. |
Technical Overview |
||||||||||||||||||||||||||
|