Skip to content

Commit e92b07e

Browse files
committed
Split memory and swap metrics
1 parent ff1004c commit e92b07e

File tree

5 files changed

+133
-76
lines changed

5 files changed

+133
-76
lines changed

scripts/cpu.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ set -e
66
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
77
source "$CURRENT_DIR/helpers.sh"
88

9+
# TODO: use less than 1 second
910
refresh_interval=$(get_tmux_option "status-interval" "5")
1011

1112
cpu_view_tmpl=$(get_tmux_option "@sysstat_cpu_view_tmpl" '#[fg=#{cpu.color}]#{cpu.pused}#[default]')
@@ -30,6 +31,7 @@ get_cpu_color(){
3031
}
3132

3233
get_cpu_usage() {
34+
echo "87.2"; return
3335
if is_osx; then
3436
if command_exists "iostat"; then
3537
iostat -c 2 -w "$refresh_interval" | tail -n 1 | awk '{ print 100-$6 }'

scripts/helpers.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,31 @@ fcomp() {
4343
awk -v n1="$1" -v n2="$2" 'BEGIN {if (n1<n2) exit 0; exit 1}'
4444
}
4545

46+
# get_mem_usage* function returns values in KiB
47+
# 1 - scale to KiB
48+
# 1024 - scale to MiB
49+
# 1048576 - scale to GiB
50+
function get_size_scale_factor(){
51+
local size_unit="$1"
52+
case "$size_unit" in
53+
G) echo 1048576;;
54+
M) echo 1024;;
55+
K) echo 1;;
56+
esac
57+
}
58+
59+
# Depending on scale factor, change precision
60+
# 12612325K - no digits after floating point
61+
# 1261M - no digits after floating point
62+
# 1.1G - 1 digit after floating point
63+
function get_size_format(){
64+
local size_unit="$1"
65+
case "$size_unit" in
66+
G) echo '%.1f%s';;
67+
M) echo '%.0f%s';;
68+
K) echo '%.0f%s';;
69+
esac
70+
}
71+
72+
73+

scripts/mem.sh

Lines changed: 8 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,10 @@ mem_view_tmpl=$(get_tmux_option "@sysstat_mem_view_tmpl" '#[fg=#{mem.color}]#{me
1111
mem_medium_threshold=$(get_tmux_option "@sysstat_mem_medium_threshold" "75")
1212
mem_stress_threshold=$(get_tmux_option "@sysstat_mem_stress_threshold" "90")
1313

14-
swap_medium_threshold=$(get_tmux_option "@sysstat_swap_medium_threshold" "25")
15-
swap_stress_threshold=$(get_tmux_option "@sysstat_swap_stress_threshold" "75")
16-
1714
mem_color_low=$(get_tmux_option "@sysstat_mem_color_low" "green")
1815
mem_color_medium=$(get_tmux_option "@sysstat_mem_color_medium" "yellow")
1916
mem_color_stress=$(get_tmux_option "@sysstat_mem_color_stress" "red")
2017

21-
swap_color_low=$(get_tmux_option "@sysstat_swap_color_low" "green")
22-
swap_color_medium=$(get_tmux_option "@sysstat_swap_color_medium" "yellow")
23-
swap_color_stress=$(get_tmux_option "@sysstat_swap_color_stress" "red")
24-
2518
size_unit=$(get_tmux_option "@sysstat_mem_size_unit" "G")
2619

2720
get_mem_color() {
@@ -36,18 +29,6 @@ get_mem_color() {
3629
fi
3730
}
3831

39-
get_swap_color() {
40-
local swap_pused=$1
41-
42-
if fcomp "$swap_stress_threshold" "$swap_pused"; then
43-
echo "$swap_color_stress";
44-
elif fcomp "$swap_medium_threshold" "$swap_pused"; then
45-
echo "$swap_color_medium";
46-
else
47-
echo "$swap_color_low";
48-
fi
49-
}
50-
5132
print_mem() {
5233
local mem_usage
5334
local scale
@@ -59,43 +40,18 @@ print_mem() {
5940
mem_usage=$(get_mem_usage_linux)
6041
fi
6142

62-
# get_mem_usage* function returns values in KiB
63-
# 1 - scale to KiB
64-
# 1024 - scale to MiB
65-
# 1048576 - scale to GiB
66-
case "$size_unit" in
67-
G) scale=1048576;;
68-
M) scale=1024;;
69-
K) scale=1;;
70-
esac
71-
72-
# Depending on scale factor, change precision
73-
# 12612325K - no digits after floating point
74-
# 1261M - no digits after floating point
75-
# 1.1G - 1 digit after floating point
76-
case "$size_unit" in
77-
G) size_format='%.1f%s';;
78-
M) size_format='%.0f%s';;
79-
K) size_format='%.0f%s';;
80-
esac
43+
local size_scale="$(get_size_scale_factor "$size_unit")"
44+
local size_format="$(get_size_format "$size_unit")"
8145

