Skip to main content
3 of 10
added 434 characters in body
Yves
  • 3.4k
  • 8
  • 38
  • 66

How to disable CPU

#Reedited The answer has been put at the end of the question (the Summary part).

#Qutstion I'm trying to disable some CPUs of my server.
I've found this link: https://www.cyberciti.biz/faq/debian-rhel-centos-redhat-suse-hotplug-cpu/linux-turn-on-off-cpu-core-commands/, which offers me a method as below:

Here is what numactl --hardware gave me:
enter image description here

I want to disable all CPUs from 16 to 63, so I write a script named opCPUs.sh as below:

#!/bin/bash

for i in {16..63}; do
        if [[ "$1" == "enable" ]]; then
                echo 1 > /sys/devices/system/cpu/cpu$i/online
        elif [[ "$1" == "disable" ]]; then
                echo 0 > /sys/devices/system/cpu/cpu$i/online
        else
                echo 'illegal parameter'
        fi
done
grep "processor" /proc/cpuinfo

Then I execute it: ./opCPUs.sh disable and I can see the result of grep in the script:
enter image description here

It seems to work.

Now I think all of processes should be in CPU 0 - 15 because others have been disabled.
So I use the existing processes dbus to verify as below:
ps -Lo psr $(pgrep dbus)

I get this:
enter image description here

The psr tells me in which CPU the process is running, right? If so, I have disabled CPU 60, CPU 52 etc, why they are still here?


#SUMMARY There are three ways to do so:

set isolcpus = 4 in grub and reboot can disable the 4th CPU permanently;

echo 0 > /sys/devices/system/cpu/cpu4/online can disable the 4th CPU, the 4th CPU will keep working for the old processes on it but no new process will be assigned to the 4th CPU anymore;

taskset -c 3 ./MyShell.sh will force the MyShell.sh to be assigned to the 3rd CPU whereas the 3th CPU can still accept other processes.

Yves
  • 3.4k
  • 8
  • 38
  • 66