Nicolas LIMARE / pro / notes / 2014 / 11 / Disabling Hyper-threading and Frequency Scaling

Folow-up on my notes on code speedup. Two quick command to disable Hyper-threading and CPU frequency scaling whe you can't access the BIOS settings. Useful for max computational performance and consistent computation time. For Linux.

Hyper-threading

Hyper-threading (HT) refers to the hardware-backed capacity of Intel CPUs to efficiently switch between two execution threads. This is faster than a single thread at a time when these two threads are using different CPU ressources at the same moment. For the system (and users), a single CPU core appears as two "virtual" CPUs. But for a parallel computational code limited by the CPU throughput, HT can slow down your programs: there is only one floating-point unit per CPU core (FPU), trying to use it in two threads will thrash the CPU cache (and flush the pipelines?).

Disabling HT is easy. Reboot your computer, enter the BIOS menu and disable "Intel Hyper-Threading Technology". Restart the system, and now you only see half of the previous CPUs, but this time these are real, hardware CPUs.

When the machine is far away and you can't access the BIOS (some rental services for example), you can achieve something similar from the command-line: just instruct the system to ignore half of the CPUs. For example, to stop using (virtual) CPU N (as root, replace N with the CPU number):

echo 0 > /sys/devices/system/cpu/cpuN/online

You can do it automagically on system start with these entries in /etc/crontab with the special @reboot field:

# disable hyperthreading, cores 4-7
@reboot         root     echo 0 > /sys/devices/system/cpu/cpu4/online
@reboot         root     echo 0 > /sys/devices/system/cpu/cpu5/online
@reboot         root     echo 0 > /sys/devices/system/cpu/cpu6/online
@reboot         root     echo 0 > /sys/devices/system/cpu/cpu7/online

Re-enable the CPU with echo 1. To decide which CPU has to be disabled, get the list of "virtual" CPUs backed by the same CPU core from /sys/devices/system/cpu/cpuN/topology/thread_siblings_list.

CPU Frequency Scaling

With frequency scaling, (Intel SpeedStep, the CPU clock speed is low when the system is idle, and increases when the load increases. But you may not like the "slow start" effect when you want consistent computation time, especially for short computations. Here again, the best option is to disable this feature from the BIOS menu. If this is not possible, or if you just want to try it and save you batteries (and the climate) after, here again you can use the /sys/devices/systen/cpu... options. For example, to lock CPU N on its maximum speed (as root, replace N with the CPU number):

echo performance > /sys/devices/system/cpu/cpuN/cpufreq/scaling_governor

/etc/crontab entries to do it on reboot are similar:

# disable frequency scaling, cores 0-3
@reboot         root     echo performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
@reboot         root     echo performance > /sys/devices/system/cpu/cpu1/cpufreq/scaling_governor
@reboot         root     echo performance > /sys/devices/system/cpu/cpu2/cpufreq/scaling_governor
@reboot         root     echo performance > /sys/devices/system/cpu/cpu3/cpufreq/scaling_governor

Re-enable dynamic frequency scaling with echo ondemand. Be careful on laptops, a system daemon (such as cpufreqd) may be independently monitoring your activity and touching the frequency settings.