Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)
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

This comment has been minimized.

Copy link

@gabrielctn gabrielctn commented Jun 2, 2020

Nice, thank you for this usefull snippet ! 👍

@samueljon

This comment has been minimized.

Copy link
Owner Author

@samueljon samueljon commented Jun 2, 2020

Nice to know that you found it useful @gabrielctn

@AntonBikineev

This comment has been minimized.

Copy link

@AntonBikineev AntonBikineev commented Jun 30, 2020

Thanks, indeed very useful!

@ha3ant

This comment has been minimized.

Copy link

@ha3ant ha3ant commented Oct 25, 2020

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

@samueljon

This comment has been minimized.

Copy link
Owner Author

@samueljon samueljon commented Oct 26, 2020

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.