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
config_file="/usr/share/z/config/zgoc.conf"
card_name=$( grep "GPU_NAME=" "$config_file" | cut -d '=' -f2-)
pci_id=$(grep "GPU_PCI_ID=" "$config_file" | cut -d '=' -f2-)
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-)
# config file
config_path="/etc/zgoc"
config_file="$config_path/zgoc.conf"
temps=( 50 60 70 80 85 90 95 ) # in °C
fans=( 0 15 30 40 55 75 100 ) # in %
# load config
[ -f "$config_file" ] && . "$config_file"
if [ "${#temps[@]}" -ne "${#fans[@]}" ]
then
error "Amount of temperature and pwm values does not match"
exit 30
fi
# default conf
[ -z "$FANC_TIME_INTERVAL" ] && FANC_TIME_INTERVAL=2
[ -z "$FANC_DEFAULT_PROFILE" ] && FANC_DEFAULT_PROFILE=2
[ -z "$FANC_PROFILE_PATH" ] && FANC_PROFILE_PATH=profiles/power
[ $(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 () {
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" ]
then
echo -n "$(pwd)/$1 = "
cat "$(pwd)/$1"
echo "$(pwd)/$1 = $(cat "$(pwd)/$1")"
elif [ -f "$1" ]
then
echo -n "$1 = "
cat "$1"
echo "$1 = $(cat "$1")"
else
error "File $1 from $(pwd) unaccessible"
fi
@ -48,28 +67,28 @@ file_write ()
_stop() {
echo ""
file_write 2 pwm1_enable
exit
exit $1
}
if [[ $(lspci | grep -c VGA) -gt 1 ]]
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"
exit 20
fi
if [ -z "$pci_id" ] ; then
pci_id=$(lspci | grep VGA | grep "$card_name" | cut -d ' ' -f1)
if [ "$(echo "$pci_id" | wc -l)" -gt 1 ] ; then
if [ -z "$GPU_PCI_ID" ] ; then
GPU_PCI_ID=$(lspci | grep VGA | grep "$GPU_NAME" | cut -d ' ' -f1)
if [ "$(echo "$GPU_PCI_ID" | wc -l)" -gt 1 ] ; then
error "More than one name match"
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"
exit 22
fi
fi
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
error "Error finding pci device"
echo 23
@ -99,34 +118,42 @@ file_write 1 pwm1_enable
while true
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
if [ "$temp" -lt "${temps[0]}" ]
if [ "$temp" -lt "$(getval "$temps" 0)" ]
then
fan=${fans[0]}
fan=$(getval "$fans" 0)
else
while [[ $I -lt "${#temps[@]}" ]]
while [ $I -lt $NT ]
do
if [ "$temp" -lt "${temps[$I]}" ]
if [ $temp -lt $(getval "$temps" $I) ]
then
LOWERTEMP=${temps[$I-1]}
HIGHERTEMP=${temps[$I]}
LOWERPWM=${fans[$I-1]}
HIGHERPWM=${fans[$I]}
LOWERTEMP=$(getval "$temps" $((I-1)))
HIGHERTEMP=$(getval "$temps" $I)
LOWERPWM=$(getval "$fans" $((I-1)))
HIGHERPWM=$(getval "$fans" $I)
fan=$(echo "( ( $temp - $LOWERTEMP ) * ( $HIGHERPWM - $LOWERPWM ) / ( $HIGHERTEMP - $LOWERTEMP ) ) + $LOWERPWM" | bc)
I=${#temps[@]}
I=$NT
fi
I=$((I + 1))
I=$(($I + 1))
done
fi
if [ -z "$fan" ]
then
fan=${fans[$((I - 1))]}
fan=$(getval "$fans" $((I-1)))
fi
fan_max=$(cat pwm1_max)
fan_min=$(cat pwm1_min)
file_write "$(echo "( ( $fan * ($fan_max - $fan_min) ) / 100 ) + $fan_min" | bc)" pwm1
sleep "$time_interval"
# get min/max values
fan_max=$(cat pwm1_max) && fan_min=$(cat pwm1_min) || _stop 1
# scale to min/max
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
exit 0
_stop 0

195
zgoc/zgoc
View file

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