gpu_power.sh
1 #!/usr/bin/env bash 2 # setting the locale, some users have issues with different locales, this forces the correct one 3 export LC_ALL=en_US.UTF-8 4 5 current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 6 source $current_dir/utils.sh 7 8 get_platform() 9 { 10 case $(uname -s) in 11 Linux) 12 # use this option for when your gpu isn't detected 13 gpu_label=$(get_tmux_option "@dracula-force-gpu" false) 14 if [[ "$gpu_label" != false ]]; then 15 echo $gpu_label 16 else 17 # attempt to detect the gpu 18 gpu=$(lspci -v | grep VGA | head -n 1 | awk '{print $5}') 19 if [[ -n $gpu ]]; then 20 # if a gpu is detected, return it 21 echo $gpu 22 elif type -a nvidia-smi >> /dev/null; then 23 # if no gpu was detected, and nvidia-smi is installed, we'll still try nvidia 24 echo "NVIDIA" 25 fi 26 fi 27 ;; 28 29 Darwin) 30 # WARNING: for this to work the powermetrics command needs to be run without password 31 # add this to the sudoers file, replacing the username and omitting the quotes. 32 # be mindful of the tabs: "username ALL = (root) NOPASSWD: /usr/bin/powermetrics" 33 echo "apple" 34 ;; 35 36 CYGWIN*|MINGW32*|MSYS*|MINGW*) 37 # TODO - windows compatability 38 ;; 39 esac 40 } 41 get_gpu() 42 { 43 gpu=$(get_platform) 44 gpu_power_percent=$(get_tmux_option "@dracula-gpu-power-percent" false) 45 if [[ "$gpu" == NVIDIA ]]; then 46 if $gpu_power_percent; then 47 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) }') 48 else 49 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) }') 50 fi 51 52 elif [[ "$gpu" == apple ]]; then 53 usage="$(sudo powermetrics --samplers gpu_power -i500 -n 1 | grep 'GPU Power' | sed 's/GPU Power: \(.*\) \(.*\)/\1\2/g')" 54 else 55 usage='unknown' 56 fi 57 normalize_percent_len $usage 58 } 59 60 main() 61 { 62 # storing the refresh rate in the variable RATE, default is 5 63 RATE=$(get_tmux_option "@dracula-refresh-rate" 5) 64 gpu_label=$(get_tmux_option "@dracula-gpu-power-label" "GPU") 65 gpu_usage=$(get_gpu) 66 echo "$gpu_label $gpu_usage" 67 sleep $RATE 68 } 69 70 # run the main driver 71 main