extend zgoc

This commit is contained in:
zawz 2020-04-18 16:51:22 +02:00
parent 86431b9abd
commit f8c7032ad9
2 changed files with 152 additions and 154 deletions

View file

@ -1,35 +1,54 @@
#!/bin/bash #!/bin/bash
config_file="/usr/share/z/config/zgoc.conf" # config file
card_name=$( grep "GPU_NAME=" "$config_file" | cut -d '=' -f2-) config_path="/etc/zgoc"
pci_id=$(grep "GPU_PCI_ID=" "$config_file" | cut -d '=' -f2-) config_file="$config_path/zgoc.conf"
profile_path=$(grep "FANC_PROFILE_PATH=" "$config_file" | cut -d '=' -f2-)
default_profile=$(grep "FANC_DEFAULT_PROFILE=" "$config_file" | cut -d '=' -f2-)
time_interval=$(grep "FANC_TIME_INTERVAL=" "$config_file" | cut -d '=' -f2-)
temps=( 50 60 70 80 85 90 95 ) # in °C # load config
fans=( 0 15 30 40 55 75 100 ) # in % [ -f "$config_file" ] && . "$config_file"
if [ "${#temps[@]}" -ne "${#fans[@]}" ] # default conf
then [ -z "$FANC_TIME_INTERVAL" ] && FANC_TIME_INTERVAL=2
error "Amount of temperature and pwm values does not match" [ -z "$FANC_DEFAULT_PROFILE" ] && FANC_DEFAULT_PROFILE=2
exit 30 [ -z "$FANC_PROFILE_PATH" ] && FANC_PROFILE_PATH=profiles/power
fi [ $(echo "$FANC_PROFILE_PATH" | cut -c1) != '/' ] && FANC_PROFILE_PATH="$config_path/$FANC_PROFILE_PATH"
# load profile
profile=$FANC_PROFILE_PATH/$FANC_DEFAULT_PROFILE
[ ! -f "$profile" ] && echo "'$profile' not found" >&2 && exit 1
{
read -r temps
read -r fans
} < "$profile"
NT=$(echo "$temps" | awk '{print NF}')
NF=$(echo "$fans" | awk '{print NF}')
error () { error () {
printf "\033[1;31m%s\033[0m\n" "$1" >&2 printf "\033[1;31m%s\033[0m\n" "$1" >&2
} }
file_report () if [ "$NT" -ne "$NF" ]
then
error "Amount of temperature and pwm values do not match"
exit 30
fi
# $1 = array , $2 = rank
getval()
{
echo "$1" | awk "{print \$$(($2+1))}"
}
file_report()
{ {
if [ -f "$(pwd)/$1" ] if [ -f "$(pwd)/$1" ]
then then
echo -n "$(pwd)/$1 = " echo "$(pwd)/$1 = $(cat "$(pwd)/$1")"
cat "$(pwd)/$1"
elif [ -f "$1" ] elif [ -f "$1" ]
then then
echo -n "$1 = " echo "$1 = $(cat "$1")"
cat "$1"
else else
error "File $1 from $(pwd) unaccessible" error "File $1 from $(pwd) unaccessible"
fi fi
@ -48,28 +67,28 @@ file_write ()
_stop() { _stop() {
echo "" echo ""
file_write 2 pwm1_enable file_write 2 pwm1_enable
exit exit $1
} }
if [[ $(lspci | grep -c VGA) -gt 1 ]] if [[ $(lspci | grep -c VGA) -gt 1 ]]
then then
if [[ -z "$card_name" ]] && [[ -z "$pci_id" ]] ; then if [[ -z "$GPU_NAME" ]] && [[ -z "$GPU_PCI_ID" ]] ; then
error "Several GPUs are present and no GPU is specified" error "Several GPUs are present and no GPU is specified"
exit 20 exit 20
fi fi
if [ -z "$pci_id" ] ; then if [ -z "$GPU_PCI_ID" ] ; then
pci_id=$(lspci | grep VGA | grep "$card_name" | cut -d ' ' -f1) GPU_PCI_ID=$(lspci | grep VGA | grep "$GPU_NAME" | cut -d ' ' -f1)
if [ "$(echo "$pci_id" | wc -l)" -gt 1 ] ; then if [ "$(echo "$GPU_PCI_ID" | wc -l)" -gt 1 ] ; then
error "More than one name match" error "More than one name match"
exit 21 exit 21
elif [ "$(echo "$pci_id" | wc -l)" -lt 1 ] ; then elif [ "$(echo "$GPU_PCI_ID" | wc -l)" -lt 1 ] ; then
error "No name match" error "No name match"
exit 22 exit 22
fi fi
fi fi
cd /sys/class/drm || exit cd /sys/class/drm || exit
if ! cd "$(ls -l card? | grep "$pci_id" | tr -s ' ' | cut -d ' ' -f9)" if ! cd "$(ls -l card? | grep "$GPU_PCI_ID" | tr -s ' ' | cut -d ' ' -f9)"
then then
error "Error finding pci device" error "Error finding pci device"
echo 23 echo 23
@ -99,34 +118,42 @@ file_write 1 pwm1_enable
while true while true
do do
temp=$(zgpu -g0 -Wnt | cut -d '.' -f1) sleep "$FANC_TIME_INTERVAL" &
temp=$(zgpu -g0 -Wnt -i0 | cut -d '.' -f1 | tr -d '\n')
I=1 I=1
if [ "$temp" -lt "${temps[0]}" ] if [ "$temp" -lt "$(getval "$temps" 0)" ]
then then
fan=${fans[0]} fan=$(getval "$fans" 0)
else else
while [[ $I -lt "${#temps[@]}" ]] while [ $I -lt $NT ]
do do
if [ "$temp" -lt "${temps[$I]}" ] if [ $temp -lt $(getval "$temps" $I) ]
then then
LOWERTEMP=${temps[$I-1]} LOWERTEMP=$(getval "$temps" $((I-1)))
HIGHERTEMP=${temps[$I]} HIGHERTEMP=$(getval "$temps" $I)
LOWERPWM=${fans[$I-1]} LOWERPWM=$(getval "$fans" $((I-1)))
HIGHERPWM=${fans[$I]} HIGHERPWM=$(getval "$fans" $I)
fan=$(echo "( ( $temp - $LOWERTEMP ) * ( $HIGHERPWM - $LOWERPWM ) / ( $HIGHERTEMP - $LOWERTEMP ) ) + $LOWERPWM" | bc) fan=$(echo "( ( $temp - $LOWERTEMP ) * ( $HIGHERPWM - $LOWERPWM ) / ( $HIGHERTEMP - $LOWERTEMP ) ) + $LOWERPWM" | bc)
I=${#temps[@]} I=$NT
fi fi
I=$((I + 1)) I=$(($I + 1))
done done
fi fi
if [ -z "$fan" ] if [ -z "$fan" ]
then then
fan=${fans[$((I - 1))]} fan=$(getval "$fans" $((I-1)))
fi fi
fan_max=$(cat pwm1_max) # get min/max values
fan_min=$(cat pwm1_min) fan_max=$(cat pwm1_max) && fan_min=$(cat pwm1_min) || _stop 1
file_write "$(echo "( ( $fan * ($fan_max - $fan_min) ) / 100 ) + $fan_min" | bc)" pwm1 # scale to min/max
sleep "$time_interval" fan=$(echo "( ( $fan * ($fan_max - $fan_min) ) / 100 ) + $fan_min" | bc)
# smooth with oldval
[ -z "$oldfan" ] && oldfan=$fan
[ "$FANC_SMOOTH" = "true" ] && fan=$(echo "($oldfan + $fan) / 2" | bc)
oldfan=$fan
# write
file_write "$fan" pwm1
wait $(jobs -p)
done done
exit 0 _stop 0

195
zgoc/zgoc
View file

@ -5,45 +5,55 @@
# Only radeon GPUs supported # Only radeon GPUs supported
# #
config_file="/usr/share/z/config/zgoc.conf" config_path="/etc/zgoc"
config_file="$config_path/zgoc.conf"
# generate default conf file
if [ ! -f "$config_file" ] if [ ! -f "$config_file" ]
then then
echo'# zgoc config file mkdir -p "$config_path" 2>/dev/null && touch "$config_file" 2>/dev/null &&\
GPU_PROFILE_PATH=/usr/share/z/zgoc/profiles/gpu echo "# zgoc config file
GPU_MEM_PATH=/usr/share/z/zgoc/profiles/mem GPU_PROFILE_PATH=profiles/gpu
GPU_POWER_PATH=/usr/share/z/zgoc/profiles/power GPU_MEM_PATH=profiles/mem
GPU_POWER_PATH=profiles/power
GPU_NAME= GPU_NAME=
GPU_PCI_ID= GPU_PCI_ID=
GPU_AUTOMEMFIX=true GPU_AUTOMEMFIX=true
GPU_MIN_POWER=30 GPU_MIN_POWER=30
FANC_PROFILE_PATH=/usr/share/z/zgoc/profiles/fan FANC_PROFILE_PATH=profiles/fan
FANC_DEFAULT_PROFILE=default FANC_DEFAULT_PROFILE=default
FANC_TIME_INTERVAL=2' > "$config_file" FANC_TIME_INTERVAL=2
FANC_SMOOTH=true" > "$config_file" 2>/dev/null
fi fi
profile_path=$(grep "GPU_PROFILE_PATH=" "$config_file" | cut -d '=' -f2-) # load config file
mem_path=$(grep "GPU_MEM_PATH=" "$config_file" | cut -d '=' -f2-) [ -f "$config_file" ] && . "$config_file"
power_path=$(grep "GPU_POWER_PATH=" "$config_file" | cut -d '=' -f2-)
card_name=$( grep "GPU_NAME=" "$config_file" | cut -d '=' -f2-) # default path values
pci_id=$(grep "GPU_PCI_ID=" "$config_file" | cut -d '=' -f2-) [ -z "$GPU_PROFILE_PATH" ] && GPU_PROFILE_PATH=profiles/gpu
automemfix=$(grep "GPU_AUTOMEMFIX=" "$config_file" | cut -d '=' -f2-) [ -z "$GPU_MEM_PATH" ] && GPU_MEM_PATH=profiles/mem
min_power=$(grep "GPU_MIN_POWER=" "$config_file" | cut -d '=' -f2-) [ -z "$GPU_POWER_PATH" ] && GPU_POWER_PATH=profiles/power
if [ ! "$automemfix" = "true" ]
# relative path resolution
[ $(echo "$GPU_PROFILE_PATH" | cut -c1) != '/' ] && GPU_PROFILE_PATH="$config_path/$GPU_PROFILE_PATH"
[ $(echo "$GPU_MEM_PATH" | cut -c1) != '/' ] && GPU_MEM_PATH="$config_path/$GPU_MEM_PATH"
[ $(echo "$GPU_POWER_PATH" | cut -c1) != '/' ] && GPU_POWER_PATH="$config_path/$GPU_POWER_PATH"
if [ "$GPU_AUTOMEMFIX" != "true" ]
then then
automemfix="" GPU_AUTOMEMFIX=""
fi fi
usage() { usage() {
echo "Usage: zgoc <arguments>" echo "Usage: zgoc <arguments>"
echo "Arguments:" echo "Arguments:"
echo " print : show current state" echo " print show current state"
echo " gpu : GPU overclocking" echo " gpu GPU overclocking"
echo " mem : memory overclocking" echo " mem memory overclocking"
echo " apply : apply GPU and memory profiles" echo " apply apply GPU and memory profiles"
echo " reset : reset GPU and memory to stock values" echo " reset reset GPU and memory to stock values"
echo " memfix : fix screen artifacting caused by low memory state" echo " memfix fix screen artifacting caused by low memory state"
echo " power : power cap" echo " power power cap"
echo "" echo ""
echo "Config file: $config_file" echo "Config file: $config_file"
} }
@ -162,7 +172,7 @@ apply () {
root_check root_check
echo "c" > "$path/device/pp_od_clk_voltage" echo "c" > "$path/device/pp_od_clk_voltage"
echo "Profile applied" echo "Profile applied"
if [ -n "$automemfix" ] ; then if [ -n "$GPU_AUTOMEMFIX" ] ; then
force_mem_state_high force_mem_state_high
fi fi
} }
@ -179,7 +189,7 @@ apply_power () {
if ! echo "$1" | grep -Eq '^[0-9]+$' ; then if ! echo "$1" | grep -Eq '^[0-9]+$' ; then
echo "Invalid power value" >&2 echo "Invalid power value" >&2
exit 2 exit 2
elif [ "$1" -lt "$min_power" ] ; then elif [ "$1" -lt "$GPU_MIN_POWER" ] ; then
echo "Power value too low" >&2 echo "Power value too low" >&2
exit 2 exit 2
elif [ -z "$1" ] ; then elif [ -z "$1" ] ; then
@ -212,22 +222,22 @@ set_profile () {
## locate device path ## locate device path
if [ "$(lspci | grep -c VGA)" -gt 1 ] if [ "$(lspci | grep -c VGA)" -gt 1 ]
then then
if [ -z "$card_name" ] && [ -z "$pci_id" ] ; then if [ -z "$GPU_NAME" ] && [ -z "$GPU_PCI_ID" ] ; then
error "Several GPUs are present and no GPU is specified" error "Several GPUs are present and no GPU is specified"
error "Edit GPU_NAME or GPU_PCI_ID in '$config_file'" error "Edit GPU_NAME or GPU_PCI_ID in '$config_file'"
exit 20 exit 20
fi fi
if [ -z "$pci_id" ] ; then if [ -z "$GPU_PCI_ID" ] ; then
pci_id=$(lspci | grep VGA | grep "$card_name" | cut -d ' ' -f1) GPU_PCI_ID=$(lspci | grep VGA | grep "$GPU_NAME" | cut -d ' ' -f1)
if [ "$(echo "$pci_id" | wc -l)" -gt 1 ] ; then if [ "$(echo "$GPU_PCI_ID" | wc -l)" -gt 1 ] ; then
error "More than one name match for GPU" error "More than one name match for GPU"
exit 21 exit 21
elif [ "$(echo "$pci_id" | wc -l)" -lt 1 ] ; then elif [ "$(echo "$GPU_PCI_ID" | wc -l)" -lt 1 ] ; then
error "No name match for GPU" error "No name match for GPU"
exit 22 exit 22
fi fi
fi fi
if ! path=$(ls -l /sys/class/drm/card? | grep "$pci_id" | tr -s ' ' | cut -d ' ' -f9) ; then if ! path=$(ls -l /sys/class/drm/card? | grep "$GPU_PCI_ID" | tr -s ' ' | cut -d ' ' -f9) ; then
error "Error finding pci device" error "Error finding pci device"
echo 23 echo 23
fi fi
@ -253,85 +263,46 @@ fi
# read arguments # read arguments
shift $((OPTIND-1)) shift $((OPTIND-1))
if [ "$#" -gt 0 ] case "$1" in
then reset) reset_stock ;;
apply) apply ;;
if [ "$1" = "reset" ]; then memfix) force_mem_state_high ;;
reset_stock print) cat $path/device/pp_od_clk_voltage ;;
gpu)
elif [ "$1" = "gpu" ]; then case "$2" in
profile)
if [ "$2" = "profile" ]; then if [ -f "$GPU_PROFILE_PATH/$3" ]
if [ -f "$profile_path/$3" ] ; then then set_profile gpu < "$GPU_PROFILE_PATH/$3"
set_profile gpu < "$profile_path/$3" else cd "$GPU_PROFILE_PATH" 2>/dev/null && ls -p | grep -v /
else fi ;;
if ! cd "$profile_path" 2>/dev/null ; then file) set_profile gpu < "$3" && apply ;;
exit *) echo "zgoc gpu <profile/file>" && exit 1 ;;
fi esac
ls -p | grep -v / ;;
fi mem)
elif [ "$2" = "file" ]; then case "$2" in
set_profile gpu < "$3" profile)
apply if [ -f "$GPU_MEM_PATH/$3" ]
else then set_profile mem < "$GPU_MEM_PATH/$3"
echo "zgoc gpu <profile/file>" else cd "$GPU_MEM_PATH" && ls -p | grep -v /
fi fi ;;
file) set_profile mem < "$3" && apply ;;
elif [ "$1" = "mem" ]; then *) echo "zgoc mem <profile/file>" && exit 1
esac
if [ "$2" = "profile" ]; then ;;
if [ -f "$mem_path/$3" ] ; then power)
set_profile mem < "$mem_path/$3"
else
if ! cd "$mem_path" 2>/dev/null ; then
exit
fi
ls -p | grep -v /
fi
elif [ "$2" = "file" ]; then
set_profile mem < "$3"
apply
else
echo "zgoc mem <profile/file>"
fi
elif [ "$1" = "power" ] ; then
kernel_req 4 20 kernel_req 4 20
case "$2" in
if [ "$2" = "profile" ]; then profile)
if [ -f "$power_path/$3" ] ; then if [ -f "$GPU_POWER_PATH/$3" ]
apply_power "$(cat "$power_path/$3")" then apply_power "$(cat "$GPU_POWER_PATH/$3")"
else else cd "$GPU_POWER_PATH" 2>/dev/null && ls -p | grep -v /
if ! cd "$power_path" 2>/dev/null ; then fi ;;
exit max) apply_power "$(val_convert 6 0 < $hwmon/power1_cap_max)" ;;
fi set) apply_power "$3" ;;
ls -p | grep -v / print) echo $(val_convert 6 0 < $hwmon/power1_cap) W ;;
fi *) echo "zgoc power <max/profile/set/print>" && exit 1
elif [ "$2" = "max" ]; then esac
apply_power "$(val_convert 6 0 < $hwmon/power1_cap_max)" ;;
elif [ "$2" = "set" ]; then *) usage && exit 1;;
apply_power "$3" esac
elif [ "$2" = "print" ]; then
echo $(val_convert 6 0 < $hwmon/power1_cap) W
else
echo "zgoc power <max/profile/set/print>"
fi
elif [ "$1" = "apply" ]; then
apply
elif [ "$1" = "memfix" ]; then
force_mem_state_high
elif [ "$1" = "print" ]; then
cat $path/device/pp_od_clk_voltage
else
usage
fi
else
usage
fi
exit 0