Skip to content

Commit

Permalink
Merge pull request #56 from tmux-plugins/cached_eval
Browse files Browse the repository at this point in the history
  • Loading branch information
casperdcl authored Jul 25, 2020
2 parents f540f07 + 3dcfbd2 commit 20120a3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 12 deletions.
12 changes: 6 additions & 6 deletions scripts/cpu_percentage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions scripts/gpu_percentage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions scripts/gram_percentage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 43 additions & 0 deletions scripts/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,46 @@ command_exists() {
local command="$1"
command -v "$command" &> /dev/null
}

get_tmp_dir() {
local tmpdir="${TMPDIR:-${TMP:-${TEMP:-/tmp}}}"
[ -d "$tmpdir" ] || local tmpdir=~/tmp
echo "$tmpdir/tmux-$EUID-cpu"
}

get_time() {
date +%s.%N
}

get_cache_val(){
local key="$1"
# seconds after which cache is invalidated
local timeout="${2:-2}"
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 command="$1"
local key="$(basename "$command")"
local val="$(get_cache_val "$key")"
if [ -z "$val" ]; then
put_cache_val "$key" "$($command "${@:2}")"
else
echo -n "$val"
fi
}
4 changes: 2 additions & 2 deletions scripts/ram_percentage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)" \
Expand Down

0 comments on commit 20120a3

Please sign in to comment.