#!/bin/bash # # cpu-ht-enable [0|1] # # Turn all hyperthreading cores on or off. # # See # https://www.golinuxhub.com/2018/01/how-to-disable-or-enable-hyper.html # https://serverfault.com/questions/235825/disable-hyperthreading-from-within-linux-no-access-to-bios # https://askubuntu.com/questions/942728/disable-hyper-threading-in-ubuntu # # Stress tests: # stress-ng --cpu 12 --io 1 --vm 1 --timeout 10s # --perf # --vm-bytes 128M # --log-file OUTPUT # # Copyright (C) by Volker Kuhlmann # http://volker.top.geek.nz/contact.html # All rights reserved. # Released under the terms of the GNU General Public License (GPL) Version 2. # See http://www.gnu.org/ for details. # # Volker Kuhlmann # 1.0, 20 May 2019 Created. # 1.1, 22 May 2019 Stress test comments. Help output. Exit !=0 if error. # Help() { echo " Usage: ${0##*/} [0|1|off|on] " } show_states() { echo " CPUs present: `cat /sys/devices/system/cpu/present` CPUs online: `cat /sys/devices/system/cpu/online` CPUs offline: `cat /sys/devices/system/cpu/offline` " } cpu_off() { local file="/sys/devices/system/cpu/cpu0/topology/thread_siblings_list" if [ ! -e "$file" ]; then echo 1>&2 "Doesn't exist: $file" exit 2 fi local cpu ret=0 for cpu in $( grep . /sys/devices/system/cpu/cpu*/topology/thread_siblings_list \ | sort -n -t ',' -k 2 -u \ | grep , \ | cut -d, -f2 ); do echo "CPU $cpu -> $state" echo -n "$state" >"/sys/devices/system/cpu/cpu$cpu/online" ret=$? test $ret -eq 0 || return $ret done } cpu_on() { local cpu cpuon state ret=0 for cpuon in $( ls -1 /sys/devices/system/cpu/cpu*/online \ | sort -t/ -k 6.4n ); do cpu="${cpuon#/sys/devices/system/cpu/cpu}" cpu="${cpu%%/*}" state="`cat /sys/devices/system/cpu/cpu$cpu/online`" test "$state" -eq 1 && continue state=1 echo "CPU $cpu -> $state" echo -n "$state" >"/sys/devices/system/cpu/cpu$cpu/online" ret=$? test $ret -eq 0 || return $ret done } cpu_on_off() { local state="$1" case "$state" in 0|[Oo]ff) state=0 cpu_off ;; 1|[Oo]n) state=1 cpu_on ;; "") Help exit ;; *) Help exit 1 ;; esac } show_states cpu_on_off "$@" || exit show_states exit grep -q '^flags.*[[:space:]]ht[[:space:]]' /proc/cpuinfo grep -H . /sys/devices/system/cpu/cpu*/topology/thread_siblings_list | sort -n -t ',' -k 2 -u grep -H . /sys/devices/system/cpu/cpu*/topology/thread_siblings_list | sort -n -t ',' -k 2 -u | grep , | cut -d, -f2 cat /sys/devices/system/cpu/cpu*/online more \ /sys/devices/system/cpu/present \ /sys/devices/system/cpu/online \ /sys/devices/system/cpu/offline