Skip to content

Commit 5555bc7

Browse files
authored
Merge pull request #2054 from boulder-roller/main
optimize performance of waybar cpuinfo and gpuinfo modules
2 parents cbd0f5c + 23bb3c8 commit 5555bc7

File tree

4 files changed

+80
-57
lines changed

4 files changed

+80
-57
lines changed

Configs/.config/waybar/modules/cpuinfo.jsonc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"custom/cpuinfo": {
2-
"exec": " cpuinfo.sh",
2+
"exec": "NO_EMOJI=1 cpuinfo.sh",
33
"return-type": "json",
44
"format": "{}",
55
"rotate": ${r_deg},
6-
"interval": 5, // once every 5 seconds
6+
"restart-interval": 5, // once every 5 seconds
77
"tooltip": true,
88
"max-length": 1000
99
},

Configs/.config/waybar/modules/gpuinfo.jsonc

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"custom/gpuinfo": {
2-
"exec": " gpuinfo.sh",
2+
"exec": "NO_EMOJI=1 gpuinfo.sh",
33
"return-type": "json",
44
"format": "{}",
55
"rotate": ${r_deg},
@@ -10,7 +10,7 @@
1010
},
1111

1212
"custom/gpuinfo#nvidia": {
13-
"exec": " gpuinfo.sh --use nvidia ",
13+
"exec": "NO_EMOJI=1 gpuinfo.sh --use nvidia ",
1414
"return-type": "json",
1515
"format": "{}",
1616
"rotate": ${r_deg},
@@ -20,7 +20,7 @@
2020
},
2121

2222
"custom/gpuinfo#amd": {
23-
"exec": " gpuinfo.sh --use amd ",
23+
"exec": "NO_EMOJI=1 gpuinfo.sh --use amd ",
2424
"return-type": "json",
2525
"format": "{}",
2626
"rotate": ${r_deg},
@@ -30,7 +30,7 @@
3030
},
3131

