How to disable one CPU

After doing some tests, I got the results as below:

echo 0 > /sys/devices/system/cpu/cpu4/online

does disable the 4th CPU.

"disable the 4th CPU" means that the later processes won't be assigned to the 4th CPU anymore. In other words, the processes located at the 4th CPU before "disable the 4th CPU" won't be moved out from the CPU while disabling the CPU.

Saying that I have four CPUs: 0, 1, 2 and 3. So I can:

echo 0 > /sys/devices/system/cpu/cpu1/online
echo 0 > /sys/devices/system/cpu/cpu2/online
echo 0 > /sys/devices/system/cpu/cpu3/online
./MyShell.sh

to force the ./MyShell.sh to be assigned to the CPU0.

By the way, reboot will enable all CPUs automatically.


Besides @Yves answer, you actually are able to use the isolcpus kernel parameter.

To disable the 4th CPU/core (CPU 3) with Debian or Ubuntu:

In /etc/default/grub add isolcpus=3 to GRUB_CMDLINE_LINUX_DEFAULT

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash isolcpus=3"

Run

sudo update-grub

Reboot the server.

isolcpus — Isolate CPUs from the kernel scheduler.

Synopsis isolcpus= cpu_number [, cpu_number ,...]

Description Remove the specified CPUs, as defined by the cpu_number values, from the general kernel SMP balancing and scheduler algroithms. The only way to move a process onto or off an "isolated" CPU is via the CPU affinity syscalls. cpu_number begins at 0, so the maximum value is 1 less than the number of CPUs on the system.

This option is the preferred way to isolate CPUs. The alternative, manually setting the CPU mask of all tasks in the system, can cause problems and suboptimal load balancer performance.

Interestingly enough, the usage of this kernel parameters can be setting aside a CPU for later on using CPU affinity to one process/pin a process to a CPU, and thus both making sure there are no more user processes running on that CPU.

In addition, also can make the server more stable having a guarantee a particular process with a very high load will have it´s own CPUs to play with. I have seen Meru doing that with their Linux based controllers before becoming aware of this setup.

The associated command to then assign a process to the fourth CPU (CPU 3), is:

sudo taskset -cp PID

taskset is used to set or retrieve the CPU affinity of a running process given its PID or to launch a new COMMAND with a given CPU affinity. CPU affinity is a scheduler property that "bonds" a process to a given set of CPUs on the system. The Linux scheduler will honor the given CPU affinity and the process will not run on any other CPUs. Note that the Linux scheduler also supports natural CPU affinity: the scheduler attempts to keep processes on the same CPU as long as practical for performance reasons. Therefore, forcing a specific CPU affinity is useful only in certain applications.


SUMMARY

There are several techniques applied to this question :

set isolcpus = 4 in grub and reboot can disable the 5th CPU/CPU 4 permanently for user land processes;

echo 0 > /sys/devices/system/cpu/cpu4/online disables the 5th CPU/CPU 4, that will still keep working for the processes that have already been assigned to it but no new processes will be assigned to CPU 4 anymore;

taskset -c 3 ./MyShell.sh will force MyShell.sh to be assigned to the 4th CPU/CPU 3 whereas the 4th CPU can still accept other user land processes if isolcpus is not excluding it from doing that.

PS. Anecdotally, my best example of using the isolcpus/taskset on the field, was an SSL frontend for a very busy site, that kept going unstable every couple of weeks, where Ansible/ssh would not let me in remotely anymore.

I applied the techniques discussed above, and it kept working in a very stable fashion ever since.