tmux-plugins / tmux-cpu

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

status-interval not respected #15

Open akiross opened 7 years ago

akiross commented 7 years ago

So, after installing the plugin I noticed that my status bar was updated too frequently. Apparently, the presence of tmux-cpu influences the update and status-interval is ignored.

For example, if I set

tmux set -g status-right "%H:%M:%S"
tmux set -g status-interval 5

I can see the seconds updated every 5 seconds. On the other hand, if I add the tmux_cpu plugin:

tmux set -g status-right "%H:%M:%S #(/path/to/plugin/scripts/cpu_percentage.sh)"
tmux set -g status-interval 5

I will see the seconds and the CPU percentage updated almost every second, disregarding the value of status-interval. I think status-interval should be taken into account, and I see this behaviour as buggy.

I can help with the debug, but not being familiar with tmux-plugins in any way I don't know where to start.

ctjhoa commented 7 years ago

I'm not sure if this is something related to tmux-cpu or if this is something related to tpm or tmux in general. Have you try the same thing with tmux-plugins/tmux-battery?

ctjhoa commented 7 years ago
ctjhoa@cl-mackbook ~ % time .tmux/plugins/tmux-cpu/scripts/cpu_percentage.sh
  8.3%
.tmux/plugins/tmux-cpu/scripts/cpu_percentage.sh  0.00s user 0.00s system 0% cpu 1.007 total

As you can see get cpu percentage is very slow because you can only get CPU from a time frame of 1 second (thanks to iostat). So maybe it have impact on the status-interval

ctjhoa commented 7 years ago

In theory, it should be something like this:

     0                  1                                                              5                6                                                             10
   start-interval                                                                    start-interval
      |------------------------------------------------------------------------------->|------------------------------------------------------------------------------->|

 start-cpu           end-cpu                                                          start-cpu           end-cpu 
      |---------------->                                                               |---------------->
akiross commented 7 years ago

cpu_percentage.sh is slow here as well because it uses an interval of 1, but it can be omitted: iostat -c is sufficient to get an immediate CPU usage statistic, instead of iostat -c 1 2 as it is now in the script.

In fact, replacing iostat -c 1 2 with iostat -c in the cpu_percentage.sh, the issue appears to be fixed and start-interval is honoured.

With the battery plugin it appears not to have this issue, at least using battery_remain.sh... Not knowing how tmux-plugins is implemented, I can't tell, but I think it's something structural causing this issue (i.e. in tpm).

EDIT: just to clarify, I'm on linux.

ctjhoa commented 7 years ago

From linux iostat man page:

The first report generated by the iostat command provides statistics concerning the time since the system was booted

so I cannot use the first report

ctjhoa commented 7 years ago

Same from BSD iostat man page:

The first statistics that are printed are averaged over the system uptime.

akiross commented 7 years ago

Right... So, another way has to be found :)

ncdc commented 7 years ago

FYI with the iostat in sysstat-11.5.4-2.fc26.x86_64, you can do iostat -c -y 1 1 to get the current cpu utilization. The docs for -y: Omit first report with statistics since system boot, if displaying multiple records at given interval.. I also had to change it to tail -n 3 instead of 2, FWIW.

BrainMaestro commented 6 years ago

I have this issue as well, and I think it's related to https://github.com/tmux/tmux/issues/797#issuecomment-282552872. Maybe, as a way to fix this, the status-interval is checked, and if that number of seconds since the last time have not passed, just return the last value to avoid a redraw. Not the nicest solution since we have to keep track of two variables between draws, but it should work

lonix1 commented 5 years ago

Is this still an issue in the latest bits, and what is the current recommended workaround (OP was a few years ago).

ctjhoa commented 5 years ago

@lonix1 iostat versions are very different across OSes and even depending on the version used. If you really care about the update frequency of your status bar, an alternative could be to add an option to customize the command used to compute CPU.

lonix1 commented 5 years ago

