Skip to content

Commit

Permalink
Add ZFS ARC (cache) calculation to memory module (#21)
Browse files Browse the repository at this point in the history
* Added zfs arc calculation

Redesigned the way 32-memory gets memory stats to make it more precise when subtracting the size of the zfs arc. If ZFS is detected on the running system, it will subtract the size of its cache to provide a more accurate output.

* Updated memory unit formatting

The memory units were being converted to mb, subtracted than converted to gb. On low-memory machines (under 1G) it would only display .3G instead of the more readable 300M. By using numfmt, this behavior is restored :)

* Updated comments
  • Loading branch information
nmclarty authored May 9, 2024
1 parent e3d3dff commit e16a3da
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions modules/32-memory
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,38 @@ source "${BASE_DIR}/framework.sh"

freeh=$(free -h)
freem=$(free -m)
meminfo=$(cat /proc/meminfo)

if [ -d /proc/spl/kstat/zfs ]; then
# if zfs is in use on system
arcstat=$(cat /proc/spl/kstat/zfs/arcstats)

# convert units to kb for easy calculation with /proc/meminfo
arc_current=$(awk '/^size/ { OFMT="%.0f"; print $3/1024 }' <<< "${arcstat}")
arc_min=$(awk '/^c_min/ { OFMT="%.0f"; print $3/1024 }' <<< "${arcstat}")

# zfs arc size is dynamic, but can't shrink below the min size
arcsize=$(bc <<< "$arc_current-$arc_min")
else
# if zfs isn't in use, set the arc to 0
arcsize=0
fi

ram() {
local memory total used available label percentage bar
memory=$(awk '/Mem/ {print $2,$3,$7}' <<< "${freeh}")
IFS=" " read -r total used available <<< "${memory}"
label=$(print_split "${WIDTH}" "RAM - ${used::-1} used, ${available::-1} available" "/ ${total::-1}")
local availmem usedmem totalmem used avail total label percentage bar

availmem=$(awk -v arcsize="$arcsize" '/^MemAvailable:/ { print $2 + arcsize }' <<< "${meminfo}")
usedmem=$(awk -v availmem="$availmem" '/^MemTotal:/ { print $2 - availmem }' <<< "${meminfo}")
totalmem=$(awk '/^MemTotal:/ { print $2 }' <<< "${meminfo}")

#label display section
used="$(numfmt --round=down --from-unit=1024 --to=iec <<< ${usedmem})"
avail="$(numfmt --round=down --from-unit=1024 --to=iec <<< ${availmem})"
total="$(numfmt --round=down --from-unit=1024 --to=iec <<< ${totalmem})"
label=$(print_split "${WIDTH}" "RAM - ${used} used, ${avail} available" "/ ${total}")

percentage=$(awk '/Mem/ {printf "%.0f", ($2-$7)/$2*100}' <<< "${freem}")
#bar display section
percentage=$(echo "$usedmem / $totalmem * 100" | bc -l | xargs printf %.0f)
bar=$(print_bar "${WIDTH}" "${percentage}")

printf "%s\n%s" "${label}" "${bar}"
Expand Down

0 comments on commit e16a3da

Please sign in to comment.