tmux-plugins / tmux-cpu

Plug and play cpu percentage and icon indicator for Tmux.
MIT License
444 stars 69 forks source link

add cached eval #56

Closed casperdcl closed 4 years ago

casperdcl commented 4 years ago

Attempts to cache runs of underlying commands so that e.g. #{gpu_bg_color}#{gpu_percentage} only calls nvidia-smi once per update.

Notes:

details

While testing this, I think I've found an upstream problem (with tmux or tpm). Basically

set -g status-right "%H:%M:%S #(sleep 1)"
set -g status-interval 5

will ignore status-interval and result in one update per second (!) which appears to be #15.

  1. 15 (status-interval not respected) looks like an upstream bug

  2. each update, commands may be run in parallel? e.g. set -g status-right #(write_hello_to_debug_file) #(write_bye_to_debug_file) won't guarantee hello is written before bye. This means that caching within an update may not work. Needs investigating... it's possible that some higher level code (outside this repo) handles caching of #(external commands)
  3. oddly, before this PR, I get updates more than 1/s, but after it's about once every 5 sec, respecting status-interval. This seems to fix (maybe) #15 but no there's not clear reason why... I suspect some higher level logic may be doing something like:
    commands = find_pattern("#(*)", status_right)
    while True:
        execution_time = exec(commands)
        if execution_time < 1:
            sleep(status_interval)

debugging

in ~/.tmux.conf:

set -g status-right '#{cpu_bg_color}#{cpu_percentage}#[bg=green][#{ram_bg_color}#{ram_percentage}#[bg=green]] #{gpu_bg_color}#{gpu_percentage}#[bg=green][#{gram_bg_color}#{gram_percentage}#[bg=green]]'

print to a debug file in ~/.tmux/plugins/tmux-cpu/debug.log:

diff --git a/scripts/helpers.sh b/scripts/helpers.sh
index df31cfb..d1e3e84 100644
--- a/scripts/helpers.sh
+++ b/scripts/helpers.sh
@@ -99,7 +99,9 @@ cached_eval(){
   local val="$(get_cache_val "$key")"
   if [ -z "$val" ]; then
     put_cache_val "$key" "$($command "${@:2}")"
+    echo miss "$key" >> ~/.tmux/plugins/tmux-cpu/debug.log
   else
     echo -n "$val"
+    echo hit "$key" >> ~/.tmux/plugins/tmux-cpu/debug.log
   fi
 }
tail -f ~/.tmux/plugins/tmux-cpu/debug.log
ctjhoa commented 4 years ago

Sorry, I have very few times to work on my open source projects. I'll try to review your work tomorrow.