From 88c422763d6e1ff763eb4a4e41c13e4bac2a3da4 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Thu, 9 Jul 2020 23:42:41 +0100 Subject: [PATCH] add cached eval - fixes #27 - related #15 - related #5 --- scripts/cpu_percentage.sh | 12 +++++------ scripts/gpu_percentage.sh | 4 ++-- scripts/gram_percentage.sh | 4 ++-- scripts/helpers.sh | 41 ++++++++++++++++++++++++++++++++++++++ scripts/ram_percentage.sh | 4 ++-- 5 files changed, 53 insertions(+), 12 deletions(-) diff --git a/scripts/cpu_percentage.sh b/scripts/cpu_percentage.sh index c180caf..7816e0e 100755 --- a/scripts/cpu_percentage.sh +++ b/scripts/cpu_percentage.sh @@ -12,22 +12,22 @@ print_cpu_percentage() { if command_exists "iostat"; then if is_linux_iostat; then - iostat -c 1 2 | sed '/^\s*$/d' | tail -n 1 | awk -v format="$cpu_percentage_format" '{usage=100-$NF} END {printf(format, usage)}' | sed 's/,/./' + cached_eval iostat -c 1 2 | sed '/^\s*$/d' | tail -n 1 | awk -v format="$cpu_percentage_format" '{usage=100-$NF} END {printf(format, usage)}' | sed 's/,/./' elif is_osx; then - iostat -c 2 disk0 | sed '/^\s*$/d' | tail -n 1 | awk -v format="$cpu_percentage_format" '{usage=100-$6} END {printf(format, usage)}' | sed 's/,/./' + cached_eval iostat -c 2 disk0 | sed '/^\s*$/d' | tail -n 1 | awk -v format="$cpu_percentage_format" '{usage=100-$6} END {printf(format, usage)}' | sed 's/,/./' elif is_freebsd || is_openbsd; then - iostat -c 2 | sed '/^\s*$/d' | tail -n 1 | awk -v format="$cpu_percentage_format" '{usage=100-$NF} END {printf(format, usage)}' | sed 's/,/./' + cached_eval iostat -c 2 | sed '/^\s*$/d' | tail -n 1 | awk -v format="$cpu_percentage_format" '{usage=100-$NF} END {printf(format, usage)}' | sed 's/,/./' else echo "Unknown iostat version please create an issue" fi elif command_exists "sar"; then - sar -u 1 1 | sed '/^\s*$/d' | tail -n 1 | awk -v format="$cpu_percentage_format" '{usage=100-$NF} END {printf(format, usage)}' | sed 's/,/./' + cached_eval sar -u 1 1 | sed '/^\s*$/d' | tail -n 1 | awk -v format="$cpu_percentage_format" '{usage=100-$NF} END {printf(format, usage)}' | sed 's/,/./' else if is_cygwin; then - usage="$(WMIC cpu get LoadPercentage | grep -Eo '^[0-9]+')" + usage="$(cached_eval WMIC cpu get LoadPercentage | grep -Eo '^[0-9]+')" printf "$cpu_percentage_format" "$usage" else - load=`ps -aux | awk '{print $3}' | tail -n+2 | awk '{s+=$1} END {print s}'` + load=`cached_eval ps -aux | awk '{print $3}' | tail -n+2 | awk '{s+=$1} END {print s}'` cpus=$(cpus_number) echo "$load $cpus" | awk -v format="$cpu_percentage_format" '{printf format, $1/$2}' fi diff --git a/scripts/gpu_percentage.sh b/scripts/gpu_percentage.sh index 30367c1..9600cb7 100755 --- a/scripts/gpu_percentage.sh +++ b/scripts/gpu_percentage.sh @@ -10,9 +10,9 @@ print_gpu_percentage() { gpu_percentage_format=$(get_tmux_option "@gpu_percentage_format" "$gpu_percentage_format") if command_exists "nvidia-smi"; then - loads=$(nvidia-smi) + loads=$(cached_eval nvidia-smi) elif command_exists "cuda-smi"; then - loads=$(cuda-smi) + loads=$(cached_eval cuda-smi) else echo "No GPU" return diff --git a/scripts/gram_percentage.sh b/scripts/gram_percentage.sh index 82945fe..9588b43 100755 --- a/scripts/gram_percentage.sh +++ b/scripts/gram_percentage.sh @@ -10,9 +10,9 @@ print_gram_percentage() { gram_percentage_format=$(get_tmux_option "@gram_percentage_format" "$gram_percentage_format") if command_exists "nvidia-smi"; then - loads=$(nvidia-smi | sed -nr 's/.*\s([0-9]+)MiB\s*\/\s*([0-9]+)MiB.*/\1 \2/p') + loads=$(cached_eval nvidia-smi | sed -nr 's/.*\s([0-9]+)MiB\s*\/\s*([0-9]+)MiB.*/\1 \2/p') elif command_exists "cuda-smi"; then - loads=$(cuda-smi | sed -nr 's/.*\s([0-9.]+) of ([0-9.]+) MB.*/\1 \2/p' | awk '{print $2-$1" "$2}') + loads=$(cached_eval cuda-smi | sed -nr 's/.*\s([0-9.]+) of ([0-9.]+) MB.*/\1 \2/p' | awk '{print $2-$1" "$2}') else echo "No GPU" return diff --git a/scripts/helpers.sh b/scripts/helpers.sh index bb5dd38..6669725 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -62,3 +62,44 @@ command_exists() { local command="$1" command -v "$command" &> /dev/null } + +get_tmp_dir() { + echo "${TMPDIR:-/tmp}/tmux-$EUID-cpu" +} + +get_time() { + date +%s +} + +get_cache_val(){ + local key="$1" + local timeout="${2:-1}" # default 1 second + local cache="$(get_tmp_dir)/$key" + if [ -f "$cache" ]; then + awk -v cache="$(head -n1 "$cache")" -v timeout=$timeout -v now=$(get_time) \ + 'BEGIN {if (now - timeout < cache) exit 0; exit 1}' \ + && tail -n+2 "$cache" + fi +} + +put_cache_val(){ + local key="$1" + local val="${@:2}" + local tmpdir="$(get_tmp_dir)" + [ ! -d "$tmpdir" ] && mkdir -p "$tmpdir" && chmod 0700 "$tmpdir" + echo "$(get_time)" > "$tmpdir/$key" + echo -n "$val" >> "$tmpdir/$key" + echo -n "$val" +} + +cached_eval(){ + local timeout=2 # seconds after which cache is invalidated + local command="$2" + local key="$(basename "$command")" + local val="$(get_cache_val "$key" "$timeout")" + if [ -z "$val" ]; then + put_cache_val "$key" "$($command "${@:3}")" + else + echo -n "$val" + fi +} diff --git a/scripts/ram_percentage.sh b/scripts/ram_percentage.sh index 84f3117..99ae6be 100755 --- a/scripts/ram_percentage.sh +++ b/scripts/ram_percentage.sh @@ -17,10 +17,10 @@ print_ram_percentage() { ram_percentage_format=$(get_tmux_option "@ram_percentage_format" "$ram_percentage_format") if command_exists "free"; then - free | awk -v format="$ram_percentage_format" '$1 ~ /Mem/ {printf(format, 100*$3/$2)}' + cached_eval free | awk -v format="$ram_percentage_format" '$1 ~ /Mem/ {printf(format, 100*$3/$2)}' elif command_exists "vm_stat"; then # page size of 4096 bytes - stats="$(vm_stat)" + stats="$(cached_eval vm_stat)" used_and_cached=$(echo "$stats" \ | grep -E "(Pages active|Pages inactive|Pages speculative|Pages wired down|Pages occupied by compressor)" \