• Linux
  • FreeBSD
  • Networking
  • Python
  • AWS
  • WebDev
  • About Us

How to set CPU affinity in FreeBSD to allocate specific cores to specific processes

Written by
FreeBSD Leave a Comment

If your hardware has multiple CPUs, you can set CPU affinity to run certain processes on specific cores. In FreeBSD, the cpuset command is used for this purpose. I have a test server on AWS with two CPU cores. First, let’s verify the hardware by running the following command:

sysctl -a | egrep -i 'hw.machine|hw.model|hw.ncpu'

So you can see I have two cpu s on this machine , its upto 2 cores ie, 0 and 1 are the cpu’s. So from the two cpu cores, I want to run all my system default processes on first cpu and only my openvpn on second cpu .

To set all the default processes on core 0

cpuset -s1 -l 0  (the last 0 is the cpu number)

To make this change permanent even after reboot , add the below line to the /etc/rc.conf file

/usr/bin/cpuset -s1 -l 0

To set the openvpn process on CPU2 , first you need to find out the openvpn process id using the ps command .

ps ax | grep openvpn

This will show the process id of your openvpn process . Then run the below command to move the process to cpu 1

cpuset -C -c -l 1 -p openvpnpid

So to get this working even after the reboot , either you need to run some startup script to find the process id and then execute the above command or run it manually . This is because every time system reboots or the service stops the process id changes , so thats something you need to figure out .

So now if you run a top command you will be able to see all my system processes are running on CPU 0 and the openvpn process on core 1

© Copyright 2020.TechieNix. All Rights Reserved.