Search code examples
linuxmulticoreprocessorhardware

Programmatically disable CPU core


It is known the way to disable logical CPUs in Linux, basically with echo 0 > /sys/devices/system/cpu/cpu<number>/online. This way, you are only telling to the OS to ignore that given (<number>) CPU.

My question goes further, is it possible not only to ignore it but to turn it off physically programmatically? I want that CPU to not receive any power, in order to make its energy consumption zero.

I know that it is possible disable cores from the BIOS (not always), but I want to know whether is possible to do it within a certain program or not.


Solution

  • When you do echo 0 > /sys/devices/system/cpu/cpu<number>/online, what happens next depends on the particular CPU. On ARM embedded systems the kernel will typically disable the clock that drives the particular core PLL so effectively you get what you want.

    On Intel X86 systems, you can only disable the interrupts and call the hlt instruction (which Linux Kernel does). This effectively puts CPU to the power-saving state until it is woken up by another CPU at user request. If you have a laptop, you can verify that power draw indeed goes down when you disable the core by reading the power from /sys/class/power_supply/BAT{0,1}/current_now (or uevent for all values such as voltage) or using the "powertop" utility.

    For example, here's the call chain for disabling the CPU core in Linux Kernel for Intel CPUs. https://github.com/torvalds/linux/blob/master/drivers/cpufreq/intel_pstate.c

    arch/x86/kernel/smp.c: smp_ops.play_dead = native_play_dead,

    arch/x86/kernel/smpboot.c : native_play_dead() -> play_dead_common() -> local_irq_disable()

    Before that, CPUFREQ also sets the CPU to the lowest power consumption level before disabling it though this does not seem to be strictly necessary.

    intel_pstate_stop_cpu -> intel_cpufreq_stop_cpu -> intel_pstate_set_min_pstate -> intel_pstate_set_pstate -> wrmsrl_on_cpu(cpu->cpu, MSR_IA32_PERF_CTL, pstate_funcs.get_val(cpu, pstate));

    On Intel X86 there does not seem to be an official way to disable the actual clocks and voltage regulators. Even if there was, it would be specific to the motherboard and thus your closest bet might be looking into BIOS such as coreboot. Hmm, I realized I have no idea about Intel except looking into kernel sources.