Enable/Disable CPUs dynamically in Linux

Fri 09 May 2008 by Thejaswi Puthraya

Enable/Disable Multi CPUs in GNU/Linux

This is a wonderful feature of the Linux kernel. Being able to dynamically enable and disable multiple CPUs (all this without rebooting).

To check if you have multiple CPUs

$ cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 15
model name      : Intel(R) Core(TM)2 Duo CPU     T7100  @ 1.80GHz
stepping        : 13
cpu MHz         : 1795.675
cache size      : 2048 KB
physical id     : 0
siblings        : 2
core id         : 0
cpu cores       : 2
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 10
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov
                  pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe
                  nx lm constant_tsc arch_perfmon pebs bts pni monitor ds_cpl
                  vmx est tm2 ssse3 cx16 xtpr lahf_lm ida
bogomips        : 3596.78
clflush size    : 64

processor       : 1
vendor_id       : GenuineIntel
cpu family      : 6
model           : 15
model name      : Intel(R) Core(TM)2 Duo CPU     T7100  @ 1.80GHz
stepping        : 13
cpu MHz         : 1795.675
cache size      : 2048 KB
physical id     : 0
siblings        : 2
core id         : 1
cpu cores       : 2
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 10
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov
                  pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe
                  nx lm constant_tsc arch_perfmon pebs bts pni monitor ds_cpl
                  vmx est tm2 ssse3 cx16 xtpr lahf_lm ida
bogomips        : 3591.16
clflush size    : 64

So I have two cpus. Now to dynamically disable the second CPU.

# echo 0 > /sys/devices/system/cpu/cpuX/online

Here X is any number between 1 and the maximum number of CPUs. Observe the output of cpuinfo to check if the previous step worked.

To dynamically enable it,

# echo 1 > /sys/devices/system/cpu/cpuX/online

Note: Not all CPU architectures have support for this feature.

There are a lot of advantages of this feature:

  • Helps in benchmark tests (when you want to know the performance of your code over multiple and single cpus).
  • Helps you save that extra bit of power (esp on battery) in desparate situations.