Skip to content
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

perf: optimize performance of waybar cpuinfo and gpuinfo modules #952

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
99 changes: 72 additions & 27 deletions Configs/.config/hypr/scripts/cpuinfo.sh
Original file line number Diff line number Diff line change
@@ -1,35 +1,80 @@
#!/usr/bin/env sh
#!/bin/bash

# CPU model
model=$(lscpu | awk -F ':' '/Model name/ {sub(/^ *| *$/,"",$2); print $2}' | awk '{NF-=3}1')

# CPU utilization
utilization=$(top -bn1 | awk '/^%Cpu/ {print 100 - $8}')
# Thee shalt find the greatest one,
# He who not more than the chosen one
map_floor() {

# Clock speed
freqlist=$(cat /proc/cpuinfo | grep "cpu MHz" | awk '{ print $4 }')
maxfreq=$(lscpu | grep "CPU max MHz" | awk -F: '{ print $2}' | sed -e 's/ //g' -e 's/\.[0-9]*//g')
frequency=$(echo $freqlist | tr ' ' '\n' | awk "{ sum+=\$1 } END {printf \"%.0f/$maxfreq MHz\", sum/NR}")
# From the depths of the string, words arise,
# Keys in pairs, a treasure in disguise.
IFS=', ' read -r -a pairs <<< "$1"

# CPU temp
temp=$(sensors | awk '/Package id 0/ {print $4}' | awk -F '[+.]' '{print $2}')
if [ -z "$temp" ]; then
temp=$(sensors | awk '/Tctl/ {print $2}' | tr -d '+°C')
fi
if [ -z "$temp" ]; then
temp="N/A"
fi
# If the final token stands alone and bold,
# Declare it the default, its worth untold.
if [[ ${pairs[-1]} != *":"* ]]; then
def_val="${pairs[-1]}"
unset 'pairs[${#pairs[@]}-1]'
fi

# Scans the map, a peak it seeks,
# The highest passed, the value speaks.
for pair in "${pairs[@]}"; do
IFS=':' read -r key value <<< "$pair"

# Behold! Thou holds the secrets they seek,
# Declare it and silence the whispers unique.
if [ ${2%%.*} -gt $key ]; then
echo "$value"
return
fi
done

# 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]'
# On this lonely shore, where silence dwells
# Even the waves, echoes words unheard
[ -n "$def_val" ] && echo $def_val || echo " "
}

thermo=$(eval_ico thermo $temp)
emoji=$(eval_ico emoji $temp)
speedo=$(eval_ico util $utilization)
# 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:󰾅, 󰾆"

# Get static CPU information
model=$(lscpu | awk -F': ' '/Model name/ {gsub(/^ *| *$| CPU.*/,"",$2); print $2}')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lscpu respects LANG and thus requires en_us for this to work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, good find. I'll add this on the next commit.

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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the loop? its not recommended to run this as a daemon process.

Copy link
Contributor Author

@mislah mislah Feb 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Continuous script

As the script has to recalculate values that doesn't change during runtime (like CPU name and max freq), it is inefficient to run on every 5s. Since waybar allows the scripts to be executed in continuous mode which allows significant improvement on performance as the static values doesn't required to be recalculated on each iteration, the cpuinfo.sh has been adjusted to run as a continuous script.

It can be made to execute once in every 5s instead by saving the prev statistics to a temp file. However, I don't see why it is not recommended to run it as a daemon as re-executing the script has to deal with unnecessary system calls, read-writes and re-interpreting the script once in every 5s.

Also, it is not going to save cpu time on the scheduler as it has to deal with the sleep syscall from the script otherwise the same from the waybar.

It is also noted that the waybar builtin modules which requires updates on periodic intervals (memory, cpu_usage) are running on separate daemon threads.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Continuous script

As the script has to recalculate values that doesn't change during runtime (like CPU name and max freq), it is inefficient to run on every 5s. Since waybar allows the scripts to be executed in continuous mode which allows significant improvement on performance as the static values doesn't required to be recalculated on each iteration, the cpuinfo.sh has been adjusted to run as a continuous script.

It can be made to execute once in every 5s instead by saving the prev statistics to a temp file. However, I don't see why it is not recommended to run it as a daemon as re-executing the script has to deal with unnecessary system calls, read-writes and re-interpreting the script once in every 5s.

Oh my bad I missed it, you did mention it, but it was too long so I didn't read :(
But still my concern is valid... I do not use the any of these monitoring modules in waybar as i often switch the waybar mode... So my question is that, will this process still run when the waybar mode changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the confgen.sh architecture, the waybar configuration file config.jsonc is regenerated on switching the waybar mode. So this process won't be summoned by new instances of waybar as the modules will not be present in the config.jsonc.

# restart waybar
if [ "$reload_flag" == "1" ] ; then
killall waybar
waybar > /dev/null 2>&1 &
fi

According the above snippet, when you switch the mode, the waybar and its entire process tree will be killed, including our poor little daemon.

Sorry for the late reply, was little too busy these days.

# 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})

# 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
36 changes: 17 additions & 19 deletions Configs/.config/hypr/scripts/gpuinfo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
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 @@ -124,7 +126,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 @@ -135,26 +137,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 seperated 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 @@ -272,13 +261,22 @@ Avalable 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
4 changes: 2 additions & 2 deletions Configs/.config/waybar/modules/cpuinfo.jsonc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"custom/cpuinfo": {
"exec": " ~/.config/hypr/scripts/cpuinfo.sh",
"exec": "NO_EMOJI=1 ~/.config/hypr/scripts/cpuinfo.sh",
"return-type": "json",
"format": "{}",
"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": " ~/.config/hypr/scripts/gpuinfo.sh",
"exec": "NO_EMOJI=1 ~/.config/hypr/scripts/gpuinfo.sh",
"return-type": "json",
"format": "{}",
"interval": 5, // once every 5 seconds
Expand All @@ -9,7 +9,7 @@
},

"custom/gpuinfo#nvidia": {
"exec": " ~/.config/hypr/scripts/gpuinfo.sh --use nvidia ",
"exec": "NO_EMOJI=1 ~/.config/hypr/scripts/gpuinfo.sh --use nvidia ",
"return-type": "json",
"format": "{}",
"interval": 5, // once every 5 seconds
Expand All @@ -18,7 +18,7 @@
},

"custom/gpuinfo#amd": {
"exec": " ~/.config/hypr/scripts/gpuinfo.sh --use amd ",
"exec": "NO_EMOJI=1 ~/.config/hypr/scripts/gpuinfo.sh --use amd ",
"return-type": "json",
"format": "{}",
"interval": 5, // once every 5 seconds
Expand All @@ -27,7 +27,7 @@
},

"custom/gpuinfo#intel": {
"exec": " ~/.config/hypr/scripts/gpuinfo.sh --use intel ",
"exec": "NO_EMOJI=1 ~/.config/hypr/scripts/gpuinfo.sh --use intel ",
"return-type": "json",
"format": "{}",
"interval": 5, // once every 5 seconds
Expand Down