8246
# Extract free and used memory in MiB, calculate total and percentage
83-
local mem_free=$(echo $mem_usage | awk -v scale="$scale" '{ print $1/scale }')
84-
local mem_used=$(echo $mem_usage | awk -v scale="$scale" '{ print $2/scale }')
47+
local mem_free=$(echo $mem_usage | awk -v scale="$size_scale" '{ print $1/scale }')
48+
local mem_used=$(echo $mem_usage | awk -v scale="$size_scale" '{ print $2/scale }')
8549
local mem_total=$(echo "$mem_free + $mem_used" | calc)
8650
local mem_pused=$(echo "($mem_used / $mem_total) * 100" | calc)
8751
local mem_pfree=$(echo "($mem_free / $mem_total) * 100" | calc)
88-
89-
# Extract swap free and used in MiB, calculate total and percentage
90-
local swap_free=$(echo $mem_usage | awk -v scale="$scale" '{ print $3/scale }')
91-
local swap_used=$(echo $mem_usage | awk -v scale="$scale" '{ print $4/scale }')
92-
local swap_total=$(echo "$swap_free + $swap_used" | calc)
93-
local swap_pused=$(echo "($swap_used / $swap_total) * 100" | calc)
94-
local swap_pfree=$(echo "($swap_free / $swap_total) * 100" | calc)
9552

9653
# Calculate colors for mem and swap
9754
local mem_color=$(get_mem_color "$mem_pused")
98-
local swap_color=$(get_swap_color "$swap_pused")
9955

