Skip to content

Commit

Permalink
Merge pull request #286 from Theoreticallyhugo/feature/better-gpu
Browse files Browse the repository at this point in the history
Feature/better gpu
  • Loading branch information
ethancedwards8 authored Sep 5, 2024
2 parents 410e844 + 6ad0720 commit ae99a70
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 23 deletions.
34 changes: 25 additions & 9 deletions scripts/gpu_power.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,48 @@ get_platform()
{
case $(uname -s) in
Linux)
# use this option for when you know that there is an NVIDIA gpu, but you cant use lspci to determine
ignore_lspci=$(get_tmux_option "@dracula-ignore-lspci" false)
if [[ "$ignore_lspci" = true ]]; then
echo "NVIDIA"
# use this option for when your gpu isn't detected
gpu_label=$(get_tmux_option "@dracula-force-gpu" false)
if [[ "$gpu_label" != false ]]; then
echo $gpu_label
else
# attempt to detect the gpu
gpu=$(lspci -v | grep VGA | head -n 1 | awk '{print $5}')
echo $gpu
if [[ -n $gpu ]]; then
# if a gpu is detected, return it
echo $gpu
elif type -a nvidia-smi >> /dev/null; then
# if no gpu was detected, and nvidia-smi is installed, we'll still try nvidia
echo "NVIDIA"
fi
fi
;;

Darwin)
# TODO - Darwin/Mac compatability
# WARNING: for this to work the powermetrics command needs to be run without password
# add this to the sudoers file, replacing the username and omitting the quotes.
# be mindful of the tabs: "username ALL = (root) NOPASSWD: /usr/bin/powermetrics"
echo "apple"
;;

CYGWIN*|MINGW32*|MSYS*|MINGW*)
# TODO - windows compatability
;;
esac
}

