Linux - CPU Affinity and Isolation
lscpu -e # view CPUs / Cores on system
nproc # show number of cores
CPU affinity
taskset -p 1234 # show affinity for this process
Pin process to specific cores:
taskset -c <cpu_list> <command>
taskset -c 5 server.sh # start proc pinned to one core
taskset -c 1,2,5-8 server.sh # start proc with core list
taskset -c 5 -p 1234 # pin existing process
numactrl -C 0 server.sh # run command pinned to numa node 0
nice -n -20 taskset -c 5 server.sh # high priority, pinned to core
chrt # another commadn to change real time scheduling prioritizes
cset # More advanced tool designed for creating CPU sets and controlling the CPU affinity of entire groups of processes.
CPU Isolation
- Isolated CPUs/cores - Nothing will run on the CPU cores that are specified to be isolated unless it is specifically pinned to that core. This is useful becasuse it wall allow a process full use of the core for optimal performance.
- Housekeeping CPUs/cores - These are not isolated. Anything is free to run on these. You need at least one reserved for normal system processes. If you have enough cores you might leave two housekeeping cores.
/etc/default/grubGRUB_CMDLINE_LINUX_DEFAULT="isolcpus=2,3 nohz_full=2,3 rcu_nocbs=2,3" GRUB_CMDLINE_LINUX_DEFAULT="quiet splash isolcpus=2,3 nohz_full=2,3 rcu_nocbs=2,3"
- isolcpus - isolate these CPUs
- nohz_full: Tells the kernel to run these cores in tickless mode for reduced interrupt frequency.
- rcu_nocbs: This disables RCU callbacks on the specified cores, ensuring minimal interference.
Update GRUB and reboot to apply changes:
sudo update-grub
sudo reboot