initial commit

This commit is contained in:
Bret R. Human 2019-05-24 04:17:46 -04:00
commit 3ccc7b2f71
4 changed files with 137 additions and 0 deletions

51
LICENSE Normal file
View File

@ -0,0 +1,51 @@
smtCtrl
===
Copyright (c) 2018, Bret R. Human
All rights reserved.
Further documentation can be found at
https://psi.cynicaloptimist.me/Caffarius/smtCtrl/
Happy modding! -Bret
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
the documentation link and note, this list of conditions, and the following
disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this
software must display the following acknowledgment:
"Built using software developed by Cynical Optimist - https://cynicaloptimist.me
Ask what we can build for you at info@cynicaloptimist.me"
4. The names "Cynical Optimist" and "Bret R. Human" and the software name listed
at the top of this document, or the domain "cynicaloptimist.me" or any
affiliated service must not be used to endorse or promote products derived
from this software without prior written permission. For written permission,
contact info@cynicaloptimist.me.
5. Products derived from this software may not be called the software name
listed at the top of this document nor may "Cynical Optimist" or the software
name listed at the top of this document appear in their names without prior
written permission of Bret R. Human.
6. Redistributions of any form whatsoever must retain the following
acknowledgment:
"Built using software developed by Cynical Optimist - https://cynicaloptimist.me
Ask what we can build for you at info@cynicaloptimist.me"
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

5
README.md Normal file
View File

@ -0,0 +1,5 @@
# smtCtrl
Script that can enable/disable SMT at will on Ryzen systems. Includes optional systemd service to disable SMT before multi-user.target.
Ad astra per aspera, security experts.

12
disable-smt.service Normal file
View File

@ -0,0 +1,12 @@
[Unit]
Description=Disable Simultaneous Multithreading
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/root/.script/smt.sh disable
ExecStop=/root/.script/smt.sh status
[Install]
WantedBy=multi-user.target

69
smt.sh Normal file
View File

@ -0,0 +1,69 @@
#!/bin/bash
## Copyright © 2018 Bret Human
## https://cynicaloptimist.me/
##
## Documentation at:
## https://mage.cynicaloptimist.me/en/smtCtrl
## https://psi.cynicaloptimist.me/Caffarius/smtCtrl
##
## For questions or comments write:
## info@cynicaloptimist.me
#
# Try your best to control SMT on Ryzen CPUs.
# 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`
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
}
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
}
status() {
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