Skip to content

Instantly share code, notes, and snippets.

@samueljon
Last active August 18, 2023 20:05
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save samueljon/e7818edeb218f5e2f1e3e258949d04c8 to your computer and use it in GitHub Desktop.
Save samueljon/e7818edeb218f5e2f1e3e258949d04c8 to your computer and use it in GitHub Desktop.
Disable / Enable HyperThreading cores on runtime - linux
#!/bin/bash
HYPERTHREADING=1
function toggleHyperThreading() {
for CPU in /sys/devices/system/cpu/cpu[0-9]*; do
CPUID=`basename $CPU | cut -b4-`
echo -en "CPU: $CPUID\t"
[ -e $CPU/online ] && echo "1" > $CPU/online
THREAD1=`cat $CPU/topology/thread_siblings_list | cut -f1 -d,`
if [ $CPUID = $THREAD1 ]; then
echo "-> enable"
[ -e $CPU/online ] && echo "1" > $CPU/online
else
if [ "$HYPERTHREADING" -eq "0" ]; then echo "-> disabled"; else echo "-> enabled"; fi
echo "$HYPERTHREADING" > $CPU/online
fi
done
}
function enabled() {
echo -en "Enabling HyperThreading\n"
HYPERTHREADING=1
toggleHyperThreading
}
function disabled() {
echo -en "Disabling HyperThreading\n"
HYPERTHREADING=0
toggleHyperThreading
}
#
ONLINE=$(cat /sys/devices/system/cpu/online)
OFFLINE=$(cat /sys/devices/system/cpu/offline)
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
echo "---------------------------------------------------"
echo -en "CPU's online: $ONLINE\t CPU's offline: $OFFLINE\n"
echo "---------------------------------------------------"
while true; do
read -p "Type in e to enable or d disable hyperThreading or q to quit [e/d/q] ?" ed
case $ed in
[Ee]* ) enabled; break;;
[Dd]* ) disabled;exit;;
[Qq]* ) exit;;
* ) echo "Please answer e for enable or d for disable hyperThreading.";;
esac
done
@gabrielctn
Copy link

Nice, thank you for this usefull snippet ! 👍

@samueljon
Copy link
Author

Nice to know that you found it useful @gabrielctn

@AntonBikineev
Copy link

Thanks, indeed very useful!

@ha3ant
Copy link

ha3ant commented Oct 25, 2020

hi sir, I'm beginner in Linux, how can I use this bash file?

@samueljon
Copy link
Author

@ha3ant I would click the "raw" button at the top of the script and copy the contents of the script. Then on the machine where you will be running the script open a file editor where you will paste the content and save as f.ex togglHt.sh. Then you would make the script executable by issuing following command in a terminal chmod +x toggleHT.sh. After that you should be able to run it from the directory where you saved it f.ex. ./toggleHT.sh

@zhongshanxu
Copy link

Great!

@nevil02
Copy link

nevil02 commented Feb 28, 2022

this is givinig me prermission denied:
bash: /sys/devices/system/cpu/cpu6/online: Permission denied
for all cpus

@samueljon
Copy link
Author

@nevil02 you must run this script as root. I have updated the script to check if the user running the script is root, otherwise printing "The script must be run as root". You could do sudo ./toogleHT.sh or become root to execute.

@nevil02
Copy link

nevil02 commented Feb 28, 2022

it is giving me following message
sudo: ./hyperthreading-on-off.sh: command not foun

@samueljon
Copy link
Author

@nevil02 did you create the script as hyperthreading-on-off.sh and have you made it executable by doing chmod +x hyperthreading-on-off.sh before running it ?

@nevil02
Copy link

nevil02 commented Feb 28, 2022

done, Thank you

@samueljon
Copy link
Author

@nevil02 my pleasure 👍

@LePingKYXK
Copy link

Hi, thank you for sharing this code.
I was wondering that if my CPU has 16 cores, should I change the code cpu[0-9] in the 6th line into cpu[0-15]?

@LePingKYXK
Copy link

@ha3ant I would click the "raw" button at the top of the script and copy the contents of the script. Then on the machine where you will be running the script open a file editor where you will paste the content and save as f.ex togglHt.sh. Then you would make the script executable by issuing following command in a terminal chmod +x toggleHT.sh. After that you should be able to run it from the directory where you saved it f.ex. ./toggleHT.sh

Hi, I am not sure what's the meaning of "save as f.ex toggIHt.sh".
The coed is a bash code, and it would be as toggIHt.sh. What is the f.ex mean?

@samueljon
Copy link
Author

@LePingKYXK i do not think you need to change code in line 6. I did test this with more than ten cpu's. F.ex means for example.

@LePingKYXK
Copy link

@LePingKYXK i do not think you need to change code in line 6. I did test this with more than ten cpu's. F.ex means for example.

Thanks a lot. It works well.

@kerwinyin
Copy link

prefect,thanks

@blazer502
Copy link

Perfect, thanks a lot.

@RohanKapurDEV
Copy link

hey this actually works, nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment