Last active
March 23, 2021 10:25
-
-
Save nonamed01/4281b34c34fd36ab85ccb3b0ec246882 to your computer and use it in GitHub Desktop.
Enable or disable HT on any GNU/Linux system at runtime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#set -x | |
################################################################################# | |
# cores.sh | |
# 2021 by Toni Castillo Girona, uploaded to github. | |
# 2018 by Toni Castillo Girona | |
# <[email protected]> | |
################################################################################# | |
# Only report total number of physical processors and quit: | |
report=0 | |
# By default, it shows total and real cores for CPUs and it | |
# prints them using a SP separator. | |
NUMA=" " | |
# Don't disable ht: | |
htdisable=0 | |
htenable=0 | |
# Force enabling or disabling HT: | |
forceht=0 | |
usage (){ | |
echo "Usage: $0 [-t] [-c #CPU] [-r #CPU] [-d #CPU] [-e #CPU] [-f] [-h]" | |
echo " -t, reports total number of physical processors (nodes) and quits." | |
echo " -c #CPU, reports total and real cores for physical processor #CPU." | |
echo " -r #CPU, reports real cores for physical processor #CPU separated with commas." | |
echo " -d #CPU [-f], disables HT on the given #CPU (requires root)." | |
echo " -e #CPU [-f], enables HT on the given #CPU (requires root)." | |
echo " -h, show this help message." | |
echo " Example: $0 -t" | |
echo " $0 -c 2" | |
echo " sudo $0 -d 0" | |
echo " sudo $0 -d 0 -f (does not ask if we want to do it, it just does)" | |
echo " sudo $0 -e 0" | |
exit 0 | |
} | |
# Let's process the arguments first: | |
while getopts "tc:r:d:e:fh" opt; do | |
case "$opt" in | |
t) | |
report=1 | |
;; | |
c) | |
node=$OPTARG | |
;; | |
r) | |
node=$OPTARG | |
NUMA="," | |
;; | |
d) | |
node=$OPTARG | |
htdisable=1 | |
;; | |
e) | |
node=$OPTARG | |
htenable=1 | |
;; | |
f) | |
forceht=1 | |
;; | |
h) | |
usage | |
;; | |
esac | |
done | |
test $# -lt 1 && usage | |
# Do we have numactl installed? | |
if [ ! -x /usr/bin/numactl ]; then | |
echo "numactl is not installed. Install it first with:" | |
echo -ne "\tsudo apt-get install numactl\n" | |
exit 1 | |
fi | |
# Total number of physical cores: | |
totalcores=`numactl -H|grep -E "^node [0-9]+"|cut -d" " -f2|sort|uniq|wc -l` | |
echo "Total physical processors on this system: ${totalcores}" | |
test $report -eq 1 && exit 0 | |
# Get cores for node $node: | |
cores=`numactl -H|grep "node $node cpus"|cut -d":" -f2` | |
if [ -z "${cores}" ]; then | |
echo "Physical processor $node is not a valid physical cpu." | |
exit 0 | |
fi | |
for c in ${cores} | |
do | |
# grab this core information: | |
cinfo=`grep . /sys/devices/system/cpu/cpu${c}/topology/thread_siblings | tr : \\t | sed 's,^, ,'` | |
ccinfo="core:$c:${cinfo}" | |
cinfos="${cinfos} `echo ${ccinfo}|tr -d ' '`" | |
ccinfos="${ccinfos} ${cinfo}" | |
done | |
# This is the uniq list of cores, the real ones: | |
realcores=`echo $ccinfos|tr ' ' '\n'|sort|uniq` | |
# Show the number for the real cores: | |
for rcore in ${realcores}; do | |
# Get its number from the first list | |
rcores="${rcores}`echo $cinfos|tr ' ' '\n'|grep ":${rcore}"|head -1|cut -d":" -f2`${NUMA}" | |
done | |
# Get the list of virtualcores | |
# Obviously, when $cores == $rcores, no enabled virtual cores present: | |
vcores=`echo "${cores}, ${rcores}" | tr -d ','|tr ' ' '\n' | sort | uniq -u|tr '\n' ' '|tr -d ","` | |
# Show total cores and real cores: | |
test "${NUMA}" == " " && echo "Total Cores for processor $node: $cores" | |
test "${NUMA}" == " " && echo "Enabled Virtual Cores for processor $node: $vcores" | |
echo "Real Cores for processor $node: $rcores" | |
# Do we want to disable HT? | |
if [ $htdisable -eq 1 ]; then | |
# If no root, abort: | |
idu=`id -u` | |
if [ $idu != 0 ]; then | |
echo "To disable HT you need to be root". | |
exit 0 | |
fi | |
# If there are not virtual cores enabled, we cannot disable them! | |
if [ -z "$vcores" ]; then | |
echo -ne "All the virtual cores for processor $node already disabled!\n" | |
exit 1 | |
fi | |
# If we are not forcing the disabling of HT, we ask first: | |
if [ $forceht == 0 ]; then | |
echo "We are about to disable HT on physical cpu: $node" | |
echo "CPU cores to be disabled: $vcores" | |
echo "Are you sure? [yn]" | |
read doit | |
fi | |
# We disable HT if we said yes or if we are forcing it: | |
if [ "$doit" == "y" -o $forceht -eq 1 ]; then | |
# For every non-real core, disable it: | |
for vc in ${vcores}; do | |
echo 0 > /sys/devices/system/cpu/cpu${vc}/online | |
done | |
fi | |
fi | |
# Do we want to enable HT? | |
# This only makes sense when there are NOT enabled virtual cores : | |
if [ $htenable -eq 1 ]; then | |
# If no root, abort: | |
idu=`id -u` | |
if [ $idu != 0 ]; then | |
echo "To enable HT you need to be root". | |
exit 0 | |
fi | |
# No enabled virtual cores, so do nothing: | |
if [ ! -z "$vcores" ]; then | |
echo -ne "All the virtual cores for processor $node already enabled!\n" | |
exit 1 | |
fi | |
# Do we want to ask before enabling them? | |
if [ $forceht -eq 0 ]; then | |
echo "We are about to enable HT on physical cpu: $node" | |
echo "Are you sure? [yn]" | |
read doit | |
fi | |
if [ "$doit" == "y" -o $forceht -eq 1 ]; then | |
# Knowing the real cores, iterate thorugh all the cores and | |
# enable those that are disabled... | |
cpus=`ls -d /sys/devices/system/cpu/cpu*/online` | |
for c in ${cpus}; do | |
# If not enabled, we enable it: | |
enabled=`cat -v $c` | |
if [ $enabled -eq 0 ]; then | |
echo 1 > $c | |
#Add this core to the enabled virtual cores: | |
vc=`echo -ne $c|cut -d"/" -f6` | |
vcores="${vcores} ${vc:3}" | |
fi | |
done | |
# We now show all the enabled Virtual Cores: | |
echo "Enabled Virtual Cores on processor $node: ${vcores}" | |
fi | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment