Skip to content

Instantly share code, notes, and snippets.

@mjm522
Last active February 16, 2024 05:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mjm522/d879fc24182dc15f22533de54096ff18 to your computer and use it in GitHub Desktop.
Save mjm522/d879fc24182dc15f22533de54096ff18 to your computer and use it in GitHub Desktop.
Isolate CPU core to run Python code Ubuntu

Note: To run a process dedicated on a CPU you can use the tasket command. For example

taskset -c 5,11 python -m timeit 'sum(range(10**7))'

However, this will not guarantee that the CPUs 5,11 will be used for that process alone. The same CPU can be interrupted by the scheduler and may not be properly isolated. So inorder to isolate the CPU the following steps are to be taken. Another feature that could be turned off for these CPUs are the interrupt feature 'IRQ'. However this is not covered here.

  1. List all available cores and hyperthreads in your processor.
lscpu --all --extended

My output looks like this:

CPU NODE SOCKET CORE L1d:L1i:L2:L3 ONLINE MAXMHZ    MINMHZ
0   0    0      0    0:0:0:0       yes    4100.0000 800.0000
1   0    0      1    1:1:1:0       yes    4100.0000 800.0000
2   0    0      2    2:2:2:0       yes    4100.0000 800.0000
3   0    0      3    3:3:3:0       yes    4100.0000 800.0000
4   0    0      4    4:4:4:0       yes    4100.0000 800.0000
5   0    0      5    5:5:5:0       yes    4100.0000 800.0000
6   0    0      0    0:0:0:0       yes    4100.0000 800.0000
7   0    0      1    1:1:1:0       yes    4100.0000 800.0000
8   0    0      2    2:2:2:0       yes    4100.0000 800.0000
9   0    0      3    3:3:3:0       yes    4100.0000 800.0000
10  0    0      4    4:4:4:0       yes    4100.0000 800.0000
11  0    0      5    5:5:5:0       yes    4100.0000 800.0000

The cores number starts from 0. That is if you have 6 cores the numbers range from 0 to 5. You will have to isolate both logical cores of each physical core. So for me, I am going isolate the physical core 5. For this the logical core numbers are 5 and 11.

  1. Open /etc/default/grub in your favourite text editor. Add the following to the GRUB_CMDLINE_LINUX option.
GRUB_CMDLINE_LINUX="isolcpus=5,11 nohz_full=5,11"

The above line will add core 5 to the isolated list. The other option nohz_full is to enable full tickless operation of the core. This means the following. By default Ubuntu uses a scheduling-clock to interrupt the core to run a scheduler. This parameter is disabled by default and specifying above arguments will enable it.

  1. Save the file and update the grub.
sudo update-grub
  1. Reboot.

  2. Check:

a) Check if the core is isolated.

cat /sys/devices/system/cpu/isolated

The above should return the core that has been set as isolated.

b) Check if full tickness is enabled.

 cat /sys/devices/system/cpu/nohz_full

If not edit the CONFIG_NO_HZ_FULL flag inside your kernel config. First know your kernel by typing uname -a. Then open the kernel config using sudo nano config-<kernel no>. Go to timers subsystem and edit the flag.

  1. Testing.

a) Without isolation

python -m timeit 'sum(range(10**7))'

b) With isolation

taskset -c 5,11 python -m timeit 'sum(range(10**7))'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment