Skip to content

add cached eval #56

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

Merged
merged 3 commits into from
Jul 25, 2020
Merged
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
12 changes: 6 additions & 6 deletions scripts/cpu_percentage.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions scripts/gpu_percentage.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions scripts/gram_percentage.sh
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions scripts/helpers.sh
Original file line number Diff line number Diff line change
@@ -62,3 +62,46 @@ command_exists() {
local command="$1"
command -v "$command" &> /dev/null
}

get_tmp_dir() {
local tmpdir="${TMPDIR:-${TMP:-${TEMP:-/tmp}}}"
Copy link
Member

Choose a reason for hiding this comment

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

From man tmux

tmux stores the server socket in a directory under TMUX_TMPDIR or /tmp if it is unset.

So either TMUX_TMPDIR or /tmp should exist

Copy link
Member

Choose a reason for hiding this comment

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

Maybe that's why tmux-copycat does that if inside tmux env TMUX_TMPDIR turn into TMPDIR

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for the back and forth

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

maybe; though I don't really see much point in reverting - surely checking TMP & TEMP is fine?

Copy link
Member

Choose a reason for hiding this comment

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

I'm ok with both solution

[ -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"
Copy link
Member

Choose a reason for hiding this comment

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

chmod 0700 is not wanted on global system tmp directory

Copy link
Collaborator Author

@casperdcl casperdcl Jul 20, 2020

Choose a reason for hiding this comment

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

it's never global. get_tmp_dir always returns an EUID-specific subdir.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

one of the main reasons I looked at tmux-copycat's implementation for reference is because they would've fixed any problems with it by now if it was wrong.

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
@@ -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)" \