-
-
Save m1st0/7f805c28ba2a28f8ce81cd8e770a460a to your computer and use it in GitHub Desktop.
Disable / Enable HyperThreading cores on runtime - linux
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 | |
# Turn hyperthreading on or off. | |
HYPERTHREADING=1 | |
RED="\033[0;31m" | |
GREEN="\033[0;32m" | |
YELLOW="\033[0;33m" | |
ENDCOLOR="\033[0m" | |
function toggleHyperThreading() { | |
for CPU in /sys/devices/system/cpu/cpu[0-9]*; do | |
CPUID=`basename ${CPU} | cut -b4-` | |
printf "${YELLOW}CPU: ${CPUID}${ENDCOLOR}\t" | |
[ -e ${CPU}/online ] && printf "1" > ${CPU}/online | |
THREAD1=`cat ${CPU}/topology/thread_siblings_list | cut -f1 -d,` | |
if [ ${CPUID} = ${THREAD1} ]; then | |
printf "${GREEN}-> enable${ENDCOLOR}\n" | |
[ -e ${CPU}/online ] && printf "1" > ${CPU}/online | |
else | |
if [ "${HYPERTHREADING}" -eq "0" ]; then printf "${YELLOW}-> disabled${ENDCOLOR}\n"; else printf "${GREEN}-> enable${ENDCOLOR}\n"; fi | |
printf "${HYPERTHREADING}" > ${CPU}/online | |
fi | |
done | |
} | |
function enabled() { | |
printf "${GREEN}Enabling HyperThreading${ENDCOLOR}\n" | |
HYPERTHREADING=1 | |
toggleHyperThreading | |
} | |
function disabled() { | |
printf "${YELLOW}Disabling HyperThreading${ENDCOLOR}\n" | |
HYPERTHREADING=0 | |
toggleHyperThreading | |
} | |
function check_privilege() { | |
WHOAMI=$(/usr/bin/whoami) | |
if [ "${WHOAMI}" != "root" ] ; then | |
printf "${RED}Script must be run with proper user priviledges.${ENDCOLOR}\n" | |
exit 1 | |
fi | |
} | |
ONLINE=$(cat /sys/devices/system/cpu/online) | |
OFFLINE=$(cat /sys/devices/system/cpu/offline) | |
printf "${YELLOW}%s\n" "---------------------------------------------------" | |
printf "CPU's online: ${GREEN}${ONLINE}${YELLOW}\t CPU's offline: ${RED}${OFFLINE}\n" | |
printf "${YELLOW}%s${ENDCOLOR}\n" "---------------------------------------------------" | |
check_privilege | |
read -p "Type in e to enable or d disable hyperThreading or q to quit [e/d/q] ?" ed | |
case ${ed} in | |
[Ee]* ) enabled; exit;; | |
[Dd]* ) disabled; exit;; | |
[Qq]* ) exit;; | |
* ) printf "${YELLOW}Please answer e for enable or d for disable hyperThreading.${ENDCOLOR}\n";; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment