Linux Hotplug a CPU

by Vivek Gite · 11 comments

How do I hotplug a CPU on a running Linux system? I would like to dynamically enable or disable a CPU on a running system?

Linux kernel does supports cpu-hotplug mechanism. You can enable or disable CPU 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. 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.

List all current cpus 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

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 ?

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

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

Further readings:

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 11 comments… read them below or add one }

1 Akshay Sulakhe 04.03.09 at 1:57 pm

How to do such a similar thing for a hard disk…Juts unmount it or anything else is necessaey to remove and then add other???

2 Vivek Gite 04.03.09 at 2:29 pm

Just unmount it and if it is hotplug just replace it.

3 Miker 04.03.09 at 2:32 pm

I’m really curious about this. Can anyone comment if they’ve actually tried it? I’m not sure if any of the x86_(64) systems I’ve ever worked with support this capability – anyone know of systems that do?

4 Vivek Gite 04.03.09 at 3:01 pm

You need high end servers such as IBM Power6 series to actually add or remove CPU / memory without rebooting the server. This is called Capacity Upgrade on Demand (CUoD). Other vendor such as Sun and HP also provides the same stuff. Your average server only support hard disk hot-plug. Another example is HP ProLiant ML570 G2 business server powered by Intel Xeon CPUs. Ask, your vendor for CUoD aware server and you will get the list. Be prepared to pay extra for all these fancy features ;)

HTH

5 jrush 04.19.09 at 1:09 pm

It’s really works !
On my desktop Atom 330 (dual core + hyperthreading = 4 CPU), i can disable 3 CPUs of 4 :)

6 Stephan 04.19.09 at 3:57 pm

I tried to offline a core in my i386 (kernel 2.6), but I was notified a permission denied.

stefano@stefano-laptop:~$ echo 1 > /sys/devices/system/cpu/cpu1/online
bash: /sys/devices/system/cpu/cpu1/online: Permission denied
stefano@stefano-laptop:~$ sudo echo 1 > /sys/devices/system/cpu/cpu1/online
bash: /sys/devices/system/cpu/cpu1/online: Permission denied

Can anybody tell me y? Is it by default enabled in the latest versions?
Thx

S.

7 Vivek Gite 04.19.09 at 4:03 pm

You must be root user to hotplug a CPU.

8 Stephan 04.20.09 at 7:34 am

I also tried with sudo, as I reported in the post, but didn’t work.
Moreover, in visudo I’ve set:

stefano ALL=(ALL) ALL

Thx.

S.

9 Stephan 04.20.09 at 11:03 am

Ok, I’v managed.
Thx.
S.

10 mangoo 07.01.09 at 9:42 am

Not only high-end servers – CPU/memory hotplugging is often used in virtualized systems.

11 Fabian Kempers 09.08.09 at 11:15 am

I have put cpu 1 to 3 offline.
When I online a cpu I get the following message:
(root @ rhel5)
# echo 1 > /sys/devices/system/cpu/cpu1/online
-bash: echo: write error: Invalid argument

Anyone else got the same message and a solution to this problem?

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous FAQ:

Next FAQ:

nixCraft FAQ PDF Collection Now Available To All