From bf373e7a0da6d7078007bd4958eca1754cdcd809 Mon Sep 17 00:00:00 2001 From: PastaMasta Date: Wed, 31 May 2023 21:32:03 +0100 Subject: [PATCH 1/4] Added support for WSL host stats --- README.md | 9 +++++++++ scripts/cpu_percentage.sh | 5 ++++- scripts/ram_percentage.sh | 10 +++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 51f627c..92dfbe1 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,15 @@ set -g @cpu_percentage_format "%5.1f%%" # Add left padding Don't forget to reload the tmux environment (`$ tmux source-file ~/.tmux.conf`) after you do this. +### WSL support + +If you're running on WSL you can set these options to get the CPU / RAM values from the Windows host system instead: + +```shell +@cpu_wsl 'true' # true/false +@ram_wsl 'true # true/false +``` + ### Tmux Plugins This plugin is part of the [tmux-plugins](https://github.com/tmux-plugins) organisation. Checkout plugins as [battery](https://github.com/tmux-plugins/tmux-battery), [logging](https://github.com/tmux-plugins/tmux-logging), [online status](https://github.com/tmux-plugins/tmux-online-status), and many more over at the [tmux-plugins](https://github.com/tmux-plugins) organisation page. diff --git a/scripts/cpu_percentage.sh b/scripts/cpu_percentage.sh index 15190f0..78a7f29 100755 --- a/scripts/cpu_percentage.sh +++ b/scripts/cpu_percentage.sh @@ -10,7 +10,10 @@ cpu_percentage_format="%3.1f%%" print_cpu_percentage() { cpu_percentage_format=$(get_tmux_option "@cpu_percentage_format" "$cpu_percentage_format") - if command_exists "iostat"; then + if $(get_tmux_option @cpu_wsl) == "true" ; then + load="$(cached_eval wmic.exe cpu get LoadPercentage | grep -Eo '^[0-9]+')" + echo "$load" | awk -v format="$cpu_percentage_format" '{printf format, $1}' + elif command_exists "iostat"; then if is_linux_iostat; then 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/,/./' diff --git a/scripts/ram_percentage.sh b/scripts/ram_percentage.sh index 25c85cd..e8888b1 100755 --- a/scripts/ram_percentage.sh +++ b/scripts/ram_percentage.sh @@ -15,7 +15,15 @@ sum_macos_vm_stats() { print_ram_percentage() { ram_percentage_format=$(get_tmux_option "@ram_percentage_format" "$ram_percentage_format") - if command_exists "free"; then + if $(get_tmux_option @ram_wsl) == "true" ; then + + # TotalPhysicalMemory is in bytes, but FreePhysicalMemory is in KiB + total=$(wmic.exe ComputerSystem get TotalPhysicalMemory | awk '/^[0-9]/{print $1/1024}') + used=$(wmic.exe OS get FreePhysicalMemory | awk '/^[0-9]/{print $1}') + + echo "$used $total" | awk -v format="$ram_percentage_format" '{printf(format, 100*$1/$2)}' + + elif command_exists "free"; then 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 From 559f9a43f4d0e6586ef3b83550fce3ad9193c294 Mon Sep 17 00:00:00 2001 From: PastaMasta Date: Wed, 31 May 2023 22:57:48 +0100 Subject: [PATCH 2/4] Only run Windows commands if inside WSL, even if @x_wsl is set --- README.md | 2 ++ scripts/cpu_percentage.sh | 2 +- scripts/ram_percentage.sh | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 92dfbe1..1c581f2 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,8 @@ If you're running on WSL you can set these options to get the CPU / RAM values f @ram_wsl 'true # true/false ``` +Note this only works if `$WSL_DISTRO_NAME` is also set, which is done by default inside WSL. + ### Tmux Plugins This plugin is part of the [tmux-plugins](https://github.com/tmux-plugins) organisation. Checkout plugins as [battery](https://github.com/tmux-plugins/tmux-battery), [logging](https://github.com/tmux-plugins/tmux-logging), [online status](https://github.com/tmux-plugins/tmux-online-status), and many more over at the [tmux-plugins](https://github.com/tmux-plugins) organisation page. diff --git a/scripts/cpu_percentage.sh b/scripts/cpu_percentage.sh index 78a7f29..9ea6ae4 100755 --- a/scripts/cpu_percentage.sh +++ b/scripts/cpu_percentage.sh @@ -10,7 +10,7 @@ cpu_percentage_format="%3.1f%%" print_cpu_percentage() { cpu_percentage_format=$(get_tmux_option "@cpu_percentage_format" "$cpu_percentage_format") - if $(get_tmux_option @cpu_wsl) == "true" ; then + if $(get_tmux_option @cpu_wsl) == "true" && test -n "${WSL_DISTRO_NAME}" ; then load="$(cached_eval wmic.exe cpu get LoadPercentage | grep -Eo '^[0-9]+')" echo "$load" | awk -v format="$cpu_percentage_format" '{printf format, $1}' elif command_exists "iostat"; then diff --git a/scripts/ram_percentage.sh b/scripts/ram_percentage.sh index e8888b1..654e96f 100755 --- a/scripts/ram_percentage.sh +++ b/scripts/ram_percentage.sh @@ -15,7 +15,7 @@ sum_macos_vm_stats() { print_ram_percentage() { ram_percentage_format=$(get_tmux_option "@ram_percentage_format" "$ram_percentage_format") - if $(get_tmux_option @ram_wsl) == "true" ; then + if $(get_tmux_option @ram_wsl) == "true" && test -n "${WSL_DISTRO_NAME}"; then # TotalPhysicalMemory is in bytes, but FreePhysicalMemory is in KiB total=$(wmic.exe ComputerSystem get TotalPhysicalMemory | awk '/^[0-9]/{print $1/1024}') From 809c90b16b5cb041d79daf0c31685009201d3090 Mon Sep 17 00:00:00 2001 From: PastaMasta Date: Wed, 31 May 2023 23:20:10 +0100 Subject: [PATCH 3/4] corrected WSL free memory calculation --- scripts/ram_percentage.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/ram_percentage.sh b/scripts/ram_percentage.sh index 654e96f..c628512 100755 --- a/scripts/ram_percentage.sh +++ b/scripts/ram_percentage.sh @@ -19,8 +19,8 @@ print_ram_percentage() { # TotalPhysicalMemory is in bytes, but FreePhysicalMemory is in KiB total=$(wmic.exe ComputerSystem get TotalPhysicalMemory | awk '/^[0-9]/{print $1/1024}') - used=$(wmic.exe OS get FreePhysicalMemory | awk '/^[0-9]/{print $1}') - + free=$(wmic.exe OS get freevirtualmemory | awk '/^[0-9]/{print $1}') + used=$(($total - $free)) echo "$used $total" | awk -v format="$ram_percentage_format" '{printf(format, 100*$1/$2)}' elif command_exists "free"; then From 6e3236ad720911f0b252d7ff002c5638d381f7b0 Mon Sep 17 00:00:00 2001 From: PastaMasta Date: Wed, 31 May 2023 23:20:24 +0100 Subject: [PATCH 4/4] renamed _wsl options to make it clearer where it's coming from --- README.md | 4 ++-- scripts/cpu_percentage.sh | 2 +- scripts/ram_percentage.sh | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1c581f2..84b4ff6 100644 --- a/README.md +++ b/README.md @@ -161,8 +161,8 @@ Don't forget to reload the tmux environment (`$ tmux source-file ~/.tmux.conf`) If you're running on WSL you can set these options to get the CPU / RAM values from the Windows host system instead: ```shell -@cpu_wsl 'true' # true/false -@ram_wsl 'true # true/false +@cpu_wsl_host 'true' # true/false +@ram_wsl_host 'true # true/false ``` Note this only works if `$WSL_DISTRO_NAME` is also set, which is done by default inside WSL. diff --git a/scripts/cpu_percentage.sh b/scripts/cpu_percentage.sh index 9ea6ae4..8299380 100755 --- a/scripts/cpu_percentage.sh +++ b/scripts/cpu_percentage.sh @@ -10,7 +10,7 @@ cpu_percentage_format="%3.1f%%" print_cpu_percentage() { cpu_percentage_format=$(get_tmux_option "@cpu_percentage_format" "$cpu_percentage_format") - if $(get_tmux_option @cpu_wsl) == "true" && test -n "${WSL_DISTRO_NAME}" ; then + if [[ $(get_tmux_option @cpu_wsl_host) == "true" && -n "${WSL_DISTRO_NAME}" ]] ; then load="$(cached_eval wmic.exe cpu get LoadPercentage | grep -Eo '^[0-9]+')" echo "$load" | awk -v format="$cpu_percentage_format" '{printf format, $1}' elif command_exists "iostat"; then diff --git a/scripts/ram_percentage.sh b/scripts/ram_percentage.sh index c628512..be32a59 100755 --- a/scripts/ram_percentage.sh +++ b/scripts/ram_percentage.sh @@ -15,7 +15,7 @@ sum_macos_vm_stats() { print_ram_percentage() { ram_percentage_format=$(get_tmux_option "@ram_percentage_format" "$ram_percentage_format") - if $(get_tmux_option @ram_wsl) == "true" && test -n "${WSL_DISTRO_NAME}"; then + if [[ $(get_tmux_option @ram_wsl_host) == "true" && -n "${WSL_DISTRO_NAME}" ]] ; then # TotalPhysicalMemory is in bytes, but FreePhysicalMemory is in KiB total=$(wmic.exe ComputerSystem get TotalPhysicalMemory | awk '/^[0-9]/{print $1/1024}')