10056
local mem_view="$mem_view_tmpl"
10157
mem_view="${mem_view//'#{mem.used}'/$(printf "$size_format" "$mem_used" "$size_unit")}"
@@ -106,15 +62,6 @@ print_mem() {
10662
mem_view="${mem_view//'#{mem.color}'/$(echo "$mem_color" | awk '{ print $1 }')}"
10763
mem_view="${mem_view//'#{mem.color2}'/$(echo "$mem_color" | awk '{ print $2 }')}"
10864
mem_view="${mem_view//'#{mem.color3}'/$(echo "$mem_color" | awk '{ print $3 }')}"
109-
110-
mem_view="${mem_view//'#{swap.used}'/$(printf "$size_format" "$swap_used" "$size_unit")}"
111-
mem_view="${mem_view//'#{swap.pused}'/$(printf "%.0f%%" "$swap_pused")}"
112-
mem_view="${mem_view//'#{swap.free}'/$(printf "$size_format" "$swap_free" "$size_unit")}"
113-
mem_view="${mem_view//'#{swap.pfree}'/$(printf "%.0f%%" "$swap_pfree")}"
114-
mem_view="${mem_view//'#{swap.total}'/$(printf "$size_format" "$swap_total" "$size_unit")}"
115-
mem_view="${mem_view//'#{swap.color}'/$(echo "$swap_color" | awk '{ print $1 }')}"
116-
mem_view="${mem_view//'#{swap.color2}'/$(echo "$swap_color" | awk '{ print $2 }')}"
117-
mem_view="${mem_view//'#{swap.color3}'/$(echo "$swap_color" | awk '{ print $3 }')}"
11865

11966
echo "$mem_view"
12067
}
@@ -127,7 +74,7 @@ print_mem() {
12774
get_mem_usage_osx(){
12875

12976
local page_size=$(sysctl -nq "vm.pagesize")
130-
local free_used=$(vm_stat | awk -v page_size=$page_size -F ':' '
77+
vm_stat | awk -v page_size=$page_size -F ':' '
13178
BEGIN { free=0; used=0 }
13279
13380
/Pages active/ ||
@@ -142,13 +89,7 @@ get_mem_usage_osx(){
14289
}
14390
14491
END { print (free * page_size)/1024, (used * page_size)/1024 }
145-
')
146-
147-
# assume swap size in MB
148-
local swap_used=$(sysctl -nq vm.swapusage | awk -F ' ' '{ print $2 }' | awk -F '=' '{gsub(/^[ ]|[M]$/, "", $2); printf "%d", $2 * 1024 }')
149-
local swap_free=$(sysctl -nq vm.swapusage | awk -F ' ' '{ print $3 }' | awk -F '=' '{gsub(/^[ ]|[M]$/, "", $2); printf "%d", $2 * 1024 }')
150-
151-
printf "%s %s %s" "$free_used" "$swap_free" "$swap_used"
92+
'
15293
}
15394

15495

@@ -160,7 +101,7 @@ get_mem_usage_osx(){
160101

161102
# See: kernel/git/torvalds/linux.git - /proc/meminfo: provide estimated available memory - https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
162103
get_mem_usage_linux(){
163-
local mem_free_used=$( </proc/meminfo awk '
104+
</proc/meminfo awk '
164105
BEGIN { total=0; free=0; }
165106
/MemTotal:/ { total=$2; }
166107
@@ -170,16 +111,7 @@ get_mem_usage_linux(){
170111
171112
/MemAvailable:/ { free=$2; exit;}
172113
END { print free, total-free }
173-
')
174-
175-
local swap_free_used=$( </proc/meminfo awk '
176-
BEGIN { total=0; free=0; }
177-
/SwapTotal:/ { total=$2; }
178-
/SwapFree:/ { free=$2; }
179-
END { print free, total-free }
180-
')
181-
182-
printf "%s %s" "$mem_free_used" "$swap_free_used"
114+
'
183115
}
184116

185117
main() {

scripts/swap.sh

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env bash
2+
3+
set -u
4+
set -e
5+
6+
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
7+
source "$CURRENT_DIR/helpers.sh"
8+
9+
swap_view_tmpl=$(get_tmux_option "@sysstat_swap_view_tmpl" '#[fg=#{swap.color}]#{swap.pused}#[default]')
10+
11+
swap_medium_threshold=$(get_tmux_option "@sysstat_swap_medium_threshold" "25")
12+
swap_stress_threshold=$(get_tmux_option "@sysstat_swap_stress_threshold" "75")
13+
14+
swap_color_low=$(get_tmux_option "@sysstat_swap_color_low" "green")
15+
swap_color_medium=$(get_tmux_option "@sysstat_swap_color_medium" "yellow")
16+
swap_color_stress=$(get_tmux_option "@sysstat_swap_color_stress" "red")
17+
18+
size_unit=$(get_tmux_option "@sysstat_swap_size_unit" "G")
19+
20+
get_swap_color() {
21+
local swap_pused=$1
22+
23+
if fcomp "$swap_stress_threshold" "$swap_pused"; then
24+
echo "$swap_color_stress";
25+
elif fcomp "$swap_medium_threshold" "$swap_pused"; then
26+
echo "$swap_color_medium";
27+
else
28+
echo "$swap_color_low";
29+
fi
30+
}
31+
32+
print_swap() {
33+
local swap_usage
34+
35+
36+
if is_osx; then
37+
swap_usage=$(get_swap_usage_osx)
38+
elif is_linux; then
39+
swap_usage=$(get_swap_usage_linux)
40+
fi
41+
42+
local size_scale="$(get_size_scale_factor "$size_unit")"
43+
local size_format="$(get_size_format "$size_unit")"
44+
45+
# Extract swap free and used in MiB, calculate total and percentage
46+
local swap_free=$(echo $swap_usage | awk -v scale="$size_scale" '{ print $1/scale }')
47+
local swap_used=$(echo $swap_usage | awk -v scale="$size_scale" '{ print $2/scale }')
48+
local swap_total=$(echo "$swap_free + $swap_used" | calc)
49+
local swap_pused=$(echo "($swap_used / $swap_total) * 100" | calc)
50+
local swap_pfree=$(echo "($swap_free / $swap_total) * 100" | calc)
51+
52+
# Calculate colors for mem and swap
53+
local swap_color=$(get_swap_color "$swap_pused")
54+
55+
local swap_view="$swap_view_tmpl"
56+
swap_view="${swap_view//'#{swap.used}'/$(printf "$size_format" "$swap_used" "$size_unit")}"
57+
swap_view="${swap_view//'#{swap.pused}'/$(printf "%.0f%%" "$swap_pused")}"
58+
swap_view="${swap_view//'#{swap.free}'/$(printf "$size_format" "$swap_free" "$size_unit")}"
59+
swap_view="${swap_view//'#{swap.pfree}'/$(printf "%.0f%%" "$swap_pfree")}"
60+
swap_view="${swap_view//'#{swap.total}'/$(printf "$size_format" "$swap_total" "$size_unit")}"
61+
swap_view="${swap_view//'#{swap.color}'/$(echo "$swap_color" | awk '{ print $1 }')}"
62+
swap_view="${swap_view//'#{swap.color2}'/$(echo "$swap_color" | awk '{ print $2 }')}"
63+
swap_view="${swap_view//'#{swap.color3}'/$(echo "$swap_color" | awk '{ print $3 }')}"
64+
65+
echo "$swap_view"
66+
}
67+
68+
get_swap_usage_osx(){
69+
70+
# assume swap size in MB
71+
local swap_used=$(sysctl -nq vm.swapusage | awk -F ' ' '{ print $2 }' | awk -F '=' '{gsub(/^[ ]|[M]$/, "", $2); printf "%d", $2 * 1024 }')
72+
local swap_free=$(sysctl -nq vm.swapusage | awk -F ' ' '{ print $3 }' | awk -F '=' '{gsub(/^[ ]|[M]$/, "", $2); printf "%d", $2 * 1024 }')
73+
74+
printf "%s %s" "$swap_free" "$swap_used"
75+
}
76+
77+
get_swap_usage_linux(){
78+
</proc/meminfo awk '
79+
BEGIN { total=0; free=0; }
80+
/SwapTotal:/ { total=$2; }
81+
/SwapFree:/ { free=$2; }
82+
END { print free, total-free }
83+
'
84+
}
85+
86+
main() {
87+
print_swap
88+
}
89+
90+
main
91+
92+
93+

sysstat.tmux

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ source "$CURRENT_DIR/scripts/helpers.sh"
66
placeholders=(
77
"\#{sysstat_cpu}"
88
"\#{sysstat_mem}"
9+
"\#{sysstat_swap}"
910
"\#{sysstat_loadavg}"
1011
)
1112

1213
commands=(
1314
"#($CURRENT_DIR/scripts/cpu.sh)"
1415
"#($CURRENT_DIR/scripts/mem.sh)"
16+
"#($CURRENT_DIR/scripts/swap.sh)"
1517
"#($CURRENT_DIR/scripts/loadavg.sh)"
1618
)
1719

0 commit comments

Comments
 (0)