get_gpu()
{
gpu=$(get_platform)
gpu_power_percent=$(get_tmux_option "@dracula-gpu-power-percent" false)
if [[ "$gpu" == NVIDIA ]]; then
usage=$(nvidia-smi --query-gpu=power.draw,power.limit --format=csv,noheader,nounits | awk '{ draw += $0; max +=$2 } END { printf("%dW/%dW\n", draw, max) }')
if $gpu_power_percent; then
usage=$(nvidia-smi --query-gpu=power.draw,power.limit --format=csv,noheader,nounits | awk '{ draw += $0; max +=$2 } END { printf("%d%%\n", draw / max * 100) }')
else
usage=$(nvidia-smi --query-gpu=power.draw,power.limit --format=csv,noheader,nounits | awk '{ draw += $0; max +=$2 } END { printf("%dW/%dW\n", draw, max) }')
fi

elif [[ "$gpu" == apple ]]; then
usage="$(sudo powermetrics --samplers gpu_power -i500 -n 1 | grep 'GPU Power' | sed 's/GPU Power: \(.*\) \(.*\)/\1\2/g')"
else
usage='unknown'
fi
Expand All @@ -45,7 +61,7 @@ main()
{
# storing the refresh rate in the variable RATE, default is 5
RATE=$(get_tmux_option "@dracula-refresh-rate" 5)
gpu_label=$(get_tmux_option "@dracula-gpu-usage-label" "GPU")
gpu_label=$(get_tmux_option "@dracula-gpu-power-label" "GPU")
gpu_usage=$(get_gpu)
echo "$gpu_label $gpu_usage"
sleep $RATE
Expand Down
33 changes: 25 additions & 8 deletions scripts/gpu_ram_info.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ get_platform()
{
case $(uname -s) in
Linux)
# use this option for when you know that there is an NVIDIA gpu, but you cant use lspci to determine
ignore_lspci=$(get_tmux_option "@dracula-ignore-lspci" false)
if [[ "$ignore_lspci" = true ]]; then
echo "NVIDIA"
# use this option for when your gpu isn't detected
gpu_label=$(get_tmux_option "@dracula-force-gpu" false)
if [[ "$gpu_label" != false ]]; then
echo $gpu_label
else
# attempt to detect the gpu
gpu=$(lspci -v | grep VGA | head -n 1 | awk '{print $5}')
echo $gpu
if [[ -n $gpu ]]; then
# if a gpu is detected, return it
echo $gpu
elif type -a nvidia-smi >> /dev/null; then
# if no gpu was detected, and nvidia-smi is installed, we'll still try nvidia
echo "NVIDIA"
fi
fi
;;

Expand All @@ -32,19 +39,29 @@ get_platform()
get_gpu()
{
gpu=$(get_platform)
gpu_vram_percent=$(get_tmux_option "@dracula-gpu-vram-percent" false)
if [[ "$gpu" == NVIDIA ]]; then
usage=$(nvidia-smi --query-gpu=memory.used,memory.total --format=csv,noheader,nounits | awk '{ used += $0; total +=$2 } END { printf("%dGB/%dGB\n", used / 1024, total / 1024) }')
if $gpu_vram_percent; then
usage=$(nvidia-smi --query-gpu=memory.used,memory.total --format=csv,noheader,nounits | awk '{ used += $0; total +=$2 } END { printf("%d%%\n", used / total * 100 ) }')
normalize_percent_len $usage
exit 0
else
# to add finer grained info
used_accuracy=$(get_tmux_option "@dracula-gpu-vram-used-accuracy" "d")
total_accuracy=$(get_tmux_option "@dracula-gpu-vram-total-accuracy" "d")
usage=$(nvidia-smi --query-gpu=memory.used,memory.total --format=csv,noheader,nounits | awk "{ used += \$0; total +=\$2 } END { printf(\"%${used_accuracy}GB/%${total_accuracy}GB\n\", used / 1024, total / 1024) }")
fi
else
usage='unknown'
fi
normalize_percent_len $usage
echo $usage
}

main()
{
# storing the refresh rate in the variable RATE, default is 5
RATE=$(get_tmux_option "@dracula-refresh-rate" 5)
gpu_label=$(get_tmux_option "@dracula-gpu-usage-label" "VRAM")
gpu_label=$(get_tmux_option "@dracula-gpu-vram-label" "VRAM")
gpu_usage=$(get_gpu)
echo "$gpu_label $gpu_usage"
sleep $RATE
Expand Down
24 changes: 18 additions & 6 deletions scripts/gpu_usage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,28 @@ get_platform()
{
case $(uname -s) in
Linux)
# use this option for when you know that there is an NVIDIA gpu, but you cant use lspci to determine
ignore_lspci=$(get_tmux_option "@dracula-ignore-lspci" false)
if [[ "$ignore_lspci" = true ]]; then
echo "NVIDIA"
# use this option for when your gpu isn't detected
gpu_label=$(get_tmux_option "@dracula-force-gpu" false)
if [[ "$gpu_label" != false ]]; then
echo $gpu_label
else
# attempt to detect the gpu
gpu=$(lspci -v | grep VGA | head -n 1 | awk '{print $5}')
echo $gpu
if [[ -n $gpu ]]; then
# if a gpu is detected, return it
echo $gpu
elif type -a nvidia-smi >> /dev/null; then
# if no gpu was detected, and nvidia-smi is installed, we'll still try nvidia
echo "NVIDIA"
fi
fi
;;

Darwin)
# TODO - Darwin/Mac compatability
# WARNING: for this to work the powermetrics command needs to be run without password
# add this to the sudoers file, replacing the username and omitting the quotes.
# be mindful of the tabs: "username ALL = (root) NOPASSWD: /usr/bin/powermetrics"
echo "apple"
;;

CYGWIN*|MINGW32*|MSYS*|MINGW*)
Expand All @@ -34,6 +44,8 @@ get_gpu()
gpu=$(get_platform)
if [[ "$gpu" == NVIDIA ]]; then
usage=$(nvidia-smi --query-gpu=utilization.gpu --format=csv,noheader,nounits | awk '{ sum += $0 } END { printf("%d%%\n", sum / NR) }')
elif [[ "$gpu" == apple ]]; then
usage="$(sudo powermetrics --samplers gpu_power -i500 -n 1 | grep 'active residency' | sed 's/[^0-9.%]//g' | sed 's/[%].*$//g')%"
else
usage='unknown'
fi
Expand Down

0 comments on commit ae99a70

Please sign in to comment.