Thursday, July 16, 2015

Linux Hotplug a CPU and Disable CPU Cores At Run Time

I would like to dynamically enable or disable a CPU on a running system. How do I hotplug a CPU on a running Linux system? How do I disable cpu cores on a Linux operating system at run time?

Linux kernel does supports cpu-hotplug mechanism. You can enable or disable CPU or CPU core without a system reboot. CPU hotplug is not just useful to replace defective components it can also be applied in other contexts to increase the productivity of a system. For example on a single system running multiple Linux partitions, as the workloads change it would be extremely useful to be able to move CPUs from one partition to the next as required without rebooting or interrupting the workloads.
Tutorial details
DifficultyIntermediate (rss)
Root privilegesYes
Estimated completion time5m
This is known as dynamic partitioning. Other applications include Instant Capacity on Demand where extra CPUs are present in a system but aren't activated. This is useful for customers that predict growth and therefore the need for more computing power but do not have at the time of purchase the means to afford. Please note that not all server supports cpu hotplug but almost all server can support disabling or enabling cpu core on a Linux operating systems. There are couple OEMS that support NUMA hardware which are hot pluggable as well, where physical node insertion and removal require support for CPU hotplug. This tutorial will explain how to hotplug a cpu and disable/enable core on a Linux.

List all current cpus and cores in the system

Type the following command:
# cd /sys/devices/system/cpu
# ls -l

Sample output:
total 0
drwxr-xr-x 4 root root    0 Apr  2 12:03 cpu0
drwxr-xr-x 4 root root    0 Feb 15 07:06 cpu1
drwxr-xr-x 4 root root    0 Feb 15 07:06 cpu2
drwxr-xr-x 4 root root    0 Feb 15 07:06 cpu3
drwxr-xr-x 4 root root    0 Feb 15 07:06 cpu4
drwxr-xr-x 4 root root    0 Feb 15 07:06 cpu5
drwxr-xr-x 4 root root    0 Feb 15 07:06 cpu6
drwxr-xr-x 4 root root    0 Feb 15 07:06 cpu7
-rw-r--r-- 1 root root 4096 Apr  2 12:03 sched_mc_power_savings
I've total 8 core cpu logically started from cpu0 to cpu7. To get more human readable format, try:
# lscpu
Sample outputs:
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                32
On-line CPU(s) list:   0-31
Thread(s) per core:    2
Core(s) per socket:    8
Socket(s):             2
NUMA node(s):          2
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 45
Stepping:              7
CPU MHz:               2000.209
BogoMIPS:              4001.65
Virtualization:        VT-x
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              20480K
NUMA node0 CPU(s):     0-7,16-23
NUMA node1 CPU(s):     8-15,24-31
Under each directory you would find an "online" file which is the control file to logically online/offline a processor.

How do I logically turn off (offline) cpu#6 ?

Warning: It is not possible to disable CPU0 on Linux systems i.e do not try to take cpu0 offline. Some architectures may have some special dependency on a certain CPU. For e.g in IA64 platforms we have ability to sent platform interrupts to the OS. a.k.a Corrected Platform Error Interrupts (CPEI). In current ACPI specifications, we didn't have a way to change the target CPU. Hence if the current ACPI version doesn't support such re-direction, we disable that CPU by making it not-removable. In such cases you will also notice that the online file is missing under cpu0.
Type the following command:
# echo 0 > /sys/devices/system/cpu/cpu6/online
# grep "processor" /proc/cpuinfo

How do I logically turn on (online) cpu#6 ?

Type the following command:
# echo 1 > /sys/devices/system/cpu/cpu6/online
# grep "processor" /proc/cpuinfo

Sample session:
Fig.01: Howto enable and disable CPU core in  on a multi-core server
Fig.01: Howto enable and disable CPU core in on a multi-core server

Once done, you can can actually remove CPU if your BIOS and server vendor supports such operation.

How do I verify cpu is online and offline?

Type the following cat command to see a list of cpus which are online:
# cat /sys/devices/system/cpu/online
To see a list of all offline cpus, run:
# cat /sys/devices/system/cpu/offline

Further readings:

No comments:

Post a Comment