3232
"custom/gpuinfo#intel": {
33-
"exec": " gpuinfo.sh --use intel ",
33+
"exec": "NO_EMOJI=1 gpuinfo.sh --use intel ",
3434
"return-type": "json",
3535
"format": "{}",
3636
"rotate": ${r_deg},

Configs/.local/share/bin/cpuinfo.sh

+57-31
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,64 @@
11
#!/usr/bin/env sh
22

3-
# CPU model
4-
model=$(cat /proc/cpuinfo | grep 'model name' | head -n 1 | awk -F ': ' '{print $2}')
5-
6-
# CPU utilization
7-
utilization=$(top -bn1 | awk '/^%Cpu/ {print 100 - $8}')
8-
9-
# Clock speed
10-
freqlist=$(cat /proc/cpuinfo | grep "cpu MHz" | awk '{ print $4 }')
11-
maxfreq=$(cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq | sed 's/...$//')
12-
frequency=$(echo $freqlist | tr ' ' '\n' | awk "{ sum+=\$1 } END {printf \"%.0f/$maxfreq MHz\", sum/NR}")
13-
14-
# CPU temp
15-
sensorsdata=$(sensors)
16-
temp=$(awk '/Package id 0/ {print $4}' <<< "$sensorsdata" | awk -F '[+.]' '{print $2}')
17-
if [ -z "$temp" ]; then
18-
temp=$(awk '/Tctl/ {print $2}' <<< "$sensorsdata" | tr -d '+°C')
19-
fi
20-
if [ -z "$temp" ]; then
21-
temp="N/A"
3+
map_floor() {
4+
5+
IFS=', ' read -r -a pairs <<< "$1"
6+
7+
if [[ ${pairs[-1]} != *":"* ]]; then
8+
def_val="${pairs[-1]}"
9+
unset 'pairs[${#pairs[@]}-1]'
10+
fi
11+
for pair in "${pairs[@]}"; do
12+
IFS=':' read -r key value <<< "$pair"
13+
if [ ${2%%.*} -gt $key ]; then
14+
echo "$value"
15+
return
16+
fi
17+
done
18+
[ -n "$def_val" ] && echo $def_val || echo " "
19+
}
20+
21+
# Define glyphs
22+
if [[ $NO_EMOJI -eq 1 ]]; then
23+
temp_lv="85:, 65:, 45:☁, ❄"
2224
else
23-
temp=`printf "%.0f\n" $temp`
25+
temp_lv="85:🌋, 65:🔥, 45:☁️, ❄️"
2426
fi
27+
util_lv="90:, 60:󰓅, 30:󰾅, 󰾆"
2528

26-
# map icons
27-
set_ico="{\"thermo\":{\"0\":\"\",\"45\":\"\",\"65\":\"\",\"85\":\"\"},\"emoji\":{\"0\":\"❄️\",\"45\":\"☁️\",\"65\":\"🔥\",\"85\":\"🌋\"},\"util\":{\"0\":\"󰾆\",\"30\":\"󰾅\",\"60\":\"󰓅\",\"90\":\"\"}}"
28-
eval_ico() {
29-
map_ico=$(echo "${set_ico}" | jq -r --arg aky "$1" --argjson avl "$2" '.[$aky] | keys_unsorted | map(tonumber) | map(select(. <= $avl)) | max')
30-
echo "${set_ico}" | jq -r --arg aky "$1" --arg avl "$map_ico" '.[$aky] | .[$avl]'
31-
}
29+
# Get static CPU information
30+
model=$(lscpu | awk -F': ' '/Model name/ {gsub(/^ *| *$| CPU.*/,"",$2); print $2}')
31+
maxfreq=$(lscpu | awk '/CPU max MHz/ { sub(/\..*/,"",$4); print $4}')
32+
33+
# Get CPU stat
34+
statFile=$(cat /proc/stat | head -1)
35+
prevStat=$(awk '{print $2+$3+$4+$6+$7+$8 }' <<< $statFile)
36+
prevIdle=$(awk '{print $5 }' <<< $statFile)
37+
38+
while true; do
39+
# Get CPU stat
40+
statFile=$(cat /proc/stat | head -1)
41+
currStat=$(awk '{print $2+$3+$4+$6+$7+$8 }' <<< $statFile)
42+
currIdle=$(awk '{print $5 }' <<< $statFile)
43+
diffStat=$((currStat-prevStat))
44+
diffIdle=$((currIdle-prevIdle))
45+
46+
# Get dynamic CPU information
47+
utilization=$(awk -v stat="$diffStat" -v idle="$diffIdle" 'BEGIN {printf "%.1f", (stat/(stat+idle))*100}')
48+
temperature=$(sensors | awk -F': ' '/Package id 0|Tctl/ { gsub(/^ *\+?|\..*/,"",$2); print $2; f=1; exit} END { if (!f) print "N/A"; }')
49+
frequency=$(cat /proc/cpuinfo | awk '/cpu MHz/{ sum+=$4; c+=1 } END { printf "%.0f", sum/c }')
50+
51+
# Generate glyphs
52+
icons=$(echo "$(map_floor "$util_lv" $utilization)$(map_floor "$temp_lv" $temperature)")
53+
speedo=$(echo ${icons:0:1})
54+
thermo=$(echo ${icons:1:1})
55+
emoji=$(echo ${icons:2})
3256

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

37-
# Print cpu info (json)
38-
echo "{\"text\":\"${thermo} ${temp}°C\", \"tooltip\":\"${model}\n${thermo} Temperature: ${temp}°C ${emoji}\n${speedo} Utilization: ${utilization}%\n Clock Speed: ${frequency}\"}"
60+
# Store state and sleep
61+
prevStat=$currStat
62+
prevIdle=$currIdle
63+
sleep 5
64+
done

Configs/.local/share/bin/gpuinfo.sh

+17-20
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
# shellcheck disable=SC1090
44
scrDir=`dirname "$(realpath "$0")"`
55
gpuQ="/tmp/hyprdots-${UID}-gpuinfo-query"
6-
76
tired=false
87
[[ " $* " =~ " tired " ]] && ! grep -q "tired" "${gpuQ}" && echo "tired=true" >>"${gpuQ}"
8+
[[ " $* " =~ " no_emoji " ]] && ! grep -q "NO_EMOJI" "${gpuQ}" && echo "NO_EMOJI=1" >>"${gpuQ}"
9+
910
if [[ ! " $* " =~ " startup " ]]; then
1011
gpuQ="${gpuQ}$2"
1112
fi
@@ -126,7 +127,7 @@ map_floor() {
126127

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

140-
# generate emoji and icon based on temperature and utilization
141-
get_icons() {
142-
# key-value pairs of temperature and utilization levels
143-
temp_lv="85:&🌋, 65:&🔥, 45:&☁️, &❄️"
144-
util_lv="90:, 60:󰓅, 30:󰾅, 󰾆"
145-
146-
# return comma separated emojis/icons
147-
icons=$(map_floor "$temp_lv" $1 | sed "s/&/,/")
148-
icons="$icons,$(map_floor "$util_lv" $2)"
149-
echo $icons
150-
}
151-
152141
generate_json() {
153-
# get emoji and icon based on temperature and utilization
154-
icons=$(get_icons "$temperature" "$utilization")
155-
thermo=$(echo $icons | awk -F, '{print $1}')
156-
emoji=$(echo $icons | awk -F, '{print $2}')
157-
speedo=$(echo $icons | awk -F, '{print $3}')
142+
# Generate glyphs
143+
icons=$(echo "$(map_floor "$util_lv" $utilization)$(map_floor "$temp_lv" $temperature)")
144+
speedo=$(echo ${icons:0:1})
145+
thermo=$(echo ${icons:1:1})
146+
emoji=$(echo ${icons:2})
158147

159-
# emoji=$(get_temperature_emoji "${temperature}")
160148
local json="{\"text\":\"${thermo} ${temperature}°C\", \"tooltip\":\"${primary_gpu}\n${thermo} Temperature: ${temperature}°C ${emoji}"
161149
#? Soon Add Something incase needed.
162150
declare -A tooltip_parts
@@ -273,13 +261,22 @@ Available GPU: ${gpu_flags//_flag/}
273261
[flags]
274262
tired * Adding this option will not query nvidia-smi if gpu is in suspend mode
275263
startup * Useful if you want a certain GPU to be set at startup
264+
no_emoji * Use glyphs instead of emoji
276265
277266
* If ${USER} declared env = WLR_DRM_DEVICES on hyprland then use this as the primary GPU
278267
EOF
279268
exit
280269
;;
281270
esac
282271

272+
# Define glyphs
273+
if [[ $NO_EMOJI -eq 1 ]]; then
274+
temp_lv="85:, 65:, 45:☁, ❄"
275+
else
276+
temp_lv="85:🌋, 65:🔥, 45:☁️, ❄️"
277+
fi
278+
util_lv="90:, 60:󰓅, 30:󰾅, 󰾆"
279+
283280
nvidia_flag=${nvidia_flag:-0} intel_flag=${intel_flag:-0} amd_flag=${amd_flag:-0}
284281
#? Based on the flags, call the corresponding function multi flags means multi GPU.
285282
if [[ "${nvidia_flag}" -eq 1 ]]; then

0 commit comments

Comments
 (0)