@ctjhoa Since you know much more about this than I do, what approach do you take/recommend? (I'm on latest ubuntu BTW).

ctjhoa commented 5 years ago

@lonix1 What I mean is, if we cannot guess what iostat version is used and there is a faster way to compute cpu percentage, we could let the user specify the command specific to his system through a tmux-cpu option. Using eval or such should be straightforward but I have doubt that a faster command exist for the majority of OSes.

casperdcl commented 4 years ago

Is this still an issue? To be clear, this should not be the case:

For example, if I set

tmux set -g status-right "%H:%M:%S"
tmux set -g status-interval 5

I can see the seconds updated every 5 seconds. On the other hand, if I add the tmux_cpu plugin:

tmux set -g status-right "%H:%M:%S #(/path/to/plugin/scripts/cpu_percentage.sh)"
tmux set -g status-interval 5

I will see the seconds and the CPU percentage updated almost every second, disregarding the value of status-interval. I think status-interval should be taken into account, and I see this behaviour as buggy.

Seems like it was an upstream bug but isn't a thing any more? My local tests show status-interval is loosely respected even with just tmux set -g status-right "%H:%M:%S".

casperdcl commented 4 years ago

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 (!)

ofdeath commented 4 years ago

I have another solution. I know this is ugly because

But it's faster than running the iostat command directly and it can get the cpu percentage with longer interval (ex: 5s) Anyway you can consider to add this optional method if you want :)

my cronjob * * * * * root mkdir -p /dev/shm/tmux-cpu && mpstat 1 59 > /dev/shm/tmux-cpu/mpstat

my tmux.conf set -g @cpu-stat-file '/dev/shm/tmux-cpu/mpstat'

modified cpu_percentage.sh cpu_percentage.sh.txt



CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

source "$CURRENT_DIR/helpers.sh"

cpu_stat_file="${TMPDIR:-/tmp}/tmux-$EUID-cpu/iostat"
cpu_stat_interval=5

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

get_stat_stat() {
        local stat_file=$1
        local stat_stat="$stat_file.stat"
        if [ -f "$stat_stat" ]; then
                local now=$(get_time)
                local time=""
                while read line; do
                        if [ -z "$time" ]; then
                                time=$line
                        else
                                if (( $now - $time < 1 )); then
                                        printf "%s" "$line"
                                        break
                                fi
                        fi
                done < $stat_stat
        fi
}

print_cpu_stat() {
        local stat_file=$1
        local stat_stat="$stat_file.stat"
        local value=$(get_stat_stat $stat_file)
        if [ -z "$value" ]; then
                local stat_interval=$(get_tmux_option "@cpu-stat-interval" "$cpu_stat_interval")
                local stat_dir=$(dirname $stat_stat)
                local time=$(get_time)
                value=$(awk '$NF~/[0-9.]+/ {print 100-$NF}' $stat_file | tail -n $stat_interval | awk '{n+=1;sum+=$1} END{printf("%3.1f%%",n>0?sum/n:0)}')
                [ ! -d "$stat_dir" ] && mkdir -p "$stat_dir" && chmod 0700 "$stat_dir"
                printf "%s\n%s" "$time" "$value" > $stat_stat
        fi
        printf "%s" "$value"
}

print_cpu_percentage() {
        stat_file=$(get_tmux_option "@cpu-stat-file" "$cpu_stat_file")

        if [ -f $stat_file ]; then
                print_cpu_stat $stat_file

        elif command_exists "iostat"; then
                if is_linux_iostat; then
                        iostat -c 1 2 | sed '/^\s*$/d' | tail -n 1 | awk '{usage=100-$NF} END {printf("%3.1f%%", usage)}' | sed 's/,/./'
                elif is_osx; then
                        iostat -c 2 disk0 | sed '/^\s*$/d' | tail -n 1 | awk '{usage=100-$6} END {printf("%3.1f%%", usage)}' | sed 's/,/./'
                elif is_freebsd || is_openbsd; then
                        iostat -c 2 | sed '/^\s*$/d' | tail -n 1 | awk '{usage=100-$NF} END {printf("%3.1f%%", 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 '{usage=100-$NF} END {printf("%3.1f%%", usage)}' | sed 's/,/./'
        else
                if is_cygwin; then
                        usage="$(WMIC cpu get LoadPercentage | grep -Eo '^[0-9]+')"
                        printf "%3.1f%%" $usage
                else
                        load=`ps -aux | awk '{print $3}' | tail -n+2 | awk '{s+=$1} END {print s}'`
                        cpus=$(cpus_number)
                        echo "$load $cpus" | awk '{printf "%3.1f%%", $1/$2}'
                fi
        fi
}

main() {
        print_cpu_percentage
}
main