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}"