aboutsummaryrefslogtreecommitdiffstats
path: root/basics/.local/bin/statusbar/cpu
diff options
context:
space:
mode:
authorPeter Son Struschka <me@peter-struschka.com>2020-08-30 20:56:36 +0800
committerPeter Son Struschka <me@peter-struschka.com>2020-08-30 20:58:04 +0800
commit37dd411698bd6eae8365260dde39b8ae9c7cbd36 (patch)
tree978dd6ea3e72a86e4a3e0314b86d5137fc394785 /basics/.local/bin/statusbar/cpu
parenta1d6fc2f3e6ab041cc51cb085ac59a646a126d25 (diff)
downloaddotfiles-37dd411698bd6eae8365260dde39b8ae9c7cbd36.tar.gz
dotfiles-37dd411698bd6eae8365260dde39b8ae9c7cbd36.tar.bz2
dotfiles-37dd411698bd6eae8365260dde39b8ae9c7cbd36.tar.lz
dotfiles-37dd411698bd6eae8365260dde39b8ae9c7cbd36.tar.xz
dotfiles-37dd411698bd6eae8365260dde39b8ae9c7cbd36.tar.zst
dotfiles-37dd411698bd6eae8365260dde39b8ae9c7cbd36.zip
all: overhaul, move dotfile specifics to .local/share/dotfiles and more
Diffstat (limited to 'basics/.local/bin/statusbar/cpu')
-rwxr-xr-xbasics/.local/bin/statusbar/cpu54
1 files changed, 54 insertions, 0 deletions
diff --git a/basics/.local/bin/statusbar/cpu b/basics/.local/bin/statusbar/cpu
new file mode 100755
index 0000000..6a89024
--- /dev/null
+++ b/basics/.local/bin/statusbar/cpu
@@ -0,0 +1,54 @@
+#!/bin/bash
+
+# TODO: finish it
+
+# /proc/stat format:
+# cpu[n] user nice system idle iowait irq softirq steal guest guest_nice
+
+# format user nice system idle steal total
+# total = user + nice + system + idle + steal
+prevstat=$(awk '
+/^cpu[0-9]+/ {print $2, $3, $4, $5, $9, ($2 + $3 + $4 + $5 + $9)}' /proc/stat)
+
+sleep 0.5
+
+
+stat=$(awk '
+/^cpu[0-9]+/ {print $2, $3, $4, $5, $9, ($2 + $3 + $4 + $5 + $9)}' /proc/stat)
+
+
+num_cpu=$(echo "$stat" | wc -l)
+
+get_load() {
+ line=${1:-1}
+ last=$(echo "$stat" | sed "${line}q;d")
+ prev=$(echo "$prevstat" | sed "${line}q;d")
+ read -r u n s last_idle st last_total <<< "$last"
+ #echo "$u $n $s $last_idle $st $last_total"
+ read -r u n s prev_idle st prev_total <<< "$prev"
+ #echo "$u $n $s $prev_idle $st $prev_total"
+ diff=$(echo "$last_total $prev_total" | awk '{printf "%d", $1 - $2}')
+ [ "$diff" = "0" ] && return
+ percentage=$(echo "$diff $last_idle $prev_idle" | awk '{printf "%.2f", 100.0 * (($1 - ($2 - $3)) / $1)}')
+ echo -e "$percentage"
+}
+
+total_load=0
+
+for i in $(seq 1 "$num_cpu"); do
+ load=$(get_load "$i")
+ total_load=$(echo "$total_load $load" | awk '{printf "%.2f", $1 + $2}')
+done
+
+total_percentage=$(echo "$total_load $num_cpu" | awk '{printf "%.2f", ($1 / $2) + 0.5}')
+echo -e "$total_percentage"
+
+#read -r cpu a b c previdle rest < /proc/stat
+#prevtotal=$((a+b+c+previdle))
+#sleep 0.5
+#read -r cpu a b c idle rest < /proc/stat
+#total=$((a+b+c+idle))
+#cpu=$((100*( (total-prevtotal) - (idle-previdle) ) / (total-prevtotal) ))
+#echo -e "$cpu"
+
+exit 0