Course contentsShow
← Linux
Lesson 470 of 1,8195. Process Management and SignalingFree lesson

CPU Affinity and Cache Locality

Describe techniques for pinning processes to specific cores to improve performance.

In this lesson, you will learn how to improve your application's performance by pinning processes to specific CPU cores to optimize cache usage.

The Problem: Cache Misses and Migrations

Modern CPUs rely heavily on "caches"—small, ultra-fast memory banks located directly on the processor. When a process runs, it loads data into this cache. However, the Linux kernel scheduler constantly moves processes between different CPU cores to ensure fairness.

If a process is moved from Core A to Core B, the data it needs is likely still sitting in the cache of Core A. The CPU must then fetch that data from the slower main RAM, causing a performance penalty known as a "cache miss." Furthermore, "cold" caches hurt performance because the processor has to rebuild its local data set from scratch.

The Solution: CPU Affinity

CPU Affinity (or "processor pinning") is the practice of binding a process to a specific set of cores. By restricting a process to Core A, we ensure that its data stays in Core A's cache, maximizing the "hit rate" and keeping the process running at peak speed.

To see the current affinity of a process, you can use the taskset utility. For example, to check the affinity of a running process with PID 1234:

taskset -cp 1234

If you want to launch a new process and lock it to the first core (Core 0), you use:

taskset -c 0 ./my_performance_critical_app

When to Use Pinning

Pinning is not always necessary; in fact, the kernel’s default scheduler is excellent for general tasks. You should consider pinning only when:

  1. Low Latency is Critical: High-frequency trading or real-time media processing benefits from avoiding context switches and cache thrashing.
  2. Heavy Computational Loads: If a process is working on a massive dataset, keeping it on one core ensures the L1/L2 caches remain "hot."

Be careful, though: pinning too many processes to a single core creates a bottleneck, as those processes will then have to compete for execution time, which often hurts performance more than the cache misses would have.

Key Takeaway: CPU affinity reduces performance degradation caused by cache misses and migrations by forcing a process to stay on a specific core, ensuring its data remains in the fast local cache.