Optimize application performance with CPU affinity
Introduction
Understand thread pinning and CPU affinity
Create a CPU-intensive program
Pin threads to cores with taskset
Set CPU affinity in source code
Next Steps
Optimize application performance with CPU affinity
Review frequently asked questions
These FAQs were drafted with an approved AI-assisted workflow and reviewed by Arm contributors before publication. Human technical review remains part of the process so the final page reflects engineering rigor, accuracy, and Arm editorial standards.
Run
lscpu | grep -i numa and check the NUMA node count.On a uniform system, select an available core for your single-threaded test. For a multithreaded test, select the specific cores that you intend to dedicate to its threads.
Compare unpinned and pinned runs for runtime stability and throughput. Use
perf to collect cache metrics. Look for differences in cache references and misses between the two cases.Set affinity immediately after creating a thread or before it enters the hot path that does the critical work. Pin only the threads that you need to control. Leave the others managed by the scheduler.
Run enough iterations to reduce noise and ensure the system isn’t under heavy background load. Verify that the chosen affinity mask targets cores that are online and available.