From e16a3dadfe61e4a5f8cb70aed78eca575e92c34f Mon Sep 17 00:00:00 2001 From: Nevan <37232202+nmclarty@users.noreply.github.com> Date: Thu, 9 May 2024 08:18:00 -0700 Subject: [PATCH] Add ZFS ARC (cache) calculation to memory module (#21) * 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 --- modules/32-memory | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/modules/32-memory b/modules/32-memory index 71a4477..bda3b83 100755 --- a/modules/32-memory +++ b/modules/32-memory @@ -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}"