ClickCease

Processor affinity, or CPU pinning or “cache affinity”, enables the binding and unbinding of a process or a thread to a central processing unit (CPU) or a range of CPUs, so that the process or thread will execute only on the designated CPU or CPUs rather than any CPU. So lets begin understanding CPU affinity.

Understanding CPU Affinity

Processor affinity takes advantage of the fact that remnants of a process that was run on a given processor may remain in that processor’s state (for example, data in the cache memory) after another process was run on that processor. Scheduling that process to execute on the same processor improves its performance by reducing performance-degrading events such as cache misses. A practical example of processor affinity is executing multiple instances of a non-threaded application, such as some graphics-rendering software.

understanding cpu affinity
with cpu affinity / pinning, we can bind a process to a particular cpu to increase performance and cache hits

Under normal conditions, the kernel determines the CPUs a process runs. Every time the scheduler reschedules a process, it can go to any of the available CPUs. While this is fine for most workloads, sometimes it is desirable to limit on which CPU(s) a process is allowed to run. For example, limiting a memory-intensive process to just one or two CPUs increases the chances of a cache hit, thus increasing overall performance.

Benefit of CPU Affinity

The benefit of CPU affinity is optimizing cache performance. Multiprocessing computers go through a lot of trouble to keep the processor caches valid. Data can be kept in only one processor’s cache at a time. Otherwise, the processor’s cache may grow out of sync, leading to the question, who has the data that is the most up-to-date copy of the main memory? But the real problem comes into play when processes bounce between processors: they constantly cause cache in-validations, and the data they want is never in the cache when they need it. Thus, cache miss rates (CPU Cache) grow very large. CPU affinity protects against this and improves cache performance.

Configuring CPU Affinity for Apache Webserver

Now, after understanding CPU affinity on Linux servers. Lets see how to configure cpu affinity for our webserver running on Red Hat Enterprise Linux 8 (RHEL 8) server.

Apache is already running on my RHEL server. And by default cpu affinity is not configured.

configuring cpu affinity on Linux server
apache is running on RHEL 8 server

We will be installing “tuna” application to check on which CPU apache is running.

yum install tuna
installing "tuna" application to check cpu affinity
installing “tuna” application to check cpu affinity

Using “tuna” to check on which CPU the httpd process is running.

tuna -t httpd -P
tuna -t httpd -P

We can clearly see under the “affinity” column that httpd is running on all CPU cores (0,1,2). Cpu Index 0 is for first CPU and so on.

Now lets pin down the httpd process to CPU 0.

Create a directory “httpd.service.d” under /etc/systemd/system/

mkdir -p /etc/systemd/system/httpd.service.d/
create directory for service httpd

Move inside the directory and create a cpuaffinity.conf (or any name with .conf extension) file with following contents.

[Service]
CPUAffinity=0
create file under /etc/systemd/system/ for cpu affinity

Finally reload the systemd daemon and restart the httpd service for implementing cpu affinity for httpd process.

Once its done, we can confirm the cpu affinity of httpd process to CPU 0 under “affinity” column by giving “tuna” command again.

systemctl daemon-reload
systemctl restart httpd
tuna -t httpd -P

Honestly, I highly recommend taking Red Hat Performance Tuning (RH442) training after you are done with your RHCSA or RHCE training. RH442 actually helps you understand the insights of Linux OS.