Absolutely Tech

[How-To] Disable CPU cores in Linux

By: Deepak Mittal

Although there’s no reason to do this, but some of you might have problem with apps not running well in multi-core environment or maybe looking to save some power (although disabling cores on sandy-bridge CPUs won’t save much power), I’ve written this simple tutorial to disable CPU cores in Linux.

I have i7 2630QM process which has 4 physical cores and 4 logical cores. I’ll show you how to make it act like a dual core machine instead.

Method 1:
This method is dynamic and doesn’t require a reboot. You can just open a terminal and try this out:

  • sudo sh -c "echo 0 > /sys/devices/system/cpu7/online"
    sudo sh -c "echo 0 > /sys/devices/system/cpu6/online"

    Repeat the above steps for cpu2 to cpu7 and it’ll leave you with cpu0 and cpu1 active which is essentially what we’re trying to achieve.

Method 2:
This method will make the linux boot with 2 cores which might make kernel more optimized for dual core environment than the method above.

  • Add maxcpus=2 to GRUB command line by doing the following:
    gksu gedit /etc/default/grub

    Find:

    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"

    and change it to

    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash maxcpus=2"
  • Then run:
    sudo update-grub

    When you reboot, linux will run on 2 cores.

Category: Ubuntu (Linux)

Tagged:

  • meowsqueak

    I did some experiments and it seems that disabling half the cores (on a quad-core Sandybridge) seems to *increase* idle power consumption by about 30%, which is quite unexpected and simple to verify with ‘powertop’ and your commands above.

  • Me

    Sorry to say it but that ‘tutorial’ is about as clear as mud – you don’t explain specifically what cpu is effected, just a vague “do this…”.  Think before you type…

    • http://www.geekdevs.com Deepak Mittal

      What part of it don’t you understand?

      `sudo sh -c “echo 0 > /sys/devices/system/cpu7/online”
      sudo sh -c “echo 0 > /sys/devices/system/cpu6/online”`

      It clearly shows cpu6 and cpu7 are being disabled. :|

    • http://gadgeticmusings.net/ Soul_Est

      “Repeat the above steps for cpu2 to cpu7 and it’ll leave you with cpu0
      and cpu1 active which is essentially what we’re trying to achieve.”

      What part of that is as clear as mud? Perhaps you should think before you type.

  • http://www.facebook.com/cbekar Can Bekar

    Maybe someone can help: “My machine has two Xeon CPU’s making 24 virtual CPU’s, and I run RHEL on top of it. I’m running a monitor for checking a multithreaded application’s memory accesses, the problem I have is cross-CPU serialisation on a mutex. I want to disable one of the CPU’s since I apparently cannot set affinity of my threads that I’m monitoring under RHEL. As a workaround, I wanted to set maxcpu’s from the bootloader so that I would end up with a single socket. BUT it didn’t work, since the latter 6 cpu’s of the 12 came from the second CPU, that is, I’ve manually disabled the virtual CPU’s. Then finally I found this site and disabled my second CPU on the fly. So finally, I had CPU0-5 and CPU12-17 running. HOWEVER, RHEL cannot schedule my threads to SMT’s showing up as CPU12-17! Do you have any suggestions, any further workarounds for the explained workarounds?”

  • chutsu

    I think you made a slight typo, its 8 logical cores not 4. You might also want to add a bit of explanation on why a quad core may have cpu-0 to cpu-7; its because the cpu-* represents logical* cpu rather than physical*. But otherwise good article :)

  • Larry Pyeatt

    Thanks for posting this. I have a machine with two 8-core chips. The CPU cooler on one of them went out. I was able to use this to disable all of the cores on that chip and keep the machine running until a new cooler arrived.

    • http://www.geekdevs.com Deepak Mittal

      Glad to help!

=