samoshkin / tmux-plugin-sysstat

Prints CPU usage, memory & swap, load average, net I/O metrics in Tmux status bar
MIT License
158 stars 48 forks source link

get_cpu_usage() not working on OSX #5

Closed jolbax closed 6 years ago

jolbax commented 6 years ago

Hi,

I hope you can help me/us. On my machine I can see that the iostat command defined in the cpu_collect.sh script in the line 18 is hanging. If I run the script in debug mode I see it hanging here:

 bash scripts/cpu_collect.sh                                               
+++ dirname scripts/cpu_collect.sh
++ cd scripts
++ pwd
+ CURRENT_DIR=/Users/jba/.tmux/plugins/tmux-plugin-sysstat/scripts
+ source /Users/jba/.tmux/plugins/tmux-plugin-sysstat/scripts/helpers.sh
++ get_tmux_option status-interval 5
++ local option=status-interval
++ local default_value=5
+++ tmux show-option -gqv status-interval
++ local option_value=5
++ '[' -z 5 ']'
++ echo 5
+ refresh_interval=5
+ samples_count=60
++ get_tmux_option @sysstat_cpu_tmp_dir /dev/null
++ local option=@sysstat_cpu_tmp_dir
++ local default_value=/dev/null
+++ tmux show-option -gqv @sysstat_cpu_tmp_dir
++ local option_value=/var/folders/4l/7gzvgyn97sb6x09cmt3cn_b8kcxzzd/T/tmp.YbccsN2w
++ '[' -z /var/folders/4l/7gzvgyn97sb6x09cmt3cn_b8kcxzzd/T/tmp.YbccsN2w ']'
++ echo /var/folders/4l/7gzvgyn97sb6x09cmt3cn_b8kcxzzd/T/tmp.YbccsN2w
+ cpu_metric_file=/var/folders/4l/7gzvgyn97sb6x09cmt3cn_b8kcxzzd/T/tmp.YbccsN2w/cpu_collect.metric
+ main
+ get_cpu_usage
+ is_osx
+ read -r value
++ uname
+ '[' Darwin == Darwin ']'
+ command_exists iostat
+ local command=iostat
+ type iostat
+ iostat -w 5 -c 60
+ stdbuf -o0 awk 'NR > 2 { print 100-$(NF-3); }'

If I copy the command in this way iostat -w 5 -c 60 | stdbuf -o0 awk 'NR > 2 { print 100-$(NF-3); }' it works without a problem, but in the script It does not work.

Do you have any idea? Regards jolbax

dubaaron commented 6 years ago

Same thing with me. It looks like it's missing stdbuf ... this answer helped me out a bit: https://apple.stackexchange.com/questions/193141/to-use-stdbuf-from-homebrews-coreutils

I had to install coreutils from brew, and then add /usr/local/opt/coreutils/libexec/gnubin before /usr/bin in the path, as noted in the "Caveats" output after running 'brew install coreutils': export PATH=/usr/local/opt/coreutils/libexec/gnubin:$PATH

That helped get it working for me; this helped too: https://apple.stackexchange.com/questions/193141/to-use-stdbuf-from-homebrews-coreutils

jolbax commented 6 years ago

@dubaaron Thanks for your tip. I actually have done this all. While checking the whole configuration of stdbuf I realised that the update of the CPU state is working but not as expected.

I can see sometimes the current CPU load on TMUX but it does not update. It keeps showing me the same value for hours.. and suddenly it changes again updating it with a new value and behaving in the same way.

Not sure if read -r value the problem is...

interi-mhickman commented 6 years ago

Here's the problem and solution.

Problem: Some systems have more than one disk device and all are displayed in the iostat output, e.g.:

≻ iostat -c 2 -w 5
              disk0               disk2               disk3       cpu    load average
    KB/t  tps  MB/s     KB/t  tps  MB/s     KB/t  tps  MB/s  us sy id   1m   5m   15m
   24.83   27  0.64   245.89    0  0.00   240.41    0  0.00  13 15 72  2.09 2.65 2.61
    0.00    0  0.00     0.00    0  0.00     0.00    0  0.00  12 14 74  2.01 2.62 2.60

The script is expecting that the output has a single device followed by the cpu stats and expects the cpu idle % to be the 6th number. To solve this, modify the iostat command to display NO devices with -n 0 so the idle cpu % will always be the 3rd number in the output:

≻ iostat -n 0 -c 2 -w 5
      cpu    load average
 us sy id   1m   5m   15m
 13 15 72  2.41 2.25 2.40
 10 12 78  2.29 2.22 2.39

Modify line 41 of cpu.sh to:

iostat -n 0 -c 2 -w "$refresh_interval" | tail -n 1 | awk '{ print 100-$3 }'
jolbax commented 6 years ago

Hi,

In my case the problem was resolved setting samples_count to 2. Anyhow the while loop in the main function cannot access the parsed values while iostat is running.

aaronlna commented 6 years ago

Having this issue too. It appears that stdbuf -o0 isn't actually unbuffering the output of iostat when running from the script, as setting a samples_count to 2 causes my CPU stat to refresh every 2 * {refresh_rate} seconds.

wuzzapcom commented 6 years ago

Could someone create fix(pull request) for this problem? @jolbax @interi-mhickman @aaronlna

aaronlna commented 6 years ago

Posted a pull request with what I changed to get it working for my OSX Sierra environment.

There might be a more ideal solution that involves getting iostat+stdbuf to actually output correctly, but I don't have time to investigate the issue further.

wuzzapcom commented 6 years ago

@aaronlna thank you!