Skip to content

UPDATED perf: optimize performance of waybar cpuinfo and gpuinfo modules #2054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Configs/.config/waybar/modules/cpuinfo.jsonc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"custom/cpuinfo": {
"exec": " cpuinfo.sh",
"exec": "NO_EMOJI=1 cpuinfo.sh",
"return-type": "json",
"format": "{}",
"rotate": ${r_deg},
"interval": 5, // once every 5 seconds
"restart-interval": 5, // once every 5 seconds
"tooltip": true,
"max-length": 1000
},
Expand Down
8 changes: 4 additions & 4 deletions Configs/.config/waybar/modules/gpuinfo.jsonc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"custom/gpuinfo": {
"exec": " gpuinfo.sh",
"exec": "NO_EMOJI=1 gpuinfo.sh",
"return-type": "json",
"format": "{}",
"rotate": ${r_deg},
Expand All @@ -10,7 +10,7 @@
},

"custom/gpuinfo#nvidia": {
"exec": " gpuinfo.sh --use nvidia ",
"exec": "NO_EMOJI=1 gpuinfo.sh --use nvidia ",
"return-type": "json",
"format": "{}",
"rotate": ${r_deg},
Expand All @@ -20,7 +20,7 @@
},

"custom/gpuinfo#amd": {
"exec": " gpuinfo.sh --use amd ",
"exec": "NO_EMOJI=1 gpuinfo.sh --use amd ",
"return-type": "json",
"format": "{}",
"rotate": ${r_deg},
Expand All @@ -30,7 +30,7 @@
},

