Giter Club home page Giter Club logo

Comments (16)

ctjhoa avatar ctjhoa commented on May 29, 2024

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?

from tmux-cpu.

ctjhoa avatar ctjhoa commented on May 29, 2024
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

from tmux-cpu.

ctjhoa avatar ctjhoa commented on May 29, 2024

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 
      |---------------->                                                               |---------------->

from tmux-cpu.

akiross avatar akiross commented on May 29, 2024

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.

from tmux-cpu.

ctjhoa avatar ctjhoa commented on May 29, 2024

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

from tmux-cpu.

ctjhoa avatar ctjhoa commented on May 29, 2024

Same from BSD iostat man page:

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

from tmux-cpu.

akiross avatar akiross commented on May 29, 2024

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

from tmux-cpu.

ncdc avatar ncdc commented on May 29, 2024

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.

from tmux-cpu.

BrainMaestro avatar BrainMaestro commented on May 29, 2024

I have this issue as well, and I think it's related to tmux/tmux#797 (comment). 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

from tmux-cpu.

lonix1 avatar lonix1 commented on May 29, 2024

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

from tmux-cpu.

ctjhoa avatar ctjhoa commented on May 29, 2024

@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.

from tmux-cpu.

lonix1 avatar lonix1 commented on May 29, 2024

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

from tmux-cpu.

ctjhoa avatar ctjhoa commented on May 29, 2024

@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.

from tmux-cpu.

casperdcl avatar casperdcl commented on May 29, 2024

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".

from tmux-cpu.

casperdcl avatar casperdcl commented on May 29, 2024

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

from tmux-cpu.

ofdeath avatar ofdeath commented on May 29, 2024

I have another solution. I know this is ugly because

  • It requires that the user needs to set up a cron job manually.
  • It doesn't check whether the stat file is updating
  • It can get inaccurate values while the stat file is rotating

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

from tmux-cpu.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.