From ecc11c20a91ca087f57e171df73cb020a155938b Mon Sep 17 00:00:00 2001 From: Theoreticallyhugo Date: Thu, 29 Aug 2024 23:29:43 +0200 Subject: [PATCH 1/6] feature/gpu usage and power for macos --- scripts/gpu_power.sh | 8 ++++++-- scripts/gpu_usage.sh | 7 ++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/scripts/gpu_power.sh b/scripts/gpu_power.sh index c82c41d2..d5af7729 100755 --- a/scripts/gpu_power.sh +++ b/scripts/gpu_power.sh @@ -20,7 +20,10 @@ get_platform() ;; 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*) @@ -28,13 +31,14 @@ get_platform() ;; esac } - get_gpu() { gpu=$(get_platform) 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) }') + 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 diff --git a/scripts/gpu_usage.sh b/scripts/gpu_usage.sh index 74d51544..44416097 100755 --- a/scripts/gpu_usage.sh +++ b/scripts/gpu_usage.sh @@ -20,7 +20,10 @@ get_platform() ;; 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*) @@ -34,6 +37,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 From 573bd9d9fb2729aaf0c5d129728e637ea6bf8419 Mon Sep 17 00:00:00 2001 From: Theoreticallyhugo Date: Thu, 29 Aug 2024 23:31:06 +0200 Subject: [PATCH 2/6] feature/separate gpu labels --- scripts/gpu_power.sh | 2 +- scripts/gpu_ram_info.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/gpu_power.sh b/scripts/gpu_power.sh index d5af7729..4c9cbdb4 100755 --- a/scripts/gpu_power.sh +++ b/scripts/gpu_power.sh @@ -49,7 +49,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 diff --git a/scripts/gpu_ram_info.sh b/scripts/gpu_ram_info.sh index 379218ed..79dae8f0 100755 --- a/scripts/gpu_ram_info.sh +++ b/scripts/gpu_ram_info.sh @@ -44,7 +44,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" "VRAM") + gpu_label=$(get_tmux_option "@dracula-gpu-vram-label" "VRAM") gpu_usage=$(get_gpu) echo "$gpu_label $gpu_usage" sleep $RATE From 84a6e0b35f2d293242fe3b3d8ca9bb3341c52752 Mon Sep 17 00:00:00 2001 From: Theoreticallyhugo Date: Sun, 1 Sep 2024 09:03:53 +0000 Subject: [PATCH 3/6] better nvidia support and forcing of detection --- scripts/gpu_usage.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/scripts/gpu_usage.sh b/scripts/gpu_usage.sh index 44416097..b338f835 100755 --- a/scripts/gpu_usage.sh +++ b/scripts/gpu_usage.sh @@ -9,9 +9,12 @@ 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 + # 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 + elif type -a nvidia-smi >> /dev/null; then + # if nvidia-smi is installed, its highly likely for the gpu to be nvidia, so stop checking echo "NVIDIA" else gpu=$(lspci -v | grep VGA | head -n 1 | awk '{print $5}') From c9f8a51942bb5901b93dd642d31e54bbf270469d Mon Sep 17 00:00:00 2001 From: Theoreticallyhugo Date: Sun, 1 Sep 2024 10:38:21 +0000 Subject: [PATCH 4/6] feature/gpu info as percentage --- scripts/gpu_power.sh | 16 ++++++++++++---- scripts/gpu_ram_info.sh | 18 ++++++++++++++---- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/scripts/gpu_power.sh b/scripts/gpu_power.sh index 4c9cbdb4..2e4c1bc6 100755 --- a/scripts/gpu_power.sh +++ b/scripts/gpu_power.sh @@ -9,9 +9,12 @@ 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 + # 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 + elif type -a nvidia-smi >> /dev/null; then + # if nvidia-smi is installed, its highly likely for the gpu to be nvidia, so stop checking echo "NVIDIA" else gpu=$(lspci -v | grep VGA | head -n 1 | awk '{print $5}') @@ -34,8 +37,13 @@ get_platform() 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')" diff --git a/scripts/gpu_ram_info.sh b/scripts/gpu_ram_info.sh index 79dae8f0..31f4021c 100755 --- a/scripts/gpu_ram_info.sh +++ b/scripts/gpu_ram_info.sh @@ -9,9 +9,12 @@ 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 + # 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 + elif type -a nvidia-smi >> /dev/null; then + # if nvidia-smi is installed, its highly likely for the gpu to be nvidia, so stop checking echo "NVIDIA" else gpu=$(lspci -v | grep VGA | head -n 1 | awk '{print $5}') @@ -32,8 +35,15 @@ 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 ) }') + else + # to add finer grained info + # usage=$(nvidia-smi --query-gpu=memory.used,memory.total --format=csv,noheader,nounits | awk '{ used += $0; total +=$2 } END { printf("%.1fGB/%dGB\n", used / 1024, total / 1024) }') + 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) }') + fi else usage='unknown' fi From 38ab48b02abdc0be21deb2668636ab290791ed37 Mon Sep 17 00:00:00 2001 From: Theoreticallyhugo Date: Sun, 1 Sep 2024 18:29:35 +0000 Subject: [PATCH 5/6] feature/vram usage as floating point option --- scripts/gpu_ram_info.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/scripts/gpu_ram_info.sh b/scripts/gpu_ram_info.sh index 31f4021c..b6210e66 100755 --- a/scripts/gpu_ram_info.sh +++ b/scripts/gpu_ram_info.sh @@ -39,15 +39,18 @@ get_gpu() if [[ "$gpu" == NVIDIA ]]; then 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 - # usage=$(nvidia-smi --query-gpu=memory.used,memory.total --format=csv,noheader,nounits | awk '{ used += $0; total +=$2 } END { printf("%.1fGB/%dGB\n", used / 1024, total / 1024) }') - 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) }') + 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() From 6ad0720a6ccf2eac79c9510b6343579ef8f06394 Mon Sep 17 00:00:00 2001 From: Theoreticallyhugo Date: Wed, 4 Sep 2024 21:04:06 +0200 Subject: [PATCH 6/6] nvidia-smi check is now fallback check --- scripts/gpu_power.sh | 12 ++++++++---- scripts/gpu_ram_info.sh | 12 ++++++++---- scripts/gpu_usage.sh | 12 ++++++++---- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/scripts/gpu_power.sh b/scripts/gpu_power.sh index 2e4c1bc6..33f1a742 100755 --- a/scripts/gpu_power.sh +++ b/scripts/gpu_power.sh @@ -13,12 +13,16 @@ get_platform() gpu_label=$(get_tmux_option "@dracula-force-gpu" false) if [[ "$gpu_label" != false ]]; then echo $gpu_label - elif type -a nvidia-smi >> /dev/null; then - # if nvidia-smi is installed, its highly likely for the gpu to be nvidia, so stop checking - echo "NVIDIA" 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 ;; diff --git a/scripts/gpu_ram_info.sh b/scripts/gpu_ram_info.sh index b6210e66..a3fceefa 100755 --- a/scripts/gpu_ram_info.sh +++ b/scripts/gpu_ram_info.sh @@ -13,12 +13,16 @@ get_platform() gpu_label=$(get_tmux_option "@dracula-force-gpu" false) if [[ "$gpu_label" != false ]]; then echo $gpu_label - elif type -a nvidia-smi >> /dev/null; then - # if nvidia-smi is installed, its highly likely for the gpu to be nvidia, so stop checking - echo "NVIDIA" 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 ;; diff --git a/scripts/gpu_usage.sh b/scripts/gpu_usage.sh index b338f835..70794aab 100755 --- a/scripts/gpu_usage.sh +++ b/scripts/gpu_usage.sh @@ -13,12 +13,16 @@ get_platform() gpu_label=$(get_tmux_option "@dracula-force-gpu" false) if [[ "$gpu_label" != false ]]; then echo $gpu_label - elif type -a nvidia-smi >> /dev/null; then - # if nvidia-smi is installed, its highly likely for the gpu to be nvidia, so stop checking - echo "NVIDIA" 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 ;;