diff --git a/smt.sh b/smt.sh index 222c242..18981e6 100644 --- a/smt.sh +++ b/smt.sh @@ -1,5 +1,5 @@ #!/bin/bash -## Copyright © 2018 Bret Human +## Copyright © 2018-2019 Bret Human ## https://cynicaloptimist.me/ ## ## Documentation at: @@ -9,61 +9,49 @@ ## For questions or comments write: ## info@cynicaloptimist.me # -# Try your best to control SMT on Ryzen CPUs. +# Try your best to control SMT on Ryzen CPUs (and maybe others?) # Usage: ./smt.sh (enable|disable|status) num_cores=`lscpu | grep "CPU(s):" | grep -v "," | grep -v "-" | cut -d: -f2 | awk '{print $1}'` num_procs=`nproc` +coreLoop() { + i="1" + while [ "${i}" -lt "${num_cores}" ]; do + echo "$1" > /sys/devices/system/cpu/cpu${i}/online + i=$[ $i + 2 ] + done + echo "Done." + return 0 +} + disable() { - if [ "${num_procs}" -lt "${num_cores}" ]; then - echo "SMT has already been disabled!" - else - echo -n "Disabling SMT: " - i="1" - while [ "${i}" -lt "${num_cores}" ]; do - echo "0" > /sys/devices/system/cpu/cpu${i}/online - i=$[ $i + 2 ] - done - unset i - echo "Done." - return 0 - fi - return 1 + if [ "${num_procs}" -lt "${num_cores}" ]; then + echo "SMT has already been disabled!" + return 1 + else + echo -n "Disabling SMT: " + coreLoop 0 + fi } enable() { - if [ "${num_procs}" -eq "${num_cores}" ]; then - echo "SMT has already been enabled!" - else - echo -n "Enabling SMT: " - i="1" - while [ "${i}" -lt "${num_cores}" ]; do - echo "1" > /sys/devices/system/cpu/cpu${i}/online - i=$[ $i + 2 ] - done - unset i - echo "Done." - return 0 - fi - return 1 + if [ "${num_procs}" -eq "${num_cores}" ]; then + echo "SMT has already been enabled!" + return 1 + else + echo -n "Enabling SMT: " + coreLoop 1 + fi } status() { - cat /proc/cpuinfo |egrep "processor|physical id|core id" | sed 's/^processor/\nprocessor/g' + cat /proc/cpuinfo |egrep "processor|physical id|core id" | sed 's/^processor/\nprocessor/g' } case "$1" in - disable) - disable - ;; - enable) - enable - ;; - status) - status - ;; - *) - echo " Usage: ./smt.sh (disable | enable | status)" - ;; -esac + disable) disable;; + enable) enable;; + status) status;; + *) echo " Usage: ./smt.sh (disable | enable | status)";; +esac \ No newline at end of file