CPU Affinity, Core Pinning, and NUMA Awareness
CPU Affinity Basics
CPU affinity restricts which cores a thread can run on. By default, threads can migrate between any core. Setting affinity pins a thread to specific cores. This eliminates migration overhead and keeps cache warm.
Use taskset or sched_setaffinity to set affinity. Pin latency critical threads to dedicated cores. Pin interrupt handlers to cores away from application threads. This prevents interrupt storms from preempting application work.
Core Pinning Benefits
Cache locality: A pinned thread always finds its data in that core L1 and L2 cache. Migrating to another core means starting with cold cache. For memory intensive workloads, cold cache costs hundreds of microseconds per migration.
Predictable latency: Pinned threads do not compete for scheduling on other cores. They always run when runnable and their core is available. This reduces jitter in latency sensitive paths.
NUMA alignment: Pin threads to cores on the same NUMA node as their memory. This ensures local memory access at 100 nanoseconds instead of remote access at 200+ nanoseconds.
Isolcpus and CPU Sets
The isolcpus kernel parameter removes cores from default scheduler. Only explicitly assigned threads run on isolated cores. Kernel threads, interrupts, and other processes cannot preempt your workload. This provides maximum isolation for latency critical applications.
Use cgroups and cpusets for container isolation. Assign containers to specific cores. Prevent noisy neighbor effects where one container spikes CPU and preempts others. Kubernetes supports CPU pinning through static CPU manager policy.