"custom/gpuinfo#intel": {
"exec": " gpuinfo.sh --use intel ",
"exec": "NO_EMOJI=1 gpuinfo.sh --use intel ",
"return-type": "json",
"format": "{}",
"rotate": ${r_deg},
Expand Down
88 changes: 57 additions & 31 deletions Configs/.local/share/bin/cpuinfo.sh
Original file line number Diff line number Diff line change
@@ -1,38 +1,64 @@
#!/usr/bin/env sh

# CPU model
model=$(cat /proc/cpuinfo | grep 'model name' | head -n 1 | awk -F ': ' '{print $2}')

# CPU utilization
utilization=$(top -bn1 | awk '/^%Cpu/ {print 100 - $8}')

# Clock speed
freqlist=$(cat /proc/cpuinfo | grep "cpu MHz" | awk '{ print $4 }')
maxfreq=$(cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq | sed 's/...$//')
frequency=$(echo $freqlist | tr ' ' '\n' | awk "{ sum+=\$1 } END {printf \"%.0f/$maxfreq MHz\", sum/NR}")

# CPU temp
sensorsdata=$(sensors)
temp=$(awk '/Package id 0/ {print $4}' <<< "$sensorsdata" | awk -F '[+.]' '{print $2}')
if [ -z "$temp" ]; then
temp=$(awk '/Tctl/ {print $2}' <<< "$sensorsdata" | tr -d '+°C')
fi
if [ -z "$temp" ]; then
temp="N/A"
map_floor() {

IFS=', ' read -r -a pairs <<< "$1"

if [[ ${pairs[-1]} != *":"* ]]; then
def_val="${pairs[-1]}"
unset 'pairs[${#pairs[@]}-1]'
fi
for pair in "${pairs[@]}"; do
IFS=':' read -r key value <<< "$pair"
if [ ${2%%.*} -gt $key ]; then
echo "$value"
return
fi
done
[ -n "$def_val" ] && echo $def_val || echo " "
}

# Define glyphs
if [[ $NO_EMOJI -eq 1 ]]; then
temp_lv="85:, 65:, 45:☁, ❄"
else
temp=`printf "%.0f\n" $temp`
temp_lv="85:🌋, 65:🔥, 45:☁️, ❄️"
fi
util_lv="90:, 60:󰓅, 30:󰾅, 󰾆"

# map icons
set_ico="{\"thermo\":{\"0\":\"\",\"45\":\"\",\"65\":\"\",\"85\":\"\"},\"emoji\":{\"0\":\"❄️\",\"45\":\"☁️\",\"65\":\"🔥\",\"85\":\"🌋\"},\"util\":{\"0\":\"󰾆\",\"30\":\"󰾅\",\"60\":\"󰓅\",\"90\":\"\"}}"
eval_ico() {
map_ico=$(echo "${set_ico}" | jq -r --arg aky "$1" --argjson avl "$2" '.[$aky] | keys_unsorted | map(tonumber) | map(select(. <= $avl)) | max')
echo "${set_ico}" | jq -r --arg aky "$1" --arg avl "$map_ico" '.[$aky] | .[$avl]'
}
# Get static CPU information
model=$(lscpu | awk -F': ' '/Model name/ {gsub(/^ *| *$| CPU.*/,"",$2); print $2}')
maxfreq=$(lscpu | awk '/CPU max MHz/ { sub(/\..*/,"",$4); print $4}')

# Get CPU stat
statFile=$(cat /proc/stat | head -1)
prevStat=$(awk '{print $2+$3+$4+$6+$7+$8 }' <<< $statFile)
prevIdle=$(awk '{print $5 }' <<< $statFile)

while true; do
# Get CPU stat
statFile=$(cat /proc/stat | head -1)
currStat=$(awk '{print $2+$3+$4+$6+$7+$8 }' <<< $statFile)
currIdle=$(awk '{print $5 }' <<< $statFile)
diffStat=$((currStat-prevStat))
diffIdle=$((currIdle-prevIdle))

# Get dynamic CPU information
utilization=$(awk -v stat="$diffStat" -v idle="$diffIdle" 'BEGIN {printf "%.1f", (stat/(stat+idle))*100}')
temperature=$(sensors | awk -F': ' '/Package id 0|Tctl/ { gsub(/^ *\+?|\..*/,"",$2); print $2; f=1; exit} END { if (!f) print "N/A"; }')
frequency=$(cat /proc/cpuinfo | awk '/cpu MHz/{ sum+=$4; c+=1 } END { printf "%.0f", sum/c }')

# Generate glyphs
icons=$(echo "$(map_floor "$util_lv" $utilization)$(map_floor "$temp_lv" $temperature)")
speedo=$(echo ${icons:0:1})
thermo=$(echo ${icons:1:1})
emoji=$(echo ${icons:2})

thermo=$(eval_ico thermo $temp)
emoji=$(eval_ico emoji $temp)
speedo=$(eval_ico util $utilization)
# Print the output
echo "{\"text\":\"$thermo $temperature°C\", \"tooltip\":\"$model\n$thermo Temperature: $temperature°C $emoji\n$speedo Utilization: $utilization%\n Clock Speed: $frequency/$maxfreq MHz\"}"

# Print cpu info (json)
echo "{\"text\":\"${thermo} ${temp}°C\", \"tooltip\":\"${model}\n${thermo} Temperature: ${temp}°C ${emoji}\n${speedo} Utilization: ${utilization}%\n Clock Speed: ${frequency}\"}"
# Store state and sleep
prevStat=$currStat
prevIdle=$currIdle
sleep 5
done
37 changes: 17 additions & 20 deletions Configs/.local/share/bin/gpuinfo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
# shellcheck disable=SC1090
scrDir=`dirname "$(realpath "$0")"`
gpuQ="/tmp/hyprdots-${UID}-gpuinfo-query"

tired=false
[[ " $* " =~ " tired " ]] && ! grep -q "tired" "${gpuQ}" && echo "tired=true" >>"${gpuQ}"
[[ " $* " =~ " no_emoji " ]] && ! grep -q "NO_EMOJI" "${gpuQ}" && echo "NO_EMOJI=1" >>"${gpuQ}"

if [[ ! " $* " =~ " startup " ]]; then
gpuQ="${gpuQ}$2"
fi
Expand Down Expand Up @@ -126,7 +127,7 @@ map_floor() {

# Behold! Thou holds the secrets they seek,
# Declare it and silence the whispers unique.
if awk -v num="$2" -v k="$key" 'BEGIN { exit !(num > k) }'; then
if [ ${2%%.*} -gt $key ]; then
echo "$value"
return
fi
Expand All @@ -137,26 +138,13 @@ map_floor() {
[ -n "$def_val" ] && echo $def_val || echo " "
}

# generate emoji and icon based on temperature and utilization
get_icons() {
# key-value pairs of temperature and utilization levels
temp_lv="85:&🌋, 65:&🔥, 45:&☁️, &❄️"
util_lv="90:, 60:󰓅, 30:󰾅, 󰾆"

# return comma separated emojis/icons
icons=$(map_floor "$temp_lv" $1 | sed "s/&/,/")
icons="$icons,$(map_floor "$util_lv" $2)"
echo $icons
}

generate_json() {
# get emoji and icon based on temperature and utilization
icons=$(get_icons "$temperature" "$utilization")
thermo=$(echo $icons | awk -F, '{print $1}')
emoji=$(echo $icons | awk -F, '{print $2}')
speedo=$(echo $icons | awk -F, '{print $3}')
# Generate glyphs
icons=$(echo "$(map_floor "$util_lv" $utilization)$(map_floor "$temp_lv" $temperature)")
speedo=$(echo ${icons:0:1})
thermo=$(echo ${icons:1:1})
emoji=$(echo ${icons:2})

# emoji=$(get_temperature_emoji "${temperature}")
local json="{\"text\":\"${thermo} ${temperature}°C\", \"tooltip\":\"${primary_gpu}\n${thermo} Temperature: ${temperature}°C ${emoji}"
#? Soon Add Something incase needed.
declare -A tooltip_parts
Expand Down Expand Up @@ -273,13 +261,22 @@ Available GPU: ${gpu_flags//_flag/}
[flags]
tired * Adding this option will not query nvidia-smi if gpu is in suspend mode
startup * Useful if you want a certain GPU to be set at startup
no_emoji * Use glyphs instead of emoji

* If ${USER} declared env = WLR_DRM_DEVICES on hyprland then use this as the primary GPU
EOF
exit
;;
esac

# Define glyphs
if [[ $NO_EMOJI -eq 1 ]]; then
temp_lv="85:, 65:, 45:☁, ❄"
else
temp_lv="85:🌋, 65:🔥, 45:☁️, ❄️"
fi
util_lv="90:, 60:󰓅, 30:󰾅, 󰾆"

nvidia_flag=${nvidia_flag:-0} intel_flag=${intel_flag:-0} amd_flag=${amd_flag:-0}
#? Based on the flags, call the corresponding function multi flags means multi GPU.
if [[ "${nvidia_flag}" -eq 1 ]]; then
Expand Down