-
-
Save samueljon/e7818edeb218f5e2f1e3e258949d04c8 to your computer and use it in GitHub Desktop.
#!/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 |
Nice to know that you found it useful @gabrielctn
Thanks, indeed very useful!
hi sir, I'm beginner in Linux, how can I use this bash file?
@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
Great!
this is givinig me prermission denied:
bash: /sys/devices/system/cpu/cpu6/online: Permission denied
for all cpus
@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.
it is giving me following message
sudo: ./hyperthreading-on-off.sh: command not foun
@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 ?
done, Thank you
@nevil02 my pleasure 👍
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]
?
@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?
@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 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.
prefect,thanks
Perfect, thanks a lot.
hey this actually works, nice!
Works like a charm on my old Slackare 14.2 with a i7-4810mq - Thanks!
@samueljon Would it be possibel for you to add a few checks?
- Check if CPU is HT capable
- Check if HT is enabled or disabled
- Check for availabe cores and threads per cores
Edit: Did some work here...
https://gist.github.com/rorar/cebde935cf4dfa6389df96c4df79ce38
Not perfect, still need polishing (colored text output, disable selected cores,...) . Do you want to give it a try?
@samueljon Would it be possibel for you to add a few checks?
- Check if CPU is HT capable
- Check if HT is enabled or disabled
- Check for availabe cores and threads per cores
Edit: Did some work here... https://gist.github.com/rorar/cebde935cf4dfa6389df96c4df79ce38 Not perfect, still need polishing (colored text output, disable selected cores,...) . Do you want to give it a try?
Nice I will have a look when I can @rorar . Thanks for the inspiration in your implementation.
Nice, thank you for this usefull snippet ! 👍